Class: Gchart
- Inherits:
-
Object
- Object
- Gchart
- Includes:
- GchartInfo
- Defined in:
- lib/gchart.rb,
lib/gchart/aliases.rb
Constant Summary
Constants included from GchartInfo
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.
-
#curved ⇒ Object
(also: #curved?)
Returns the value of attribute curved.
-
#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.
-
#grid_lines ⇒ Object
Returns the value of attribute grid_lines.
-
#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.
-
#labels ⇒ Object
Returns the value of attribute labels.
-
#legend ⇒ Object
Returns the value of attribute legend.
-
#legend_position ⇒ Object
Returns the value of attribute legend_position.
-
#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.
-
#new_markers ⇒ Object
Returns the value of attribute new_markers.
-
#range_markers ⇒ Object
Returns the value of attribute range_markers.
-
#thickness ⇒ Object
Returns the value of attribute thickness.
-
#title ⇒ Object
Returns the value of attribute title.
-
#title_alignment ⇒ Object
Returns the value of attribute title_alignment.
-
#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.
-
#use_ssl ⇒ Object
Returns the value of attribute use_ssl.
-
#usemap ⇒ Object
Returns the value of attribute usemap.
-
#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(use_ssl = false) ⇒ 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.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/gchart.rb', line 75 def initialize(={}) @type = [:type] || 'line' @data = [] @width = 300 @height = 200 @curved = false @horizontal = false @grouped = false @use_ssl = 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.
41 42 43 |
# File 'lib/gchart.rb', line 41 def alt @alt end |
#axis_labels ⇒ Object
Returns the value of attribute axis_labels.
41 42 43 |
# File 'lib/gchart.rb', line 41 def axis_labels @axis_labels end |
#axis_range ⇒ Object
Returns the value of attribute axis_range.
41 42 43 |
# File 'lib/gchart.rb', line 41 def axis_range @axis_range end |
#axis_with_labels ⇒ Object
Returns the value of attribute axis_with_labels.
41 42 43 |
# File 'lib/gchart.rb', line 41 def axis_with_labels @axis_with_labels end |
#bar_colors ⇒ Object
Returns the value of attribute bar_colors.
41 42 43 |
# File 'lib/gchart.rb', line 41 def @bar_colors end |
#bar_width_and_spacing ⇒ Object
Returns the value of attribute bar_width_and_spacing.
41 42 43 |
# File 'lib/gchart.rb', line 41 def @bar_width_and_spacing end |
#bg_angle ⇒ Object
Returns the value of attribute bg_angle.
45 46 47 |
# File 'lib/gchart.rb', line 45 def bg_angle @bg_angle end |
#bg_color ⇒ Object
Returns the value of attribute bg_color.
45 46 47 |
# File 'lib/gchart.rb', line 45 def bg_color @bg_color end |
#bg_type ⇒ Object
Returns the value of attribute bg_type.
45 46 47 |
# File 'lib/gchart.rb', line 45 def bg_type @bg_type end |
#chart_angle ⇒ Object
Returns the value of attribute chart_angle.
45 46 47 |
# File 'lib/gchart.rb', line 45 def chart_angle @chart_angle end |
#chart_color ⇒ Object
Returns the value of attribute chart_color.
45 46 47 |
# File 'lib/gchart.rb', line 45 def chart_color @chart_color end |
#chart_type ⇒ Object
Returns the value of attribute chart_type.
45 46 47 |
# File 'lib/gchart.rb', line 45 def chart_type @chart_type end |
#colors ⇒ Object
Returns the value of attribute colors.
41 42 43 |
# File 'lib/gchart.rb', line 41 def colors @colors end |
#country_codes ⇒ Object
Returns the value of attribute country_codes.
41 42 43 |
# File 'lib/gchart.rb', line 41 def country_codes @country_codes end |
#curved ⇒ Object Also known as: curved?
Returns the value of attribute curved.
41 42 43 |
# File 'lib/gchart.rb', line 41 def curved @curved end |
#custom ⇒ Object
Returns the value of attribute custom.
41 42 43 |
# File 'lib/gchart.rb', line 41 def custom @custom end |
#data ⇒ Object
Returns the value of attribute data.
41 42 43 |
# File 'lib/gchart.rb', line 41 def data @data end |
#encoding ⇒ Object
Returns the value of attribute encoding.
41 42 43 |
# File 'lib/gchart.rb', line 41 def encoding @encoding end |
#filename ⇒ Object
Returns the value of attribute filename.
41 42 43 |
# File 'lib/gchart.rb', line 41 def filename @filename end |
#geographical_area ⇒ Object
Returns the value of attribute geographical_area.
41 42 43 |
# File 'lib/gchart.rb', line 41 def geographical_area @geographical_area end |
#grid_lines ⇒ Object
Returns the value of attribute grid_lines.
45 46 47 |
# File 'lib/gchart.rb', line 45 def grid_lines @grid_lines end |
#grouped ⇒ Object Also known as: grouped?
Returns the value of attribute grouped.
41 42 43 |
# File 'lib/gchart.rb', line 41 def grouped @grouped end |
#height ⇒ Object
Returns the value of attribute height.
41 42 43 |
# File 'lib/gchart.rb', line 41 def height @height end |
#horizontal ⇒ Object Also known as: horizontal?
Returns the value of attribute horizontal.
41 42 43 |
# File 'lib/gchart.rb', line 41 def horizontal @horizontal end |
#id ⇒ Object
Returns the value of attribute id.
41 42 43 |
# File 'lib/gchart.rb', line 41 def id @id end |
#klass ⇒ Object
Returns the value of attribute klass.
41 42 43 |
# File 'lib/gchart.rb', line 41 def klass @klass end |
#labels ⇒ Object
Returns the value of attribute labels.
41 42 43 |
# File 'lib/gchart.rb', line 41 def labels @labels end |
#legend ⇒ Object
Returns the value of attribute legend.
41 42 43 |
# File 'lib/gchart.rb', line 41 def legend @legend end |
#legend_position ⇒ Object
Returns the value of attribute legend_position.
41 42 43 |
# File 'lib/gchart.rb', line 41 def legend_position @legend_position end |
#map_colors ⇒ Object
Returns the value of attribute map_colors.
41 42 43 |
# File 'lib/gchart.rb', line 41 def map_colors @map_colors end |
#max ⇒ Object
Returns the value of attribute max.
41 42 43 |
# File 'lib/gchart.rb', line 41 def max @max end |
#max_value ⇒ Object
Returns the value of attribute max_value.
47 48 49 |
# File 'lib/gchart.rb', line 47 def max_value @max_value end |
#min ⇒ Object
Returns the value of attribute min.
41 42 43 |
# File 'lib/gchart.rb', line 41 def min @min end |
#min_value ⇒ Object
Returns the value of attribute min_value.
47 48 49 |
# File 'lib/gchart.rb', line 47 def min_value @min_value end |
#new_markers ⇒ Object
Returns the value of attribute new_markers.
45 46 47 |
# File 'lib/gchart.rb', line 45 def new_markers @new_markers end |
#range_markers ⇒ Object
Returns the value of attribute range_markers.
41 42 43 |
# File 'lib/gchart.rb', line 41 def range_markers @range_markers end |
#thickness ⇒ Object
Returns the value of attribute thickness.
45 46 47 |
# File 'lib/gchart.rb', line 45 def thickness @thickness end |
#title ⇒ Object
Returns the value of attribute title.
41 42 43 |
# File 'lib/gchart.rb', line 41 def title @title end |
#title_alignment ⇒ Object
Returns the value of attribute title_alignment.
41 42 43 |
# File 'lib/gchart.rb', line 41 def title_alignment @title_alignment end |
#title_color ⇒ Object
Returns the value of attribute title_color.
41 42 43 |
# File 'lib/gchart.rb', line 41 def title_color @title_color end |
#title_size ⇒ Object
Returns the value of attribute title_size.
41 42 43 |
# File 'lib/gchart.rb', line 41 def title_size @title_size end |
#type ⇒ Object
Returns the value of attribute type.
41 42 43 |
# File 'lib/gchart.rb', line 41 def type @type end |
#use_ssl ⇒ Object
Returns the value of attribute use_ssl.
45 46 47 |
# File 'lib/gchart.rb', line 45 def use_ssl @use_ssl end |
#usemap ⇒ Object
Returns the value of attribute usemap.
41 42 43 |
# File 'lib/gchart.rb', line 41 def usemap @usemap end |
#width ⇒ Object
Returns the value of attribute width.
41 42 43 |
# File 'lib/gchart.rb', line 41 def width @width end |
Class Method Details
.chars ⇒ Object
29 30 31 |
# File 'lib/gchart.rb', line 29 def self.chars @chars ||= simple_chars + ['-', '.'] end |
.default_filename ⇒ Object
37 38 39 |
# File 'lib/gchart.rb', line 37 def self.default_filename 'chart.png' end |
.ext_pairs ⇒ Object
33 34 35 |
# File 'lib/gchart.rb', line 33 def self.ext_pairs @ext_pairs ||= chars.map { |char_1| chars.map { |char_2| char_1 + char_2 } }.flatten end |
.jstize(string) ⇒ Object
252 253 254 255 256 257 258 |
# File 'lib/gchart.rb', line 252 def self.jstize(string) # See http://github.com/mattetti/googlecharts/issues#issue/27 #URI.escape( string ).gsub("%7C", "|") # See discussion: http://github.com/mattetti/googlecharts/commit/9b5cfb93aa51aae06611057668e631cd515ec4f3#comment_51347 string.gsub(' ', '+').gsub(/\[|\{|\}|\\|\^|\[|\]|\`|\]/) {|c| "%#{c[0].to_s.upcase}"} #string.gsub(' ', '+').gsub(/\[|\{|\}|\||\\|\^|\[|\]|\`|\]/) {|c| "%#{c[0].to_s.upcase}"} end |
.method_missing(m, options = {}) ⇒ Object
71 72 73 |
# File 'lib/gchart.rb', line 71 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
25 26 27 |
# File 'lib/gchart.rb', line 25 def self.simple_chars @simple_chars ||= ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a end |
.supported_types ⇒ Object
100 101 102 |
# File 'lib/gchart.rb', line 100 def self.supported_types self.types.join(' ') end |
.types ⇒ Object
21 22 23 |
# File 'lib/gchart.rb', line 21 def self.types @types ||= ['line', 'line_xy', 'scatter', 'bar', 'venn', 'pie', 'pie_3d', 'jstize', 'sparkline', 'meter', 'map', 'radar'] end |
.url(use_ssl = false) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/gchart.rb', line 13 def self.url(use_ssl = false) if use_ssl 'https://chart.googleapis.com/chart?' else 'http://chart.apis.google.com/chart?' end end |
.version ⇒ Object
67 68 69 |
# File 'lib/gchart.rb', line 67 def self.version VERSION::STRING end |
Instance Method Details
#bg=(options) ⇒ Object Also known as: background=
133 134 135 136 137 138 139 140 141 |
# File 'lib/gchart.rb', line 133 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
229 230 231 232 233 234 235 236 237 |
# File 'lib/gchart.rb', line 229 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
240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/gchart.rb', line 240 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
114 115 116 117 |
# File 'lib/gchart.rb', line 114 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)
263 264 265 266 267 268 269 270 271 272 |
# File 'lib/gchart.rb', line 263 def fetch url = URI.parse(self.class.url(use_ssl)) req = Net::HTTP::Post.new(url.path) req.body = query_builder req.content_type = 'application/x-www-form-urlencoded' http = Net::HTTP.new(url.host, url.port) http.verify_mode = OpenSSL::SSL::VERIFY_PEER if use_ssl http.use_ssl = use_ssl http.start {|resp| resp.request(req) }.body end |
#file ⇒ Object
302 303 304 |
# File 'lib/gchart.rb', line 302 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
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 220 221 222 223 224 225 226 227 |
# File 'lib/gchart.rb', line 171 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 @calculated_axis_range = true @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=
143 144 145 146 147 148 149 150 151 |
# File 'lib/gchart.rb', line 143 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
283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/gchart.rb', line 283 def image_tag image = "<img" image += " id=\"#{id}\"" if id image += " class=\"#{klass}\"" if klass image += " src=\"#{url_builder(:html)}\"" image += " width=\"#{width}\"" image += " height=\"#{height}\"" image += " alt=\"#{alt}\"" image += " title=\"#{title}\"" if title image += " usemap=\"#{usemap}\"" if usemap image += " />" end |
#jstize(string) ⇒ Object
307 308 309 |
# File 'lib/gchart.rb', line 307 def jstize(string) self.class.jstize(string) end |
#orientation=(orientation = 'h') ⇒ Object
Sets the orientation of a bar graph
120 121 122 123 124 125 126 |
# File 'lib/gchart.rb', line 120 def orientation=(orientation='h') if orientation == 'h' || orientation == 'horizontal' self.horizontal = true elsif orientation == 'v' || orientation == 'vertical' self.horizontal = false end end |
#size ⇒ Object
110 111 112 |
# File 'lib/gchart.rb', line 110 def size "#{width}x#{height}" end |
#size=(size = '300x200') ⇒ Object
Defines the Graph size using the following format: width X height
106 107 108 |
# File 'lib/gchart.rb', line 106 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)
129 130 131 |
# File 'lib/gchart.rb', line 129 def stacked=(option=true) @grouped = option ? false : true end |
#url ⇒ Object
298 299 300 |
# File 'lib/gchart.rb', line 298 def url url_builder end |
#write ⇒ Object
Writes the chart’s generated PNG to a file. (borrowed from John’s gchart.rubyforge.org)
275 276 277 278 279 |
# File 'lib/gchart.rb', line 275 def write io_or_file = filename || self.class.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 |