Harry Seldon's blog

Fractals, Chaos, and Control Systems on Rails

Read

Scatter line chart example with OFC2

Posted by Harry Seldon on October 22, 2008

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
end

The 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).

Thanks John and thanks Charlie.
H

Posted in | 8 comments | Tags , , , , | atom

Delicious

Trackbacks

Use the following link to trackback from your own site:
http://harryseldon.thinkosphere.com/trackbacks?article_id=20

  1. Open Flash Charts examples with Rails From Harry Seldon's blog
    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...

Comments

Leave a response

  1. charlie on October 23, 2008 at 07:22 am

    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

  2. Harry Seldon on October 23, 2008 at 08:03 pm

    Charlie, I heard you, here is a radar chart test and example.

    Well done, it works flawlessly even without needing tests !

  3. monk.e.boy on October 24, 2008 at 10:06 am

    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)

  4. Harry Seldon on October 24, 2008 at 08:14 pm

    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 ;-)

  5. Chetan Thapliyal on January 09, 2009 at 07:36 am

    Hi, I am just wondering if it is possible to get the points plotted on four quadrants rather than one.

  6. Harry Seldon on January 09, 2009 at 07:35 pm

    Hi, You can plot the points where you want. That is they can have negative or positive coordinates. However, I am not sure it is possible to have the axis placed at a specific place, and not at the bottom left corner.
    That would be a nice feature. You can ask for it on OFC forums: http://forums.openflashchart.com/ .

  7. Thomas Glasgow on February 13, 2010 at 12:11 am

    Hi,

    Your example doesn’t work for me. The chart doesn’t get plotted. Other examples work for me.

    Any ideas why?

  8. Harry Seldon on February 13, 2010 at 12:10 pm

    Hi Thomas, I guess you are using a recent version. I haven’t tested this code since one year. You can check the version I used in the app described in this post: http://harryseldon.thinkosphere.com/2008/11/07/a-test-and-example-app-for-open-flash-chart-rails-plugin

Leave a comment

Markdown enabled




Search

Navigate


Recent Comments

Recent Posts

Tags

actuators aircraft atc blog chaos chaos_theory charts control controllers controls crisis economy finance flight fractals git gnc gs guidance linux mandelbrot marketing navigation ns ofc on pilot rails ruby sas scs sensors statistics systems techcrunch thinkosphere tutorial typo ubuntu wifi

Categories

Archives

Syndicate


Sponsor