Class: Gchart

Inherits:
Object
  • Object
show all
Includes:
GchartInfo
Defined in:
lib/gchart.rb,
lib/gchart/aliases.rb

Constant Summary

Constants included from GchartInfo

GchartInfo::VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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
99
100
101
102
103
104
105
106
# File 'lib/gchart.rb', line 75

def initialize(options={})
  # Allow Gchart to take a theme too
  @theme = options[:theme] 
  options = @theme ? Chart::Theme.load(@theme).to_options.merge(options) : options
  options.delete(:theme)

  @type = options[:type] || 'line'
  @data = []
  @width = 300
  @height = 200
  @curved = false
  @horizontal = false

  @grouped = false
  @overlapped = false

  @use_ssl = false
  @encoding = 'simple'
  # @max_value = 'auto'
  # @min_value defaults to nil meaning zero
  @filename = options[: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 = options[:class] || false
  # set the options value if definable
  options.each do |attribute, value| 
    send("#{attribute}=", value) if self.respond_to?("#{attribute}=")
  end
end

Instance Attribute Details

#altObject

Returns the value of attribute alt.



41
42
43
# File 'lib/gchart.rb', line 41

def alt
  @alt
end

#axis_labelsObject

Returns the value of attribute axis_labels.



41
42
43
# File 'lib/gchart.rb', line 41

def axis_labels
  @axis_labels
end

#axis_rangeObject

Returns the value of attribute axis_range.



41
42
43
# File 'lib/gchart.rb', line 41

def axis_range
  @axis_range
end

#axis_with_labelsObject

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_colorsObject

Returns the value of attribute bar_colors.



41
42
43
# File 'lib/gchart.rb', line 41

def bar_colors
  @bar_colors
end

#bar_width_and_spacingObject

Returns the value of attribute bar_width_and_spacing.



41
42
43
# File 'lib/gchart.rb', line 41

def bar_width_and_spacing
  @bar_width_and_spacing
end

#bg_angleObject

Returns the value of attribute bg_angle.



45
46
47
# File 'lib/gchart.rb', line 45

def bg_angle
  @bg_angle
end

#bg_colorObject

Returns the value of attribute bg_color.



45
46
47
# File 'lib/gchart.rb', line 45

def bg_color
  @bg_color
end

#bg_typeObject

Returns the value of attribute bg_type.



45
46
47
# File 'lib/gchart.rb', line 45

def bg_type
  @bg_type
end

#chart_angleObject

Returns the value of attribute chart_angle.



45
46
47
# File 'lib/gchart.rb', line 45

def chart_angle
  @chart_angle
end

#chart_colorObject

Returns the value of attribute chart_color.



45
46
47
# File 'lib/gchart.rb', line 45

def chart_color
  @chart_color
end

#chart_typeObject

Returns the value of attribute chart_type.



45
46
47
# File 'lib/gchart.rb', line 45

def chart_type
  @chart_type
end

#colorsObject

Returns the value of attribute colors.



41
42
43
# File 'lib/gchart.rb', line 41

def colors
  @colors
end

#country_codesObject

Returns the value of attribute country_codes.



41
42
43
# File 'lib/gchart.rb', line 41

def country_codes
  @country_codes
end

#curvedObject Also known as: curved?

Returns the value of attribute curved.



41
42
43
# File 'lib/gchart.rb', line 41

def curved
  @curved
end

#customObject

Returns the value of attribute custom.



41
42
43
# File 'lib/gchart.rb', line 41

def custom
  @custom
end

#custom_axis_with_labelsObject

Returns the value of attribute custom_axis_with_labels.



41
42
43
# File 'lib/gchart.rb', line 41

def custom_axis_with_labels
  @custom_axis_with_labels
end

#dataObject

Returns the value of attribute data.



41
42
43
# File 'lib/gchart.rb', line 41

def data
  @data
end

#encodingObject

Returns the value of attribute encoding.



41
42
43
# File 'lib/gchart.rb', line 41

def encoding
  @encoding
end

#filenameObject

Returns the value of attribute filename.



41
42
43
# File 'lib/gchart.rb', line 41

def filename
  @filename
end

#geographical_areaObject

Returns the value of attribute geographical_area.



41
42
43
# File 'lib/gchart.rb', line 41

def geographical_area
  @geographical_area
end

#grid_linesObject

Returns the value of attribute grid_lines.



45
46
47
# File 'lib/gchart.rb', line 45

def grid_lines
  @grid_lines
end

#groupedObject

Returns the value of attribute grouped.



41
42
43
# File 'lib/gchart.rb', line 41

def grouped
  @grouped
end

#heightObject

Returns the value of attribute height.



41
42
43
# File 'lib/gchart.rb', line 41

def height
  @height
end

#horizontalObject Also known as: horizontal?

Returns the value of attribute horizontal.



41
42
43
# File 'lib/gchart.rb', line 41

def horizontal
  @horizontal
end

#idObject

Returns the value of attribute id.



41
42
43
# File 'lib/gchart.rb', line 41

def id
  @id
end

#klassObject

Returns the value of attribute klass.



41
42
43
# File 'lib/gchart.rb', line 41

def klass
  @klass
end

#labelsObject

Returns the value of attribute labels.



41
42
43
# File 'lib/gchart.rb', line 41

def labels
  @labels
end

#legendObject

Returns the value of attribute legend.



41
42
43
# File 'lib/gchart.rb', line 41

def legend
  @legend
end

#legend_positionObject

Returns the value of attribute legend_position.



41
42
43
# File 'lib/gchart.rb', line 41

def legend_position
  @legend_position
end

#map_colorsObject

Returns the value of attribute map_colors.



41
42
43
# File 'lib/gchart.rb', line 41

def map_colors
  @map_colors
end

#maxObject

Returns the value of attribute max.



41
42
43
# File 'lib/gchart.rb', line 41

def max
  @max
end

#max_valueObject

Returns the value of attribute max_value.



47
48
49
# File 'lib/gchart.rb', line 47

def max_value
  @max_value
end

#minObject

Returns the value of attribute min.



41
42
43
# File 'lib/gchart.rb', line 41

def min
  @min
end

#min_valueObject

Returns the value of attribute min_value.



47
48
49
# File 'lib/gchart.rb', line 47

def min_value
  @min_value
end

#new_markersObject

Returns the value of attribute new_markers.



45
46
47
# File 'lib/gchart.rb', line 45

def new_markers
  @new_markers
end

#overlappedObject

Returns the value of attribute overlapped.



41
42
43
# File 'lib/gchart.rb', line 41

def overlapped
  @overlapped
end

#range_markersObject

Returns the value of attribute range_markers.



41
42
43
# File 'lib/gchart.rb', line 41

def range_markers
  @range_markers
end

#thicknessObject

Returns the value of attribute thickness.



45
46
47
# File 'lib/gchart.rb', line 45

def thickness
  @thickness
end

#titleObject

Returns the value of attribute title.



41
42
43
# File 'lib/gchart.rb', line 41

def title
  @title
end

#title_alignmentObject

Returns the value of attribute title_alignment.



41
42
43
# File 'lib/gchart.rb', line 41

def title_alignment
  @title_alignment
end

#title_colorObject

Returns the value of attribute title_color.



41
42
43
# File 'lib/gchart.rb', line 41

def title_color
  @title_color
end

#title_sizeObject

Returns the value of attribute title_size.



41
42
43
# File 'lib/gchart.rb', line 41

def title_size
  @title_size
end

#typeObject

Returns the value of attribute type.



41
42
43
# File 'lib/gchart.rb', line 41

def type
  @type
end

#use_sslObject

Returns the value of attribute use_ssl.



45
46
47
# File 'lib/gchart.rb', line 45

def use_ssl
  @use_ssl
end

#usemapObject

Returns the value of attribute usemap.



41
42
43
# File 'lib/gchart.rb', line 41

def usemap
  @usemap
end

#widthObject

Returns the value of attribute width.



41
42
43
# File 'lib/gchart.rb', line 41

def width
  @width
end

Class Method Details

.charsObject



29
30
31
# File 'lib/gchart.rb', line 29

def self.chars
  @chars ||= simple_chars + ['-', '.']
end

.default_filenameObject



37
38
39
# File 'lib/gchart.rb', line 37

def self.default_filename
  'chart.png'
end

.ext_pairsObject



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



265
266
267
268
269
270
271
# File 'lib/gchart.rb', line 265

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

Raises:

  • (NoMethodError)


71
72
73
# File 'lib/gchart.rb', line 71

def self.method_missing(m, options={})
  raise NoMethodError, "#{m} is not a supported chart format. Please use one of the following: #{supported_types}."
end

.simple_charsObject



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_typesObject



108
109
110
# File 'lib/gchart.rb', line 108

def self.supported_types
  self.types.join(' ')
end

.typesObject



21
22
23
# File 'lib/gchart.rb', line 21

def self.types
  @types ||= ['line', 'line_xy', 'scatter', 'bar', 'venn', 'pie', 'pie_3d', 'pie_c', '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

.versionObject



67
68
69
# File 'lib/gchart.rb', line 67

def self.version
  VERSION::STRING
end

Instance Method Details

#bar_presentationObject



136
137
138
139
140
141
142
143
144
# File 'lib/gchart.rb', line 136

def bar_presentation
  if @overlapped
    'o'
  elsif @grouped
    'g'
  else
    's'
  end
end

#bg=(options) ⇒ Object Also known as: background=



146
147
148
149
150
151
152
153
154
# File 'lib/gchart.rb', line 146

def bg=(options)
  if options.is_a?(String)
    @bg_color = options
  elsif options.is_a?(Hash)
    @bg_color = options[:color]
    @bg_type  = options[:type]
    @bg_angle = options[:angle]
  end
end

#datasetObject



242
243
244
245
246
247
248
249
250
# File 'lib/gchart.rb', line 242

def dataset
  if @dataset
    @dataset 
  else
    @dataset = convert_dataset(data || [])
    full_data_range(@dataset)   # unless axis_range
    @dataset
  end
end

#datasetsObject

Sets of data to handle multiple sets



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/gchart.rb', line 253

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

#dimensionsObject



122
123
124
125
# File 'lib/gchart.rb', line 122

def dimensions
  # TODO: maybe others?
  [:line_xy, :scatter].include?(type) ? 2 : 1
end

#fetchObject

Returns the chart’s generated PNG as a blob. (borrowed from John’s gchart.rubyforge.org)



276
277
278
279
280
281
282
283
284
285
# File 'lib/gchart.rb', line 276

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

#fileObject



315
316
317
# File 'lib/gchart.rb', line 315

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



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
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/gchart.rb', line 184

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=



156
157
158
159
160
161
162
163
164
# File 'lib/gchart.rb', line 156

def graph_bg=(options)
  if options.is_a?(String)
    @chart_color = options
  elsif options.is_a?(Hash)
    @chart_color = options[:color]
    @chart_type  =  options[:type]
    @chart_angle = options[:angle]
  end
end

#image_tagObject Also known as: img_tag

Format



296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/gchart.rb', line 296

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



320
321
322
# File 'lib/gchart.rb', line 320

def jstize(string)
  self.class.jstize(string)
end

#orientation=(orientation = 'h') ⇒ Object

Sets the orientation of a bar graph



128
129
130
131
132
133
134
# File 'lib/gchart.rb', line 128

def orientation=(orientation='h')
  if orientation == 'h' || orientation == 'horizontal'
    self.horizontal = true
  elsif orientation == 'v' || orientation == 'vertical'
    self.horizontal = false
  end
end

#sizeObject



118
119
120
# File 'lib/gchart.rb', line 118

def size
  "#{width}x#{height}"
end

#size=(size = '300x200') ⇒ Object

Defines the Graph size using the following format: width X height



114
115
116
# File 'lib/gchart.rb', line 114

def size=(size='300x200')
  @width, @height = size.split("x").map { |dimension| dimension.to_i }
end

#urlObject



311
312
313
# File 'lib/gchart.rb', line 311

def url
  url_builder
end

#writeObject

Writes the chart’s generated PNG to a file. (borrowed from John’s gchart.rubyforge.org)



288
289
290
291
292
# File 'lib/gchart.rb', line 288

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