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', 'map']
- @@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
-
#alt ⇒ Object
Returns the value of attribute alt.
-
#axis_labels ⇒ Object
Returns the value of attribute axis_labels.
-
#axis_range ⇒ Object
Returns the value of attribute axis_range.
-
#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.
-
#class ⇒ Object
Returns the value of attribute class.
-
#country_codes ⇒ Object
Returns the value of attribute country_codes.
-
#custom ⇒ Object
Returns the value of attribute custom.
-
#data ⇒ Object
Returns the value of attribute data.
-
#encoding ⇒ Object
Returns the value of attribute encoding.
-
#geographical_area ⇒ Object
Returns the value of attribute geographical_area.
-
#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.
-
#id ⇒ Object
Returns the value of attribute id.
-
#legend ⇒ Object
Returns the value of attribute legend.
-
#map_colors ⇒ Object
Returns the value of attribute map_colors.
-
#range_markers ⇒ Object
Returns the value of attribute range_markers.
-
#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=)
- #dataset ⇒ Object
- #dimensions ⇒ Object
-
#full_data_range(ds) ⇒ Object
auto sets the range if required it also sets the axis_range if not defined.
- #graph_bg=(options) ⇒ Object (also: #chart_bg=, #chart_color=, #chart_background=)
-
#initialize(options = {}) ⇒ Gchart
constructor
A new instance of Gchart.
- #max_value=(max_value) ⇒ Object
- #min_value=(min_value) ⇒ Object
-
#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.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/gchart.rb', line 47 def initialize(={}) @type = :line @data = [] @width = 300 @height = 200 @horizontal = false @grouped = false @encoding = 'simple' self.min_value = 'auto' self.max_value = 'auto' # Sets the alt tag when chart is exported as image tag @alt = 'Google Chart' # Sets the CSS id selector when chart is exported as image tag @id = false # Sets the CSS class selector when chart is exported as image tag @class = false # 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
#alt ⇒ Object
Returns the value of attribute alt.
20 21 22 |
# File 'lib/gchart.rb', line 20 def alt @alt end |
#axis_labels ⇒ Object
Returns the value of attribute axis_labels.
20 21 22 |
# File 'lib/gchart.rb', line 20 def axis_labels @axis_labels end |
#axis_range ⇒ Object
Returns the value of attribute axis_range.
20 21 22 |
# File 'lib/gchart.rb', line 20 def axis_range @axis_range end |
#axis_with_labels ⇒ Object
Returns the value of attribute axis_with_labels.
20 21 22 |
# File 'lib/gchart.rb', line 20 def axis_with_labels @axis_with_labels end |
#bar_colors ⇒ Object
Returns the value of attribute bar_colors.
20 21 22 |
# File 'lib/gchart.rb', line 20 def end |
#bar_width_and_spacing ⇒ Object
Returns the value of attribute bar_width_and_spacing.
20 21 22 |
# File 'lib/gchart.rb', line 20 def end |
#class ⇒ Object
Returns the value of attribute class.
20 21 22 |
# File 'lib/gchart.rb', line 20 def class @class end |
#country_codes ⇒ Object
Returns the value of attribute country_codes.
20 21 22 |
# File 'lib/gchart.rb', line 20 def country_codes @country_codes end |
#custom ⇒ Object
Returns the value of attribute custom.
20 21 22 |
# File 'lib/gchart.rb', line 20 def custom @custom end |
#data ⇒ Object
Returns the value of attribute data.
20 21 22 |
# File 'lib/gchart.rb', line 20 def data @data end |
#encoding ⇒ Object
Returns the value of attribute encoding.
20 21 22 |
# File 'lib/gchart.rb', line 20 def encoding @encoding end |
#geographical_area ⇒ Object
Returns the value of attribute geographical_area.
20 21 22 |
# File 'lib/gchart.rb', line 20 def geographical_area @geographical_area end |
#grouped ⇒ Object Also known as: grouped?
Returns the value of attribute grouped.
20 21 22 |
# File 'lib/gchart.rb', line 20 def grouped @grouped end |
#height ⇒ Object
Returns the value of attribute height.
20 21 22 |
# File 'lib/gchart.rb', line 20 def height @height end |
#horizontal ⇒ Object Also known as: horizontal?
Returns the value of attribute horizontal.
20 21 22 |
# File 'lib/gchart.rb', line 20 def horizontal @horizontal end |
#id ⇒ Object
Returns the value of attribute id.
20 21 22 |
# File 'lib/gchart.rb', line 20 def id @id end |
#legend ⇒ Object
Returns the value of attribute legend.
20 21 22 |
# File 'lib/gchart.rb', line 20 def legend @legend end |
#map_colors ⇒ Object
Returns the value of attribute map_colors.
20 21 22 |
# File 'lib/gchart.rb', line 20 def map_colors @map_colors end |
#range_markers ⇒ Object
Returns the value of attribute range_markers.
20 21 22 |
# File 'lib/gchart.rb', line 20 def range_markers @range_markers end |
#title ⇒ Object
Returns the value of attribute title.
20 21 22 |
# File 'lib/gchart.rb', line 20 def title @title end |
#title_color ⇒ Object
Returns the value of attribute title_color.
20 21 22 |
# File 'lib/gchart.rb', line 20 def title_color @title_color end |
#title_size ⇒ Object
Returns the value of attribute title_size.
20 21 22 |
# File 'lib/gchart.rb', line 20 def title_size @title_size end |
#type ⇒ Object
Returns the value of attribute type.
20 21 22 |
# File 'lib/gchart.rb', line 20 def type @type end |
#width ⇒ Object
Returns the value of attribute width.
20 21 22 |
# File 'lib/gchart.rb', line 20 def width @width end |
Class Method Details
.jstize(string) ⇒ Object
176 177 178 |
# File 'lib/gchart.rb', line 176 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’)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/gchart.rb', line 25 def self.method_missing(m, ={}) # Start with theme defaults if a theme is set theme = [:theme] = theme ? Chart::Theme.load(theme)..merge() : # Extract the format and optional filename, then clean the hash format = [:format] || 'url' @@file_name = [:filename] unless [:filename].nil? .delete(:format) .delete(:filename) #update map_colors to be bar_colors .update(:bar_colors => [:map_colors]) if .has_key?(:map_colors) # 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
70 71 72 |
# File 'lib/gchart.rb', line 70 def self.supported_types @@types.join(' ') end |
Instance Method Details
#bg=(options) ⇒ Object Also known as: background=
103 104 105 106 107 108 109 110 111 |
# File 'lib/gchart.rb', line 103 def bg=() if .is_a?(String) @bg_color = elsif .is_a?(Hash) @bg_color = [:color] @bg_type = [:type] @bg_angle = [:angle] end end |
#dataset ⇒ Object
170 171 172 173 174 |
# File 'lib/gchart.rb', line 170 def dataset @dataset ||= prepare_dataset(data) full_data_range(@dataset) @dataset end |
#dimensions ⇒ Object
84 85 86 87 |
# File 'lib/gchart.rb', line 84 def dimensions # TODO: maybe others? [:line_xy, :scatter].include?(@type) ? 2 : 1 end |
#full_data_range(ds) ⇒ Object
auto sets the range if required it also sets the axis_range if not defined
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/gchart.rb', line 137 def full_data_range(ds) return if @max_value == false @axis = [] # TODO: text encoding should use @axis too if dimensions == 2 and @encoding != :text ds.each_with_index do |mds,index| cmds = mds.compact if cmds.empty? @axis << nil next end @axis[index%dimensions] ||= [] ax = @axis[index%dimensions] ax[0] = @min_value.nil? ? [ax[0], cmds.min].compact.min : @min_value ax[1] = @max_value.nil? ? [ax[0], cmds.max].compact.max : @max_value end else @min_value = ds.compact.map{|mds| mds.compact.min}.min if @min_value.nil? @max_value = ds.compact.map{|mds| mds.compact.max}.max if @max_value.nil? @axis << [@min_value, @max_value] end if not @axis_range @axis_range = @axis.map{|axis| axis.nil? ? [] : axis} if @type != :line_xy and (@type != :bar or not @horizontal) tmp = @axis_range.fetch(0, []) @axis_range[0] = @axis_range.fetch(1, []) @axis_range[1] = tmp end end end |
#graph_bg=(options) ⇒ Object Also known as: chart_bg=, chart_color=, chart_background=
113 114 115 116 117 118 119 120 121 |
# File 'lib/gchart.rb', line 113 def graph_bg=() if .is_a?(String) @chart_color = elsif .is_a?(Hash) @chart_color = [:color] @chart_type = [:type] @chart_angle = [:angle] end end |
#max_value=(max_value) ⇒ Object
123 124 125 126 127 |
# File 'lib/gchart.rb', line 123 def max_value=(max_value) @max_value = max_value @max_value = nil if ['auto', :auto].include? @max_value @max_value = false if ['false', :false].include? @max_value end |
#min_value=(min_value) ⇒ Object
129 130 131 132 133 |
# File 'lib/gchart.rb', line 129 def min_value=(min_value) @min_value = min_value @min_value = nil if ['auto', :auto].include? @min_value @min_value = false if ['false', :false].include? @min_value end |
#orientation=(orientation = 'h') ⇒ Object
Sets the orientation of a bar graph
90 91 92 93 94 95 96 |
# File 'lib/gchart.rb', line 90 def orientation=(orientation='h') if orientation == 'h' || orientation == 'horizontal' self.horizontal = true elsif orientation == 'v' || orientation == 'vertical' self.horizontal = false end end |
#size ⇒ Object
80 81 82 |
# File 'lib/gchart.rb', line 80 def size "#{@width}x#{@height}" end |
#size=(size = '300x200') ⇒ Object
Defines the Graph size using the following format: width X height
76 77 78 |
# File 'lib/gchart.rb', line 76 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)
99 100 101 |
# File 'lib/gchart.rb', line 99 def stacked=(option=true) @grouped = option ? false : true end |