Skip to content

Commit

Permalink
Added link to sample project on Github in README; incorporated fixes …
Browse files Browse the repository at this point in the history
…contributed by Alexey Kuleshov, including refactored specs, fix for hardcoded chart HTML element, and fixes for data row calculations; bumped version.
  • Loading branch information
Bantik committed Mar 1, 2010
1 parent 89b1ed6 commit 67b062e
Show file tree
Hide file tree
Showing 22 changed files with 286 additions and 193 deletions.
4 changes: 4 additions & 0 deletions CONTRIBUTORS
@@ -0,0 +1,4 @@
Thanks go to the following individuals for their contributions:

Alexey Kuleshov (http://github.com/kulesa)

4 changes: 3 additions & 1 deletion README.rdoc
Expand Up @@ -20,7 +20,7 @@ In your controller:

In your view:

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

<%= Seer::visualize(
@widgets,
Expand All @@ -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
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -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",
Expand Down
18 changes: 12 additions & 6 deletions lib/seer/area_chart.rb
Expand Up @@ -11,7 +11,7 @@ module Seer
#
# In your view:
#
# <div id="chart" class="chart"></div>
# <div id="chart"></div>
#
# <%= Seer::visualize(
# @data,
Expand Down Expand Up @@ -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"
Expand All @@ -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:
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/seer/bar_chart.rb
Expand Up @@ -9,7 +9,7 @@ module Seer
#
# In your view:
#
# <div id="chart" class="chart"></div>
# <div id="chart"></div>
#
# <%= Seer::visualize(
# @widgets,
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 5 additions & 3 deletions lib/seer/chart.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/seer/column_chart.rb
Expand Up @@ -9,7 +9,7 @@ module Seer
#
# In your view:
#
# <div id="chart" class="chart"></div>
# <div id="chart"></div>
#
# <%= Seer::visualize(
# @widgets,
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/seer/gauge.rb
Expand Up @@ -9,7 +9,7 @@ module Seer
#
# In your view:
#
# <div id="chart" class="chart"></div>
# <div id="chart"></div>
#
# <%= Seer::visualize(
# @data,
Expand Down Expand Up @@ -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}');
Expand Down
14 changes: 10 additions & 4 deletions lib/seer/line_chart.rb
Expand Up @@ -11,7 +11,7 @@ module Seer
#
# In your view:
#
# <div id="chart" class="chart"></div>
# <div id="chart"></div>
#
# <%= Seer::visualize(
# @data,
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/seer/pie_chart.rb
Expand Up @@ -9,7 +9,7 @@ module Seer
#
# In your view:
#
# <div id="chart" class="chart"></div>
# <div id="chart"></div>
#
# <%= Seer::visualize(
# @data,
Expand Down Expand Up @@ -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);
}
Expand Down
9 changes: 6 additions & 3 deletions seer.gemspec
Expand Up @@ -5,19 +5,20 @@

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 = [
"LICENSE",
"README.rdoc"
]
s.files = [
"LICENSE",
"CONTRIBUTORS",
"LICENSE",
"README.rdoc",
"Rakefile",
"init.rb",
Expand All @@ -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",
Expand Down
103 changes: 69 additions & 34 deletions spec/area_chart_spec.rb
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 67b062e

Please sign in to comment.