Class: GChart::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gchart/base.rb

Direct Known Subclasses

Bar, Line, Pie, Scatter, Venn, XYLine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Base

Returns a new instance of Base.

Yields:

  • (_self)

Yield Parameters:

  • _self (GChart::Base)

    the object that the method was called on



30
31
32
33
34
35
36
37
38
# File 'lib/gchart/base.rb', line 30

def initialize(options={}, &block)
  @data = []
  @extras = {}
  @width = 300
  @height = 200
  
  options.each { |k, v| send("#{k}=", v) }
  yield(self) if block_given?
end

Instance Attribute Details

#colorsObject

Array of rrggbb colors, one per data set.



16
17
18
# File 'lib/gchart/base.rb', line 16

def colors
  @colors
end

#dataObject

Array of chart data. See subclasses for specific usage.



7
8
9
# File 'lib/gchart/base.rb', line 7

def data
  @data
end

#extrasObject

Hash of additional HTTP query params.



10
11
12
# File 'lib/gchart/base.rb', line 10

def extras
  @extras
end

#heightObject

Chart height, in pixels.



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

def height
  @height
end

#legendObject

Array of legend text, one per data set.



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

def legend
  @legend
end

#maxObject

Max data value for quantization.



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

def max
  @max
end

#titleObject

Chart title.



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

def title
  @title
end

#widthObject

Chart width, in pixels.



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

def width
  @width
end

Instance Method Details

#fetchObject

Returns the chart’s generated PNG as a blob.



82
83
84
# File 'lib/gchart/base.rb', line 82

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

#sizeObject

Returns the chart’s size as “WIDTHxHEIGHT”.



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

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

#size=(size) ⇒ Object

Sets the chart’s size as “WIDTHxHEIGHT”. Raises ArgumentError if width * height is greater than 300,000 pixels.



67
68
69
70
71
72
73
# File 'lib/gchart/base.rb', line 67

def size=(size)
  self.width, self.height = size.split("x").collect { |n| Integer(n) }
  
  if (width * height) > 300_000
    raise ArgumentError, "Invalid size: #{size.inspect} yields a graph with more than 300,000 pixels"
  end
end

#to_urlObject

Returns the chart’s URL.



76
77
78
79
# File 'lib/gchart/base.rb', line 76

def to_url
  query = query_params.collect { |k, v| "#{k}=#{URI.escape(v)}" }.join("&")
  "#{GChart::URL}?#{query}"
end

#write(io_or_file = "chart.png") ⇒ Object

Writes the chart’s generated PNG to a file. If io_or_file quacks like an IO, calls write on it instead.



88
89
90
91
# File 'lib/gchart/base.rb', line 88

def write(io_or_file="chart.png")
  return io_or_file.write(fetch) if io_or_file.respond_to?(:write)
  open(io_or_file, "w+") { |io| io.write(fetch) }
end