Class: Gchart
- Inherits:
-
Object
- Object
- Gchart
- Includes:
- GchartInfo
- Defined in:
- lib/gchart.rb,
lib/gchart/aliases.rb
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.
-
#bg_angle ⇒ Object
Returns the value of attribute bg_angle.
-
#bg_color ⇒ Object
Returns the value of attribute bg_color.
-
#bg_type ⇒ Object
Returns the value of attribute bg_type.
-
#chart_angle ⇒ Object
Returns the value of attribute chart_angle.
-
#chart_color ⇒ Object
Returns the value of attribute chart_color.
-
#chart_type ⇒ Object
Returns the value of attribute chart_type.
-
#colors ⇒ Object
Returns the value of attribute colors.
-
#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.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#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.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#legend ⇒ Object
Returns the value of attribute legend.
-
#map_colors ⇒ Object
Returns the value of attribute map_colors.
-
#max ⇒ Object
Returns the value of attribute max.
-
#max_value ⇒ Object
Returns the value of attribute max_value.
-
#min ⇒ Object
Returns the value of attribute min.
-
#min_value ⇒ Object
Returns the value of attribute min_value.
-
#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
- .chars ⇒ Object
- .default_filename ⇒ Object
- .ext_pairs ⇒ Object
- .jstize(string) ⇒ Object
- .method_missing(m, options = {}) ⇒ Object
- .simple_chars ⇒ Object
- .supported_types ⇒ Object
- .types ⇒ Object
- .url ⇒ Object
- .version ⇒ Object
Instance Method Summary collapse
- #bg=(options) ⇒ Object (also: #background=)
- #dataset ⇒ Object
-
#datasets ⇒ Object
Sets of data to handle multiple sets.
- #dimensions ⇒ Object
-
#fetch ⇒ Object
Returns the chart’s generated PNG as a blob.
- #file ⇒ Object
-
#full_data_range(ds) ⇒ Object
returns the full data range as an array it also sets the data range if not defined.
- #graph_bg=(options) ⇒ Object (also: #chart_bg=, #chart_color=, #chart_background=)
-
#image_tag ⇒ Object
(also: #img_tag)
Format.
-
#initialize(options = {}) ⇒ Gchart
constructor
A new instance of Gchart.
- #jstize(string) ⇒ 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).
- #url ⇒ Object
-
#write ⇒ Object
Writes the chart’s generated PNG to a file.
Constructor Details
#initialize(options = {}) ⇒ Gchart
Returns a new instance of Gchart.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/gchart.rb', line 70 def initialize(={}) @type = [:type] || 'line' @data = [] @width = 300 @height = 200 @horizontal = false @grouped = false @encoding = 'simple' # @max_value = 'auto' # @min_value defaults to nil meaning zero @filename = [:filename] # 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 @klass = [:class] || false # set the options value if definable .each do |attribute, value| send("#{attribute}=", value) if self.respond_to?("#{attribute}=") end end |
Instance Attribute Details
#alt ⇒ Object
Returns the value of attribute alt.
36 37 38 |
# File 'lib/gchart.rb', line 36 def alt @alt end |
#axis_labels ⇒ Object
Returns the value of attribute axis_labels.
36 37 38 |
# File 'lib/gchart.rb', line 36 def axis_labels @axis_labels end |
#axis_range ⇒ Object
Returns the value of attribute axis_range.
36 37 38 |
# File 'lib/gchart.rb', line 36 def axis_range @axis_range end |
#axis_with_labels ⇒ Object
Returns the value of attribute axis_with_labels.
36 37 38 |
# File 'lib/gchart.rb', line 36 def axis_with_labels @axis_with_labels end |
#bar_colors ⇒ Object
Returns the value of attribute bar_colors.
36 37 38 |
# File 'lib/gchart.rb', line 36 def @bar_colors end |
#bar_width_and_spacing ⇒ Object
Returns the value of attribute bar_width_and_spacing.
36 37 38 |
# File 'lib/gchart.rb', line 36 def @bar_width_and_spacing end |
#bg_angle ⇒ Object
Returns the value of attribute bg_angle.
40 41 42 |
# File 'lib/gchart.rb', line 40 def bg_angle @bg_angle end |
#bg_color ⇒ Object
Returns the value of attribute bg_color.
40 41 42 |
# File 'lib/gchart.rb', line 40 def bg_color @bg_color end |
#bg_type ⇒ Object
Returns the value of attribute bg_type.
40 41 42 |
# File 'lib/gchart.rb', line 40 def bg_type @bg_type end |
#chart_angle ⇒ Object
Returns the value of attribute chart_angle.
40 41 42 |
# File 'lib/gchart.rb', line 40 def chart_angle @chart_angle end |
#chart_color ⇒ Object
Returns the value of attribute chart_color.
40 41 42 |
# File 'lib/gchart.rb', line 40 def chart_color @chart_color end |
#chart_type ⇒ Object
Returns the value of attribute chart_type.
40 41 42 |
# File 'lib/gchart.rb', line 40 def chart_type @chart_type end |
#colors ⇒ Object
Returns the value of attribute colors.
36 37 38 |
# File 'lib/gchart.rb', line 36 def colors @colors end |
#country_codes ⇒ Object
Returns the value of attribute country_codes.
36 37 38 |
# File 'lib/gchart.rb', line 36 def country_codes @country_codes end |
#custom ⇒ Object
Returns the value of attribute custom.
36 37 38 |
# File 'lib/gchart.rb', line 36 def custom @custom end |
#data ⇒ Object
Returns the value of attribute data.
36 37 38 |
# File 'lib/gchart.rb', line 36 def data @data end |
#encoding ⇒ Object
Returns the value of attribute encoding.
36 37 38 |
# File 'lib/gchart.rb', line 36 def encoding @encoding end |
#filename ⇒ Object
Returns the value of attribute filename.
36 37 38 |
# File 'lib/gchart.rb', line 36 def filename @filename end |
#geographical_area ⇒ Object
Returns the value of attribute geographical_area.
36 37 38 |
# File 'lib/gchart.rb', line 36 def geographical_area @geographical_area end |
#grouped ⇒ Object Also known as: grouped?
Returns the value of attribute grouped.
36 37 38 |
# File 'lib/gchart.rb', line 36 def grouped @grouped end |
#height ⇒ Object
Returns the value of attribute height.
36 37 38 |
# File 'lib/gchart.rb', line 36 def height @height end |
#horizontal ⇒ Object Also known as: horizontal?
Returns the value of attribute horizontal.
36 37 38 |
# File 'lib/gchart.rb', line 36 def horizontal @horizontal end |
#id ⇒ Object
Returns the value of attribute id.
36 37 38 |
# File 'lib/gchart.rb', line 36 def id @id end |
#klass ⇒ Object
Returns the value of attribute klass.
36 37 38 |
# File 'lib/gchart.rb', line 36 def klass @klass end |
#legend ⇒ Object
Returns the value of attribute legend.
36 37 38 |
# File 'lib/gchart.rb', line 36 def legend @legend end |
#map_colors ⇒ Object
Returns the value of attribute map_colors.
36 37 38 |
# File 'lib/gchart.rb', line 36 def map_colors @map_colors end |
#max ⇒ Object
Returns the value of attribute max.
36 37 38 |
# File 'lib/gchart.rb', line 36 def max @max end |
#max_value ⇒ Object
Returns the value of attribute max_value.
42 43 44 |
# File 'lib/gchart.rb', line 42 def max_value @max_value end |
#min ⇒ Object
Returns the value of attribute min.
36 37 38 |
# File 'lib/gchart.rb', line 36 def min @min end |
#min_value ⇒ Object
Returns the value of attribute min_value.
42 43 44 |
# File 'lib/gchart.rb', line 42 def min_value @min_value end |
#range_markers ⇒ Object
Returns the value of attribute range_markers.
36 37 38 |
# File 'lib/gchart.rb', line 36 def range_markers @range_markers end |
#title ⇒ Object
Returns the value of attribute title.
36 37 38 |
# File 'lib/gchart.rb', line 36 def title @title end |
#title_color ⇒ Object
Returns the value of attribute title_color.
36 37 38 |
# File 'lib/gchart.rb', line 36 def title_color @title_color end |
#title_size ⇒ Object
Returns the value of attribute title_size.
36 37 38 |
# File 'lib/gchart.rb', line 36 def title_size @title_size end |
#type ⇒ Object
Returns the value of attribute type.
36 37 38 |
# File 'lib/gchart.rb', line 36 def type @type end |
#width ⇒ Object
Returns the value of attribute width.
36 37 38 |
# File 'lib/gchart.rb', line 36 def width @width end |
Class Method Details
.chars ⇒ Object
24 25 26 |
# File 'lib/gchart.rb', line 24 def self.chars @chars ||= simple_chars + ['-', '.'] end |
.default_filename ⇒ Object
32 33 34 |
# File 'lib/gchart.rb', line 32 def self.default_filename 'chart.png' end |
.ext_pairs ⇒ Object
28 29 30 |
# File 'lib/gchart.rb', line 28 def self.ext_pairs @ext_pairs ||= chars.map { |char_1| chars.map { |char_2| char_1 + char_2 } }.flatten end |
.jstize(string) ⇒ Object
244 245 246 |
# File 'lib/gchart.rb', line 244 def self.jstize(string) string.gsub(' ', '+').gsub(/\[|\{|\}|\\|\^|\[|\]|\`|\]/) {|c| "%#{c[0].to_s.upcase}"} end |
.method_missing(m, options = {}) ⇒ Object
66 67 68 |
# File 'lib/gchart.rb', line 66 def self.method_missing(m, ={}) raise NoMethodError, "#{m} is not a supported chart format, please use one of the following: #{supported_types}." end |
.simple_chars ⇒ Object
20 21 22 |
# File 'lib/gchart.rb', line 20 def self.simple_chars @simple_chars ||= ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a end |
.supported_types ⇒ Object
93 94 95 |
# File 'lib/gchart.rb', line 93 def self.supported_types Gchart.types.join(' ') end |
.types ⇒ Object
16 17 18 |
# File 'lib/gchart.rb', line 16 def self.types @types ||= ['line', 'line_xy', 'scatter', 'bar', 'venn', 'pie', 'pie_3d', 'jstize', 'sparkline', 'meter', 'map'] end |
.url ⇒ Object
12 13 14 |
# File 'lib/gchart.rb', line 12 def self.url "http://chart.apis.google.com/chart?" end |
Instance Method Details
#bg=(options) ⇒ Object Also known as: background=
126 127 128 129 130 131 132 133 134 |
# File 'lib/gchart.rb', line 126 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
221 222 223 224 225 226 227 228 229 |
# File 'lib/gchart.rb', line 221 def dataset if @dataset @dataset else @dataset = convert_dataset(data || []) full_data_range(@dataset) # unless axis_range @dataset end end |
#datasets ⇒ Object
Sets of data to handle multiple sets
232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/gchart.rb', line 232 def datasets datasets = [] dataset.each do |d| if d[:data].first.is_a?(Array) datasets += d[:data] else datasets << d[:data] end end datasets end |
#dimensions ⇒ Object
107 108 109 110 |
# File 'lib/gchart.rb', line 107 def dimensions # TODO: maybe others? [:line_xy, :scatter].include?(type) ? 2 : 1 end |
#fetch ⇒ Object
Returns the chart’s generated PNG as a blob. (borrowed from John’s gchart.rubyforge.org)
251 252 253 |
# File 'lib/gchart.rb', line 251 def fetch open(query_builder) { |io| io.read } end |
#file ⇒ Object
282 283 284 |
# File 'lib/gchart.rb', line 282 def file write end |
#full_data_range(ds) ⇒ Object
returns the full data range as an array it also sets the data range if not defined
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/gchart.rb', line 164 def full_data_range(ds) return if max_value == false ds.each_with_index do |mds, mds_index| mds[:min_value] ||= min_value mds[:max_value] ||= max_value if mds_index == 0 && type.to_s == 'bar' # TODO: unless you specify a zero line (using chp or chds), # the min_value of a bar chart is always 0. #mds[:min_value] ||= mds[:data].first.to_a.compact.min mds[:min_value] ||= 0 end if (mds_index == 0 && type.to_s == 'bar' && !grouped && mds[:data].first.is_a?(Array)) totals = [] mds[:data].each do |l| l.each_with_index do |v, index| next if v.nil? totals[index] ||= 0 totals[index] += v end end mds[:max_value] ||= totals.compact.max else all = mds[:data].flatten.compact # default min value should be 0 unless set to auto if mds[:min_value] == 'auto' mds[:min_value] = all.min else min = all.min mds[:min_value] ||= (min && min < 0 ? min : 0) end mds[:max_value] ||= all.max end end unless axis_range @axis_range = ds.map{|mds| [mds[:min_value], mds[:max_value]]} if dimensions == 1 && (type.to_s != 'bar' || horizontal) tmp = axis_range.fetch(0, []) @axis_range[0] = axis_range.fetch(1, []) @axis_range[1] = tmp end end # return [min, max] unless (min.nil? || max.nil?) # @max = (max_value.nil? || max_value == 'auto') ? ds.compact.map{|mds| mds.compact.max}.max : max_value # # if min_value.nil? # min_ds_value = ds.compact.map{|mds| mds.compact.min}.min || 0 # @min = (min_ds_value < 0) ? min_ds_value : 0 # else # @min = min_value == 'auto' ? ds.compact.map{|mds| mds.compact.min}.min || 0 : min_value # end # @axis_range = [[min,max]] end |
#graph_bg=(options) ⇒ Object Also known as: chart_bg=, chart_color=, chart_background=
136 137 138 139 140 141 142 143 144 |
# File 'lib/gchart.rb', line 136 def graph_bg=() if .is_a?(String) @chart_color = elsif .is_a?(Hash) @chart_color = [:color] @chart_type = [:type] @chart_angle = [:angle] end end |
#image_tag ⇒ Object Also known as: img_tag
Format
264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/gchart.rb', line 264 def image_tag image = "<img" image += " id=\"#{id}\"" if id image += " class=\"#{klass}\"" if klass image += " src=\"#{query_builder(:html)}\"" image += " width=\"#{width}\"" image += " height=\"#{height}\"" image += " alt=\"#{alt}\"" image += " title=\"#{title}\"" if title image += " />" end |
#jstize(string) ⇒ Object
287 288 289 |
# File 'lib/gchart.rb', line 287 def jstize(string) Gchart.jstize(string) end |
#orientation=(orientation = 'h') ⇒ Object
Sets the orientation of a bar graph
113 114 115 116 117 118 119 |
# File 'lib/gchart.rb', line 113 def orientation=(orientation='h') if orientation == 'h' || orientation == 'horizontal' self.horizontal = true elsif orientation == 'v' || orientation == 'vertical' self.horizontal = false end end |
#size ⇒ Object
103 104 105 |
# File 'lib/gchart.rb', line 103 def size "#{width}x#{height}" end |
#size=(size = '300x200') ⇒ Object
Defines the Graph size using the following format: width X height
99 100 101 |
# File 'lib/gchart.rb', line 99 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)
122 123 124 |
# File 'lib/gchart.rb', line 122 def stacked=(option=true) @grouped = option ? false : true end |
#url ⇒ Object
278 279 280 |
# File 'lib/gchart.rb', line 278 def url query_builder end |
#write ⇒ Object
Writes the chart’s generated PNG to a file. (borrowed from John’s gchart.rubyforge.org)
256 257 258 259 260 |
# File 'lib/gchart.rb', line 256 def write io_or_file = filename || Gchart.default_filename return io_or_file.write(fetch) if io_or_file.respond_to?(:write) open(io_or_file, "wb+") { |io| io.write(fetch) } end |