ncri-seer 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,118 @@
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"></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
+ " Seer.chartsData[chartIndex].setValue(#{column}, 0,'#{datum.send(label_method)}');\r",
65
+ " Seer.chartsData[chartIndex].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 chartIndex = Seer.chartsCount;
91
+ Seer.chartsData[chartIndex] = new google.visualization.DataTable();
92
+ #{data_columns}
93
+ #{data_table.to_s}
94
+ var options = {};
95
+ #{options}
96
+ var container = document.getElementById('#{self.chart_element}');
97
+ Seer.charts[chartIndex] = new google.visualization.PieChart(container);
98
+ Seer.charts[chartIndex].draw(Seer.chartsData[chartIndex], options);
99
+ Seer.chartsCount += 1;
100
+ }
101
+ </script>
102
+ }
103
+ end
104
+
105
+ def self.render(data, args) #:nodoc:
106
+ graph = Seer::PieChart.new(
107
+ :data => data,
108
+ :label_method => args[:series][:series_label],
109
+ :data_method => args[:series][:data_method],
110
+ :chart_options => args[:chart_options],
111
+ :chart_element => args[:in_element] || 'chart'
112
+ )
113
+ graph.to_js
114
+ end
115
+
116
+ end
117
+
118
+ end
@@ -0,0 +1,108 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Seer::AreaChart" do
4
+
5
+ before :each do
6
+ @chart = Seer::AreaChart.new(
7
+ :data => [0,1,2,3],
8
+ :series_label => 'to_s',
9
+ :data_series => [[1,2,3],[3,4,5]],
10
+ :data_label => 'to_s',
11
+ :data_method => 'size',
12
+ :chart_options => {},
13
+ :chart_element => 'chart'
14
+ )
15
+ end
16
+
17
+ describe 'defaults' do
18
+ it_should_behave_like 'it sets default values'
19
+ end
20
+
21
+ describe 'graph options' do
22
+
23
+ [:axis_color, :axis_background_color, :axis_font_size, :background_color, :border_color, :enable_tooltip, :focus_border_color, :height, :is_stacked, :legend, :legend_background_color, :legend_font_size, :legend_text_color, :line_size, :log_scale, :max, :min, :point_size, :reverse_axis, :show_categories, :title, :title_x, :title_y, :title_color, :title_font_size, :tooltip_font_size, :tooltip_height, :number, :tooltip_width, :width].each do |accessor|
24
+ it "sets its #{accessor} value" do
25
+ @chart.send("#{accessor}=", 'foo')
26
+ @chart.send(accessor).should == 'foo'
27
+ end
28
+ end
29
+
30
+ it_should_behave_like 'it has colors attribute'
31
+ end
32
+
33
+ it 'renders as JavaScript' do
34
+ (@chart.to_js =~ /javascript/).should be_true
35
+ (@chart.to_js =~ /areachart/).should be_true
36
+ end
37
+
38
+ it 'sets its data columns' do
39
+ @chart.data_columns.should =~ /addRows\(5\)/
40
+ @chart.data_columns.should add_column('string', 'Date')
41
+ @chart.data_columns.should add_column('number', '0')
42
+ @chart.data_columns.should add_column('number', '1')
43
+ @chart.data_columns.should add_column('number', '2')
44
+ @chart.data_columns.should add_column('number', '3')
45
+ end
46
+
47
+ it 'sets its data table' do
48
+ @chart.data_table.to_s.should set_cell(0, 0,'1')
49
+ @chart.data_table.to_s.should set_cell(1, 0,'2')
50
+ @chart.data_table.to_s.should set_cell(2, 0,'3')
51
+ @chart.data_table.to_s.should set_cell(3, 0,'4')
52
+ @chart.data_table.to_s.should set_cell(4, 0,'5')
53
+ @chart.data_table.to_s.should set_cell(0,1,8)
54
+ @chart.data_table.to_s.should set_cell(2,1,8)
55
+ @chart.data_table.to_s.should set_cell(0,2,8)
56
+ @chart.data_table.to_s.should set_cell(1,2,8)
57
+ @chart.data_table.to_s.should set_cell(2,2,8)
58
+ end
59
+
60
+ describe 'when data_series is an array of arrays of arrays/hashes' do
61
+ before(:each) do
62
+ data_series = Array.new(3) {|i| [[i, i+1], [i+1, i+2]]}
63
+ @chart = Seer::AreaChart.new(
64
+ :data => [0,1,2,3],
65
+ :series_label => 'to_s',
66
+ :data_series => data_series,
67
+ :data_label => 'first',
68
+ :data_method => 'size',
69
+ :chart_options => {},
70
+ :chart_element => 'chart'
71
+ )
72
+ end
73
+
74
+ it 'calculates number of rows' do
75
+ @chart.data_columns.should =~ /addRows\(4\)/
76
+ end
77
+
78
+ it 'sets its data table' do
79
+ @chart.data_table.to_s.should set_cell(0, 0,'0')
80
+ @chart.data_table.to_s.should set_cell(1, 0,'1')
81
+ @chart.data_table.to_s.should set_cell(2, 0,'2')
82
+ @chart.data_table.to_s.should set_cell(3, 0,'3')
83
+ end
84
+ end
85
+
86
+ describe 'should receive options' do
87
+ before(:each) do
88
+ data_series = Array.new(3) {|i| [[i, i+1], [i+1, i+2]]}
89
+ @options = {
90
+ :data => [0,1,2,3],
91
+ :series_label => 'to_s',
92
+ :data_series => data_series,
93
+ :data_label => 'first',
94
+ :data_method => 'size',
95
+ :chart_options => {},
96
+ :chart_element => 'chart'
97
+ }
98
+ end
99
+
100
+ it 'should receive :is_stacked option' do
101
+ create_chart_with_option(:is_stacked => true).to_js.should =~ /options\['isStacked'\] = true/
102
+ end
103
+ end
104
+ end
105
+
106
+ def create_chart_with_option(option)
107
+ Seer::AreaChart.new(@options.merge(option))
108
+ end
@@ -0,0 +1,51 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Seer::BarChart" do
4
+
5
+ before :each do
6
+ @chart = Seer::BarChart.new(
7
+ :data => [0,1,2,3],
8
+ :label_method => 'to_s',
9
+ :data_method => 'size',
10
+ :chart_options => {},
11
+ :chart_element => 'chart'
12
+ )
13
+ end
14
+
15
+ describe 'defaults' do
16
+ it_should_behave_like 'it sets default values'
17
+ end
18
+
19
+ describe 'graph options' do
20
+
21
+ [: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].each do |accessor|
22
+ it "sets its #{accessor} value" do
23
+ @chart.send("#{accessor}=", 'foo')
24
+ @chart.send(accessor).should == 'foo'
25
+ end
26
+ end
27
+
28
+ it_should_behave_like 'it has colors attribute'
29
+ end
30
+
31
+ it 'renders as JavaScript' do
32
+ (@chart.to_js =~ /javascript/).should be_true
33
+ (@chart.to_js =~ /barchart/).should be_true
34
+ end
35
+
36
+ it 'sets its data columns' do
37
+ @chart.data_columns.should =~ /addRows\(4\)/
38
+ end
39
+
40
+ it 'sets its data table' do
41
+ @chart.data_table.to_s.should set_value(0, 0,'0')
42
+ @chart.data_table.to_s.should set_value(0, 1, 8)
43
+ @chart.data_table.to_s.should set_value(1, 0,'1')
44
+ @chart.data_table.to_s.should set_value(1, 1, 8)
45
+ @chart.data_table.to_s.should set_value(2, 0,'2')
46
+ @chart.data_table.to_s.should set_value(2, 1, 8)
47
+ @chart.data_table.to_s.should set_value(3, 0,'3')
48
+ @chart.data_table.to_s.should set_value(3, 1, 8)
49
+ end
50
+
51
+ end
@@ -0,0 +1,62 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Seer::Chart" do
4
+
5
+ before :all do
6
+ @chart = Seer::AreaChart.new(
7
+ :data => [0,1,2,3],
8
+ :series_label => 'to_s',
9
+ :data_series => [[1,2,3],[3,4,5]],
10
+ :data_label => 'to_s',
11
+ :data_method => 'size',
12
+ :chart_options => {
13
+ :legend => 'right',
14
+ :title_x => 'Something'
15
+ },
16
+ :chart_element => 'chart'
17
+ )
18
+ end
19
+
20
+ it 'sets the chart element' do
21
+ @chart.in_element = 'foo'
22
+ @chart.chart_element.should == 'foo'
23
+ end
24
+
25
+ describe 'sets colors' do
26
+
27
+ it 'accepting valid values' do
28
+ @chart.colors = ["#ff0000", "#00ff00"]
29
+ @chart.colors.should == ["#ff0000", "#00ff00"]
30
+ end
31
+
32
+ it 'raising an error on invalid values' do
33
+ lambda do
34
+ @chart.colors = 'fred'
35
+ end.should raise_error(ArgumentError)
36
+ lambda do
37
+ @chart.colors = [0,1,2]
38
+ end.should raise_error(ArgumentError)
39
+ end
40
+
41
+ end
42
+
43
+ it 'formats colors' do
44
+ @chart.colors = ["#ff0000"]
45
+ @chart.formatted_colors.should == "['ff0000']"
46
+ end
47
+
48
+ it 'sets its data columns' do
49
+ @chart.data_columns.should =~ /addRows\(5\)/
50
+ @chart.data_columns.should =~ /addColumn\('string', 'Date'\)/
51
+ @chart.data_columns.should =~ /addColumn\('number', '0'\)/
52
+ @chart.data_columns.should =~ /addColumn\('number', '1'\)/
53
+ @chart.data_columns.should =~ /addColumn\('number', '2'\)/
54
+ @chart.data_columns.should =~ /addColumn\('number', '3'\)/
55
+ end
56
+
57
+ it 'sets its options' do
58
+ @chart.options.should =~ /options\['titleX'\] = 'Something'/
59
+ end
60
+
61
+
62
+ end
@@ -0,0 +1,51 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Seer::ColumnChart" do
4
+
5
+ before :each do
6
+ @chart = Seer::ColumnChart.new(
7
+ :data => [0,1,2,3],
8
+ :label_method => 'to_s',
9
+ :data_method => 'size',
10
+ :chart_options => {},
11
+ :chart_element => 'chart'
12
+ )
13
+ end
14
+
15
+ describe 'defaults' do
16
+ it_should_behave_like 'it sets default values'
17
+ end
18
+
19
+ describe 'graph options' do
20
+
21
+ [: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].each do |accessor|
22
+ it "sets its #{accessor} value" do
23
+ @chart.send("#{accessor}=", 'foo')
24
+ @chart.send(accessor).should == 'foo'
25
+ end
26
+ end
27
+
28
+ it_should_behave_like 'it has colors attribute'
29
+ end
30
+
31
+ it 'renders as JavaScript' do
32
+ (@chart.to_js =~ /javascript/).should be_true
33
+ (@chart.to_js =~ /columnchart/).should be_true
34
+ end
35
+
36
+ it 'sets its data columns' do
37
+ @chart.data_columns.should =~ /addRows\(4\)/
38
+ end
39
+
40
+ it 'sets its data table' do
41
+ @chart.data_table.to_s.should set_value(0, 0,'0')
42
+ @chart.data_table.to_s.should set_value(0, 1, 8)
43
+ @chart.data_table.to_s.should set_value(1, 0,'1')
44
+ @chart.data_table.to_s.should set_value(1, 1, 8)
45
+ @chart.data_table.to_s.should set_value(2, 0,'2')
46
+ @chart.data_table.to_s.should set_value(2, 1, 8)
47
+ @chart.data_table.to_s.should set_value(3, 0,'3')
48
+ @chart.data_table.to_s.should set_value(3, 1, 8)
49
+ end
50
+
51
+ end
@@ -0,0 +1,23 @@
1
+ module CustomMatcher
2
+ def set_cell(row, column, value)
3
+ value = "'#{value}'" if value.is_a?(String)
4
+
5
+ simple_matcher("setCell(#{row}, #{column}, #{value})") do |actual|
6
+ actual =~ /data\.setCell\(#{row},\s*#{column},\s*#{value}\)/
7
+ end
8
+ end
9
+
10
+ def set_value(row, column, value)
11
+ value = "'#{value}'" if value.is_a?(String)
12
+
13
+ simple_matcher("setValue(#{row}, #{column}, #{value})") do |actual|
14
+ actual =~ /data\.setValue\(#{row},\s*#{column},\s*#{value}\)/
15
+ end
16
+ end
17
+
18
+ def add_column(column_type, value)
19
+ simple_matcher("addColumn('#{column_type}', '#{value}')") do |actual|
20
+ actual =~ /data\.addColumn\('#{column_type}',\s*'#{value}'\)/
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,59 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Seer::Gauge" do
4
+
5
+ before :each do
6
+ @chart = Seer::Gauge.new(
7
+ :data => [0,1,2,3],
8
+ :label_method => 'to_s',
9
+ :data_method => 'size',
10
+ :chart_options => {},
11
+ :chart_element => 'chart'
12
+ )
13
+ end
14
+
15
+ describe 'defaults' do
16
+
17
+ it 'height' do
18
+ @chart.height.should == Seer::Chart::DEFAULT_HEIGHT
19
+ end
20
+
21
+ it 'width' do
22
+ @chart.width.should == Seer::Chart::DEFAULT_WIDTH
23
+ end
24
+
25
+ end
26
+
27
+ describe 'graph options' do
28
+
29
+ [:green_from, :green_to, :height, :major_ticks, :max, :min, :minor_ticks, :red_from, :red_to, :width, :yellow_from, :yellow_to].each do |accessor|
30
+ it "sets its #{accessor} value" do
31
+ @chart.send("#{accessor}=", 'foo')
32
+ @chart.send(accessor).should == 'foo'
33
+ end
34
+ end
35
+
36
+ it_should_behave_like 'it has colors attribute'
37
+ end
38
+
39
+ it 'renders as JavaScript' do
40
+ (@chart.to_js =~ /javascript/).should be_true
41
+ (@chart.to_js =~ /gauge/).should be_true
42
+ end
43
+
44
+ it 'sets its data columns' do
45
+ @chart.data_columns.should =~ /addRows\(4\)/
46
+ end
47
+
48
+ it 'sets its data table' do
49
+ @chart.data_table.to_s.should set_value(0, 0,'0')
50
+ @chart.data_table.to_s.should set_value(0, 1, 8)
51
+ @chart.data_table.to_s.should set_value(1, 0,'1')
52
+ @chart.data_table.to_s.should set_value(1, 1, 8)
53
+ @chart.data_table.to_s.should set_value(2, 0,'2')
54
+ @chart.data_table.to_s.should set_value(2, 1, 8)
55
+ @chart.data_table.to_s.should set_value(3, 0,'3')
56
+ @chart.data_table.to_s.should set_value(3, 1, 8)
57
+ end
58
+
59
+ end