Class: GoogleChart::Base
- Inherits:
-
Object
- Object
- GoogleChart::Base
- Defined in:
- lib/google_chart/base.rb
Direct Known Subclasses
Constant Summary collapse
- BASE_URL =
"http://chart.apis.google.com/chart?"- SIMPLE_ENCODING =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.split('')
- COMPLEX_ENCODING_ALPHABET =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.'.split('')
- @@complex_encoding =
[]
Instance Attribute Summary collapse
-
#chart_size ⇒ Object
Returns the value of attribute chart_size.
-
#chart_title ⇒ Object
Returns the value of attribute chart_title.
-
#chart_type ⇒ Object
Returns the value of attribute chart_type.
-
#data_encoding ⇒ Object
Returns the value of attribute data_encoding.
-
#params ⇒ Object
Returns the value of attribute params.
-
#show_labels ⇒ Object
Returns the value of attribute show_labels.
-
#show_legend ⇒ Object
Returns the value of attribute show_legend.
Instance Method Summary collapse
-
#axis(type, options = {}) ⇒ Object
Adds an axis to the graph.
-
#data(name, value, color = nil) ⇒ Object
Adds the data to the chart, according to the type of the graph being generated.
-
#fill(bg_or_c, type, options = {}) ⇒ Object
Adds a background or chart fill.
-
#grid(options = {}) ⇒ Object
Adds a grid to the graph.
-
#initialize(chart_size, chart_title) ⇒ Base
constructor
A new instance of Base.
-
#to_url(extras = {}) ⇒ Object
Generates the URL string that can be used to retrieve the graph image in PNG format.
Constructor Details
#initialize(chart_size, chart_title) ⇒ Base
Returns a new instance of Base.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/google_chart/base.rb', line 18 def initialize(chart_size, chart_title) self.params = Hash.new @labels = [] @data = [] @colors = [] @axis = [] self.chart_size = chart_size self.chart_title = chart_title self.data_encoding = :simple self.show_legend = true self.show_labels = false end |
Instance Attribute Details
#chart_size ⇒ Object
Returns the value of attribute chart_size.
16 17 18 |
# File 'lib/google_chart/base.rb', line 16 def chart_size @chart_size end |
#chart_title ⇒ Object
Returns the value of attribute chart_title.
16 17 18 |
# File 'lib/google_chart/base.rb', line 16 def chart_title @chart_title end |
#chart_type ⇒ Object
Returns the value of attribute chart_type.
16 17 18 |
# File 'lib/google_chart/base.rb', line 16 def chart_type @chart_type end |
#data_encoding ⇒ Object
Returns the value of attribute data_encoding.
16 17 18 |
# File 'lib/google_chart/base.rb', line 16 def data_encoding @data_encoding end |
#params ⇒ Object
Returns the value of attribute params.
16 17 18 |
# File 'lib/google_chart/base.rb', line 16 def params @params end |
#show_labels ⇒ Object
Returns the value of attribute show_labels.
16 17 18 |
# File 'lib/google_chart/base.rb', line 16 def show_labels @show_labels end |
#show_legend ⇒ Object
Returns the value of attribute show_legend.
16 17 18 |
# File 'lib/google_chart/base.rb', line 16 def show_legend @show_legend end |
Instance Method Details
#axis(type, options = {}) ⇒ Object
Adds an axis to the graph. Not applicable for Pie Chart (GoogleChart::PieChart) or Venn Diagram (GoogleChart::VennDiagram)
type-
is a symbol which can be one of
:x,:y,:right,:top options-
is a hash containing the options (see below)
Options
Not all the options are mandatory.
:labels-
An array containing the labels for the axis
:position-
An Array containing the positions for the labels
:range-
An array containing 2 elements, the start value and end value
axis styling options have to be specified as follows
:color-
Hexadecimal RGB value for the color to represent the data for the axis labels
:font_size-
Font size of the labels in pixels
:alignment-
can be one of
:left,:centeror:right
Examples
lc.axis :y, :range => [0,6], :color => 'ff00ff', :font_size => 16, :alignment => :center
125 126 127 128 |
# File 'lib/google_chart/base.rb', line 125 def axis(type, = {}) raise "Illegal axis type" unless [:x, :y, :right, :top].member?(type) @axis << [type, ] end |
#data(name, value, color = nil) ⇒ Object
Adds the data to the chart, according to the type of the graph being generated.
name-
is a string containing a label for the data.
value-
is either a number or an array of numbers containing the data. Pie Charts and Venn Diagrams take a single number, but other graphs require an array of numbers
color(optional)-
is a hexadecimal RGB value for the color to represent the data
Examples
for GoogleChart::LineChart (normal)
lc.data "Trend 1", [1,2,3,4,5], 'ff00ff'
for GoogleChart::LineChart (XY chart)
lc.data "Trend 2", [[4,5], [2,2], [1,1], [3,4]], 'ff00ff'
for GoogleChart::PieChart
lc.data "Apples", 5, 'ff00ff'
lc.data "Oranges", 7, '00ffff'
75 76 77 78 79 |
# File 'lib/google_chart/base.rb', line 75 def data(name, value, color=nil) @data << value @labels << name @colors << color if color end |
#fill(bg_or_c, type, options = {}) ⇒ Object
Adds a background or chart fill. Call this option twice if you want both a background and a chart fill
bg_or_c-
Can be one of
:backgroundor:chartdepending on the kind of fill requested type-
Can be one of
:solid,:gradientor:stripes options-
: Options depend on the type of fill selected above
Options
For :solid type
-
A
:coloroption which specifies the RGB hex value of the color to be used as a fill. For e.glc.fill(:chart, :solid, {:color => 'ffcccc'})
For :gradient type
-
An <tt>:angle</p>, which is the angle of the gradient between 0(horizontal) and 90(vertical)
-
A
:coloroption which is a 2D array containing the colors and an offset each, which specifies at what point the color is pure where: 0 specifies the right-most chart position and 1 the left-most. e,glc.fill :background, :gradient, :angle => 0, :color => [['76A4FB',1],['ffffff',0]]
For :stripes type
-
An <tt>:angle</p>, which is the angle of the stripe between 0(horizontal) and 90(vertical)
-
A
:coloroption which is a 2D array containing the colors and width value each, which must be between 0 and 1 where 1 is the full width of the chart. for e.glc.fill :chart, :stripes, :angle => 90, :color => [ ['76A4FB',0.2], ['ffffff',0.2] ]
97 98 99 100 101 102 103 104 |
# File 'lib/google_chart/base.rb', line 97 def fill(bg_or_c, type, = {}) case bg_or_c when :background @background_fill = "bg," + (type, ) when :chart @chart_fill = "c," + (type, ) end end |
#grid(options = {}) ⇒ Object
Adds a grid to the graph. Applicable only for Line Chart (GoogleChart::LineChart) and Scatter Chart (available soon)
options-
is a hash containing the options (see below)
Options
:xstep-
X axis step size
:ystep-
Y axis step size
:length_segment(optional)-
Length of the line segement. Useful with the :length_blank value to have dashed lines
:length_blank(optional)-
Length of the blank segment. use 0 if you want a solid grid
Examples
lc.grid :x_step => 5, :y_step => 5, :length_segment => 1, :length_blank => 0
144 145 146 147 148 149 |
# File 'lib/google_chart/base.rb', line 144 def grid(={}) @grid_str = "#{options[:x_step].to_f},#{options[:y_step].to_f}" if [:length_segment] or [:length_blank] @grid_str += ",#{options[:length_segment].to_f},#{options[:length_blank].to_f}" end end |
#to_url(extras = {}) ⇒ Object
Generates the URL string that can be used to retrieve the graph image in PNG format. Use this after assigning all the properties to the graph You can pass in additional params as a hash for features that may not have been implemented For e.g
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({:chm => "000000,0,0.1,0.11"}) # Single black line as a horizontal marker
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/google_chart/base.rb', line 40 def to_url(extras={}) params.clear set_size set_type set_colors add_axis add_grid add_data add_labels(@labels) if show_labels add_legend(@labels) if show_legend add_title if chart_title.to_s.length > 0 params.merge!(extras) query_string = params.map { |k,v| "#{k}=#{URI.escape(v.to_s)}" }.join('&') BASE_URL + query_string end |