Class: GoogleChart::LineChart
- Defined in:
- lib/google_chart/line_chart.rb
Overview
Generates a Line Chart. You can specify whether it is a standard line chart or an XY Line chart
Examples
# Line Chart
lc = GoogleChart::LineChart.new('320x200', "Line Chart", false)
lc.data "Trend 1", [5,4,3,1,3,5,6], '0000ff'
lc.data "Trend 2", [1,2,3,4,5,6], '00ff00'
lc.data "Trend 3", [6,5,4,3,2,1], 'ff0000'
puts lc.to_url
# Line XY Chart
lcxy = GoogleChart::LineChart.new('320x200', "Line XY Chart", true)
lcxy.data "Trend 1", [[1,1], [2,2], [3,3], [4,4]], '0000ff'
lcxy.data "Trend 2", [[4,5], [2,2], [1,1], [3,4]], '00ff00'
puts lcxy.to_url
Constant Summary
Constants inherited from Base
Base::BASE_URL, Base::COMPLEX_ENCODING_ALPHABET, Base::SIMPLE_ENCODING
Instance Attribute Summary collapse
-
#is_xy ⇒ Object
Returns the value of attribute is_xy.
Attributes inherited from Base
#chart_size, #chart_title, #chart_type, #data_encoding, #params, #show_labels, #show_legend
Instance Method Summary collapse
-
#initialize(chart_size = '300x200', chart_title = nil, is_xy = false) ⇒ LineChart
constructor
Initializes a Line Chart object with a
chart_sizeandchart_title. - #process_data ⇒ Object
Methods inherited from Base
#axis, #data, #fill, #grid, #to_url
Constructor Details
#initialize(chart_size = '300x200', chart_title = nil, is_xy = false) ⇒ LineChart
Initializes a Line Chart object with a chart_size and chart_title. Specify is_xy as true to generate a Line XY chart
23 24 25 26 |
# File 'lib/google_chart/line_chart.rb', line 23 def initialize(chart_size='300x200', chart_title=nil, is_xy=false) super(chart_size, chart_title) self.is_xy = is_xy end |
Instance Attribute Details
#is_xy ⇒ Object
Returns the value of attribute is_xy.
20 21 22 |
# File 'lib/google_chart/line_chart.rb', line 20 def is_xy @is_xy end |
Instance Method Details
#process_data ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/google_chart/line_chart.rb', line 44 def process_data if self.is_xy or @data.size > 1 if self.is_xy # XY Line graph data series max_value = @data.flatten.max x_data = @data.collect do |series| series.collect { |val| val.first } end y_data = @data.collect do |series| series.collect { |val| val.last } end encoded_data = [] @data.size.times { |i| # Interleave X and Y co-ordinate data encoded_data << join_encoded_data([encode_data(x_data[i],max_value), encode_data(y_data[i],max_value)]) } join_encoded_data(encoded_data) else # Line graph multiple data series max_value = @data.flatten.max join_encoded_data(@data.collect { |series| encode_data(series, max_value) }) end else encode_data(@data.flatten) end end |