Class: GoogleChart::LineChart

Inherits:
Base
  • Object
show all
Defined in:
lib/google_chart/line_chart.rb

Overview

Generates a Line chart. An option can be passed that allows you to create a Line XY 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'
lc.axis :y, :range => [0,6], :color => 'ff00ff', :font_size => 16, :alignment => :center
lc.axis :x, :range => [0,6], :color => '00ffff', :font_size => 16, :alignment => :center

# 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::DEFAULT_LINE_STYLE, Base::SHAPE_MARKERS, Base::SIMPLE_ENCODING

Instance Attribute Summary collapse

Attributes inherited from Base

#chart_size, #chart_title, #chart_type, #data_encoding, #params, #show_legend, #title_color, #title_font_size

Instance Method Summary collapse

Methods inherited from Base

#axis, #data, #fill, #fill_area, #grid, #max_value, #range_marker, #shape_marker, #to_escaped_url, #to_url

Constructor Details

#initialize(chart_size = '300x200', chart_title = nil, is_xy = false) {|_self| ... } ⇒ LineChart

Specify the

  • chart_size in WIDTHxHEIGHT format

  • chart_title as a string

  • is_xy is false by default. Set it to true if you want to plot a Line XY chart

Yields:

  • (_self)

Yield Parameters:



28
29
30
31
32
33
# File 'lib/google_chart/line_chart.rb', line 28

def initialize(chart_size='300x200', chart_title=nil, is_xy=false) # :yield: self
  super(chart_size, chart_title)
  self.is_xy = is_xy
  @line_styles = []
  yield self if block_given?
end

Instance Attribute Details

#is_xyObject

Returns the value of attribute is_xy.



22
23
24
# File 'lib/google_chart/line_chart.rb', line 22

def is_xy
  @is_xy
end

Instance Method Details

#line_style(data_set_index, options = {}) ⇒ Object

Defines a line style. Applicable for line charts.

data_set_index

Can be one of :background or :chart depending on the kind of fill requested

options

: Options for the style, specifying things like line thickness and lengths of the line segment and blank portions

Options

  • :line_thickness (mandatory) option which specifies the thickness of the line segment in pixels

  • :length_segment, which specifies the length of the line segment

  • :length_blank, which specifies the lenght of the blank segment



55
56
57
58
# File 'lib/google_chart/line_chart.rb', line 55

def line_style(data_set_index, options={})
  @line_styles[data_set_index] = "#{options[:line_thickness]}"
  @line_styles[data_set_index] += ",#{options[:length_segment]},#{options[:length_blank]}" if options[:length_segment]
end

#process_dataObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/google_chart/line_chart.rb', line 60

def process_data
  if self.is_xy or @data.size > 1
    if self.is_xy # XY Line graph data series
      encoded_data = []
      @data.size.times { |i|
        # Interleave X and Y co-ordinate data
        encoded_data << join_encoded_data([encode_data(x_data[i],max_x_value), encode_data(y_data[i],max_y_value)])
      }
      join_encoded_data(encoded_data)
    else # Line graph multiple data series          
      join_encoded_data(@data.collect { |series|
                          encode_data(series, max_data_value)
                        })
    end
  else
    encode_data(@data.flatten, max_data_value)
  end
end