Class: Plasticine::Builder::Line

Inherits:
Base
  • Object
show all
Defined in:
lib/plasticine/builder/line.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#visual

Instance Method Summary collapse

Methods inherited from Base

#chart_style=, #id, #l, #period, #set_period_from_data, #t, #title, #to_json

Constructor Details

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



4
5
6
7
8
9
# File 'lib/plasticine/builder/line.rb', line 4

def initialize(id, options={})
  super

  @visual.merge! lines: [], nature: 'line', axis_x_format: :string, axis_y_format: :number, axis_y_tick_count: 10, chart_layout: 'line', max_y_ratio: 1, order: nil, quarter_start_month: 1
  @lines = {}
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



2
3
4
# File 'lib/plasticine/builder/line.rb', line 2

def lines
  @lines
end

Instance Method Details

#add_dot(line_id, x, y) ⇒ Object



44
45
46
47
# File 'lib/plasticine/builder/line.rb', line 44

def add_dot(line_id, x, y)
  @lines[line_id][:dots] << { x: x, y: y }
  @lines[line_id][:total_y] += y
end

#add_line(id, label) ⇒ Object



40
41
42
# File 'lib/plasticine/builder/line.rb', line 40

def add_line(id, label)
  @lines[id] = { label: label, dots: [], total_y: 0 } if not @lines[id]
end

#axis_x_format=(format) ⇒ Object



12
13
14
# File 'lib/plasticine/builder/line.rb', line 12

def axis_x_format=(format)
  @visual[:axis_x_format] = format
end

#axis_y_format=(format) ⇒ Object



16
17
18
# File 'lib/plasticine/builder/line.rb', line 16

def axis_y_format=(format)
  @visual[:axis_y_format] = format
end

#axis_y_tick_count=(tick_count) ⇒ Object



20
21
22
# File 'lib/plasticine/builder/line.rb', line 20

def axis_y_tick_count=(tick_count)
  @visual[:axis_y_tick_count] = tick_count
end

#chart_layout=(clayout) ⇒ Object



24
25
26
# File 'lib/plasticine/builder/line.rb', line 24

def chart_layout=(clayout)
  @visual[:chart_layout] = clayout
end

#close_visualObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/plasticine/builder/line.rb', line 49

def close_visual
  super

  #@chart[:legend] = (@chart[:lines].length > 1) if @chart[:legend].nil?
  set_max_y
  @visual[:lines] = @lines.each_value.map { |d| d }

  if @visual[:order]
    @visual[:lines] = @visual[:lines].sort_by{ |l| l[:total_y] }
    @visual[:lines].reverse! if @visual[:order] == 'asc'
  end

end

#max_y_ratio=(max_y_ratio) ⇒ Object



28
29
30
# File 'lib/plasticine/builder/line.rb', line 28

def max_y_ratio=(max_y_ratio)
  @visual[:max_y_ratio] = max_y_ratio
end

#order=(direction) ⇒ Object



36
37
38
# File 'lib/plasticine/builder/line.rb', line 36

def order=(direction)
  @visual[:order] = direction # asc or desc
end

#quarter_start_month=(month) ⇒ Object



32
33
34
# File 'lib/plasticine/builder/line.rb', line 32

def quarter_start_month=(month)
  @visual[:quarter_start_month] = month
end

#set_max_yObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/plasticine/builder/line.rb', line 63

def set_max_y
  stacks = {}
  max_y = 0

  @lines.each_value do |line|
    line[:dots].each do |dot|
      key = dot[:x].to_s
      stacks[key] = 0 if not stacks[key]
      stacks[key] += dot[:y]
      max_y = dot[:y] if max_y < dot[:y]
    end
  end

  if max_y == 0
    @visual[:max_y_stack] = 0
    @visual[:max_y] = 0
  else
    @visual[:max_y_stack] = stacks.max_by{|k,v| v}[1] # Return the highest stacked value for a specific X
    @visual[:max_y] = max_y * @visual[:max_y_ratio]
  end
end