Class: GoogleChart::Base
- Inherits:
-
Object
- Object
- GoogleChart::Base
- Defined in:
- lib/google_chart/base.rb
Direct Known Subclasses
BarChart, FinancialLineChart, LineChart, PieChart, ScatterChart, VennDiagram
Constant Summary collapse
- BASE_URL =
"http://chart.apis.google.com/chart?"
- SIMPLE_ENCODING =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.split('')
- COMPLEX_ENCODING_ALPHABET =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.'.split('')
- SHAPE_MARKERS =
{:arrow => "a", :cross => "c", :diamond => "d", :circle => "o", :square => "s", :vline_segment => "v", :vline_full => "V", :hline_full => "h", :x => "x" }
- DEFAULT_LINE_STYLE =
'1'
- @@complex_encoding =
[]
Instance Attribute Summary collapse
-
#chart_size ⇒ Object
Size of the chart in WIDTHxHEIGHT format.
-
#chart_title ⇒ Object
Chart title.
-
#chart_type ⇒ Object
Type of the chart.
-
#data_encoding ⇒ Object
Data encoding to use.
-
#params ⇒ Object
A hash of the params used to construct the URL.
-
#show_legend ⇒ Object
Set to
true
orfalse
to show or hide the chart legend. -
#title_color ⇒ Object
RRGGBB hex value for the color of the title.
-
#title_font_size ⇒ Object
Font size of the title.
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.
-
#fill_area(color, start_index, end_index) ⇒ Object
Defines a Fill area.
-
#grid(options = {}) ⇒ Object
Adds a grid to the graph.
-
#initialize(chart_size, chart_title) ⇒ Base
constructor
A new instance of Base.
-
#max_value(value) ⇒ Object
Allows (optional) setting of a max value for the chart, which will be used for data encoding and axis plotting.
-
#range_marker(alignment, options = {}) ⇒ Object
Defines a horizontal or vertical range marker.
-
#shape_marker(type, options = {}) ⇒ Object
Defines a shape marker.
-
#to_escaped_url(extras = {}) ⇒ Object
Generates a fully encoded URL string that can be used to retrieve the graph image in PNG format.
-
#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.
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/google_chart/base.rb', line 53 def initialize(chart_size, chart_title) self.params = Hash.new @labels = [] @data = [] @colors = [] @axis = [] @markers = [] @line_styles = [] self.chart_size = chart_size self.chart_title = chart_title self.data_encoding = :simple self.show_legend = true end |
Instance Attribute Details
#chart_size ⇒ Object
Size of the chart in WIDTHxHEIGHT format
30 31 32 |
# File 'lib/google_chart/base.rb', line 30 def chart_size @chart_size end |
#chart_title ⇒ Object
Chart title
36 37 38 |
# File 'lib/google_chart/base.rb', line 36 def chart_title @chart_title end |
#chart_type ⇒ Object
Type of the chart. Usually, you do not need to set this yourself
33 34 35 |
# File 'lib/google_chart/base.rb', line 33 def chart_type @chart_type end |
#data_encoding ⇒ Object
Data encoding to use. Can be one of :simple
, :text
or :extended
(see code.google.com/apis/chart/#chart_data)
45 46 47 |
# File 'lib/google_chart/base.rb', line 45 def data_encoding @data_encoding end |
#params ⇒ Object
A hash of the params used to construct the URL
48 49 50 |
# File 'lib/google_chart/base.rb', line 48 def params @params end |
#show_legend ⇒ Object
Set to true
or false
to show or hide the chart legend. Not applicable for Scatter Chart.
51 52 53 |
# File 'lib/google_chart/base.rb', line 51 def show_legend @show_legend end |
#title_color ⇒ Object
RRGGBB hex value for the color of the title
39 40 41 |
# File 'lib/google_chart/base.rb', line 39 def title_color @title_color end |
#title_font_size ⇒ Object
Font size of the title
42 43 44 |
# File 'lib/google_chart/base.rb', line 42 def title_font_size @title_font_size 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
:positions
-
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
,:center
or:right
Examples
lc.axis :y, :range => [0,6], :color => 'ff00ff', :font_size => 16, :alignment => :center
192 193 194 195 |
# File 'lib/google_chart/base.rb', line 192 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'
119 120 121 122 123 |
# File 'lib/google_chart/base.rb', line 119 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
:background
or:chart
depending on the kind of fill requested type
-
Can be one of
:solid
,:gradient
or:stripes
options
-
: Options depend on the type of fill selected above
Options
For :solid
type
-
A
:color
option 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
:angle
, which is the angle of the gradient between 0(horizontal) and 90(vertical) -
A
:color
option 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
:angle
, which is the angle of the stripe between 0(horizontal) and 90(vertical) -
A
:color
option 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] ]
164 165 166 167 168 169 170 171 |
# File 'lib/google_chart/base.rb', line 164 def fill(bg_or_c, type, = {}) case bg_or_c when :background @background_fill = "bg," + (type, ) when :chart @chart_fill = "c," + (type, ) end end |
#fill_area(color, start_index, end_index) ⇒ Object
Defines a Fill area. Applicable for line charts only
color
-
is the color of the fill area
start_index
-
is the index of the line at which the fill starts. This is 0 for the first data set, 1 for the second and so on.
end_index
-
is the index of the line at which the fill ends.
Examples
# Fill Area (Multiple Datasets)
lc = GoogleChart::LineChart.new('320x200', "Line Chart", false) do |lc|
lc.show_legend = false
lc.data "Trend 1", [5,5,6,5,5], 'ff0000'
lc.data "Trend 2", [3,3,4,3,3], '00ff00'
lc.data "Trend 3", [1,1,2,1,1], '0000ff'
lc.data "Trend 4", [0,0,0,0,0], 'ffffff'
lc.fill_area '0000ff',2,3
lc.fill_area '00ff00',1,2
lc.fill_area 'ff0000',0,1
end
puts "\nFill Area (Multiple Datasets)"
puts lc.to_url
# Fill Area (Single Dataset)
lc = GoogleChart::LineChart.new('320x200', "Line Chart", false) do |lc|
lc.show_legend = false
lc.data "Trend 1", [5,5,6,5,5], 'ff0000'
lc.fill_area 'cc6633', 0, 0
end
puts "\nFill Area (Single Dataset)"
puts lc.to_url
287 288 289 290 291 292 293 |
# File 'lib/google_chart/base.rb', line 287 def fill_area(color, start_index, end_index) if (start_index == 0 and end_index == 0) @markers << "B,#{color},0,0,0" else @markers << "b,#{color},#{start_index},#{end_index},0" end end |
#grid(options = {}) ⇒ Object
Adds a grid to the graph. Applicable only for Line Chart (GoogleChart::LineChart) and Scatter Chart (GoogleChart::ScatterChart)
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
210 211 212 213 214 215 |
# File 'lib/google_chart/base.rb', line 210 def grid(={}) @grid_str = "#{[:x_step].to_f},#{[:y_step].to_f}" if [:length_segment] or [:length_blank] @grid_str += ",#{[:length_segment].to_f},#{[:length_blank].to_f}" end end |
#max_value(value) ⇒ Object
Allows (optional) setting of a max value for the chart, which will be used for data encoding and axis plotting. The value to pass depends on the type of chart
-
For Line Chart and Bar Charts it should be a single integer or float value
-
For Scatter Charts and Line XY Charts, you MUST pass an array containing the maximum values for X and Y
Examples
For bar charts
bc.max_value 5 # 5 will be used to calculate the relative encoding values
For scatter chart
sc.max_value [5,6] # 5 is the max x value and 6 is the max y value
Note : MAKE SURE you are passing the right values otherwise an exception will be raised
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/google_chart/base.rb', line 137 def max_value(value) if [:lxy, :s].member?(self.chart_type) and value.is_a?(Array) @max_x = value.first @max_y = value.last elsif [:lc,:bhg,:bhs,:bvg,:bvs] and (value.is_a?(Integer) or value.is_a?(Float)) @max_data = value else raise "Invalid max value for this chart type" end end |
#range_marker(alignment, options = {}) ⇒ Object
Defines a horizontal or vertical range marker. Applicable for line charts and vertical charts
alignment
-
can be
:horizontal
or:vertical
options
-
specifies the color, start point and end point
Options
:color
-
RRGGBB hex value for the color of the range marker
:start_point
-
position on the x-axis/y-axis at which the range starts where 0.00 is the left/bottom and 1.00 is the right/top
:end_point
-
position on the x-axis/y-axis at which the range ends where 0.00 is the left/bottom and 1.00 is the right/top
Examples
lc.range_marker :horizontal, :color => 'E5ECF9', :start_point => 0.1, :end_point => 0.5
lc.range_marker :vertical, :color => 'a0bae9', :start_point => 0.1, :end_point => 0.5
230 231 232 233 234 235 |
# File 'lib/google_chart/base.rb', line 230 def range_marker(alignment, ={}) raise "Invalid alignment specified" unless [:horizontal, :vertical].member?(alignment) str = (alignment == :horizontal ) ? "r" : "R" str += ",#{[:color]},0,#{[:start_point]},#{[:end_point]}" @markers << str end |
#shape_marker(type, options = {}) ⇒ Object
Defines a shape marker. Applicable for line charts and scatter plots
type
-
can be
:arrow
,:cross
,:diamond
,:circle
,:square
,:vline_segment
,:vline_full
,:hline_full
,:x
options
-
specifies the color, data set index, data point index and size in pixels
Options
:color
-
RRGGBB hex value for the color of the range marker
:data_set_index
-
the index of the line on which to draw the marker. This is 0 for the first data set, 1 for the second and so on.
:data_point_index
-
is a floating point value that specifies on which data point of the data set the marker will be drawn. This is 0 for the first data point, 1 for the second and so on. Specify a fraction to interpolate a marker between two points.
:size
-
is the size of the marker in pixels.
Examples
lcxy.shape_marker :circle, :color => "000000", :data_set_index => 1, :data_point_index => 2, :pixel_size => 10
lcxy.shape_marker :cross, :color => "E5ECF9", :data_set_index => 0, :data_point_index => 0.5, :pixel_size => 10
251 252 253 254 255 |
# File 'lib/google_chart/base.rb', line 251 def shape_marker(type, ={}) raise "Invalid shape marker type specified" unless SHAPE_MARKERS.has_key?(type) shape_marker_str = "#{SHAPE_MARKERS[type]},#{[:color]},#{[:data_set_index]},#{[:data_point_index]},#{[:pixel_size]}" @markers << shape_marker_str end |
#to_escaped_url(extras = {}) ⇒ Object
Generates a fully encoded URL string that can be used to retrieve the graph image in PNG format. For less verbose URLs, use the to_url
method. Use this only if you are doing further processing with the URLs, like passing the URL to a method for downloading the images
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_escaped_url({:chm => "000000,0,0.1,0.11"}) # Single black line as a horizontal marker
95 96 97 98 99 100 |
# File 'lib/google_chart/base.rb', line 95 def to_escaped_url(extras={}) prepare_params params.merge!(extras) query_string = params.map { |k,v| "#{k}=#{URI.escape(v.to_s)}" }.join('&') BASE_URL + query_string 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
76 77 78 79 80 81 |
# File 'lib/google_chart/base.rb', line 76 def to_url(extras={}) prepare_params params.merge!(extras) query_string = params.map { |k,v| "#{k}=#{URI.escape(v.to_s).gsub(/%20/,'+').gsub(/%7C/,'|')}" }.join('&') BASE_URL + query_string end |