Class: Seer::Gauge

Inherits:
Object
  • Object
show all
Includes:
Chart
Defined in:
lib/seer/gauge.rb

Overview

USAGE

In your controller:

@data = Widgets.all # Must be an array, and must respond
                    # to the data method specified below (in this example, 'quantity')

In your view:

<div id="chart"></div>

<%= Seer::visualize(
      @data, 
      :as => :gauge,
      :in_element => 'chart',
      :series => {:series_label => 'name', :data_method => 'quantity'},
      :chart_options => {
        :green_from => 0,
        :green_to => 50,
        :height => 300,
        :max => 100,
        :min => 0,
        :minor_ticks => 5,
        :red_from => 76,
        :red_to => 100,
        :width => 600,
        :yellow_from => 51,
        :yellow_to => 75
      }
    )
 -%>

For details on the chart options, see the Google API docs at code.google.com/apis/visualization/documentation/gallery/gauge.html

Constant Summary

Constants included from Chart

Chart::DEFAULT_COLORS, Chart::DEFAULT_HEIGHT, Chart::DEFAULT_LEGEND_LOCATION, Chart::DEFAULT_WIDTH

Instance Attribute Summary collapse

Attributes included from Chart

#chart_element, #colors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Chart

#data_columns, #formatted_colors, #in_element=, #options

Constructor Details

#initialize(args = {}) ⇒ Gauge

:nodoc:


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/seer/gauge.rb', line 48

def initialize(args={})  #:nodoc:

  # Standard options
  args.each{ |method,arg| self.send("#{method}=",arg) if self.respond_to?(method) }

  # Chart options
  args[:chart_options].each{ |method, arg| self.send("#{method}=",arg) if self.respond_to?(method) }

  # Handle defaults      
  @height ||= args[:chart_options][:height] || DEFAULT_HEIGHT
  @width  ||= args[:chart_options][:width] || DEFAULT_WIDTH

  @data_table = []
  
end

Instance Attribute Details

#dataObject

Graph data


46
47
48
# File 'lib/seer/gauge.rb', line 46

def data
  @data
end

#data_methodObject

Graph data


46
47
48
# File 'lib/seer/gauge.rb', line 46

def data_method
  @data_method
end

#data_tableObject

:nodoc:


46
47
48
# File 'lib/seer/gauge.rb', line 46

def data_table
  @data_table
end

#green_fromObject

Chart options accessors


43
44
45
# File 'lib/seer/gauge.rb', line 43

def green_from
  @green_from
end

#green_toObject

Chart options accessors


43
44
45
# File 'lib/seer/gauge.rb', line 43

def green_to
  @green_to
end

#heightObject

Chart options accessors


43
44
45
# File 'lib/seer/gauge.rb', line 43

def height
  @height
end

#label_methodObject

Graph data


46
47
48
# File 'lib/seer/gauge.rb', line 46

def label_method
  @label_method
end

#major_ticksObject

Chart options accessors


43
44
45
# File 'lib/seer/gauge.rb', line 43

def major_ticks
  @major_ticks
end

#maxObject

Chart options accessors


43
44
45
# File 'lib/seer/gauge.rb', line 43

def max
  @max
end

#minObject

Chart options accessors


43
44
45
# File 'lib/seer/gauge.rb', line 43

def min
  @min
end

#minor_ticksObject

Chart options accessors


43
44
45
# File 'lib/seer/gauge.rb', line 43

def minor_ticks
  @minor_ticks
end

#red_fromObject

Chart options accessors


43
44
45
# File 'lib/seer/gauge.rb', line 43

def red_from
  @red_from
end

#red_toObject

Chart options accessors


43
44
45
# File 'lib/seer/gauge.rb', line 43

def red_to
  @red_to
end

#widthObject

Chart options accessors


43
44
45
# File 'lib/seer/gauge.rb', line 43

def width
  @width
end

#yellow_fromObject

Chart options accessors


43
44
45
# File 'lib/seer/gauge.rb', line 43

def yellow_from
  @yellow_from
end

#yellow_toObject

Chart options accessors


43
44
45
# File 'lib/seer/gauge.rb', line 43

def yellow_to
  @yellow_to
end

Class Method Details

.render(data, args) ⇒ Object

:nodoc:


106
107
108
109
110
111
112
113
114
115
# File 'lib/seer/gauge.rb', line 106

def self.render(data, args)  #:nodoc:
  graph = Seer::Gauge.new(
    :data           => data,
    :label_method   => args[:series][:series_label],
    :data_method    => args[:series][:data_method],
    :chart_options  => args[:chart_options],
    :chart_element  => args[:in_element] || 'chart'
  )
  graph.to_js
end

Instance Method Details

#is_3_dObject

:nodoc:


74
75
76
# File 'lib/seer/gauge.rb', line 74

def is_3_d  #:nodoc:
  @is_3_d.blank? ? false : @is_3_d
end

#nonstring_optionsObject

:nodoc:


78
79
80
# File 'lib/seer/gauge.rb', line 78

def nonstring_options  #:nodoc:
  [:green_from, :green_to, :height, :major_ticks, :max, :min, :minor_ticks, :red_from, :red_to, :width, :yellow_from, :yellow_to]
end

#string_optionsObject

:nodoc:


82
83
84
# File 'lib/seer/gauge.rb', line 82

def string_options  #:nodoc:
  []
end

#to_jsObject

:nodoc:


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/seer/gauge.rb', line 86

def to_js  #:nodoc:

  %{
    <script type="text/javascript">
      google.load('visualization', '1', {'packages':['gauge']});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
#{data_columns}
#{data_table.join}
        var options = {};
#{options}
        var container = document.getElementById('#{self.chart_element}');
        var chart = new google.visualization.Gauge(container);
        chart.draw(data, options);
      }
    </script>
  }
end