Class: SmartChart::BaseChart

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_chart/base_chart.rb

Overview

Method names are called attributes, data for URL are called parameters. Use attr_writers for all attributes, and wrte readers so they instantiate the correct object type.

Direct Known Subclasses

MultipleDataSetChart, SingleDataSetChart

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BaseChart

Accept attributes and attempt to assign each to an attribute.



56
57
58
59
60
61
62
63
64
# File 'lib/smart_chart/base_chart.rb', line 56

def initialize(options = {})
  options.each do |k,v|
    begin
      send("#{k}=", v)
    rescue NoMethodError
      raise NoAttributeError.new(self, k)
    end
  end
end

Instance Attribute Details

#backgroundObject

chart background



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

def background
  @background
end

#dataObject

chart data



30
31
32
# File 'lib/smart_chart/base_chart.rb', line 30

def data
  @data
end

#heightObject

dimensions of chart image, in pixels



27
28
29
# File 'lib/smart_chart/base_chart.rb', line 27

def height
  @height
end

#legendObject

legend properties



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

def legend
  @legend
end

#marginsObject

chart margins



39
40
41
# File 'lib/smart_chart/base_chart.rb', line 39

def margins
  @margins
end

#orientationObject

bar chart orientation – :vertical (default) or :horizontal pie chart orientation – degrees of rotation



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

def orientation
  @orientation
end

#styleObject

bar – :grouped (default) or :stacked pie – nil (2D, default), “3d”, or :concentric radar – nil (default) or :filled



51
52
53
# File 'lib/smart_chart/base_chart.rb', line 51

def style
  @style
end

#widthObject

dimensions of chart image, in pixels



27
28
29
# File 'lib/smart_chart/base_chart.rb', line 27

def width
  @width
end

#y_max=(value) ⇒ Object

chart range



33
34
35
# File 'lib/smart_chart/base_chart.rb', line 33

def y_max=(value)
  @y_max = value
end

#y_min=(value) ⇒ Object

chart range



33
34
35
# File 'lib/smart_chart/base_chart.rb', line 33

def y_min=(value)
  @y_min = value
end

Instance Method Details

#to_html(encode = true, validation = true, attributes = {}) ⇒ Object

Chart as an HTML tag.



85
86
87
# File 'lib/smart_chart/base_chart.rb', line 85

def to_html(encode = true, validation = true, attributes = {})
  '<img src="%s"%s />' % [to_url(encode, validation), tag_attributes(attributes)]
end

#to_query_string(encode = true, validation = true) ⇒ Object

Get the chart URL query string.



69
70
71
72
# File 'lib/smart_chart/base_chart.rb', line 69

def to_query_string(encode = true, validation = true)
  validate if validation
  query_string(encode)
end

#to_url(encode = true, validation = true) ⇒ Object

Get the full chart URL.



77
78
79
80
# File 'lib/smart_chart/base_chart.rb', line 77

def to_url(encode = true, validation = true)
  "http://chart.apis.google.com/chart?" +
    to_query_string(encode, validation)
end

#valid?Boolean

Does the chart pass all validations?

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
# File 'lib/smart_chart/base_chart.rb', line 99

def valid?
  begin
    validate
    true
  rescue ValidationError
    false
  end
end

#validate!Object

Run validation (may raise exceptions).



92
93
94
# File 'lib/smart_chart/base_chart.rb', line 92

def validate!
  validate
end