Scatter line chart example with OFC2
Yesterday, I needed a scatter line chart for my project. I saw that my current version of OFC did not do it. However, I saw that a new version that has the feature had come out : OFC version 2 Gamera. Looking at pullmonkey’s blog, there was nothing new so I was afraid the update was not made yet. Another look, this time directly on github and good news Charlie had updated OFC2 plugin. To be precise, last commit says “open flash chart gamera not tested yet, but available for those that want to try it out”. So let’s try out the part that interests me: the scatter line chart. I am directly translating this example from Teethgrinder just like I had done for another OFC example.
For simplicity sake, I do not use the ajax way to draw the chart. Notice also that if you adhere to the skinny controller fat model policy, the graph_code function should be in your model. Before using the following code, you need to install the edge version of OFC2 plugin (script/plugin install git://github.com/pullmonkey/open_flash_chart.git –force). Do not forget to update public/open-flash-chart.swf and public/javascripts/swfobject.js.
Then, here is the controller (test_it_controller.rb). You still have the original php code (and I still hate the $ sign before every variable).
class TestItController < ApplicationController
def index_scatterline
@graph = open_flash_chart_object(600,300,"/test_it/graph_code_scatterline")
end
def graph_code_scatterline
# based on this example - http://teethgrinder.co.uk/open-flash-chart-2/scatter-line-chart.php
#$chart = new open_flash_chart();
chart =OpenFlashChart.new
#$title = new title( date("D M d Y") );
title = Title.new(Date.today.to_s)
#$chart->set_title( $title );
chart.set_title(title)
#$s = new scatter_line( '#FFD600', 3 );
scatter_line = ScatterLine.new( '#FFD600', 3 )
#$v = array();
v = Array.new
#$x = 0.0;
x = 0
#$y = 0;
y = 0
#do
while x < 25
# $v[] = new scatter_value( $x, $y );
v.push(ScatterValue.new(x,y))
# // move up or down in Y axis:
# $y += rand(-20, 20)/10;
y += (-1+2*rand)*2
# if( $y > 10 )
# $y = 10;
if y>10
y=10
end
# if( $y < -10 )
# $y = -10;
if y<-10
y=-10
end
# $x += rand(5, 15)/10;
x += (5+10*rand)/10
#while( $x < 25 );
end
#$s->set_values( $v );
scatter_line.set_values(v)
#$chart->add_element( $s );
chart.add_element(scatter_line)
#$x = new x_axis();
x_axis = XAxis.new
#$x->set_range( 0, 25 );
x_axis.set_range(0,25)
#$chart->set_x_axis( $x );
chart.x_axis = x_axis
#$y = new x_axis();
y_axis = YAxis.new
#$y->set_range( -10, 10 );
y_axis.set_range( -10, 10 )
#$chart->add_y_axis( $y );
chart.y_axis = y_axis
#echo $chart->toPrettyString();
render :text => chart.to_s
end
endThe view code is as simple as usual (index_scatterline.hml.erb):
<script type="text/javascript" src="/javascripts/swfobject.js"></script>
<%= @graph %>You can check out here the graph that is made. The graph is randomly generated so if you refresh it, it is normal that it changes. As a bonus you notice this graph looks like a stock market chart (hum is stock market random ? I will need another post to explain that. Until then use your common sense).
Posted in Ruby on Rails | 4 comments | atom
Sphere: Related Content
Trackbacks
Use the following link to trackback from your own site:
http://harryseldon.thinkosphere.com/trackbacks?article_id=scatter-line-chart-example-with-ofc2&day=22&month=10&year=2008
home
about
archives

Harry - nice work. I like that you added the php comments to your source code, that will definitely help a lot of users that don’t know/understand that they can use the documentation straight from Monk.e.boy’s ofc examples with just a little bit of conversion.
Thanks for the test, glad it works :)
Now, I just need someone to test radar charts …. heh
Charlie, I heard you, here is a radar chart test and example.
Well done, it works flawlessly even without needing tests !
Damn you ruby guys are quick ;-)
I need to fix the hollow part of the scatter dots to punch holes in the line (like normal line charts) and add a halo… I’ll take a look this evening.
Trouble is I just downloaded magic the gathering online which is soaking up the hours….
monk.e.boy (prof.mnk.fssh)
Glad to see you here monk.e.boy ! Thanks for this nice update.
He he, we are quick because ruby is gathering the magic for us ;-)