Permalink
Browse files

Bug fixes; compatability with Rails 3

  • Loading branch information...
lleger authored and joshuamiller committed May 18, 2010
1 parent cf9cc07 commit 88dda0875e69241fe74ce68428343fa3c713acf5
Showing with 26 additions and 28 deletions.
  1. +2 −8 init.rb
  2. +2 −2 install.rb
  3. +22 −18 lib/flotilla.rb
View
10 init.rb
@@ -1,9 +1,3 @@
-# Check for presence of jrails plugin
-begin
- require 'jrails'
-rescue
- p "Flotilla will not work without the jrails plugin installed:"
- p "Visit http://ennerchi.googlecode.com/"
-end
-
require 'flotilla'
+
+ActionView::Helpers
View
@@ -4,8 +4,8 @@
# Copy required javascripts to public/javascripts
begin
- FileUtils.cp "#{dir}/javascripts/excanvas.js", "#{dir}/../../../public/javascripts/excanvas.js"
- FileUtils.cp "#{dir}/javascripts/jquery.flot.js", "#{dir}/../../../public/javascripts/jquery.flot.js"
+ FileUtils.cp "#{dir}/javascripts/excanvas.pack.js", "#{dir}/../../../public/javascripts/excanvas.pack.js"
+ FileUtils.cp "#{dir}/javascripts/jquery.flot.pack.js", "#{dir}/../../../public/javascripts/jquery.flot.pack.js"
rescue
puts "Could not copy excanvas.js and jquery.flot.js. Please manually copy them to your public/javascripts directory."
end
View
@@ -1,29 +1,24 @@
-begin
- require 'json'
-rescue LoadError
- p "Flotilla will not work without the 'json' gem"
-end
+require 'json'
module ActionView
module Helpers
module ScriptaculousHelper
# Insert a flot chart into the page. <tt>placeholder</tt> should be the
- # name of the div that will hold the chart, <tt>collections</tt> is a hash
+ # name of the div that will hold the chart, <tt>size</tt> is the dimensions of
+ # the placeholder (in widthxheight form), <tt>collections</tt> is a hash
# of legends (as strings) and datasets with options as hashes, <tt>options</tt>
# contains graph-wide options.
#
# Example usage:
#
- # chart("graph_div", {
+ # chart("graph_div", "widthxheight", {
# "January" => { :collection => @january, :x => :day, :y => :sales, :options => { :lines => {:show =>true}} },
# "February" => { :collection => @february, :x => :day, :y => :sales, :options => { :points => {:show =>true} } },
# :grid => { :backgroundColor => "#fffaff" })
#
- # Options:
- # :js_tags - wraps resulting javascript in javascript tags if true. Defaults to true.
- def chart(placeholder, series, options = {}, html_options = {})
- html_options.reverse_merge!({ :js_tags => true })
+ def chart(placeholder, size, series, options = {})
+
data, x_is_date, y_is_date = series_to_json(series)
if x_is_date
options[:xaxis] ||= {}
@@ -33,23 +28,32 @@ def chart(placeholder, series, options = {}, html_options = {})
options[:yaxis] ||= {}
options[:yaxis].merge!({ :mode => 'time' })
end
+
+ width, height = size.split("x") if size.respond_to?(:split)
- chart_js = "$.plot($('##{placeholder}'), #{data}, #{options.to_json});"
- html_options[:js_tags] ? javascript_tag(chart_js) : chart_js
+ output = <<EOF
+ <!--[if IE]><script language="javascript" type="text/javascript" src="/javascripts/excanvas.pack.js"></script><![endif]-->
+ <script language="javascript" type="text/javascript" src="/javascripts/jquery.flot.pack.js"></script>
+ <script type="text/javascript">
+ $(function () {
+ jQuery.plot($('##{placeholder}'), #{data}, #{options.to_json});
+ });
+ </script>
+EOF
+ output += content_tag(:div, nil, :id => placeholder, :style => "width:#{width}px;height:#{height}px;")
+ output.html_safe
end
private
+
def series_to_json(series)
data_sets = []
x_is_date, y_is_date = false, false
series.each do |name, values|
set, data = {}, []
set[:label] = name
- first = values[:collection].first
- if first
- x_is_date = first.send(values[:x]).acts_like?(:date) || first.send(values[:x]).acts_like?(:time)
- y_is_date = first.send(values[:y]).acts_like?(:date) || first.send(values[:y]).acts_like?(:time)
- end
+ x_is_date = values[:collection].first.send(values[:x]).is_a? Date
+ y_is_date = values[:collection].first.send(values[:y]).is_a? Date
values[:collection].each do |object|
x_value, y_value = object.send(values[:x]), object.send(values[:y])
x = x_is_date ? x_value.to_time.to_i * 1000 : x_value.to_f

0 comments on commit 88dda08

Please sign in to comment.