Class: Gchart

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options={})
  @type = options[:type] || 'line'
  @data = []
  @width = 300
  @height = 200
  @horizontal = false
  @grouped = 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.



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

def alt
  @alt
end

#axis_labelsObject

Returns the value of attribute axis_labels.



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

def axis_labels
  @axis_labels
end

#axis_rangeObject

Returns the value of attribute axis_range.



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

def axis_range
  @axis_range
end

#axis_with_labelsObject

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_colorsObject

Returns the value of attribute bar_colors.



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

def bar_colors
  @bar_colors
end

#bar_width_and_spacingObject

Returns the value of attribute bar_width_and_spacing.



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

def bar_width_and_spacing
  @bar_width_and_spacing
end

#bg_angleObject

Returns the value of attribute bg_angle.



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

def bg_angle
  @bg_angle
end

#bg_colorObject

Returns the value of attribute bg_color.



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

def bg_color
  @bg_color
end

#bg_typeObject

Returns the value of attribute bg_type.



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

def bg_type
  @bg_type
end

#chart_angleObject

Returns the value of attribute chart_angle.



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

def chart_angle
  @chart_angle
end

#chart_colorObject

Returns the value of attribute chart_color.



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

def chart_color
  @chart_color
end

#chart_typeObject

Returns the value of attribute chart_type.



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

def chart_type
  @chart_type
end

#colorsObject

Returns the value of attribute colors.



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

def colors
  @colors
end

#country_codesObject

Returns the value of attribute country_codes.



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

def country_codes
  @country_codes
end

#customObject

Returns the value of attribute custom.



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

def custom
  @custom
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#encodingObject

Returns the value of attribute encoding.



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

def encoding
  @encoding
end

#filenameObject

Returns the value of attribute filename.



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

def filename
  @filename
end

#geographical_areaObject

Returns the value of attribute geographical_area.



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

def geographical_area
  @geographical_area
end

#groupedObject Also known as: grouped?

Returns the value of attribute grouped.



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

def grouped
  @grouped
end

#heightObject

Returns the value of attribute height.



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

def height
  @height
end

#horizontalObject Also known as: horizontal?

Returns the value of attribute horizontal.



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

def horizontal
  @horizontal
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#klassObject

Returns the value of attribute klass.



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

def klass
  @klass
end

#legendObject

Returns the value of attribute legend.



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

def legend
  @legend
end

#map_colorsObject

Returns the value of attribute map_colors.



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

def map_colors
  @map_colors
end

#maxObject

Returns the value of attribute max.



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

def max
  @max
end

#max_valueObject

Returns the value of attribute max_value.



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

def max_value
  @max_value
end

#minObject

Returns the value of attribute min.



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

def min
  @min
end

#min_valueObject

Returns the value of attribute min_value.



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

def min_value
  @min_value
end

#range_markersObject

Returns the value of attribute range_markers.



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

def range_markers
  @range_markers
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#title_colorObject

Returns the value of attribute title_color.



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

def title_color
  @title_color
end

#title_sizeObject

Returns the value of attribute title_size.



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

def title_size
  @title_size
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

#widthObject

Returns the value of attribute width.



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

def width
  @width
end

Class Method Details

.charsObject



24
25
26
# File 'lib/gchart.rb', line 24

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

.default_filenameObject



32
33
34
# File 'lib/gchart.rb', line 32

def self.default_filename
  'chart.png'
end

.ext_pairsObject



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

Raises:

  • (NoMethodError)


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

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



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_typesObject



93
94
95
# File 'lib/gchart.rb', line 93

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

.typesObject



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

.urlObject



12
13
14
# File 'lib/gchart.rb', line 12

def self.url
  "http://chart.apis.google.com/chart?" 
end

.versionObject



62
63
64
# File 'lib/gchart.rb', line 62

def self.version
  Gchart::VERSION::STRING
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=(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



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

#datasetsObject

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

#dimensionsObject



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

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)



251
252
253
# File 'lib/gchart.rb', line 251

def fetch
  open(query_builder) { |io| io.read }
end

#fileObject



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=(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



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

#sizeObject



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

#urlObject



278
279
280
# File 'lib/gchart.rb', line 278

def url
  query_builder
end

#writeObject

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