| Adding grafico_report_tag and required raphael asset - reportable - Fork of rep… | |
| Log | |
| Files | |
| Refs | |
| README | |
| --- | |
| commit bd7abefecd4586413307561c5ff84c683870058f | |
| parent c359b0eb474d71f094ab70340137e3060f448da7 | |
| Author: Martin Kavalar <[email protected]> | |
| Date: Wed, 21 Apr 2010 14:44:11 +0200 | |
| Adding grafico_report_tag and required raphael asset | |
| Diffstat: | |
| M generators/reportable_assets/repor… | 5 +++++ | |
| M generators/reportable_assets/templ… | 2 +- | |
| A generators/reportable_assets/templ… | 8 ++++++++ | |
| M lib/saulabs/reportable/report_tag_… | 68 +++++++++++++++++++++++++++… | |
| M spec/other/sparkline_tag_helper_sp… | 12 ++++++------ | |
| 5 files changed, 85 insertions(+), 10 deletions(-) | |
| --- | |
| diff --git a/generators/reportable_assets/reportable_assets_generator.rb b/gene… | |
| @@ -7,6 +7,10 @@ if Saulabs::Reportable::IS_RAILS3 | |
| def create_grafico_file | |
| empty_directory('public/javascripts') | |
| copy_file( | |
| + File.join(File.dirname(__FILE__), 'templates', 'raphael.js'), | |
| + 'public/javascripts/raphael.js' | |
| + ) | |
| + copy_file( | |
| File.join(File.dirname(__FILE__), 'templates', 'grafico.min.js'), | |
| 'public/javascripts/grafico.min.js' | |
| ) | |
| @@ -22,6 +26,7 @@ else | |
| def manifest | |
| record do |m| | |
| m.directory('public/javascripts') | |
| + m.file('raphael.js', 'public/javascripts/raphael.js') | |
| m.file('grafico.min.js', 'public/javascripts/grafico.min.js') | |
| m.readme('NOTES') | |
| end | |
| diff --git a/generators/reportable_assets/templates/NOTES b/generators/reportab… | |
| @@ -1,4 +1,4 @@ | |
| ** Beware that Grafico depends on Prototype.js! | |
| - ** Also don't forget to include Grafico as well as Prototype.js in your layo… | |
| + ** Also don't forget to include Raphael, Grafico as well as Prototype.js in … | |
| diff --git a/generators/reportable_assets/templates/raphael.js b/generators/rep… | |
| @@ -0,0 +1,7 @@ | |
| +/* | |
| + * Raphael 1.3.2 - JavaScript Vector Library | |
| + * | |
| + * Copyright (c) 2009 Dmitry Baranovskiy (http://raphaeljs.com) | |
| + * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)… | |
| + */ | |
| +Raphael=(function(){var a=/[, ]+/,aY=/^(circle|rect|path|ellipse|text|image)$/… | |
| +\ No newline at end of file | |
| diff --git a/lib/saulabs/reportable/report_tag_helper.rb b/lib/saulabs/reportab… | |
| @@ -6,7 +6,7 @@ module Saulabs | |
| module ReportTagHelper | |
| - # Renders a sparkline with the given data. | |
| + # Renders a sparkline with the given data using the google drawing api. | |
| # | |
| # @param [Array<Array<DateTime, Float>>] data | |
| # an array of report data as returned by {Saulabs::Reportable::Report#… | |
| @@ -35,7 +35,7 @@ module Saulabs | |
| # | |
| # <%= report_tag(User.registrations_report, :width => 200, :height => … | |
| # | |
| - def report_tag(data, options = {}) | |
| + def google_report_tag(data, options = {}) | |
| options.reverse_merge!({ :width => 300, :height => 34, :line_color => … | |
| data = data.collect { |d| d[1] } | |
| labels = '' | |
| @@ -57,8 +57,70 @@ module Saulabs | |
| ) | |
| end | |
| + | |
| + # Renders a sparkline with the given data using grafico. | |
| + # | |
| + # @param [Array<Array<DateTime, Float>>] data | |
| + # an array of report data as returned by {Saulabs::Reportable::Report#… | |
| + # @param [Hash] options | |
| + # options for width, height, the dom id and the format | |
| + # @param [Hash] grafico_options | |
| + # options for grafico which to_json get called on, | |
| + # see the grafico documentation for more information. | |
| + # | |
| + # @option options [Fixnum] :width (300) | |
| + # the width of the generated graph | |
| + # @option options [Fixnum] :height (34) | |
| + # the height of the generated graph | |
| + # @option options [Array<Symbol>] :dom_id ("reportable_#{Time.now.to_i}") | |
| + # the dom id of the generated div | |
| + # | |
| + # @return [String] | |
| + # an div tag and the javascript code showing a sparkline for the passe… | |
| + # | |
| + # @example Rendering a sparkline tag for report data | |
| + # | |
| + # <%= report_tag(User.registrations_report, {:width => 200, :height =>… | |
| + # | |
| + def grafico_report_tag(data, options = {}, grafico_options = {}) | |
| + options.reverse_merge!( | |
| + { | |
| + :width => 300, | |
| + :height => 100, | |
| + :dom_id => "reportable_#{Time.now.to_i}", | |
| + :format => 'to_i' | |
| + }) | |
| + grafico_options.reverse_merge!( | |
| + { | |
| + :area_opacity => 0.3, | |
| + :markers => 'value', | |
| + :grid => false, | |
| + :draw_axis => false, | |
| + :plot_padding => 0, | |
| + :padding_left =>0, | |
| + :padding_bottom => 0, | |
| + :padding_right => 0, | |
| + :padding_top => 0, | |
| + :stroke_width => 2, | |
| + :show_vertical_labels => false, | |
| + :show_horizontal_labels => false, | |
| + :hover_color => '#000', | |
| + :hover_text_color => '#fff', | |
| + :vertical_label_unit => '', | |
| + :colors => { :data => '#2F69BF' }, | |
| + :curve_amount => 1, | |
| + :focus_hint => false | |
| + }) | |
| + %Q{<div id="#{options[:dom_id]}" style="width:#{options[:width]}px;hei… | |
| + <script type="text/javascript" charset="utf-8"> | |
| + new Grafico.AreaGraph( | |
| + $('#{options[:dom_id]}'), | |
| + { data: #{data.map{|d| eval options[:format], d[1].send(:binding) … | |
| + #{grafico_options.to_json}); | |
| + </script>} | |
| + end | |
| + | |
| end | |
| - | |
| end | |
| end | |
| diff --git a/spec/other/sparkline_tag_helper_spec.rb b/spec/other/sparkline_tag… | |
| @@ -6,7 +6,7 @@ describe Saulabs::Reportable::ReportTagHelper do | |
| @helper = TestHelper.new | |
| end | |
| - describe '#report_tag' do | |
| + describe '#google_report_tag' do | |
| it 'should render an image with the correct source' do | |
| @helper.should_receive(:image_tag).once.with( | |
| @@ -14,7 +14,7 @@ describe Saulabs::Reportable::ReportTagHelper do | |
| { :title => '', :alt => '' } | |
| ) | |
| - @helper.report_tag([[DateTime.now, 1.0], [DateTime.now, 2.0], [DateTime.… | |
| + @helper.google_report_tag([[DateTime.now, 1.0], [DateTime.now, 2.0], [Da… | |
| end | |
| it 'should add parameters for labels to the source of the image if renderi… | |
| @@ -23,7 +23,7 @@ describe Saulabs::Reportable::ReportTagHelper do | |
| { :title => '', :alt => '' } | |
| ) | |
| - @helper.report_tag([[DateTime.now, 1.0], [DateTime.now, 2.0], [DateTime.… | |
| + @helper.google_report_tag([[DateTime.now, 1.0], [DateTime.now, 2.0], [Da… | |
| end | |
| it 'should set the parameters for custom colors if custom colors are speci… | |
| @@ -32,7 +32,7 @@ describe Saulabs::Reportable::ReportTagHelper do | |
| { :title => '', :alt => '' } | |
| ) | |
| - @helper.report_tag([[DateTime.now, 1.0], [DateTime.now, 2.0], [DateTime.… | |
| + @helper.google_report_tag([[DateTime.now, 1.0], [DateTime.now, 2.0], [Da… | |
| end | |
| it 'should set the parameters for a custom title if a title specified' do | |
| @@ -41,7 +41,7 @@ describe Saulabs::Reportable::ReportTagHelper do | |
| { :title => 'title', :alt => '' } | |
| ) | |
| - @helper.report_tag([[DateTime.now, 1.0], [DateTime.now, 2.0], [DateTime.… | |
| + @helper.google_report_tag([[DateTime.now, 1.0], [DateTime.now, 2.0], [Da… | |
| end | |
| it 'should use a specified alt text as alt text for the image' do | |
| @@ -50,7 +50,7 @@ describe Saulabs::Reportable::ReportTagHelper do | |
| { :title => '', :alt => 'alt' } | |
| ) | |
| - @helper.report_tag([[DateTime.now, 1.0], [DateTime.now, 2.0], [DateTime.… | |
| + @helper.google_report_tag([[DateTime.now, 1.0], [DateTime.now, 2.0], [Da… | |
| end | |
| end |