Class: GChart::Base
- Inherits:
-
Object
- Object
- GChart::Base
- Defined in:
- lib/gchart/base.rb
Instance Attribute Summary collapse
-
#axes ⇒ Object
Array of
GChart::Axis
objects. -
#chart_background ⇒ Object
Background rrggbb color of just chart area of chart image.
-
#colors ⇒ Object
Array of rrggbb colors, one per data set.
-
#data ⇒ Object
Array of chart data.
-
#entire_background ⇒ Object
Background rrggbb color of entire chart image.
-
#extras ⇒ Object
Hash of additional HTTP query params.
-
#height ⇒ Object
Chart height, in pixels.
-
#legend ⇒ Object
Array of legend text, one per data set.
-
#max ⇒ Object
Max data value for quantization.
-
#title ⇒ Object
Chart title.
-
#width ⇒ Object
Chart width, in pixels.
Instance Method Summary collapse
-
#axis(axis_type, &block) ⇒ Object
Adds an
axis_type
GChart::Axis
to the chart’s set ofaxes
. -
#fetch ⇒ Object
Returns the chart’s generated PNG as a blob.
-
#initialize(options = {}) {|_self| ... } ⇒ Base
constructor
A new instance of Base.
-
#size ⇒ Object
Returns the chart’s size as “WIDTHxHEIGHT”.
-
#size=(size) ⇒ Object
Sets the chart’s size as “WIDTHxHEIGHT”.
-
#to_url ⇒ Object
Returns the chart’s URL.
-
#write(io_or_file = "chart.png") ⇒ Object
Writes the chart’s generated PNG to a file.
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ Base
Returns a new instance of Base.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/gchart/base.rb', line 39 def initialize(={}, &block) @data = [] @colors = [] @legend = [] @axes = [] @extras = {} @width = 300 @height = 200 .each { |k, v| send("#{k}=", v) } yield(self) if block_given? end |
Instance Attribute Details
#axes ⇒ Object
Array of GChart::Axis
objects.
37 38 39 |
# File 'lib/gchart/base.rb', line 37 def axes @axes end |
#chart_background ⇒ Object
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 |
#colors ⇒ Object
Array of rrggbb colors, one per data set.
16 17 18 |
# File 'lib/gchart/base.rb', line 16 def colors @colors end |
#data ⇒ Object
Array of chart data. See subclasses for specific usage.
7 8 9 |
# File 'lib/gchart/base.rb', line 7 def data @data end |
#entire_background ⇒ Object
Background rrggbb color of entire chart image.
31 32 33 |
# File 'lib/gchart/base.rb', line 31 def entire_background @entire_background end |
#extras ⇒ Object
Hash of additional HTTP query params.
10 11 12 |
# File 'lib/gchart/base.rb', line 10 def extras @extras end |
#height ⇒ Object
Chart height, in pixels.
28 29 30 |
# File 'lib/gchart/base.rb', line 28 def height @height end |
#legend ⇒ Object
Array of legend text, one per data set.
19 20 21 |
# File 'lib/gchart/base.rb', line 19 def legend @legend end |
#max ⇒ Object
Max data value for quantization.
22 23 24 |
# File 'lib/gchart/base.rb', line 22 def max @max end |
#title ⇒ Object
Chart title.
13 14 15 |
# File 'lib/gchart/base.rb', line 13 def title @title end |
#width ⇒ Object
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 |
#fetch ⇒ Object
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 |
#size ⇒ Object
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_url ⇒ Object
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 |