seer 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,58 @@
1
+ module Seer
2
+
3
+ module Chart #:nodoc:
4
+
5
+ attr_accessor :chart_element, :colors
6
+
7
+ DEFAULT_COLORS = ['#324F69','#919E4B', '#A34D4D', '#BEC8BE']
8
+ DEFAULT_LEGEND_LOCATION = 'bottom'
9
+ DEFAULT_HEIGHT = 350
10
+ DEFAULT_WIDTH = 550
11
+
12
+ def in_element=(elem)
13
+ @chart_element = elem
14
+ end
15
+
16
+ def colors=(colors_list)
17
+ unless colors_list.include?('darker')
18
+ raise ArgumentError, "Invalid color option: #{colors_list}" unless colors_list.is_a?(Array)
19
+ colors_list.each do |color|
20
+ raise ArgumentError, "Invalid color option: #{colors_list}" unless Seer.valid_hex_number?(color)
21
+ end
22
+ end
23
+ @colors = colors_list
24
+ end
25
+
26
+ def formatted_colors
27
+ if @colors.include?('darker')
28
+ @colors
29
+ else
30
+ "[#{@colors.map{|color| "'#{color.gsub(/\#/,'')}'"} * ','}]"
31
+ end
32
+ end
33
+
34
+ def data_columns
35
+ _data_columns = " data.addRows(#{data_table.size});\r"
36
+ _data_columns << " data.addColumn('string', '#{label_method}');\r"
37
+ _data_columns << " data.addColumn('number', '#{data_method}');\r"
38
+ _data_columns
39
+ end
40
+
41
+ def options
42
+ _options = ""
43
+ nonstring_options.each do |opt|
44
+ if opt == :colors
45
+ _options << " options['#{opt.to_s.camelize(:lower)}'] = #{self.send(:formatted_colors)};\r" if self.send(opt)
46
+ else
47
+ _options << " options['#{opt.to_s.camelize(:lower)}'] = #{self.send(opt)};\r" if self.send(opt)
48
+ end
49
+ end
50
+ string_options.each do |opt|
51
+ _options << " options['#{opt.to_s.camelize(:lower)}'] = '#{self.send(opt)}';\r" if self.send(opt)
52
+ end
53
+ _options
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,124 @@
1
+ module Seer
2
+
3
+ # =USAGE
4
+ #
5
+ # In your controller:
6
+ #
7
+ # @data = Widgets.all # Must be an array, and must respond
8
+ # # to the data method specified below (in this example, 'quantity')
9
+ #
10
+ # In your view:
11
+ #
12
+ # <div id="chart" class="chart"></div>
13
+ #
14
+ # <%= Seer::visualize(
15
+ # @widgets,
16
+ # :as => :column_chart,
17
+ # :in_element => 'chart',
18
+ # :series => {:series_label => 'name', :data_method => 'quantity'},
19
+ # :chart_options => {
20
+ # :height => 300,
21
+ # :width => 300,
22
+ # :is_3_d => true,
23
+ # :legend => 'none',
24
+ # :colors => "[{color:'#990000', darker:'#660000'}]",
25
+ # :title => "Widget Quantities",
26
+ # :title_x => 'Widgets',
27
+ # :title_y => 'Quantities'
28
+ # }
29
+ # )
30
+ # -%>
31
+ #
32
+ # Colors are treated differently for 2d and 3d graphs. If you set is_3_d to false, set the
33
+ # graph color like this:
34
+ #
35
+ # :colors => "#990000"
36
+ #
37
+ # For details on the chart options, see the Google API docs at
38
+ # http://code.google.com/apis/visualization/documentation/gallery/columnchart.html
39
+ #
40
+ class ColumnChart
41
+
42
+ include Seer::Chart
43
+
44
+ # Chart options accessors
45
+ attr_accessor :axis_color, :axis_background_color, :axis_font_size, :background_color, :border_color, :enable_tooltip, :focus_border_color, :height, :is_3_d, :is_stacked, :legend, :legend_background_color, :legend_font_size, :legend_text_color, :log_scale, :max, :min, :reverse_axis, :show_categories, :title, :title_x, :title_y, :title_color, :title_font_size, :tooltip_font_size, :tooltip_height, :tooltip_width, :width
46
+
47
+ # Graph data
48
+ attr_accessor :data, :data_method, :data_table, :label_method
49
+
50
+ def initialize(args={}) #:nodoc:
51
+
52
+ # Standard options
53
+ args.each{ |method,arg| self.send("#{method}=",arg) if self.respond_to?(method) }
54
+
55
+ # Chart options
56
+ args[:chart_options].each{ |method, arg| self.send("#{method}=",arg) if self.respond_to?(method) }
57
+
58
+ # Handle defaults
59
+ @colors ||= args[:chart_options][:colors] || DEFAULT_COLORS
60
+ @legend ||= args[:chart_options][:legend] || DEFAULT_LEGEND_LOCATION
61
+ @height ||= args[:chart_options][:height] || DEFAULT_HEIGHT
62
+ @width ||= args[:chart_options][:width] || DEFAULT_WIDTH
63
+ @is_3_d ||= args[:chart_options][:is_3_d]
64
+
65
+ @data_table = []
66
+
67
+ end
68
+
69
+ def data_table #:nodoc:
70
+ data.each_with_index do |datum, column|
71
+ @data_table << [
72
+ " data.setValue(#{column}, 0,'#{datum.send(label_method)}');\r",
73
+ " data.setValue(#{column}, 1, #{datum.send(data_method)});\r"
74
+ ]
75
+ end
76
+ @data_table
77
+ end
78
+
79
+ def is_3_d #:nodoc:
80
+ @is_3_d.blank? ? false : @is_3_d
81
+ end
82
+
83
+ def nonstring_options #:nodoc:
84
+ [:axis_font_size, :colors, :enable_tooltip, :height, :is_3_d, :is_stacked, :legend_font_size, :log_scale, :max, :min, :reverse_axis, :show_categories, :title_font_size, :tooltip_font_size, :tooltip_width, :width]
85
+ end
86
+
87
+ def string_options #:nodoc:
88
+ [: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]
89
+ end
90
+
91
+ def to_js #:nodoc:
92
+
93
+ %{
94
+ <script type="text/javascript">
95
+ google.load('visualization', '1', {'packages':['columnchart']});
96
+ google.setOnLoadCallback(drawChart);
97
+ function drawChart() {
98
+ var data = new google.visualization.DataTable();
99
+ #{data_columns}
100
+ #{data_table.to_s}
101
+ var options = {};
102
+ #{options}
103
+ var container = document.getElementById('chart');
104
+ var chart = new google.visualization.ColumnChart(container);
105
+ chart.draw(data, options);
106
+ }
107
+ </script>
108
+ }
109
+ end
110
+
111
+ def self.render(data, args) #:nodoc:
112
+ graph = Seer::ColumnChart.new(
113
+ :data => data,
114
+ :label_method => args[:series][:series_label],
115
+ :data_method => args[:series][:data_method],
116
+ :chart_options => args[:chart_options],
117
+ :chart_element => args[:in_element] || 'chart'
118
+ )
119
+ graph.to_js
120
+ end
121
+
122
+ end
123
+
124
+ end
@@ -0,0 +1,119 @@
1
+ module Seer
2
+
3
+ # =USAGE
4
+ #
5
+ # In your controller:
6
+ #
7
+ # @data = Widgets.all # Must be an array, and must respond
8
+ # # to the data method specified below (in this example, 'quantity')
9
+ #
10
+ # In your view:
11
+ #
12
+ # <div id="chart" class="chart"></div>
13
+ #
14
+ # <%= Seer::visualize(
15
+ # @data,
16
+ # :as => :gauge,
17
+ # :in_element => 'chart',
18
+ # :series => {:series_label => 'name', :data_method => 'quantity'},
19
+ # :chart_options => {
20
+ # :green_from => 0,
21
+ # :green_to => 50,
22
+ # :height => 300,
23
+ # :max => 100,
24
+ # :min => 0,
25
+ # :minor_ticks => 5,
26
+ # :red_from => 76,
27
+ # :red_to => 100,
28
+ # :width => 600,
29
+ # :yellow_from => 51,
30
+ # :yellow_to => 75
31
+ # }
32
+ # )
33
+ # -%>
34
+ #
35
+ # For details on the chart options, see the Google API docs at
36
+ # http://code.google.com/apis/visualization/documentation/gallery/gauge.html
37
+ #
38
+ class Gauge
39
+
40
+ include Seer::Chart
41
+
42
+ # Chart options accessors
43
+ attr_accessor :green_from, :green_to, :height, :major_ticks, :max, :min, :minor_ticks, :red_from, :red_to, :width, :yellow_from, :yellow_to
44
+
45
+ # Graph data
46
+ attr_accessor :data, :data_method, :data_table, :label_method
47
+
48
+ def initialize(args={}) #:nodoc:
49
+
50
+ # Standard options
51
+ args.each{ |method,arg| self.send("#{method}=",arg) if self.respond_to?(method) }
52
+
53
+ # Chart options
54
+ args[:chart_options].each{ |method, arg| self.send("#{method}=",arg) if self.respond_to?(method) }
55
+
56
+ # Handle defaults
57
+ @height ||= args[:chart_options][:height] || DEFAULT_HEIGHT
58
+ @width ||= args[:chart_options][:width] || DEFAULT_WIDTH
59
+
60
+ @data_table = []
61
+
62
+ end
63
+
64
+ def data_table #:nodoc:
65
+ data.each_with_index do |datum, column|
66
+ @data_table << [
67
+ " data.setValue(#{column}, 0,'#{datum.send(label_method)}');\r",
68
+ " data.setValue(#{column}, 1, #{datum.send(data_method)});\r"
69
+ ]
70
+ end
71
+ @data_table
72
+ end
73
+
74
+ def is_3_d #:nodoc:
75
+ @is_3_d.blank? ? false : @is_3_d
76
+ end
77
+
78
+ def nonstring_options #:nodoc:
79
+ [:green_from, :green_to, :height, :major_ticks, :max, :min, :minor_ticks, :red_from, :red_to, :width, :yellow_from, :yellow_to]
80
+ end
81
+
82
+ def string_options #:nodoc:
83
+ []
84
+ end
85
+
86
+ def to_js #:nodoc:
87
+
88
+ %{
89
+ <script type="text/javascript">
90
+ google.load('visualization', '1', {'packages':['gauge']});
91
+ google.setOnLoadCallback(drawChart);
92
+ function drawChart() {
93
+ var data = new google.visualization.DataTable();
94
+ #{data_columns}
95
+ #{data_table.to_s}
96
+ var options = {};
97
+ #{options}
98
+ var container = document.getElementById('#{self.chart_element}');
99
+ var chart = new google.visualization.Gauge(container);
100
+ chart.draw(data, options);
101
+ }
102
+ </script>
103
+ }
104
+ end
105
+
106
+ def self.render(data, args) #:nodoc:
107
+ graph = Seer::Gauge.new(
108
+ :data => data,
109
+ :label_method => args[:series][:series_label],
110
+ :data_method => args[:series][:data_method],
111
+ :chart_options => args[:chart_options],
112
+ :chart_element => args[:in_element] || 'chart'
113
+ )
114
+ graph.to_js
115
+ end
116
+
117
+ end
118
+
119
+ end
@@ -0,0 +1,135 @@
1
+ module Seer
2
+
3
+ # =USAGE
4
+ #
5
+ # In your controller:
6
+ #
7
+ # @data = Widgets.all # Must be an array of objects that respond to the specidied data method
8
+ # # (In this example, 'quantity'
9
+ #
10
+ # @series = @data.map{|w| w.widget_stats} # An array of arrays
11
+ #
12
+ # In your view:
13
+ #
14
+ # <div id="chart" class="chart"></div>
15
+ #
16
+ # <%= Seer::visualize(
17
+ # @data,
18
+ # :as => :line_chart,
19
+ # :in_element => 'chart',
20
+ # :series => {
21
+ # :series_label => 'name',
22
+ # :data_label => 'date',
23
+ # :data_method => 'quantity',
24
+ # :data_series => @series
25
+ # },
26
+ # :chart_options => {
27
+ # :height => 300,
28
+ # :width => 300,
29
+ # :axis_font_size => 11,
30
+ # :colors => ['#7e7587','#990000','#009900'],
31
+ # :title => "Widget Quantities",
32
+ # :point_size => 5
33
+ # }
34
+ # )
35
+ # -%>
36
+ #
37
+ # For details on the chart options, see the Google API docs at
38
+ # http://code.google.com/apis/visualization/documentation/gallery/linechart.html
39
+ #
40
+ class LineChart
41
+
42
+ include Seer::Chart
43
+
44
+ # Graph options
45
+ attr_accessor :axis_color, :axis_background_color, :axis_font_size, :background_color, :border_color, :enable_tooltip, :focus_border_color, :height, :legend, :legend_background_color, :legend_font_size, :legend_text_color, :line_size, :log_scale, :max, :min, :point_size, :reverse_axis, :show_categories, :smooth_line, :title, :title_x, :title_y, :title_color, :title_font_size, :tooltip_font_size, :tooltip_height, :number, :tooltip_width, :width
46
+
47
+ # Graph data
48
+ attr_accessor :data, :data_label, :data_method, :data_series, :data_table, :series_label
49
+
50
+ def initialize(args={}) #:nodoc:
51
+
52
+ # Standard options
53
+ args.each{ |method,arg| self.send("#{method}=",arg) if self.respond_to?(method) }
54
+
55
+ # Chart options
56
+ args[:chart_options].each{ |method, arg| self.send("#{method}=",arg) if self.respond_to?(method) }
57
+
58
+ # Handle defaults
59
+ @colors ||= args[:chart_options][:colors] || DEFAULT_COLORS
60
+ @legend ||= args[:chart_options][:legend] || DEFAULT_LEGEND_LOCATION
61
+ @height ||= args[:chart_options][:height] || DEFAULT_HEIGHT
62
+ @width ||= args[:chart_options][:width] || DEFAULT_WIDTH
63
+
64
+ @data_table = []
65
+
66
+ end
67
+
68
+ def data_columns #:nodoc:
69
+ _data_columns = " data.addRows(#{data_series.first.map{|d| d.send(data_label)}.uniq.size});\r"
70
+ _data_columns << " data.addColumn('string', 'Date');\r"
71
+ data.each do |datum|
72
+ _data_columns << " data.addColumn('number', '#{datum.send(series_label)}');\r"
73
+ end
74
+ _data_columns
75
+ end
76
+
77
+ def data_table #:nodoc:
78
+ _rows = data_series.first.map{|d| d.send(data_label)}.uniq
79
+ _rows.each_with_index do |r,i|
80
+ @data_table << " data.setCell(#{i}, 0,'#{r}');\r"
81
+ end
82
+ data_series.each_with_index do |column,i|
83
+ column.each_with_index do |c,j|
84
+ @data_table << "data.setCell(#{j},#{i+1},#{c.send(data_method)});\r"
85
+ end
86
+ end
87
+ @data_table
88
+ end
89
+
90
+ def nonstring_options #:nodoc:
91
+ [ :axis_font_size, :colors, :enable_tooltip, :height, :legend_font_size, :line_size, :log_scale, :max, :min, :point_size, :reverse_axis, :show_categories, :smooth_line, :title_font_size, :tooltip_font_size, :tooltip_height, :tooltip_width, :width]
92
+ end
93
+
94
+ def string_options #:nodoc:
95
+ [ :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 ]
96
+ end
97
+
98
+ def to_js #:nodoc:
99
+
100
+ %{
101
+ <script type="text/javascript">
102
+ google.load('visualization', '1', {'packages':['linechart']});
103
+ google.setOnLoadCallback(drawChart);
104
+ function drawChart() {
105
+ var data = new google.visualization.DataTable();
106
+ #{data_columns}
107
+ #{data_table.to_s}
108
+ var options = {};
109
+ #{options}
110
+ var container = document.getElementById('chart');
111
+ var chart = new google.visualization.LineChart(container);
112
+ chart.draw(data, options);
113
+ }
114
+ </script>
115
+ }
116
+ end
117
+
118
+ # ====================================== Class Methods =========================================
119
+
120
+ def self.render(data, args) #:nodoc:
121
+ graph = Seer::LineChart.new(
122
+ :data => data,
123
+ :series_label => args[:series][:series_label],
124
+ :data_series => args[:series][:data_series],
125
+ :data_label => args[:series][:data_label],
126
+ :data_method => args[:series][:data_method],
127
+ :chart_options => args[:chart_options],
128
+ :chart_element => args[:in_element] || 'chart'
129
+ )
130
+ graph.to_js
131
+ end
132
+
133
+ end
134
+
135
+ end
@@ -0,0 +1,116 @@
1
+ module Seer
2
+
3
+ # =USAGE
4
+ #
5
+ # In your controller:
6
+ #
7
+ # @data = Widgets.all # Must be an array of objects that respond to the specidied data method
8
+ # # (In this example, 'quantity'
9
+ #
10
+ # In your view:
11
+ #
12
+ # <div id="chart" class="chart"></div>
13
+ #
14
+ # <%= Seer::visualize(
15
+ # @data,
16
+ # :as => :pie_chart,
17
+ # :series => {:series_label => 'name', :data_method => 'quantity'},
18
+ # :chart_options => {
19
+ # :height => 300,
20
+ # :width => 300,
21
+ # :axis_font_size => 11,
22
+ # :title => "Widget Quantities",
23
+ # :point_size => 5,
24
+ # :is_3_d => true
25
+ # }
26
+ # )
27
+ # -%>
28
+ #
29
+ # For details on the chart options, see the Google API docs at
30
+ # http://code.google.com/apis/visualization/documentation/gallery/piechart.html
31
+ #
32
+ class PieChart
33
+
34
+ include Seer::Chart
35
+
36
+ # Chart options accessors
37
+ attr_accessor :background_color, :border_color, :enable_tooltip, :focus_border_color, :height, :is_3_d, :legend, :legend_background_color, :legend_font_size, :legend_text_color, :pie_join_angle, :pie_minimal_angle, :title, :title_x, :title_y, :title_color, :title_font_size, :tooltip_font_size, :tooltip_height, :tooltip_width, :width
38
+
39
+ # Graph data
40
+ attr_accessor :data, :data_method, :data_table, :label_method
41
+
42
+ def initialize(args={}) #:nodoc:
43
+
44
+ # Standard options
45
+ args.each{ |method,arg| self.send("#{method}=",arg) if self.respond_to?(method) }
46
+
47
+ # Chart options
48
+ args[:chart_options].each{ |method, arg| self.send("#{method}=",arg) if self.respond_to?(method) }
49
+
50
+ # Handle defaults
51
+ @colors ||= args[:chart_options][:colors] || DEFAULT_COLORS
52
+ @legend ||= args[:chart_options][:legend] || DEFAULT_LEGEND_LOCATION
53
+ @height ||= args[:chart_options][:height] || DEFAULT_HEIGHT
54
+ @width ||= args[:chart_options][:width] || DEFAULT_WIDTH
55
+ @is_3_d ||= args[:chart_options][:is_3_d]
56
+
57
+ @data_table = []
58
+
59
+ end
60
+
61
+ def data_table #:nodoc:
62
+ data.each_with_index do |datum, column|
63
+ @data_table << [
64
+ " data.setValue(#{column}, 0,'#{datum.send(label_method)}');\r",
65
+ " data.setValue(#{column}, 1, #{datum.send(data_method)});\r"
66
+ ]
67
+ end
68
+ @data_table
69
+ end
70
+
71
+ def is_3_d #:nodoc:
72
+ @is_3_d.blank? ? false : @is_3_d
73
+ end
74
+
75
+ def nonstring_options #:nodoc:
76
+ [:colors, :enable_tooltip, :height, :is_3_d, :legend_font_size, :pie_join_angle, :pie_minimal_angle, :title_font_size, :tooltip_font_size, :tooltip_width, :width]
77
+ end
78
+
79
+ def string_options #:nodoc:
80
+ [:background_color, :border_color, :focus_border_color, :legend, :legend_background_color, :legend_text_color, :title, :title_color]
81
+ end
82
+
83
+ def to_js #:nodoc:
84
+
85
+ %{
86
+ <script type="text/javascript">
87
+ google.load('visualization', '1', {'packages':['piechart']});
88
+ google.setOnLoadCallback(drawChart);
89
+ function drawChart() {
90
+ var data = new google.visualization.DataTable();
91
+ #{data_columns}
92
+ #{data_table.to_s}
93
+ var options = {};
94
+ #{options}
95
+ var container = document.getElementById('chart');
96
+ var chart = new google.visualization.PieChart(container);
97
+ chart.draw(data, options);
98
+ }
99
+ </script>
100
+ }
101
+ end
102
+
103
+ def self.render(data, args) #:nodoc:
104
+ graph = Seer::PieChart.new(
105
+ :data => data,
106
+ :label_method => args[:series][:series_label],
107
+ :data_method => args[:series][:data_method],
108
+ :chart_options => args[:chart_options],
109
+ :chart_element => args[:in_element] || 'chart'
110
+ )
111
+ graph.to_js
112
+ end
113
+
114
+ end
115
+
116
+ end