Class: Seer::AreaChart

Inherits:
Object
  • Object
show all
Includes:
Chart
Defined in:
lib/seer/area_chart.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')

@series = @data.map{|w| w.widget_stats} # An array of arrays

In your view:

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

<%= Seer::visualize(
      @data,
      :as => :area_chart,
      :in_element => 'chart',
      :series => {
        :series_label => 'name',
        :data_label => 'date',
        :data_method => 'quantity',
        :data_series => @series
      },
      :chart_options => {
        :height => 300,
        :width => 300,
        :axis_font_size => 11,
        :colors => ['#7e7587','#990000','#009900'],
        :title => "Widget Quantities",
        :point_size => 5
      }
     )
 -%>

For details on the chart options, see the Google API docs at code.google.com/apis/visualization/documentation/gallery/areachart.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

#formatted_colors, #in_element=, #options

Constructor Details

#initialize(args = {}) ⇒ AreaChart

:nodoc:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/seer/area_chart.rb', line 50

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
  @colors ||= args[:chart_options][:colors] || DEFAULT_COLORS
  @legend ||= args[:chart_options][:legend] || DEFAULT_LEGEND_LOCATION
  @height ||= args[:chart_options][:height] || DEFAULT_HEIGHT
  @width  ||= args[:chart_options][:width] || DEFAULT_WIDTH

  @data_table = []

end

Instance Attribute Details

#axis_background_colorObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def axis_background_color
  @axis_background_color
end

#axis_colorObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def axis_color
  @axis_color
end

#axis_font_sizeObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def axis_font_size
  @axis_font_size
end

#background_colorObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def background_color
  @background_color
end

#border_colorObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def border_color
  @border_color
end

#dataObject

Graph data



48
49
50
# File 'lib/seer/area_chart.rb', line 48

def data
  @data
end

#data_labelObject

Graph data



48
49
50
# File 'lib/seer/area_chart.rb', line 48

def data_label
  @data_label
end

#data_methodObject

Graph data



48
49
50
# File 'lib/seer/area_chart.rb', line 48

def data_method
  @data_method
end

#data_seriesObject

Graph data



48
49
50
# File 'lib/seer/area_chart.rb', line 48

def data_series
  @data_series
end

#data_tableObject

:nodoc:



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def data_table
  @data_table
end

#enable_tooltipObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def enable_tooltip
  @enable_tooltip
end

#focus_border_colorObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def focus_border_color
  @focus_border_color
end

#heightObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def height
  @height
end

#is_stackedObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def is_stacked
  @is_stacked
end

#legendObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def legend
  @legend
end

#legend_background_colorObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def legend_background_color
  @legend_background_color
end

#legend_font_sizeObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def legend_font_size
  @legend_font_size
end

#legend_text_colorObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def legend_text_color
  @legend_text_color
end

#line_sizeObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def line_size
  @line_size
end

#log_scaleObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def log_scale
  @log_scale
end

#maxObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def max
  @max
end

#minObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def min
  @min
end

#numberObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def number
  @number
end

#point_sizeObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def point_size
  @point_size
end

#reverse_axisObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def reverse_axis
  @reverse_axis
end

#series_labelObject

Graph data



48
49
50
# File 'lib/seer/area_chart.rb', line 48

def series_label
  @series_label
end

#show_categoriesObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def show_categories
  @show_categories
end

#titleObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def title
  @title
end

#title_colorObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def title_color
  @title_color
end

#title_font_sizeObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def title_font_size
  @title_font_size
end

#title_xObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def title_x
  @title_x
end

#title_yObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def title_y
  @title_y
end

#tooltip_font_sizeObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def tooltip_font_size
  @tooltip_font_size
end

#tooltip_heightObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def tooltip_height
  @tooltip_height
end

#tooltip_widthObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def tooltip_width
  @tooltip_width
end

#widthObject

Graph options



45
46
47
# File 'lib/seer/area_chart.rb', line 45

def width
  @width
end

Class Method Details

.render(data, args) ⇒ Object

:nodoc:



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/seer/area_chart.rb', line 124

def self.render(data, args) #:nodoc:
  graph = Seer::AreaChart.new(
    :data => data,
    :series_label   => args[:series][:series_label],
    :data_series    => args[:series][:data_series],
    :data_label     => args[:series][:data_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

#data_columnsObject

:nodoc:



68
69
70
71
72
73
74
75
# File 'lib/seer/area_chart.rb', line 68

def data_columns #:nodoc:
  _data_columns =  "            data.addRows(#{data_rows.size});\r"
  _data_columns << "            data.addColumn('string', 'Date');\r"
  data.each do |datum|
    _data_columns << "            data.addColumn('number', '#{datum.send(series_label)}');\r"
  end
  _data_columns
end

#data_rowsObject



90
91
92
93
94
# File 'lib/seer/area_chart.rb', line 90

def data_rows
  data_series.inject([]) do |rows, element|
    rows |= element.map { |e| e.send(data_label) }
  end
end

#nonstring_optionsObject

:nodoc:



96
97
98
# File 'lib/seer/area_chart.rb', line 96

def nonstring_options #:nodoc:
  [ :axis_font_size, :colors, :enable_tooltip, :height, :is_stacked, :legend_font_size, :line_size, :log_scale, :max, :min, :point_size, :reverse_axis, :show_categories, :title_font_size, :tooltip_font_size, :tooltip_height, :tooltip_width, :width]
end

#string_optionsObject

:nodoc:



100
101
102
# File 'lib/seer/area_chart.rb', line 100

def string_options #:nodoc:
  [ :axis_color, :axis_background_color, :background_color, :border_color, :focus_border_color, :legend, :legend_background_color, :legend_text_color, :title, :title_x, :title_y, :title_color ]
end

#to_jsObject

:nodoc:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/seer/area_chart.rb', line 104

def to_js #:nodoc:

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