Class: Quacky::LineGraphBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/quacky/line_graph_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(init_data = []) ⇒ LineGraphBuilder

Returns a new instance of LineGraphBuilder.



6
7
8
# File 'lib/quacky/line_graph_builder.rb', line 6

def initialize(init_data = [])
  @data = init_data
end

Instance Method Details

#drawObject

Return a content tag that can be selected by the client-side, and drawn on. The data attribute of the HTML tag is @data.



12
13
14
# File 'lib/quacky/line_graph_builder.rb', line 12

def draw
  "<div class='line-graph-container'><div class='y-axis'></div><div class='line-graph' data-chart='#{self.get_data}'></div></div>".html_safe
end

#get_dataObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/quacky/line_graph_builder.rb', line 16

def get_data
  output = []
  
  @data.each do |event|
    output << {
      x: event[:time].to_i,
      y: event[:data]
    }
  end
  
  output.to_json
end