ncri-seer 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/seer/chart.rb ADDED
@@ -0,0 +1,60 @@
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 = " Seer.chartsData[chartIndex].addRows(#{data_table.size});\r"
36
+ _data_columns << " Seer.chartsData[chartIndex].addColumn('string', '#{label_method}');\r"
37
+ _data_columns << " Seer.chartsData[chartIndex].addColumn('number', '#{data_method}');\r"
38
+ _data_columns
39
+ end
40
+
41
+ def options
42
+ _options = ""
43
+ nonstring_options.each do |opt|
44
+ next unless self.send(opt)
45
+ if opt == :colors
46
+ _options << " options['#{opt.to_s.camelize(:lower)}'] = #{self.send(:formatted_colors)};\r"
47
+ else
48
+ _options << " options['#{opt.to_s.camelize(:lower)}'] = #{self.send(opt)};\r"
49
+ end
50
+ end
51
+ string_options.each do |opt|
52
+ next unless self.send(opt)
53
+ _options << " options['#{opt.to_s.camelize(:lower)}'] = '#{self.send(opt)}';\r"
54
+ end
55
+ _options
56
+ end
57
+
58
+ end
59
+
60
+ end
@@ -0,0 +1,167 @@
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"></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, :on_select
46
+
47
+ # Graph data
48
+ #attr_accessor :data, :data_method, :data_table, :label_method
49
+
50
+ # Graph data
51
+ attr_accessor :data, :data_label, :data_method, :data_series, :data_table, :series_label
52
+
53
+ def initialize(args={}) #:nodoc:
54
+
55
+ # Standard options
56
+ args.each{ |method,arg| self.send("#{method}=",arg) if self.respond_to?(method) }
57
+
58
+ # Chart options
59
+ args[:chart_options].each{ |method, arg| self.send("#{method}=",arg) if self.respond_to?(method) }
60
+
61
+ # Handle defaults
62
+ @colors ||= args[:chart_options][:colors] || DEFAULT_COLORS
63
+ @legend ||= args[:chart_options][:legend] || DEFAULT_LEGEND_LOCATION
64
+ @height ||= args[:chart_options][:height] || DEFAULT_HEIGHT
65
+ @width ||= args[:chart_options][:width] || DEFAULT_WIDTH
66
+ @is_3_d ||= args[:chart_options][:is_3_d]
67
+
68
+ @data_table = []
69
+
70
+ end
71
+
72
+ # def data_table #:nodoc:
73
+ # data.each_with_index do |datum, column|
74
+ # @data_table << [
75
+ # " Seer.chartsData[chartIndex].setValue(#{column}, 0,'#{datum.send(label_method)}');\r",
76
+ # " Seer.chartsData[chartIndex].setValue(#{column}, 1, #{datum.send(data_method)});\r"
77
+ # ]
78
+ # end
79
+ # @data_table
80
+ # end
81
+
82
+ def is_3_d #:nodoc:
83
+ @is_3_d.blank? ? false : @is_3_d
84
+ end
85
+
86
+ def nonstring_options #:nodoc:
87
+ [: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]
88
+ end
89
+
90
+ def string_options #:nodoc:
91
+ [: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, :on_select]
92
+ end
93
+
94
+ def data_columns #:nodoc:
95
+ _data_columns = " Seer.chartsData[chartIndex].addRows(#{data_rows.size});\r"
96
+ _data_columns << " Seer.chartsData[chartIndex].addColumn('string', 'Date');\r"
97
+ data.each do |datum|
98
+ _data_columns << " Seer.chartsData[chartIndex].addColumn('number', '#{datum.send(series_label)}');\r"
99
+ end
100
+ _data_columns
101
+ end
102
+
103
+ def data_table #:nodoc:
104
+ _rows = data_rows
105
+ _rows.each_with_index do |r,i|
106
+ @data_table << " Seer.chartsData[chartIndex].setCell(#{i}, 0,'#{r}');\r"
107
+ end
108
+ data_series.each_with_index do |column,i|
109
+ column.each_with_index do |c,j|
110
+ @data_table << "Seer.chartsData[chartIndex].setCell(#{j},#{i+1},#{c.send(data_method)});\r"
111
+ end
112
+ end
113
+ @data_table
114
+ end
115
+
116
+ def data_rows
117
+ data_series.inject([]) do |rows, element|
118
+ rows |= element.map { |e| e.send(data_label) }
119
+ end
120
+ end
121
+
122
+ def to_js #:nodoc:
123
+
124
+ %{
125
+ <script type="text/javascript">
126
+ google.load('visualization', '1', {'packages':['columnchart']});
127
+ google.setOnLoadCallback(drawChart);
128
+ function drawChart() {
129
+ var chartIndex = Seer.chartsCount;
130
+ Seer.chartsData[chartIndex] = new google.visualization.DataTable();
131
+ #{data_columns}
132
+ #{data_table.to_s}
133
+ var options = {};
134
+ #{options}
135
+ var container = document.getElementById('#{self.chart_element}');
136
+ Seer.charts[chartIndex] = new google.visualization.ColumnChart(container);
137
+ Seer.charts[chartIndex].draw(Seer.chartsData[chartIndex], options);
138
+ #{ @on_select ? "\ngoogle.visualization.events.addListener(Seer.charts[chartIndex], 'select', " + @on_select + ");" : ''}
139
+ Seer.chartsCount += 1;
140
+ }
141
+ </script>
142
+ }
143
+ end
144
+
145
+ def self.render(data, args) #:nodoc:
146
+ graph = Seer::ColumnChart.new(
147
+ # :data => data,
148
+ # :label_method => args[:series][:series_label],
149
+ # :data_method => args[:series][:data_method],
150
+ # :chart_options => args[:chart_options],
151
+ # :chart_element => args[:in_element] || 'chart'
152
+ #
153
+ #
154
+ :data => data,
155
+ :series_label => args[:series][:series_label],
156
+ :data_series => args[:series][:data_series],
157
+ :data_label => args[:series][:data_label],
158
+ :data_method => args[:series][:data_method],
159
+ :chart_options => args[:chart_options],
160
+ :chart_element => args[:in_element] || 'chart'
161
+ )
162
+ graph.to_js
163
+ end
164
+
165
+ end
166
+
167
+ end
data/lib/seer/gauge.rb ADDED
@@ -0,0 +1,121 @@
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"></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
+ " Seer.chartsData[chartIndex].setValue(#{column}, 0,'#{datum.send(label_method)}');\r",
68
+ " Seer.chartsData[chartIndex].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 chartIndex = Seer.chartsCount;
94
+ Seer.chartsData[chartIndex] = new google.visualization.DataTable();
95
+ #{data_columns}
96
+ #{data_table.join}
97
+ var options = {};
98
+ #{options}
99
+ var container = document.getElementById('#{self.chart_element}');
100
+ Seer.charts[chartIndex] = new google.visualization.Gauge(container);
101
+ Seer.charts[chartIndex].draw(Seer.chartsData[chartIndex], options);
102
+ Seer.chartsCount += 1;
103
+ }
104
+ </script>
105
+ }
106
+ end
107
+
108
+ def self.render(data, args) #:nodoc:
109
+ graph = Seer::Gauge.new(
110
+ :data => data,
111
+ :label_method => args[:series][:series_label],
112
+ :data_method => args[:series][:data_method],
113
+ :chart_options => args[:chart_options],
114
+ :chart_element => args[:in_element] || 'chart'
115
+ )
116
+ graph.to_js
117
+ end
118
+
119
+ end
120
+
121
+ end
@@ -0,0 +1,144 @@
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"></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, :on_select
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 = " Seer.chartsData[chartIndex].addRows(#{data_rows.size});\r"
70
+ _data_columns << " Seer.chartsData[chartIndex].addColumn('string', 'Date');\r"
71
+ data.each do |datum|
72
+ _data_columns << " Seer.chartsData[chartIndex].addColumn('number', '#{datum.send(series_label)}');\r"
73
+ end
74
+ _data_columns
75
+ end
76
+
77
+ def data_table #:nodoc:
78
+ _rows = data_rows
79
+ _rows.each_with_index do |r,i|
80
+ @data_table << " Seer.chartsData[chartIndex].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 << "Seer.chartsData[chartIndex].setCell(#{j},#{i+1},#{c.send(data_method)});\r"
85
+ end
86
+ end
87
+ @data_table
88
+ end
89
+
90
+ def data_rows
91
+ data_series.inject([]) do |rows, element|
92
+ rows |= element.map { |e| e.send(data_label) }
93
+ end
94
+ end
95
+
96
+ def nonstring_options #:nodoc:
97
+ [ :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]
98
+ end
99
+
100
+ def string_options #:nodoc:
101
+ [ :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 ]
102
+ end
103
+
104
+ def to_js #:nodoc:
105
+
106
+ %{
107
+ <script type="text/javascript">
108
+ google.load('visualization', '1', {'packages':['linechart']});
109
+ google.setOnLoadCallback(drawChart);
110
+ function drawChart() {
111
+ var chartIndex = Seer.chartsCount;
112
+ Seer.chartsData[chartIndex] = new google.visualization.DataTable();
113
+ #{data_columns}
114
+ #{data_table.to_s}
115
+ var options = {};
116
+ #{options}
117
+ var container = document.getElementById('#{self.chart_element}');
118
+ Seer.charts[chartIndex] = new google.visualization.LineChart(container);
119
+ Seer.charts[chartIndex].draw(Seer.chartsData[chartIndex], options);
120
+ #{ @on_select ? "\ngoogle.visualization.events.addListener(Seer.charts[chartIndex], 'select', " + @on_select + ");" : ''}
121
+ Seer.chartsCount += 1;
122
+ }
123
+ </script>
124
+ }
125
+ end
126
+
127
+ # ====================================== Class Methods =========================================
128
+
129
+ def self.render(data, args) #:nodoc:
130
+ graph = Seer::LineChart.new(
131
+ :data => data,
132
+ :series_label => args[:series][:series_label],
133
+ :data_series => args[:series][:data_series],
134
+ :data_label => args[:series][:data_label],
135
+ :data_method => args[:series][:data_method],
136
+ :chart_options => args[:chart_options],
137
+ :chart_element => args[:in_element] || 'chart'
138
+ )
139
+ graph.to_js
140
+ end
141
+
142
+ end
143
+
144
+ end