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.
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 end
The view code is as simple as usual (index_scatterline.hml.erb):
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).
Use the following link to trackback from your own site:
I have updated the [ofc test app](http://harryseldon.thinkosphere.com/2008/11/07/a-test-and-example-app-for-open-flash-chart-rails-plugin) with the examples given by [Giovanni](http://ingiroingiro.blogspot.com/). You have: * [A radar chart with it...