Class: ChartCandy::Builder::Line

Inherits:
Base
  • Object
show all
Defined in:
lib/chart-candy/builder/line.rb

Instance Method Summary collapse

Methods inherited from Base

#filename, #id, #l, #period, #set_period_from_data, #t, #title, #to_json, #to_xls

Constructor Details

#initialize(id, options = {}) ⇒ Line

Returns a new instance of Line.



3
4
5
6
7
# 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

Instance Method Details

#add_dot(dot, id, x_name, y_name) ⇒ Object



9
10
11
12
13
14
15
16
# 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) ⇒ Object



18
19
20
21
22
23
24
# 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) ⇒ Object



26
27
28
29
30
31
32
33
# 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) ⇒ Object



35
36
37
# 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 = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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 = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
# 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 = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# 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_chartObject



77
78
79
80
81
# 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?Boolean

Returns:

  • (Boolean)


83
84
85
# 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) ⇒ Object



87
88
89
# File 'lib/chart-candy/builder/line.rb', line 87

def get_total(data)
  { label: 'Total', value: data.sum{ |d| d[:y] } }
end

#legendObject



95
96
97
# File 'lib/chart-candy/builder/line.rb', line 95

def legend
  @chart[:legend]
end

#legend=(active) ⇒ Object



91
92
93
# File 'lib/chart-candy/builder/line.rb', line 91

def legend=(active)
  @chart[:legend] = active
end

#money?(key) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/chart-candy/builder/line.rb', line 99

def money?(key)
  @chart[:axis][key.to_sym][:nature] == :money
end

#to_money_format(value) ⇒ Object



103
104
105
# File 'lib/chart-candy/builder/line.rb', line 103

def to_money_format(value)
  (BigDecimal.new(value) / 100).round(2)
end

#tooltipObject



111
112
113
# File 'lib/chart-candy/builder/line.rb', line 111

def tooltip
  @chart[:tooltip]
end

#tooltip=(active) ⇒ Object



107
108
109
# File 'lib/chart-candy/builder/line.rb', line 107

def tooltip=(active)
  @chart[:tooltip] = active
end