Class: StaticChart

Inherits:
Object
  • Object
show all
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

Area

Defined Under Namespace

Classes: Area

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options={})
  if options.key?(:data_by_color)
    hash = options.delete(:data_by_color)
    options.merge!(data: hash.values, colors: hash.keys)
  end

  @options = defaults.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'app/models/static_chart.rb', line 23

def options
  @options
end

Instance Method Details

#axesObject



74
75
76
# File 'app/models/static_chart.rb', line 74

def axes
  options[:axes]
end

#bgObject



82
83
84
# File 'app/models/static_chart.rb', line 82

def bg
  options[:bg]
end

#colorsObject



66
67
68
# File 'app/models/static_chart.rb', line 66

def colors
  options[:colors]
end

#dataObject



62
63
64
# File 'app/models/static_chart.rb', line 62

def data
  options[:data]
end

#defaultsObject



25
26
27
28
# File 'app/models/static_chart.rb', line 25

def defaults
  { font_size: 9,
    bg: "FFFFFF00" } # transparent background
end

#empty?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'app/models/static_chart.rb', line 86

def empty?
  data.flatten.empty?
end

#font_sizeObject



52
53
54
# File 'app/models/static_chart.rb', line 52

def font_size
  retina? ? options[:font_size] * 2 : options[:font_size]
end

#heightObject



36
37
38
# File 'app/models/static_chart.rb', line 36

def height
  options[:height]
end

#img_heightObject



48
49
50
# File 'app/models/static_chart.rb', line 48

def img_height
  retina? ? height * 2 : height
end

#img_widthObject



44
45
46
# File 'app/models/static_chart.rb', line 44

def img_width
  retina? ? width * 2 : width
end

#labelsObject



70
71
72
# File 'app/models/static_chart.rb', line 70

def labels
  options[:labels]
end

#render_graphObject



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

Returns:

  • (Boolean)


40
41
42
# File 'app/models/static_chart.rb', line 40

def retina?
  options.fetch(:retina, false)
end

#sizeObject



56
57
58
# File 'app/models/static_chart.rb', line 56

def size
  "#{img_width}x#{img_height}"
end

#srcObject

Raises:

  • (NotImplementedError)


92
93
94
# File 'app/models/static_chart.rb', line 92

def src
  raise NotImplementedError
end

#titleObject



78
79
80
# File 'app/models/static_chart.rb', line 78

def title
  options[:title]
end

#to_sObject



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

#widthObject



32
33
34
# File 'app/models/static_chart.rb', line 32

def width
  options[:width]
end