Class: Gchart
- Inherits:
-
Object
- Object
- Gchart
- Includes:
- GchartInfo
- Defined in:
- lib/gchart.rb,
lib/gchart/aliases.rb
Constant Summary collapse
- @@url =
"http://chart.apis.google.com/chart?"
- @@types =
['line', 'line_xy', 'scatter', 'bar', 'venn', 'pie', 'pie_3d', 'jstize', 'sparkline', 'meter']
- @@simple_chars =
('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
- @@chars =
@@simple_chars + ['-', '.']
- @@ext_pairs =
@@chars.map { |char_1| @@chars.map { |char_2| char_1 + char_2 } }.flatten
- @@file_name =
'chart.png'
Instance Attribute Summary collapse
-
#axis_labels ⇒ Object
Returns the value of attribute axis_labels.
-
#axis_with_labels ⇒ Object
Returns the value of attribute axis_with_labels.
-
#bar_colors ⇒ Object
Returns the value of attribute bar_colors.
-
#bar_width_and_spacing ⇒ Object
Returns the value of attribute bar_width_and_spacing.
-
#custom ⇒ Object
Returns the value of attribute custom.
-
#data ⇒ Object
Returns the value of attribute data.
-
#encoding ⇒ Object
Returns the value of attribute encoding.
-
#grouped ⇒ Object
(also: #grouped?)
Returns the value of attribute grouped.
-
#height ⇒ Object
Returns the value of attribute height.
-
#horizontal ⇒ Object
(also: #horizontal?)
Returns the value of attribute horizontal.
-
#legend ⇒ Object
Returns the value of attribute legend.
-
#max_value ⇒ Object
Returns the value of attribute max_value.
-
#title ⇒ Object
Returns the value of attribute title.
-
#title_color ⇒ Object
Returns the value of attribute title_color.
-
#title_size ⇒ Object
Returns the value of attribute title_size.
-
#type ⇒ Object
Returns the value of attribute type.
-
#width ⇒ Object
Returns the value of attribute width.
Class Method Summary collapse
- .jstize(string) ⇒ Object
-
.method_missing(m, options = {}) ⇒ Object
Support for Gchart.line(:title => ‘my title’, :size => ‘400x600’).
- .supported_types ⇒ Object
Instance Method Summary collapse
- #bg=(options) ⇒ Object (also: #background=)
- #graph_bg=(options) ⇒ Object (also: #chart_bg=, #chart_color=, #chart_background=)
-
#initialize(options = {}) ⇒ Gchart
constructor
A new instance of Gchart.
-
#orientation=(orientation = 'h') ⇒ Object
Sets the orientation of a bar graph.
- #size ⇒ Object
-
#size=(size = '300x200') ⇒ Object
Defines the Graph size using the following format: width X height.
-
#stacked=(option = true) ⇒ Object
Sets the bar graph presentation (stacked or grouped).
Constructor Details
#initialize(options = {}) ⇒ Gchart
Returns a new instance of Gchart.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/gchart.rb', line 41 def initialize(={}) @type = :line @data = [] @width = 300 @height = 200 @horizontal = false @grouped = false @encoding = 'simple' @max_value = 'auto' # set the options value if definable .each do |attribute, value| send("#{attribute.to_s}=", value) if self.respond_to?("#{attribute}=") end end |
Instance Attribute Details
#axis_labels ⇒ Object
Returns the value of attribute axis_labels.
17 18 19 |
# File 'lib/gchart.rb', line 17 def axis_labels @axis_labels end |
#axis_with_labels ⇒ Object
Returns the value of attribute axis_with_labels.
17 18 19 |
# File 'lib/gchart.rb', line 17 def axis_with_labels @axis_with_labels end |
#bar_colors ⇒ Object
Returns the value of attribute bar_colors.
17 18 19 |
# File 'lib/gchart.rb', line 17 def @bar_colors end |
#bar_width_and_spacing ⇒ Object
Returns the value of attribute bar_width_and_spacing.
17 18 19 |
# File 'lib/gchart.rb', line 17 def @bar_width_and_spacing end |
#custom ⇒ Object
Returns the value of attribute custom.
17 18 19 |
# File 'lib/gchart.rb', line 17 def custom @custom end |
#data ⇒ Object
Returns the value of attribute data.
17 18 19 |
# File 'lib/gchart.rb', line 17 def data @data end |
#encoding ⇒ Object
Returns the value of attribute encoding.
17 18 19 |
# File 'lib/gchart.rb', line 17 def encoding @encoding end |
#grouped ⇒ Object Also known as: grouped?
Returns the value of attribute grouped.
17 18 19 |
# File 'lib/gchart.rb', line 17 def grouped @grouped end |
#height ⇒ Object
Returns the value of attribute height.
17 18 19 |
# File 'lib/gchart.rb', line 17 def height @height end |
#horizontal ⇒ Object Also known as: horizontal?
Returns the value of attribute horizontal.
17 18 19 |
# File 'lib/gchart.rb', line 17 def horizontal @horizontal end |
#legend ⇒ Object
Returns the value of attribute legend.
17 18 19 |
# File 'lib/gchart.rb', line 17 def legend @legend end |
#max_value ⇒ Object
Returns the value of attribute max_value.
17 18 19 |
# File 'lib/gchart.rb', line 17 def max_value @max_value end |
#title ⇒ Object
Returns the value of attribute title.
17 18 19 |
# File 'lib/gchart.rb', line 17 def title @title end |
#title_color ⇒ Object
Returns the value of attribute title_color.
17 18 19 |
# File 'lib/gchart.rb', line 17 def title_color @title_color end |
#title_size ⇒ Object
Returns the value of attribute title_size.
17 18 19 |
# File 'lib/gchart.rb', line 17 def title_size @title_size end |
#type ⇒ Object
Returns the value of attribute type.
17 18 19 |
# File 'lib/gchart.rb', line 17 def type @type end |
#width ⇒ Object
Returns the value of attribute width.
17 18 19 |
# File 'lib/gchart.rb', line 17 def width @width end |
Class Method Details
.jstize(string) ⇒ Object
105 106 107 |
# File 'lib/gchart.rb', line 105 def self.jstize(string) string.gsub(' ', '+').gsub(/\[|\{|\}|\||\\|\^|\[|\]|\`|\]/) {|c| "%#{c[0].to_s(16).upcase}"} end |
.method_missing(m, options = {}) ⇒ Object
Support for Gchart.line(:title => ‘my title’, :size => ‘400x600’)
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/gchart.rb', line 22 def method_missing(m, ={}) # Extract the format and optional filename, then clean the hash format = [:format] || 'url' @@file_name = [:filename] unless [:filename].nil? .delete(:format) .delete(:filename) # create the chart and return it in the format asked for if @@types.include?(m.to_s) chart = new(.merge!({:type => m})) chart.send(format) elsif m.to_s == 'version' Gchart::VERSION::STRING else "#{m} is not a supported chart format, please use one of the following: #{supported_types}." end end |
.supported_types ⇒ Object
57 58 59 |
# File 'lib/gchart.rb', line 57 def self.supported_types @@types.join(' ') end |
Instance Method Details
#bg=(options) ⇒ Object Also known as: background=
85 86 87 88 89 90 91 92 93 |
# File 'lib/gchart.rb', line 85 def bg=() if .is_a?(String) @bg_color = elsif .is_a?(Hash) @bg_color = [:color] @bg_type = [:type] @bg_angle = [:angle] end end |
#graph_bg=(options) ⇒ Object Also known as: chart_bg=, chart_color=, chart_background=
95 96 97 98 99 100 101 102 103 |
# File 'lib/gchart.rb', line 95 def graph_bg=() if .is_a?(String) @chart_color = elsif .is_a?(Hash) @chart_color = [:color] @chart_type = [:type] @chart_angle = [:angle] end end |
#orientation=(orientation = 'h') ⇒ Object
Sets the orientation of a bar graph
72 73 74 75 76 77 78 |
# File 'lib/gchart.rb', line 72 def orientation=(orientation='h') if orientation == 'h' || orientation == 'horizontal' self.horizontal = true elsif orientation == 'v' || orientation == 'vertical' self.horizontal = false end end |
#size ⇒ Object
67 68 69 |
# File 'lib/gchart.rb', line 67 def size "#{@width}x#{@height}" end |
#size=(size = '300x200') ⇒ Object
Defines the Graph size using the following format: width X height
63 64 65 |
# File 'lib/gchart.rb', line 63 def size=(size='300x200') @width, @height = size.split("x").map { |dimension| dimension.to_i } end |
#stacked=(option = true) ⇒ Object
Sets the bar graph presentation (stacked or grouped)
81 82 83 |
# File 'lib/gchart.rb', line 81 def stacked=(option=true) @grouped = option ? false : true end |