Flotomatic is a Rails wrapper around the flot javascript graphing library.
Flotomatic now works with Rails 3’s conventions too.
Flotomatic was originally developed for graphing migraine histories on MigraineLiving.com.
Known issues:
-
Zooming in / out with overview plot only works after zooming in with the main plot one initial time
-
When selecting datasets using the flot_selections div, the plot zoom is lost… it zooms all the way out
To install the flotomatic plugin in rails 3:
rails generate flotomatic
The following rake tasks are also available:
-
rake
spec by default, runs specs
-
rake rdoc
create documentation in rdoc folder
Please note this is not intended to be a complete working an example. It’s purpose is only to illustrate some of what you can do through the API.
It assumes a couple of collections have been created (@journals, @migraines) with very specific attributes on them. For a working example see the walk through here: xdotcommer.wordpress.com/2009/11/30/flotomatic-a-new-graphing-plugin-for-rails/
I’ve recently included a working example in the project under the example directory. Just start up script/server in the example/ subdirectory and navigate to localhost:3000/flotomatic
@flot = TimeFlot.new('graph') do |f| f.bars f.grid :hoverable => true f.selection :mode => "xy" f.filter {|collection| collection.select {|j| j.entry_date < Date.parse("7/8/2007")}} f.series_for("Stress", @journals, :x => :entry_date, :y => :stress_rating) f.series_for("Hours of Sleep", @migraines, :x => :entry_date, :y => :hours_of_sleep) f.series_for("Restful Night?", @migraines, :x => :entry_date, :y => lambda {|record| record.restful_night ? 5 : 0 }, :options => {:points => {:show => true}, :bars => {:show => false}}) f.series_for("Migraines", @migraines, :x => :entry_date, :y => lambda {|record| record.migraine? ? 4 : nil }, :options => {:points => {:show => true}, :bars => {:show => false}}) end
<%= flot_includes.html_safe %> <h2>Graph the following items</h2> <div class='flot_dataset_picker'> <%= flot_selections %> </div> <h2>My Graph</h2> <%= flot_canvas("graph") %> <h2>Zoom In / Out</h2> <%= flot_overview("asdflkjasdf") %> <!-- do the actual plotting... --> <% flot_graph("graph", @flot) do %> <%= flot_plot("graph, ":dynamic => true, :overview => true) %> <%= flot_tooltip %> <% end %>
Within the <% flot_graph %> block you will have access to the flotomatic
variable.
The flotomatic variable is created with the id of the canvas div, the data, and options supplied through the Flot object passed in to the flot_graph
call. For instance:
<% flot_graph("canvas", @flot) %> // Results in the following: var flotomatic = new Flotomatic("canvas", @flot.data, @flot.options)
Plotting Methods
// actually draws the plot with $.plot draw: function(placeholder, data, initialOptions, ranges, dynamic, zoom) // calls draw & sets flotomatic.plot to the returned plot handle graph: function(overview, dynamic) // dynamically graphs with choice input checkboxes for each dataset in flotomatic.data graphDynamic: function() // plots the overview (zoom in / out) graph graphOverview: function()
Tooltip Methods
createTooltip: function(tooltipFormatter) // default tooltipFormatter tooltipFormatter: function(item) { var date = new Date(item.datapoint[0]), label = item.series.label; return label + ": " + item.datapoint[1] + " on " + (date.getMonth() + 1) + "/" + date.getDate() + "</a>"; }
Copyright © 2009 Michael Cowden, released under the MIT license