class ReportsKits::Reports::Data::ChartOptions

Constants

DEFAULT_COLORS
DEFAULT_OPTIONS

Attributes

chart_options[RW]
data[RW]
dataset_options[RW]
inferred_options[RW]
options[RW]
type[RW]

Public Class Methods

new(data, options:, inferred_options: {}) click to toggle source
# File lib/reports_kits/reports/data/chart_options.rb, line 56
def initialize(data, options:, inferred_options: {})
  self.data = data
  self.options = options.try(:except, :options) || {}
  self.chart_options = options.try(:[], :options) || {}
  self.dataset_options = options.try(:[], :datasets)
  self.type = options.try(:[], :type) || 'bar'

  self.options = inferred_options.deep_merge(self.options) if inferred_options.present?
end

Public Instance Methods

perform() click to toggle source
# File lib/reports_kits/reports/data/chart_options.rb, line 66
def perform
  set_colors
  set_chart_options
  set_dataset_options
  set_type
  data
end

Private Instance Methods

default_options() click to toggle source
# File lib/reports_kits/reports/data/chart_options.rb, line 101
def default_options
  @default_options ||= begin
    return {} if donut_or_pie_chart?

    default_options = DEFAULT_OPTIONS.deep_dup

    x_axis_label = options[:x_axis_label]
    if x_axis_label
      default_options[:scales] ||= {}
      default_options[:scales][:xAxes] ||= []
      default_options[:scales][:xAxes][0] ||= {}
      default_options[:scales][:xAxes][0][:scaleLabel] ||= {}
      default_options[:scales][:xAxes][0][:scaleLabel][:display] ||= true
      default_options[:scales][:xAxes][0][:scaleLabel][:labelString] ||= x_axis_label
    end

    y_axis_label = options[:y_axis_label]
    if y_axis_label
      default_options[:scales] ||= {}
      default_options[:scales][:yAxes] ||= []
      default_options[:scales][:yAxes][0] ||= {}
      default_options[:scales][:yAxes][0][:scaleLabel] ||= {}
      default_options[:scales][:yAxes][0][:scaleLabel][:display] ||= true
      default_options[:scales][:yAxes][0][:scaleLabel][:labelString] ||= y_axis_label
    end

    default_options
  end
end
donut_or_pie_chart?() click to toggle source
# File lib/reports_kits/reports/data/chart_options.rb, line 149
def donut_or_pie_chart?
  type.in?(%w(donut pie))
end
set_chart_options() click to toggle source
# File lib/reports_kits/reports/data/chart_options.rb, line 131
def set_chart_options
  merged_options = default_options
  merged_options = merged_options.deep_merge(chart_options) if chart_options
  data[:chart_data][:options] = merged_options
end
set_colors() click to toggle source
# File lib/reports_kits/reports/data/chart_options.rb, line 76
def set_colors
  if donut_or_pie_chart?
    set_record_scoped_colors
  else
    set_dataset_scoped_colors
  end
end
set_dataset_options() click to toggle source
# File lib/reports_kits/reports/data/chart_options.rb, line 137
def set_dataset_options
  return if data[:chart_data][:datasets].blank? || dataset_options.blank?
  data[:chart_data][:datasets] = data[:chart_data][:datasets].map do |dataset|
    dataset.merge(dataset_options)
  end
end
set_dataset_scoped_colors() click to toggle source
# File lib/reports_kits/reports/data/chart_options.rb, line 92
def set_dataset_scoped_colors
  data[:chart_data][:datasets] = data[:chart_data][:datasets].map.with_index do |dataset, index|
    color = DEFAULT_COLORS[index % DEFAULT_COLORS.length]
    dataset[:backgroundColor] = color
    dataset[:borderColor] = color
    dataset
  end
end
set_record_scoped_colors() click to toggle source
# File lib/reports_kits/reports/data/chart_options.rb, line 84
def set_record_scoped_colors
  data[:chart_data][:datasets] = data[:chart_data][:datasets].map do |dataset|
    length = dataset[:data].length
    dataset[:backgroundColor] = DEFAULT_COLORS * (length.to_f / DEFAULT_COLORS.length).ceil
    dataset
  end
end
set_type() click to toggle source
# File lib/reports_kits/reports/data/chart_options.rb, line 144
def set_type
  return if type.blank?
  data[:type] = type
end