Class: StaticChart
- Inherits:
-
Object
- Object
- StaticChart
- Defined in:
- app/models/static_chart.rb,
app/models/static_chart/area.rb
Overview
This class provides view helpers for drawing pie, bar, area, and other kinds of graphs.
Its current implementation relies on Google Image Charts which has been deprecated. That service will cease in April 2015.
Tentatively, I plan to keep the API that this helper publishes but re-implement it to generate SVG charts, server-side.
Google limits charts to 300000 pixels
Direct Known Subclasses
Defined Under Namespace
Classes: Area
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #axes ⇒ Object
- #bg ⇒ Object
- #colors ⇒ Object
- #data ⇒ Object
- #defaults ⇒ Object
- #empty? ⇒ Boolean
- #font_size ⇒ Object
- #height ⇒ Object
- #img_height ⇒ Object
- #img_width ⇒ Object
-
#initialize(options = {}) ⇒ StaticChart
constructor
A new instance of StaticChart.
- #labels ⇒ Object
- #render_graph ⇒ Object
- #retina? ⇒ Boolean
- #size ⇒ Object
- #src ⇒ Object
- #title ⇒ Object
- #to_s ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ StaticChart
Returns a new instance of StaticChart.
14 15 16 17 18 19 20 21 |
# File 'app/models/static_chart.rb', line 14 def initialize(={}) if .key?(:data_by_color) hash = .delete(:data_by_color) .merge!(data: hash.values, colors: hash.keys) end @options = defaults.merge() end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
23 24 25 |
# File 'app/models/static_chart.rb', line 23 def @options end |
Instance Method Details
#axes ⇒ Object
74 75 76 |
# File 'app/models/static_chart.rb', line 74 def axes [:axes] end |
#bg ⇒ Object
82 83 84 |
# File 'app/models/static_chart.rb', line 82 def bg [:bg] end |
#colors ⇒ Object
66 67 68 |
# File 'app/models/static_chart.rb', line 66 def colors [:colors] end |
#data ⇒ Object
62 63 64 |
# File 'app/models/static_chart.rb', line 62 def data [:data] end |
#defaults ⇒ Object
25 26 27 28 |
# File 'app/models/static_chart.rb', line 25 def defaults { font_size: 9, bg: "FFFFFF00" } # transparent background end |
#empty? ⇒ Boolean
86 87 88 |
# File 'app/models/static_chart.rb', line 86 def empty? data.flatten.empty? end |
#font_size ⇒ Object
52 53 54 |
# File 'app/models/static_chart.rb', line 52 def font_size retina? ? [:font_size] * 2 : [:font_size] end |
#height ⇒ Object
36 37 38 |
# File 'app/models/static_chart.rb', line 36 def height [:height] end |
#img_height ⇒ Object
48 49 50 |
# File 'app/models/static_chart.rb', line 48 def img_height retina? ? height * 2 : height end |
#img_width ⇒ Object
44 45 46 |
# File 'app/models/static_chart.rb', line 44 def img_width retina? ? width * 2 : width end |
#labels ⇒ Object
70 71 72 |
# File 'app/models/static_chart.rb', line 70 def labels [:labels] end |
#render_graph ⇒ Object
96 97 98 99 100 |
# File 'app/models/static_chart.rb', line 96 def render_graph src = self.src Rails.logger.debug "[gcharts] URL length: #{src.length} (#{title})" "<img src=\"#{src}\" width=\"#{width}\" height=\"#{height}\" alt=\"#{title}\" class=\"google-chart\" />" end |
#retina? ⇒ Boolean
40 41 42 |
# File 'app/models/static_chart.rb', line 40 def retina? .fetch(:retina, false) end |
#size ⇒ Object
56 57 58 |
# File 'app/models/static_chart.rb', line 56 def size "#{img_width}x#{img_height}" end |
#src ⇒ Object
92 93 94 |
# File 'app/models/static_chart.rb', line 92 def src raise NotImplementedError end |
#title ⇒ Object
78 79 80 |
# File 'app/models/static_chart.rb', line 78 def title [:title] end |
#to_s ⇒ Object
102 103 104 105 106 107 108 |
# File 'app/models/static_chart.rb', line 102 def to_s return "" if empty? html = render_graph html << "<h5>#{title}</h5>" if title html.html_safe end |
#width ⇒ Object
32 33 34 |
# File 'app/models/static_chart.rb', line 32 def width [:width] end |