Class: Gchart

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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
# File 'lib/gchart.rb', line 47

def initialize(options={})
    @type = :line
    @data = []
    @width = 300
    @height = 200
    @horizontal = false
    @grouped = false
    @encoding = 'simple'
    @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
    options.each do |attribute, value| 
        send("#{attribute.to_s}=", value) if self.respond_to?("#{attribute}=")
    end
end

Instance Attribute Details

#altObject

Returns the value of attribute alt.



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

def alt
  @alt
end

#axis_labelsObject

Returns the value of attribute axis_labels.



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

def axis_labels
  @axis_labels
end

#axis_rangeObject

Returns the value of attribute axis_range.



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

def axis_range
  @axis_range
end

#axis_with_labelsObject

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_colorsObject

Returns the value of attribute bar_colors.



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

def bar_colors
  @bar_colors
end

#bar_width_and_spacingObject

Returns the value of attribute bar_width_and_spacing.



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

def bar_width_and_spacing
  @bar_width_and_spacing
end

#classObject

Returns the value of attribute class.



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

def class
  @class
end

#country_codesObject

Returns the value of attribute country_codes.



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

def country_codes
  @country_codes
end

#customObject

Returns the value of attribute custom.



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

def custom
  @custom
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#encodingObject

Returns the value of attribute encoding.



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

def encoding
  @encoding
end

#geographical_areaObject

Returns the value of attribute geographical_area.



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

def geographical_area
  @geographical_area
end

#groupedObject Also known as: grouped?

Returns the value of attribute grouped.



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

def grouped
  @grouped
end

#heightObject

Returns the value of attribute height.



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

def height
  @height
end

#horizontalObject Also known as: horizontal?

Returns the value of attribute horizontal.



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

def horizontal
  @horizontal
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#legendObject

Returns the value of attribute legend.



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

def legend
  @legend
end

#map_colorsObject

Returns the value of attribute map_colors.



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

def map_colors
  @map_colors
end

#max_valueObject

Returns the value of attribute max_value.



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

def max_value
  @max_value
end

#min_valueObject

Returns the value of attribute min_value.



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

def min_value
  @min_value
end

#range_markersObject

Returns the value of attribute range_markers.



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

def range_markers
  @range_markers
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#title_colorObject

Returns the value of attribute title_color.



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

def title_color
  @title_color
end

#title_sizeObject

Returns the value of attribute title_size.



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

def title_size
  @title_size
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

#widthObject

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



137
138
139
# File 'lib/gchart.rb', line 137

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, options={})
  # Start with theme defaults if a theme is set
  theme = options[:theme]
  options = theme ? Chart::Theme.load(theme).to_options.merge(options) : options 
  # Extract the format and optional filename, then clean the hash
  format = options[:format] || 'url'
  @@file_name = options[:filename] unless options[:filename].nil?
  options.delete(:format)
  options.delete(:filename)
  #update map_colors to be bar_colors
  options.update(:bar_colors => options[:map_colors]) if options.has_key?(:map_colors)
  # create the chart and return it in the format asked for
  if @@types.include?(m.to_s)  
    chart = new(options.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_typesObject



69
70
71
# File 'lib/gchart.rb', line 69

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

Instance Method Details

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



97
98
99
100
101
102
103
104
105
# File 'lib/gchart.rb', line 97

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



131
132
133
134
135
# File 'lib/gchart.rb', line 131

def dataset
  @dataset ||= prepare_dataset(data)
  full_data_range(@dataset) unless @axis_range
  @dataset
end

#full_data_range(ds) ⇒ Object

returns the full data range as an array it also sets the data range if not defined



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gchart.rb', line 119

def full_data_range(ds)
  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_value == 'auto') 
    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  
  end 
  @axis_range = [[@min,@max]]
end

#graph_bg=(options) ⇒ Object Also known as: chart_bg=, chart_color=, chart_background=



107
108
109
110
111
112
113
114
115
# File 'lib/gchart.rb', line 107

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

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

Sets the orientation of a bar graph



84
85
86
87
88
89
90
# File 'lib/gchart.rb', line 84

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

#sizeObject



79
80
81
# File 'lib/gchart.rb', line 79

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

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

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



75
76
77
# File 'lib/gchart.rb', line 75

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)



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

def stacked=(option=true)
 @grouped = option ? false : true
end