class ActiveCharts::Chart

Constants

CSS_CLASSES
MARGIN

Attributes

collection[R]
columns_count[R]
data_formatters[R]
extra_css_classes[R]
label_height[R]
max_values[R]
rows_count[R]
series_labels[R]
title[R]

Public Class Methods

new(collection, options = {}) click to toggle source
# File lib/active_charts/chart.rb, line 19
def initialize(collection, options = {})
  @collection = Util.array_of_arrays?(collection) ? collection : [[]]
  @rows_count = @collection.count
  @columns_count = @collection.map(&:count).max
  process_options(options)
end

Public Instance Methods

chart_svg_tag() click to toggle source
# File lib/active_charts/chart.rb, line 36
def chart_svg_tag; end
legend_list_tag() click to toggle source
# File lib/active_charts/chart.rb, line 38
def legend_list_tag
  list_items = series_labels.map.with_index do |label, index| 
    tag.li(label, class: series_class(index)) 
  end
  
  tag.ul(list_items.join.html_safe, class: 'ac-chart ac-series-legend')
end
to_html() click to toggle source
# File lib/active_charts/chart.rb, line 29
def to_html
  inner_html = [tag.figcaption(title, class: 'ac-chart-title'), chart_svg_tag, legend_list_tag].join('
      ')
  
  tag.figure(inner_html.html_safe, class: container_classes)
end

Private Instance Methods

container_classes() click to toggle source
# File lib/active_charts/chart.rb, line 76
def container_classes
  ['ac-chart-container', 'ac-clearfix', extra_css_classes].join(' ')
end
css_class() click to toggle source
# File lib/active_charts/chart.rb, line 116
def css_class
  class_name = self.class.to_s.gsub('ActiveCharts::', '')
  'ac-' + underscore(class_name).tr('_', '-')
end
formatted_val(val, formatter = nil) click to toggle source
# File lib/active_charts/chart.rb, line 90
def formatted_val(val, formatter = nil)
  case formatter
  when :percent
    number_to_percentage(Util.safe_to_dec(val) * 100, precision: 1)
  when :date
    Util.date_label(val)
  when :rounded
    Util.safe_to_dec(val).round
  when :currency
    number_to_currency(val, precision: 0)
  when :year
    val.to_i.to_s
  else
    number_with_delimiter(val)
  end
end
process_options(options) click to toggle source
# File lib/active_charts/chart.rb, line 48
def process_options(options)
  @title = options[:title] || ''
  @extra_css_classes = options[:class] || ''
  @data_formatters = options[:data_formatters] || []
  @max_values = valid_max_values(options[:max_values], options[:single_y_scale])
  @label_height = options[:label_height] || MARGIN / 2
  @series_labels = options[:series_labels] || []
end
series_class(index) click to toggle source
# File lib/active_charts/chart.rb, line 80
def series_class(index)
  CSS_CLASSES[index % CSS_CLASSES.size]
end
svg_options() click to toggle source
# File lib/active_charts/chart.rb, line 107
def svg_options
  { 
    xmlns: 'http://www.w3.org/2000/svg',
    style: "width: #{svg_width}px; height: auto;",
    viewBox: "0 0 #{svg_width} #{svg_height}",
    class: ['ac-chart', css_class].join(' ')
  }
end
tag_options(opts, whitelist = nil) click to toggle source
# File lib/active_charts/chart.rb, line 84
def tag_options(opts, whitelist = nil)
  opts = opts.select { |k, _v| whitelist.include? k.to_s } if whitelist
  tag_builder = TagBuilder.new(self)
  opts.map { |k, v| tag_builder.tag_option(k, v, true) }.join(' ')
end
valid_max_values(custom_max_values = nil, single_y_scale = false) click to toggle source
# File lib/active_charts/chart.rb, line 57
def valid_max_values(custom_max_values = nil, single_y_scale = false)
  return custom_max_values if valid_max_values?(custom_max_values)
  
  computed_max_values = Util.max_values(@collection)
  
  return computed_max_values unless single_y_scale
  
  Array.new(columns_count, computed_max_values.max)
end
valid_max_values?(max_values) click to toggle source
# File lib/active_charts/chart.rb, line 67
def valid_max_values?(max_values)
  return false unless max_values.is_a?(Array)
  return false unless max_values.count.eql?(columns_count)
  
  Util.max_values(@collection).map.with_index do |computed_val, index|
    computed_val <= max_values[index]
  end.all?
end