class ChartCandy::Builder::Line

Public Class Methods

new(id, options={}) click to toggle source
Calls superclass method ChartCandy::Builder::Base::new
# File lib/chart-candy/builder/line.rb, line 3
def initialize(id, options={})
  super

  @chart.merge! axis: {}, legend: nil, lines: [], nature: 'line', tooltip: true
end

Public Instance Methods

add_dot(dot, id, x_name, y_name) click to toggle source
# File lib/chart-candy/builder/line.rb, line 9
def add_dot(dot, id, x_name, y_name)
  {
    x: dot[x_name],
    y: dot[y_name],
    label_x: add_dot_label(id, dot[x_name], @chart[:axis][:x][:nature]),
    label_y: add_dot_label(id, dot[y_name], @chart[:axis][:y][:nature])
  }
end
add_dot_label(id, value, nature) click to toggle source
# File lib/chart-candy/builder/line.rb, line 18
def add_dot_label(id, value, nature)
  case nature
    when :date then add_dot_label_date value
    when :money then add_dot_label_money value
    else value.to_s + ' ' + t("lines.#{id}.unit")
  end
end
add_dot_label_date(date) click to toggle source
# File lib/chart-candy/builder/line.rb, line 26
def add_dot_label_date(date)
  case @chart[:step]
    when 'day' then l(date, format: :date_long)
    when 'week' then ChartCandy.translate('date.week') + ' ' + l(date, format: :date_long).strip
    when 'month' then l(date, format: :date_without_day).capitalize
    else l(date, format: :date_long)
  end
end
add_dot_label_money(amount) click to toggle source
# File lib/chart-candy/builder/line.rb, line 35
def add_dot_label_money(amount)
  sprintf("%0.02f", amount.round(2)).gsub('.', ',') + ' $'
end
add_line(id, original_data, options={}) click to toggle source
# File lib/chart-candy/builder/line.rb, line 39
def add_line(id, original_data, options={})
  options.reverse_merge! axis_y: "left", txt_vars: {}, key_x: "time", key_y: "value"

  data = original_data.map{ |d| add_dot(d, id, options[:key_x], options[:key_y]) }

  [:x, :y].each do |key|
    [:min, :max].each { |m| @chart[:axis][key][m] = to_money_format(@chart[:axis][key][m]) } if money? key
  end

  data = original_data.map do |d|
    [:key_x, :key_y].each { |key| d[options[key]] = to_money_format(d[options[key]]) if money?(key[-1,1]) }
    add_dot(d, id, options[:key_x], options[:key_y])
  end

  @chart[:lines] << { axis_y: options[:axis_y], data: data, label: t("lines.#{id}.label", options[:txt_vars]), unit: t("lines.#{id}.unit"), total: get_total(data) }
end
add_x_axis(nature, original_data, options={}) click to toggle source
# File lib/chart-candy/builder/line.rb, line 56
def add_x_axis(nature, original_data, options={})
  options.reverse_merge! key: "time"

  data = original_data.map{ |d| d[options[:key]] }

  set_period_from_data data if not @from and nature == :date

  @chart[:axis][:x] = { nature: nature, label: t("axis.x.label"), min: data.min, max: data.max, max_ticks: data.length }
end
add_y_axis(nature, original_data, options={}) click to toggle source
# File lib/chart-candy/builder/line.rb, line 66
def add_y_axis(nature, original_data, options={})
  options.reverse_merge! key: 'value', max: nil, min: nil

  data = original_data.map{ |d| d[options[:key]] }

  min = options[:min] ? options[:min] : data.min
  max = options[:max] ? options[:max] : data.max

  @chart[:axis][:y] = { nature: nature, label: t('axis.y.label'), min: min, max: max, max_ticks: data.length }
end
close_chart() click to toggle source
Calls superclass method ChartCandy::Builder::Base#close_chart
# File lib/chart-candy/builder/line.rb, line 77
def close_chart
  super

  @chart[:legend] = (@chart[:lines].length > 1) if @chart[:legend].nil?
end
date_based?() click to toggle source
# File lib/chart-candy/builder/line.rb, line 83
def date_based?
  @chart[:axis][:x] and @chart[:axis][:x][:nature] == :date
end
get_total(data) click to toggle source
# File lib/chart-candy/builder/line.rb, line 87
def get_total(data)
  { label: 'Total', value: data.sum{ |d| d[:y] } }
end
legend() click to toggle source
# File lib/chart-candy/builder/line.rb, line 95
def legend
  @chart[:legend]
end
legend=(active) click to toggle source
# File lib/chart-candy/builder/line.rb, line 91
def legend=(active)
  @chart[:legend] = active
end
money?(key) click to toggle source
# File lib/chart-candy/builder/line.rb, line 99
def money?(key)
  @chart[:axis][key.to_sym][:nature] == :money
end
to_money_format(value) click to toggle source
# File lib/chart-candy/builder/line.rb, line 103
def to_money_format(value)
  (BigDecimal.new(value || 0) / 100).round(2)
end
tooltip() click to toggle source
# File lib/chart-candy/builder/line.rb, line 111
def tooltip
  @chart[:tooltip]
end
tooltip=(active) click to toggle source
# File lib/chart-candy/builder/line.rb, line 107
def tooltip=(active)
  @chart[:tooltip] = active
end