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']
@@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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Gchart

Returns a new instance of Gchart.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gchart.rb', line 30

def initialize(options={})
    @type = :line
    @data = []
    @width = 300
    @height = 200
    @horizontal = false
    @grouped = false
    @encoding = 'simple'
    @max_value = 'auto'

    # 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

#axis_labelsObject

Returns the value of attribute axis_labels.



14
15
16
# File 'lib/gchart.rb', line 14

def axis_labels
  @axis_labels
end

#axis_with_labelsObject

Returns the value of attribute axis_with_labels.



14
15
16
# File 'lib/gchart.rb', line 14

def axis_with_labels
  @axis_with_labels
end

#bar_colorsObject

Returns the value of attribute bar_colors.



14
15
16
# File 'lib/gchart.rb', line 14

def bar_colors
  @bar_colors
end

#customObject

Returns the value of attribute custom.



14
15
16
# File 'lib/gchart.rb', line 14

def custom
  @custom
end

#dataObject

Returns the value of attribute data.



14
15
16
# File 'lib/gchart.rb', line 14

def data
  @data
end

#encodingObject

Returns the value of attribute encoding.



14
15
16
# File 'lib/gchart.rb', line 14

def encoding
  @encoding
end

#groupedObject Also known as: grouped?

Returns the value of attribute grouped.



14
15
16
# File 'lib/gchart.rb', line 14

def grouped
  @grouped
end

#heightObject

Returns the value of attribute height.



14
15
16
# File 'lib/gchart.rb', line 14

def height
  @height
end

#horizontalObject Also known as: horizontal?

Returns the value of attribute horizontal.



14
15
16
# File 'lib/gchart.rb', line 14

def horizontal
  @horizontal
end

#legendObject

Returns the value of attribute legend.



14
15
16
# File 'lib/gchart.rb', line 14

def legend
  @legend
end

#max_valueObject

Returns the value of attribute max_value.



14
15
16
# File 'lib/gchart.rb', line 14

def max_value
  @max_value
end

#titleObject

Returns the value of attribute title.



14
15
16
# File 'lib/gchart.rb', line 14

def title
  @title
end

#title_colorObject

Returns the value of attribute title_color.



14
15
16
# File 'lib/gchart.rb', line 14

def title_color
  @title_color
end

#title_sizeObject

Returns the value of attribute title_size.



14
15
16
# File 'lib/gchart.rb', line 14

def title_size
  @title_size
end

#typeObject

Returns the value of attribute type.



14
15
16
# File 'lib/gchart.rb', line 14

def type
  @type
end

#widthObject

Returns the value of attribute width.



14
15
16
# File 'lib/gchart.rb', line 14

def width
  @width
end

Class Method Details

.method_missing(m, options = {}, format = 'raw') ⇒ Object

Support for Gchart.line(:title => ‘my title’, :size => ‘400x600’)



19
20
21
22
23
24
25
26
# File 'lib/gchart.rb', line 19

def method_missing(m, options={}, format='raw')
  if @@types.include?(m.to_s)
    chart = new(options.merge!({:type => m}))
    chart.send(format)
  else
    "#{m} is not a supported chart format, please use one of the following: #{supported_types}."
  end  
end

.supported_typesObject



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

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

Instance Method Details

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



74
75
76
77
78
79
80
81
82
# File 'lib/gchart.rb', line 74

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

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



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

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



61
62
63
64
65
66
67
# File 'lib/gchart.rb', line 61

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

#sizeObject



56
57
58
# File 'lib/gchart.rb', line 56

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

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

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



52
53
54
# File 'lib/gchart.rb', line 52

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)



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

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