Open Flash Chart II – Updates, Radar Charts and Scatter Lines
Recently, some good work has gone into the Open Flash Chart plugin. First, it is now up to date with the official php version of OFC. That means we have Radar Charts and Scatter Line capabilities now.
Also, some nice people have helped this plugin along to maturity. I would like to thank David and Harry for their work.
David made some very much needed improvements and brought Open Flash Chart II plugin up to speed with Rails 2.x.
So take note, this is the "modern" way to work with the charts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class TestItController < ApplicationController ## EDIT dont need this line with the latest plugin. #include OpenFlashChart def index respond_to do |wants| wants.html { @graph = open_flash_chart_object( 600, 300, url_for( :action => 'index', :format => :json ) ) } wants.json { # Edit:: don't do the OpenFlashChart::Base stuff anymore #chart = OpenFlashChart::Base.new( :title => Title.new("MY TITLE") ) do |c| chart = OpenFlashChart.new( :title => Title.new("MY TITLE") ) do |c| c << BarGlass.new( :values => (1..10).sort_by{rand} ) end render :text => chart, :layout => false } end end end |
Open Flash Chart was put in to the appropriate OpenFlashChart namespace/module, to ensure we don't run into any conflicts. It also now takes blocks which will be helpful. The example above is courtesy of David.
Harry, has done a great job of providing examples for the latest features of Open Flash Chart.
Check out his Radar Chart Example.
Also, check out his Scatter Line Chart Example.
Thank you for the great work guys.
29 Responses
to “Open Flash Chart II – Updates, Radar Charts and Scatter Lines”
Sorry, comments for this entry are closed at this time.
Thanks Charlie for this update.
I am just pointing out that the scatter line feature is very important for those <a href="http://www.thinkosphere.com/poll/mcq_txt/show_time_results/19">like me</a> interested in time series. To make it short it allows having a plot command such as plot(x,y) where x is NOT regularly spaced.
Hi! the job you are making is simply amazing. this plugin is just what i wanted.
After trying to use ofc in my work i was really disappointed cause i just kept on getting the same error. today i put the new controller code and magically it worked!!!
but i cant understand how to put data…my goal is to insert them via a database. your answer would be helpfull.
Once again a big thanx for helping me out.
best regards
@TaSozzZ – I have an example for you, using a database – <a href="http://pullmonkey.com/2008/7/25/using-a-database-to-populate-an-open-flash-chart-graph">http://pullmonkey.com/2008/7/25/using-a-database-to-populate-an-open-flash-chart-graph</a>. The only changes you need to make with the latest version of the plugins are:
1) include OpenFlashChart at the top of the controller
2) change OpenFlashChart.new to OpenFlashChart::Base.new
Hope that helps.
Hi —
I’m having problems installing the plugin — I had it working before, but I can’t seem to do it now. Using script/plugin install git://github.com/pullmonkey/open_flash_chart.git, it does not do an install, it just says "removing: C:/(path)/vendor/plugins/open_flash_chart/.git"
Not sure what the problem is. Plugin does not seem to be already installed.
Can anyone help me out?
Charlie, why is there this change that forces us to use "OpenFlashChart::Base.new" instead of "OpenFlashChart.new" ?
First, it is less pretty code. Then it forces to change all the calls to OpenFlashChart.
@Harry – You should actually just be able to say Chart.new(..) assuming you include OpenFlashChart. OpenFlashChart is a module now. Maybe the module should be OFC and we should keep the class as OpenFlashChart for backward compatibility.
@T – Try:
script/plugin install http://github.com/pullmonkey/open_flash_chart.git –force
With the http:// instead of git:// and put the –force at the end.
Charlie, OK I see the point. But yes I think you should indeed preserve the backward compatibility by making what you said. (Unless you want to rewrite all your examples on your blog 😉 )
@Harry – latest release allows complete backward compatibility and no need to include the module, so same as before just use OpenFlashChart.new 🙂
Is this compatible with 1.2.6?
@George – It probably is, although, let me know.
Is there a way to only show 4 grid lines for a Radar graph that has values up to 100? I just want it to show the grid lines for 0, 25, 50, 75, and 100.
Also, is there a cleaner way to show only those numbers than this?
<filter:code attributes=lang="ruby">
labels = RadarAxisLabels.new(Array.new([‘0′, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ’25’,
”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ’50’,
”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ’75’,
”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ”, ‘100’ ]))
</filter:code>
@Jeo – haven’t had too much time to look into it, but based on your example I would do this:<filter:code attributes=lang="ruby">labels = RadarAxisLabels.new([0,25,50,75,100].map{|x| x == 100 ? x : [x] + [”] * 24}.flatten)</filter:code>Might be a little easier to work with. Hopefully, there is an easier way, so if I find something, I will let you know.
I love this plugin and also this "modern" approach – kudos, guys!
While trying to cook up my own RESTful implementation, I came up with a few things that will hopefully help out others:
First of all, assuming that we’re working with a Person model, and we’re interested in some data on a specific person…
<filter:code attributes=lang="ruby">
# config/initializers/mime_types.rb
Mime::Type.register_alias "text/html", :ofc
</filter:code>
In your (RESTful) controller, use the method "formatted_person_path" to generate the proper url to send to flash (depends on the default "map.resources :people" in your routes.rb file):
<filter:code attributes=lang="ruby">
# GET /people/1
# GET /people/1.ofc
def show
@person = Person.find(params[:id])
respond_to do |wants|
wants.html {
@graph = open_flash_chart_object(600, 300, formatted_person_path(@person, :ofc))
}
wants.ofc
end
</filter:code>
In your model:
<filter:code attributes=lang="ruby">
include OpenFlashChart
def to_ofc
chart = OpenFlashChart.new(‘MY TITLE’) do |c|
# code for rendering your chart goes here
end
end
</filter:code>
Finally, we use 2 different view files…
show.html.erb
<filter:code attributes=lang="ruby">
<h1>Showing Data for <%= @person.name %></h1>
<%= @graph %>
</filter:code>
and show.ofc.erb
<filter:code attributes=lang="ruby">
<%= @person.to_ofc %>
</filter:code>
PROS:
Makes it possible for sharing of OFC-ready data from one application to another
CONS:
With current plugin implementation, this approach necessitates inclusion of the module in your models ("include OpenFlashChart")
May bloat your model code, but of course that’s better than bloating the controller
I can foresee that this approach might pave the way for easily showing graphs on nested resources, or implementing custom to_ofc methods on associated collections, perhaps by using named_scope or similar:
To show all scores for exam #15:
/exams/15/scores.ofc
To show most popular tags for a specific blog category
/categories/my-category-title/tags.ofc
Thoughts?
@Anthony Navarre – very nice, thanks for providing your example.
Hi,
Quick question… I tried to use this example code, and it worked fine when I included the graph on the index action. However, I want to show a graph on the show action. When I try I get:
This is the URL that I tried to open:http://localhost:3000/casses/show/2.json
instead of the graph. If I go directly to that URL I get a DoubleRender error from rails.
Any ideas? I am very excited about this library, and really appreciate your work on it.
@gnubbs – I would have to see some code to be sure. You will not see the graph on show/2.json, you will see it at show/2 or show/2.html which will call show/2.json for you to produce the graph.
Example of a graph line with error band and another example of radar chart
http://ingiroingiro.blogspot.com/
Many thanks for this plugin, was what I needed
Hey guys, I have updated the OFC test app so that it uses the latest version of OFC plugin.
Anthony and Giovanni, that would be great if you could put your codes in that app.
For reference, the OFC test app <a href="http://harryseldon.thinkosphere.com/2008/11/07/a-test-and-example-app-for-open-flash-chart-rails-plugin">is described here.</a>
How to use GIT to contribute <a href="http://harryseldon.thinkosphere.com/2008/11/08/grand-gardening-with-git">is explained here.</a>
Maybe a bit off topic, but I have problems trying to use multiple parameters for the graphs, is this a problem in OFC? If I do something like /ofc/graph?foo=bar&stuff=fluff I only get params[:foo] in the data request.
@Kimmo, that looks fine to me, I can’t imagine OFC having any affect on request params. But maybe you should post params.inspect and your controller code just to be sure.
@Kimmo, you figure this out? I’m having the same problem with OFC. It’s converting the & separating variables to & in the javascript and there’s no way I can figure out to rid it of this problem…anyone have an idea?
@gonzoFish or @Kimmo – could one of you post some sample code that is having this problem? Then I will take a look.
@Kimmo, @gonzoFish just replace the "&" by "%26", this work for me.
Nice plugin. But is there a way to change the background color of the charts? I know this is possible in PHP but I can’t find a way to do it in the Rails plugin. I searched the source for it but just couldn’t find a way to do this.
Is it possible? Will it be in the future? Please, let me know.
@Christoffer – Yes, you can set the background color. Just like you do for PHP. chart.set_background_colour(‘#fff’)
Or more rubyish – chart.background_colour = ‘#fff’
The plugin uses method_missing() to do everything. There really is no source code. If you want to get a better idea of how it works, read this post about the upcoming version of OFC – http://pullmonkey.com/2009/4/30/open-flash-chart-ii-fully-automated
@Christoffer – actually it is bg_colour.
I use it <a href="http://pullmonkey.com/2008/7/25/using-a-database-to-populate-an-open-flash-chart-graph">here</a>.
Had a very basic question. Which editor are you using??
Vandita – I’m using vim (with rails.vim) and tmux.