class Quby::Compiler::DSL::ChartBuilder

Public Class Methods

chart_class() click to toggle source

rubocop:enable AccessorMethodName

# File lib/quby/compiler/dsl/charting/chart_builder.rb, line 15
def self.chart_class
  @chart_class
end
new(questionnaire, key, **options) click to toggle source
# File lib/quby/compiler/dsl/charting/chart_builder.rb, line 19
def initialize(questionnaire, key, **options)
  @chart = self.class.chart_class.new(key, **options)
  @questionnaire = questionnaire
end
set_chart_class(chart_class) click to toggle source

rubocop:disable AccessorMethodName

# File lib/quby/compiler/dsl/charting/chart_builder.rb, line 10
def self.set_chart_class(chart_class)
  @chart_class = chart_class
end

Public Instance Methods

build(&block) click to toggle source
# File lib/quby/compiler/dsl/charting/chart_builder.rb, line 66
def build(&block)
  instance_eval(&block)
  validate!
  @chart
end
chart_type(type) click to toggle source
# File lib/quby/compiler/dsl/charting/chart_builder.rb, line 62
def chart_type(type)
  @chart.chart_type = type
end
plot(key, **options) click to toggle source
# File lib/quby/compiler/dsl/charting/chart_builder.rb, line 52
def plot(key, **options)
  unless plottable = @questionnaire.find_plottable(key)
    fail "Questionnaire #{@questionnaire.key} chart #{@chart.key} references unknown score or question #{key}"
  end

  configure_options plottable, options

  @chart.plottables << Entities::Charting::Plottable.new(plottable.key, options)
end
plotband(from, to, color) click to toggle source
# File lib/quby/compiler/dsl/charting/chart_builder.rb, line 44
def plotband(from, to, color)
  @chart.plotbands << {from: from, to: to, color: color}
end
plotline(value, color) click to toggle source
# File lib/quby/compiler/dsl/charting/chart_builder.rb, line 48
def plotline(value, color)
  @chart.plotlines << {value: value, color: color}
end
range(range) click to toggle source
# File lib/quby/compiler/dsl/charting/chart_builder.rb, line 28
def range(range)
  @chart.y_range = range
end
tick_interval(tick_interval) click to toggle source
# File lib/quby/compiler/dsl/charting/chart_builder.rb, line 32
def tick_interval(tick_interval)
  @chart.tick_interval = tick_interval
end
title(title) click to toggle source
# File lib/quby/compiler/dsl/charting/chart_builder.rb, line 24
def title(title)
  @chart.title = title
end
validate!() click to toggle source
# File lib/quby/compiler/dsl/charting/chart_builder.rb, line 72
def validate!
  if @chart.y_categories.present? && @chart.y_range.present? &&
     @chart.y_range != (0..(@chart.y_categories.count - 1))
    fail ArgumentError, 'Y_categories size and range do not match'
  end

  true
end
y_categories(y_categories) click to toggle source
# File lib/quby/compiler/dsl/charting/chart_builder.rb, line 36
def y_categories(y_categories)
  @chart.y_categories = y_categories
end
y_range_categories(*y_range_categories) click to toggle source
# File lib/quby/compiler/dsl/charting/chart_builder.rb, line 40
def y_range_categories(*y_range_categories)
  @chart.y_range_categories = RangeCategories.new(*y_range_categories).as_range_hash
end

Private Instance Methods

configure_options(plottable, options) click to toggle source
# File lib/quby/compiler/dsl/charting/chart_builder.rb, line 83
def configure_options(plottable, options)
  case plottable
  when Entities::ScoreCalculation
    options.reverse_merge! plottable.options
  when Entities::Question
    options[:label] ||= plottable.title
  end
  options[:questionnaire_key] = @questionnaire.key
end