Class: GChart::Base

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

Direct Known Subclasses

Bar, Line, Map, Meter, Pie, Scatter, Sparkline, 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



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gchart/base.rb', line 39

def initialize(options={}, &block)
  @data   = []
  @colors = []
  @legend = []
  @axes   = []
  @extras = {}

  @width = 300
  @height = 200
  
  options.each { |k, v| send("#{k}=", v) }
  yield(self) if block_given?
end

Instance Attribute Details

#axesObject

Array of GChart::Axis objects.



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

def axes
  @axes
end

#chart_backgroundObject

Background rrggbb color of just chart area of chart image.



34
35
36
# File 'lib/gchart/base.rb', line 34

def chart_background
  @chart_background
end

#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

#entire_backgroundObject

Background rrggbb color of entire chart image.



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

def entire_background
  @entire_background
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

#axis(axis_type, &block) ⇒ Object

Adds an axis_type GChart::Axis to the chart’s set of axes. See GChart::Axis::AXIS_TYPES.



108
109
110
111
112
# File 'lib/gchart/base.rb', line 108

def axis(axis_type, &block)
  axis = GChart::Axis.create(axis_type, &block)
  @axes.push(axis)
  axis
end

#fetchObject

Returns the chart’s generated PNG as a blob.



95
96
97
# File 'lib/gchart/base.rb', line 95

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

#sizeObject

Returns the chart’s size as “WIDTHxHEIGHT”.



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

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.



80
81
82
83
84
85
86
# File 'lib/gchart/base.rb', line 80

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.



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

def to_url
  pluck_out_data_points! if url_to_try.length > GChart::URL_MAXIMUM_LENGTH
  url_to_try
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.



101
102
103
104
# File 'lib/gchart/base.rb', line 101

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