From 67b062e7010bb16283a6cd5b7afc8a7e71c1b91f Mon Sep 17 00:00:00 2001 From: Bantik Date: Sun, 28 Feb 2010 19:11:43 -0600 Subject: [PATCH] Added link to sample project on Github in README; incorporated fixes contributed by Alexey Kuleshov, including refactored specs, fix for hardcoded chart HTML element, and fixes for data row calculations; bumped version. --- CONTRIBUTORS | 4 ++ README.rdoc | 4 +- Rakefile | 1 + lib/seer/area_chart.rb | 18 ++++--- lib/seer/bar_chart.rb | 4 +- lib/seer/chart.rb | 8 +-- lib/seer/column_chart.rb | 4 +- lib/seer/gauge.rb | 4 +- lib/seer/line_chart.rb | 14 ++++-- lib/seer/pie_chart.rb | 4 +- seer.gemspec | 9 ++-- spec/area_chart_spec.rb | 103 +++++++++++++++++++++++++------------- spec/bar_chart_spec.rb | 38 +++++--------- spec/chart_spec.rb | 5 +- spec/column_chart_spec.rb | 40 +++++---------- spec/custom_matchers.rb | 23 +++++++++ spec/gauge_spec.rb | 18 ++++--- spec/helpers.rb | 37 ++++++++++++++ spec/line_chart_spec.rb | 81 +++++++++++++++++------------- spec/pie_chart_spec.rb | 40 +++++---------- spec/seer_spec.rb | 12 ++--- spec/spec_helper.rb | 8 +-- 22 files changed, 286 insertions(+), 193 deletions(-) create mode 100644 CONTRIBUTORS create mode 100644 spec/custom_matchers.rb create mode 100644 spec/helpers.rb diff --git a/CONTRIBUTORS b/CONTRIBUTORS new file mode 100644 index 0000000..d859201 --- /dev/null +++ b/CONTRIBUTORS @@ -0,0 +1,4 @@ +Thanks go to the following individuals for their contributions: + + Alexey Kuleshov (http://github.com/kulesa) + diff --git a/README.rdoc b/README.rdoc index 9c91dfd..29bc2f2 100644 --- a/README.rdoc +++ b/README.rdoc @@ -20,7 +20,7 @@ In your controller: In your view: -
+
<%= Seer::visualize( @widgets, @@ -44,6 +44,8 @@ In your view: For examples of additional chart types, refer to the documentation for each of the individual chart objects, or see the blog post announcing Seer: {Simple, Semantic Graphing for Ruby on Rails with Seer}[http://www.idolhands.com/ruby-on-rails/gems-plugins-and-engines/graphing-for-ruby-on-rails-with-seer] +You can also download a sample project that demonstrates each of the chart types: [http://github.com/Bantik/seer_samples] + Seer is developed and maintained by {Corey Ehmke}[http://www.idolhands.com/] at {SEO Logic}[http://www.seologic.com/]. Copyright (c) 2010 Corey Ehmke / SEO Logic, released under the MIT license diff --git a/Rakefile b/Rakefile index 0a9a3d1..72bb99a 100644 --- a/Rakefile +++ b/Rakefile @@ -12,6 +12,7 @@ begin gem.authors = ["Corey Ehmke / SEO Logic"] gem.add_development_dependency "rspec", ">= 1.2.9" gem.files = [ + "CONTRIBUTORS", "init.rb", "lib/seer.rb", "lib/seer/area_chart.rb", diff --git a/lib/seer/area_chart.rb b/lib/seer/area_chart.rb index d9c7200..14ff447 100644 --- a/lib/seer/area_chart.rb +++ b/lib/seer/area_chart.rb @@ -11,7 +11,7 @@ module Seer # # In your view: # - #
+ #
# # <%= Seer::visualize( # @data, @@ -66,7 +66,7 @@ def initialize(args={}) #:nodoc: end def data_columns #:nodoc: - _data_columns = " data.addRows(#{data_series.first.map{|d| d.send(data_label)}.uniq.size});\r" + _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" @@ -75,20 +75,26 @@ def data_columns #:nodoc: end def data_table #:nodoc: - _rows = data_series.first.map{|d| d.send(data_label)}.uniq + _rows = data_rows _rows.each_with_index do |r,i| @data_table << " data.setCell(#{i}, 0,'#{r}');\r" end data_series.each_with_index do |column,i| column.each_with_index do |c,j| - @data_table << "data.setCell(#{j},#{i+1},#{c.send(data_method)});\r" + @data_table << " data.setCell(#{j},#{i+1},#{c.send(data_method)});\r" end end @data_table end + + def data_rows + data_series.inject([]) do |rows, element| + rows |= element.map { |e| e.send(data_label) } + end + end def nonstring_options #:nodoc: - [ :axis_font_size, :colors, :enable_tooltip, :height, :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] + [ :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 def string_options #:nodoc: @@ -107,7 +113,7 @@ def to_js #:nodoc: #{data_table.to_s} var options = {}; #{options} - var container = document.getElementById('chart'); + var container = document.getElementById('#{self.chart_element}'); var chart = new google.visualization.AreaChart(container); chart.draw(data, options); } diff --git a/lib/seer/bar_chart.rb b/lib/seer/bar_chart.rb index b60b25d..8f9e3e7 100644 --- a/lib/seer/bar_chart.rb +++ b/lib/seer/bar_chart.rb @@ -9,7 +9,7 @@ module Seer # # In your view: # - #
+ #
# # <%= Seer::visualize( # @widgets, @@ -100,7 +100,7 @@ def to_js #:nodoc: #{data_table.to_s} var options = {}; #{options} - var container = document.getElementById('chart'); + var container = document.getElementById('#{self.chart_element}'); var chart = new google.visualization.BarChart(container); chart.draw(data, options); } diff --git a/lib/seer/chart.rb b/lib/seer/chart.rb index 713f0ad..c49e7d0 100644 --- a/lib/seer/chart.rb +++ b/lib/seer/chart.rb @@ -41,14 +41,16 @@ def data_columns def options _options = "" nonstring_options.each do |opt| + next unless self.send(opt) if opt == :colors - _options << " options['#{opt.to_s.camelize(:lower)}'] = #{self.send(:formatted_colors)};\r" if self.send(opt) + _options << " options['#{opt.to_s.camelize(:lower)}'] = #{self.send(:formatted_colors)};\r" else - _options << " options['#{opt.to_s.camelize(:lower)}'] = #{self.send(opt)};\r" if self.send(opt) + _options << " options['#{opt.to_s.camelize(:lower)}'] = #{self.send(opt)};\r" end end string_options.each do |opt| - _options << " options['#{opt.to_s.camelize(:lower)}'] = '#{self.send(opt)}';\r" if self.send(opt) + next unless self.send(opt) + _options << " options['#{opt.to_s.camelize(:lower)}'] = '#{self.send(opt)}';\r" end _options end diff --git a/lib/seer/column_chart.rb b/lib/seer/column_chart.rb index 140aa05..b646a0d 100644 --- a/lib/seer/column_chart.rb +++ b/lib/seer/column_chart.rb @@ -9,7 +9,7 @@ module Seer # # In your view: # - #
+ #
# # <%= Seer::visualize( # @widgets, @@ -100,7 +100,7 @@ def to_js #:nodoc: #{data_table.to_s} var options = {}; #{options} - var container = document.getElementById('chart'); + var container = document.getElementById('#{self.chart_element}'); var chart = new google.visualization.ColumnChart(container); chart.draw(data, options); } diff --git a/lib/seer/gauge.rb b/lib/seer/gauge.rb index 27dccf5..6b8903f 100644 --- a/lib/seer/gauge.rb +++ b/lib/seer/gauge.rb @@ -9,7 +9,7 @@ module Seer # # In your view: # - #
+ #
# # <%= Seer::visualize( # @data, @@ -92,7 +92,7 @@ def to_js #:nodoc: function drawChart() { var data = new google.visualization.DataTable(); #{data_columns} -#{data_table.to_s} +#{data_table.join} var options = {}; #{options} var container = document.getElementById('#{self.chart_element}'); diff --git a/lib/seer/line_chart.rb b/lib/seer/line_chart.rb index 5678b61..d6c6a4b 100644 --- a/lib/seer/line_chart.rb +++ b/lib/seer/line_chart.rb @@ -11,7 +11,7 @@ module Seer # # In your view: # - #
+ #
# # <%= Seer::visualize( # @data, @@ -66,7 +66,7 @@ def initialize(args={}) #:nodoc: end def data_columns #:nodoc: - _data_columns = " data.addRows(#{data_series.first.map{|d| d.send(data_label)}.uniq.size});\r" + _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" @@ -75,7 +75,7 @@ def data_columns #:nodoc: end def data_table #:nodoc: - _rows = data_series.first.map{|d| d.send(data_label)}.uniq + _rows = data_rows _rows.each_with_index do |r,i| @data_table << " data.setCell(#{i}, 0,'#{r}');\r" end @@ -86,6 +86,12 @@ def data_table #:nodoc: end @data_table end + + def data_rows + data_series.inject([]) do |rows, element| + rows |= element.map { |e| e.send(data_label) } + end + end def nonstring_options #:nodoc: [ :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] @@ -107,7 +113,7 @@ def to_js #:nodoc: #{data_table.to_s} var options = {}; #{options} - var container = document.getElementById('chart'); + var container = document.getElementById('#{self.chart_element}'); var chart = new google.visualization.LineChart(container); chart.draw(data, options); } diff --git a/lib/seer/pie_chart.rb b/lib/seer/pie_chart.rb index 624008d..00dea1a 100644 --- a/lib/seer/pie_chart.rb +++ b/lib/seer/pie_chart.rb @@ -9,7 +9,7 @@ module Seer # # In your view: # - #
+ #
# # <%= Seer::visualize( # @data, @@ -92,7 +92,7 @@ def to_js #:nodoc: #{data_table.to_s} var options = {}; #{options} - var container = document.getElementById('chart'); + var container = document.getElementById('#{self.chart_element}'); var chart = new google.visualization.PieChart(container); chart.draw(data, options); } diff --git a/seer.gemspec b/seer.gemspec index 28aeaf9..a58f72c 100644 --- a/seer.gemspec +++ b/seer.gemspec @@ -5,11 +5,11 @@ Gem::Specification.new do |s| s.name = %q{seer} - s.version = "0.4.0" + s.version = "0.5.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Corey Ehmke / SEO Logic"] - s.date = %q{2010-02-17} + s.date = %q{2010-02-28} s.description = %q{ Seer is a lightweight, semantically rich wrapper for the Google Visualization API. It allows you to easily create a visualization of data in a variety of formats, including area charts, bar charts, column charts, gauges, line charts, and pie charts.} s.email = %q{corey@seologic.com} s.extra_rdoc_files = [ @@ -17,7 +17,8 @@ Gem::Specification.new do |s| "README.rdoc" ] s.files = [ - "LICENSE", + "CONTRIBUTORS", + "LICENSE", "README.rdoc", "Rakefile", "init.rb", @@ -43,7 +44,9 @@ Gem::Specification.new do |s| "spec/bar_chart_spec.rb", "spec/chart_spec.rb", "spec/column_chart_spec.rb", + "spec/custom_matchers.rb", "spec/gauge_spec.rb", + "spec/helpers.rb", "spec/line_chart_spec.rb", "spec/pie_chart_spec.rb", "spec/seer_spec.rb", diff --git a/spec/area_chart_spec.rb b/spec/area_chart_spec.rb index 38e23be..266d1bc 100644 --- a/spec/area_chart_spec.rb +++ b/spec/area_chart_spec.rb @@ -14,34 +14,20 @@ ) end - describe 'defaults' do - - it 'colors' do - @chart.colors.should == Seer::Chart::DEFAULT_COLORS - end - - it 'legend' do - @chart.legend.should == Seer::Chart::DEFAULT_LEGEND_LOCATION - end - - it 'height' do - @chart.height.should == Seer::Chart::DEFAULT_HEIGHT - end - - it 'width' do - @chart.width.should == Seer::Chart::DEFAULT_WIDTH - end - + describe 'defaults' do + it_should_behave_like 'it sets default values' end describe 'graph options' do - [:axis_color, :axis_background_color, :axis_font_size, :background_color, :border_color, :colors, :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| + [: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| it "sets its #{accessor} value" do @chart.send("#{accessor}=", 'foo') @chart.send(accessor).should == 'foo' end end + + it_should_behave_like 'it has colors attribute' end it 'renders as JavaScript' do @@ -50,24 +36,73 @@ end it 'sets its data columns' do - @chart.data_columns.should =~ /addRows\(3\)/ - @chart.data_columns.should =~ /addColumn\('string', 'Date'\)/ - @chart.data_columns.should =~ /addColumn\('string', 'Date'\)/ - @chart.data_columns.should =~ /addColumn\('number', '0'\)/ - @chart.data_columns.should =~ /addColumn\('number', '1'\)/ - @chart.data_columns.should =~ /addColumn\('number', '2'\)/ - @chart.data_columns.should =~ /addColumn\('number', '3'\)/ + @chart.data_columns.should =~ /addRows\(5\)/ + @chart.data_columns.should add_column('string', 'Date') + @chart.data_columns.should add_column('number', '0') + @chart.data_columns.should add_column('number', '1') + @chart.data_columns.should add_column('number', '2') + @chart.data_columns.should add_column('number', '3') end it 'sets its data table' do - @chart.data_table.to_s.should =~ /data\.setCell\(0, 0,'1'\)/ - @chart.data_table.to_s.should =~ /data\.setCell\(1, 0,'2'\)/ - @chart.data_table.to_s.should =~ /data\.setCell\(2, 0,'3'\)/ - @chart.data_table.to_s.should =~ /data\.setCell\(0,1,8\)/ - @chart.data_table.to_s.should =~ /data\.setCell\(2,1,8\)/ - @chart.data_table.to_s.should =~ /data\.setCell\(0,2,8\)/ - @chart.data_table.to_s.should =~ /data\.setCell\(1,2,8\)/ - @chart.data_table.to_s.should =~ /data\.setCell\(2,2,8\)/ + @chart.data_table.to_s.should set_cell(0, 0,'1') + @chart.data_table.to_s.should set_cell(1, 0,'2') + @chart.data_table.to_s.should set_cell(2, 0,'3') + @chart.data_table.to_s.should set_cell(3, 0,'4') + @chart.data_table.to_s.should set_cell(4, 0,'5') + @chart.data_table.to_s.should set_cell(0,1,8) + @chart.data_table.to_s.should set_cell(2,1,8) + @chart.data_table.to_s.should set_cell(0,2,8) + @chart.data_table.to_s.should set_cell(1,2,8) + @chart.data_table.to_s.should set_cell(2,2,8) + end + + describe 'when data_series is an array of arrays of arrays/hashes' do + before(:each) do + data_series = Array.new(3) {|i| [[i, i+1], [i+1, i+2]]} + @chart = Seer::AreaChart.new( + :data => [0,1,2,3], + :series_label => 'to_s', + :data_series => data_series, + :data_label => 'first', + :data_method => 'size', + :chart_options => {}, + :chart_element => 'chart' + ) + end + + it 'calculates number of rows' do + @chart.data_columns.should =~ /addRows\(4\)/ + end + + it 'sets its data table' do + @chart.data_table.to_s.should set_cell(0, 0,'0') + @chart.data_table.to_s.should set_cell(1, 0,'1') + @chart.data_table.to_s.should set_cell(2, 0,'2') + @chart.data_table.to_s.should set_cell(3, 0,'3') + end end + describe 'should receive options' do + before(:each) do + data_series = Array.new(3) {|i| [[i, i+1], [i+1, i+2]]} + @options = { + :data => [0,1,2,3], + :series_label => 'to_s', + :data_series => data_series, + :data_label => 'first', + :data_method => 'size', + :chart_options => {}, + :chart_element => 'chart' + } + end + + it 'should receive :is_stacked option' do + create_chart_with_option(:is_stacked => true).to_js.should =~ /options\['isStacked'\] = true/ + end + end +end + +def create_chart_with_option(option) + Seer::AreaChart.new(@options.merge(option)) end diff --git a/spec/bar_chart_spec.rb b/spec/bar_chart_spec.rb index f4b1389..e9e6af7 100644 --- a/spec/bar_chart_spec.rb +++ b/spec/bar_chart_spec.rb @@ -13,33 +13,19 @@ end describe 'defaults' do - - it 'colors' do - @chart.colors.should == Seer::Chart::DEFAULT_COLORS - end - - it 'legend' do - @chart.legend.should == Seer::Chart::DEFAULT_LEGEND_LOCATION - end - - it 'height' do - @chart.height.should == Seer::Chart::DEFAULT_HEIGHT - end - - it 'width' do - @chart.width.should == Seer::Chart::DEFAULT_WIDTH - end - + it_should_behave_like 'it sets default values' end describe 'graph options' do - [:axis_color, :axis_background_color, :axis_font_size, :background_color, :border_color, :colors, :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| + [: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| it "sets its #{accessor} value" do @chart.send("#{accessor}=", 'foo') @chart.send(accessor).should == 'foo' end end + + it_should_behave_like 'it has colors attribute' end it 'renders as JavaScript' do @@ -52,14 +38,14 @@ end it 'sets its data table' do - @chart.data_table.to_s.should =~ /data\.setValue\(0, 0,'0'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(0, 1, 8\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(1, 0,'1'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(1, 1, 8\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(2, 0,'2'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(2, 1, 8\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(3, 0,'3'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(3, 1, 8\)/ + @chart.data_table.to_s.should set_value(0, 0,'0') + @chart.data_table.to_s.should set_value(0, 1, 8) + @chart.data_table.to_s.should set_value(1, 0,'1') + @chart.data_table.to_s.should set_value(1, 1, 8) + @chart.data_table.to_s.should set_value(2, 0,'2') + @chart.data_table.to_s.should set_value(2, 1, 8) + @chart.data_table.to_s.should set_value(3, 0,'3') + @chart.data_table.to_s.should set_value(3, 1, 8) end end diff --git a/spec/chart_spec.rb b/spec/chart_spec.rb index 680c91a..be217f2 100644 --- a/spec/chart_spec.rb +++ b/spec/chart_spec.rb @@ -46,8 +46,7 @@ end it 'sets its data columns' do - @chart.data_columns.should =~ /addRows\(3\)/ - @chart.data_columns.should =~ /addColumn\('string', 'Date'\)/ + @chart.data_columns.should =~ /addRows\(5\)/ @chart.data_columns.should =~ /addColumn\('string', 'Date'\)/ @chart.data_columns.should =~ /addColumn\('number', '0'\)/ @chart.data_columns.should =~ /addColumn\('number', '1'\)/ @@ -56,7 +55,7 @@ end it 'sets its options' do - puts @chart.options.should =~ /options\['titleX'\] = 'Something'/ + @chart.options.should =~ /options\['titleX'\] = 'Something'/ end diff --git a/spec/column_chart_spec.rb b/spec/column_chart_spec.rb index 03d1129..35c7f5e 100644 --- a/spec/column_chart_spec.rb +++ b/spec/column_chart_spec.rb @@ -12,34 +12,20 @@ ) end - describe 'defaults' do - - it 'colors' do - @chart.colors.should == Seer::Chart::DEFAULT_COLORS - end - - it 'legend' do - @chart.legend.should == Seer::Chart::DEFAULT_LEGEND_LOCATION - end - - it 'height' do - @chart.height.should == Seer::Chart::DEFAULT_HEIGHT - end - - it 'width' do - @chart.width.should == Seer::Chart::DEFAULT_WIDTH - end - + describe 'defaults' do + it_should_behave_like 'it sets default values' end describe 'graph options' do - [:axis_color, :axis_background_color, :axis_font_size, :background_color, :border_color, :colors, :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| + [: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| it "sets its #{accessor} value" do @chart.send("#{accessor}=", 'foo') @chart.send(accessor).should == 'foo' end end + + it_should_behave_like 'it has colors attribute' end it 'renders as JavaScript' do @@ -52,14 +38,14 @@ end it 'sets its data table' do - @chart.data_table.to_s.should =~ /data\.setValue\(0, 0,'0'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(0, 1, 8\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(1, 0,'1'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(1, 1, 8\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(2, 0,'2'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(2, 1, 8\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(3, 0,'3'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(3, 1, 8\)/ + @chart.data_table.to_s.should set_value(0, 0,'0') + @chart.data_table.to_s.should set_value(0, 1, 8) + @chart.data_table.to_s.should set_value(1, 0,'1') + @chart.data_table.to_s.should set_value(1, 1, 8) + @chart.data_table.to_s.should set_value(2, 0,'2') + @chart.data_table.to_s.should set_value(2, 1, 8) + @chart.data_table.to_s.should set_value(3, 0,'3') + @chart.data_table.to_s.should set_value(3, 1, 8) end end diff --git a/spec/custom_matchers.rb b/spec/custom_matchers.rb new file mode 100644 index 0000000..2c20462 --- /dev/null +++ b/spec/custom_matchers.rb @@ -0,0 +1,23 @@ +module CustomMatcher + def set_cell(row, column, value) + value = "'#{value}'" if value.is_a?(String) + + simple_matcher("setCell(#{row}, #{column}, #{value})") do |actual| + actual =~ /data\.setCell\(#{row},\s*#{column},\s*#{value}\)/ + end + end + + def set_value(row, column, value) + value = "'#{value}'" if value.is_a?(String) + + simple_matcher("setValue(#{row}, #{column}, #{value})") do |actual| + actual =~ /data\.setValue\(#{row},\s*#{column},\s*#{value}\)/ + end + end + + def add_column(column_type, value) + simple_matcher("addColumn('#{column_type}', '#{value}')") do |actual| + actual =~ /data\.addColumn\('#{column_type}',\s*'#{value}'\)/ + end + end +end \ No newline at end of file diff --git a/spec/gauge_spec.rb b/spec/gauge_spec.rb index e25f07c..82f109b 100644 --- a/spec/gauge_spec.rb +++ b/spec/gauge_spec.rb @@ -32,6 +32,8 @@ @chart.send(accessor).should == 'foo' end end + + it_should_behave_like 'it has colors attribute' end it 'renders as JavaScript' do @@ -44,14 +46,14 @@ end it 'sets its data table' do - @chart.data_table.to_s.should =~ /data\.setValue\(0, 0,'0'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(0, 1, 8\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(1, 0,'1'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(1, 1, 8\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(2, 0,'2'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(2, 1, 8\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(3, 0,'3'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(3, 1, 8\)/ + @chart.data_table.to_s.should set_value(0, 0,'0') + @chart.data_table.to_s.should set_value(0, 1, 8) + @chart.data_table.to_s.should set_value(1, 0,'1') + @chart.data_table.to_s.should set_value(1, 1, 8) + @chart.data_table.to_s.should set_value(2, 0,'2') + @chart.data_table.to_s.should set_value(2, 1, 8) + @chart.data_table.to_s.should set_value(3, 0,'3') + @chart.data_table.to_s.should set_value(3, 1, 8) end end diff --git a/spec/helpers.rb b/spec/helpers.rb new file mode 100644 index 0000000..03df178 --- /dev/null +++ b/spec/helpers.rb @@ -0,0 +1,37 @@ +describe "it has colors attribute", :shared => true do + it 'sets its colors value' do + color_list = ['#7e7587','#990000','#009900', '#3e5643', '#660000', '#003300'] + @chart.colors = color_list + @chart.colors.should == color_list + end + + it 'colors should be an array' do + lambda { + @chart.colors = '#000000' + }.should raise_error(ArgumentError) + end + + it 'colors should be valid hex values' do + lambda { + @chart.colors = ['#000000', 'NOTHEX'] + }.should raise_error(ArgumentError) + end +end + +describe "it sets default values", :shared => true do + it 'colors' do + @chart.colors.should == Seer::Chart::DEFAULT_COLORS + end + + it 'legend' do + @chart.legend.should == Seer::Chart::DEFAULT_LEGEND_LOCATION + end + + it 'height' do + @chart.height.should == Seer::Chart::DEFAULT_HEIGHT + end + + it 'width' do + @chart.width.should == Seer::Chart::DEFAULT_WIDTH + end +end \ No newline at end of file diff --git a/spec/line_chart_spec.rb b/spec/line_chart_spec.rb index c1280ff..a5d3496 100644 --- a/spec/line_chart_spec.rb +++ b/spec/line_chart_spec.rb @@ -14,34 +14,20 @@ ) end - describe 'defaults' do - - it 'colors' do - @chart.colors.should == Seer::Chart::DEFAULT_COLORS - end - - it 'legend' do - @chart.legend.should == Seer::Chart::DEFAULT_LEGEND_LOCATION - end - - it 'height' do - @chart.height.should == Seer::Chart::DEFAULT_HEIGHT - end - - it 'width' do - @chart.width.should == Seer::Chart::DEFAULT_WIDTH - end - + describe 'defaults' do + it_should_behave_like 'it sets default values' end describe 'graph options' do - [:axis_color, :axis_background_color, :axis_font_size, :background_color, :border_color, :colors, :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].each do |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].each do |accessor| it "sets its #{accessor} value" do @chart.send("#{accessor}=", 'foo') @chart.send(accessor).should == 'foo' end end + + it_should_behave_like 'it has colors attribute' end it 'renders as JavaScript' do @@ -50,24 +36,51 @@ end it 'sets its data columns' do - @chart.data_columns.should =~ /addRows\(3\)/ - @chart.data_columns.should =~ /addColumn\('string', 'Date'\)/ - @chart.data_columns.should =~ /addColumn\('string', 'Date'\)/ - @chart.data_columns.should =~ /addColumn\('number', '0'\)/ - @chart.data_columns.should =~ /addColumn\('number', '1'\)/ - @chart.data_columns.should =~ /addColumn\('number', '2'\)/ - @chart.data_columns.should =~ /addColumn\('number', '3'\)/ + @chart.data_columns.should =~ /addRows\(5\)/ + @chart.data_columns.should add_column('string', 'Date') + @chart.data_columns.should add_column('number', '0') + @chart.data_columns.should add_column('number', '1') + @chart.data_columns.should add_column('number', '2') + @chart.data_columns.should add_column('number', '3') end it 'sets its data table' do - @chart.data_table.to_s.should =~ /data\.setCell\(0, 0,'1'\)/ - @chart.data_table.to_s.should =~ /data\.setCell\(1, 0,'2'\)/ - @chart.data_table.to_s.should =~ /data\.setCell\(2, 0,'3'\)/ - @chart.data_table.to_s.should =~ /data\.setCell\(0,1,8\)/ - @chart.data_table.to_s.should =~ /data\.setCell\(2,1,8\)/ - @chart.data_table.to_s.should =~ /data\.setCell\(0,2,8\)/ - @chart.data_table.to_s.should =~ /data\.setCell\(1,2,8\)/ - @chart.data_table.to_s.should =~ /data\.setCell\(2,2,8\)/ + @chart.data_table.to_s.should set_cell(0, 0,'1') + @chart.data_table.to_s.should set_cell(1, 0,'2') + @chart.data_table.to_s.should set_cell(2, 0,'3') + @chart.data_table.to_s.should set_cell(3, 0,'4') + @chart.data_table.to_s.should set_cell(4, 0,'5') + @chart.data_table.to_s.should set_cell(0,1,8) + @chart.data_table.to_s.should set_cell(2,1,8) + @chart.data_table.to_s.should set_cell(0,2,8) + @chart.data_table.to_s.should set_cell(1,2,8) + @chart.data_table.to_s.should set_cell(2,2,8) + end + + describe 'when data_series is an array of arrays of arrays/hashes' do + before(:each) do + data_series = Array.new(3) {|i| [[i, i+1], [i+1, i+2]]} + @chart = Seer::LineChart.new( + :data => [0,1,2,3], + :series_label => 'to_s', + :data_series => data_series, + :data_label => 'first', + :data_method => 'size', + :chart_options => {}, + :chart_element => 'chart' + ) + end + + it 'calculates number of rows' do + @chart.data_columns.should =~ /addRows\(4\)/ + end + + it 'sets its data table' do + @chart.data_table.to_s.should set_cell(0, 0,'0') + @chart.data_table.to_s.should set_cell(1, 0,'1') + @chart.data_table.to_s.should set_cell(2, 0,'2') + @chart.data_table.to_s.should set_cell(3, 0,'3') + end end end diff --git a/spec/pie_chart_spec.rb b/spec/pie_chart_spec.rb index 7051e58..401eee9 100644 --- a/spec/pie_chart_spec.rb +++ b/spec/pie_chart_spec.rb @@ -12,34 +12,20 @@ ) end - describe 'defaults' do - - it 'colors' do - @chart.colors.should == Seer::Chart::DEFAULT_COLORS - end - - it 'legend' do - @chart.legend.should == Seer::Chart::DEFAULT_LEGEND_LOCATION - end - - it 'height' do - @chart.height.should == Seer::Chart::DEFAULT_HEIGHT - end - - it 'width' do - @chart.width.should == Seer::Chart::DEFAULT_WIDTH - end - + describe 'defaults' do + it_should_behave_like 'it sets default values' end describe 'graph options' do - [:background_color, :border_color, :colors, :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].each do |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].each do |accessor| it "sets its #{accessor} value" do @chart.send("#{accessor}=", 'foo') @chart.send(accessor).should == 'foo' end end + + it_should_behave_like 'it has colors attribute' end it 'renders as JavaScript' do @@ -52,14 +38,14 @@ end it 'sets its data table' do - @chart.data_table.to_s.should =~ /data\.setValue\(0, 0,'0'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(0, 1, 8\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(1, 0,'1'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(1, 1, 8\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(2, 0,'2'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(2, 1, 8\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(3, 0,'3'\)/ - @chart.data_table.to_s.should =~ /data\.setValue\(3, 1, 8\)/ + @chart.data_table.to_s.should set_value(0, 0,'0') + @chart.data_table.to_s.should set_value(0, 1, 8) + @chart.data_table.to_s.should set_value(1, 0,'1') + @chart.data_table.to_s.should set_value(1, 1, 8) + @chart.data_table.to_s.should set_value(2, 0,'2') + @chart.data_table.to_s.should set_value(2, 1, 8) + @chart.data_table.to_s.should set_value(3, 0,'3') + @chart.data_table.to_s.should set_value(3, 1, 8) end end diff --git a/spec/seer_spec.rb b/spec/seer_spec.rb index 0644347..c496c6c 100644 --- a/spec/seer_spec.rb +++ b/spec/seer_spec.rb @@ -68,8 +68,8 @@ :as => :bar_chart, :in_element => 'chart', :series => { - :label => 'to_s', - :data => 'size' + :series_label => 'to_s', + :data_method => 'size' }, :chart_options => {} ) =~ /barchart/).should be_true @@ -81,8 +81,8 @@ :as => :column_chart, :in_element => 'chart', :series => { - :label => 'to_s', - :data => 'size' + :series_label => 'to_s', + :data_method => 'size' }, :chart_options => {} ) =~ /columnchart/).should be_true @@ -94,8 +94,8 @@ :as => :gauge, :in_element => 'chart', :series => { - :label => 'to_s', - :data => 'size' + :series_label => 'to_s', + :data_method => 'size' }, :chart_options => {} ) =~ /gauge/).should be_true diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6691cea..79f08bc 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,11 +1,13 @@ $LOAD_PATH.unshift(File.dirname(__FILE__)) $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) +require 'rubygems' require 'actionpack' -require 'activesupport' +require 'active_support' require 'spec' require 'spec/autorun' require 'seer' - +require File.dirname(__FILE__) + "/custom_matchers" +require File.dirname(__FILE__) + '/helpers' Spec::Runner.configure do |config| - + config.include(CustomMatcher) end