Harry Seldon's blog

Fractals, Chaos, and Control Systems on Rails

Read

Radar chart example with OFC2

Posted by Harry Seldon on October 23, 2008

Following up from yesterday’s example of a scatter line chart. Here is an example of a radar chart. The purpose is double. the first one is to test OFC2 plugin update (let’s say it now: it works !), the second one is to make an example. As Charlie pointed it out, I am leaving the php code to show that if you need further information on OFC2 rails plugin API you actually only need to go to OFC’s original website. For instance, the code from this example is taken here. Notice there are 2 other examples of radar charts here. Let me know in the comments if you have translated to Ruby one of them.

Here is the controller (test_it_controller.rb).

class TestItController < ApplicationController
  def index_radar
    @graph = open_flash_chart_object(600,300,"/test_it/graph_code_radar")
  end

  def graph_code_radar
    # based on this example - http://teethgrinder.co.uk/open-flash-chart-2/radar-chart-lines.php
#    $chart = new open_flash_chart();
     chart = OpenFlashChart.new
#    $chart->set_title( new title( 'Radar Chart' ) );
     chart.set_title(Title.new('Radar Chart'))
#    $line_1 = new line_hollow();
     line_1 = LineHollow.new
#    $line_1->set_values(array(3, 4, 5, 4, 3, 3, 2.5));
     line_1.set_values(Array.new([3, 4, 5, 4, 3, 3, 2.5]))
#    $line_1->set_halo_size( 2 );
     line_1.set_halo_size( 2 )
#    $line_1->set_width( 1 );
     line_1.set_width( 1 )
#    $line_1->set_dot_size( 3 );
     line_1.set_dot_size( 3 )
#    $line_1->set_colour( '#FBB829' );
     line_1.set_colour( '#FBB829' )
#    $line_1->set_tooltip( "Gold<br>#val#" );
     line_1.set_tooltip( "Gold<br>#val#" )
#    $line_1->set_key( 'Mr Gold', 10 );
     line_1.set_key( 'Mr Gold', 10 )

#    $line_2 = new line_dot();
     line_2 = LineDot.new
#    $line_2->set_values(array(2, 2, 2, 2, 2, 2, 2));
     line_2.set_values(Array.new([2, 2, 2, 2, 2, 2, 2]));
#    $line_2->set_halo_size( 2 );
     line_2.set_halo_size( 2 )
#    $line_2->set_width( 1 );
     line_2.set_width( 1 )
#    $line_2->set_dot_size( 3 );
     line_2.set_dot_size( 3 )
#    $line_2->set_colour( '#8000FF' );
     line_2.set_colour( '#8000FF' )
#    $line_2->set_tooltip( "Purple<br>#val#" );
     line_2.set_tooltip( "Purple<br>#val#" )
#    $line_2->set_key( 'Mr Purple', 10 );
     line_2.set_key( 'Mr Purple', 10 )
#    $line_2->loop();
     line_2.loop() # to close the loop

#// add the area object to the chart:
#    $chart->add_element( $line_1 );
     chart.add_element( line_1 )
#    $chart->add_element( $line_2 );
     chart.add_element( line_2 )
#    
#    $r = new radar_axis( 5 );
     r = RadarAxis.new( 5 )

#    $r->set_colour( '#DAD5E0' );
     r.set_colour( '#DAD5E0' )
#    $r->set_grid_colour( '#DAD5E0' );
     r.set_grid_colour( '#DAD5E0' )
#    $labels = new radar_axis_labels( array('Zero','','','Middle','','High') );
     labels = RadarAxisLabels.new(Array.new(['Zero','','','Middle','','High']))
#    $labels->set_colour( '#9F819F' );
     labels.set_colour( '#9F819F' )
#    $r->set_labels( $labels );
     r.set_labels( labels );

#    $spoke_labels = new radar_spoke_labels(array(
#        'Strength',
#        'Smarts',
#        'Sweet<br>Tooth',
#        'Armour',
#        'Max Hit Points',
#        '???',
#        'Looks Like a Monkey') );
    spoke_labels = RadarSpokeLabels.new(Array.new([
        'Strength',
        'Smarts',
        'Sweet<br>Tooth',
        'Armour',
        'Max Hit Points',
        '???',
        'Looks Like a Monkey']))

#    $spoke_labels->set_colour( '#9F819F' );
     spoke_labels.set_colour( '#9F819F' )
#    $r->set_spoke_labels( $spoke_labels );
     r.set_spoke_labels( spoke_labels )
#    $chart->set_radar_axis( $r );
     chart.set_radar_axis( r )
#    $tooltip = new tooltip();
     tooltip = Tooltip.new()
#    $tooltip->set_proximity();
     tooltip.set_proximity()
#    $chart->set_tooltip( $tooltip );
     chart.set_tooltip( tooltip )
#    $chart->set_bg_colour( '#ffffff' );
     chart.set_bg_colour( '#ffffff' )
#    echo $chart->toPrettyString();
    render :text => chart.to_s
  end
end

The view code is as simple as usual (index_radar.hml.erb):

<script type="text/javascript" src="/javascripts/swfobject.js"></script>
<%= @graph %>

You can check out here the graph that has been made.

Thanks John and thanks Charlie.
H

Posted in | 11 comments | Tags , , , | atom

Delicious

Trackbacks

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

  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 08:25 pm

    Looks really good - Nice work again! Thanks for letting me know it is up, I will link to your last two posts, so people will know where to look. Thank you.

  2. Joe on November 03, 2008 at 04:55 pm

    Hey, this is exactly what I’ve been looking for… unfortunately, your example is not working for me. I have followed the example (except I put it in “charts” controller), but do not get a graph rendered: instead, I just get an image of text (like this http://img.skitch.com/20081103-tetqnqtmk63jgn8sd4w764tmwy.jpg)

    Do you have any advice on what to do about this?

    Thanks for any help.

  3. Joe on November 03, 2008 at 05:33 pm

    Dang, I can’t edit my earlier comment.

    Turns out that this is the problem. LoadError: Expected /projects/my_project/vendor/plugins/open_flash_chart/lib/open_flash_chart.rb to define OpenFlashChart

  4. Harry Seldon on November 03, 2008 at 06:48 pm

    OK I am going to look at that and your posts on the rails list.
    In the mean time, I suggest you get the OFC plugin in date of October 22nd 2008 on github. I do not know how to tell git to get a version at a certain date but I am guessing it is possible.

  5. Harry Seldon on November 03, 2008 at 07:25 pm

    I have tested the code of this post on the edge version and it works except that you need to add the following lines to the file open_flash_chart.rb :

    require ‘open_flash_chart/scatter_line’
    require ‘open_flash_chart/radar_axis_labels’
    require ‘open_flash_chart/radar_axis’
    require ‘open_flash_chart/radar_spoke_labels’

  6. Harry Seldon on November 03, 2008 at 07:31 pm

    For info, because of Markdown, you need to put a backslash before the underscores else they mean emphasize like this.

  7. Joe on November 03, 2008 at 08:51 pm

    Thanks very much, I just got it to work with the radar chart. I did indeed have an older version. Time to start customizing the chart, thanks again.

    By the way, is Harry Seldon your real name, or is it from Hari Seldon in the Foundation series :)

  8. Harry Seldon on November 03, 2008 at 08:55 pm

    You are welcome.

    And sure it is a pseudo taken from Asimov’s Foundation series as said in the about page

  9. Harry Seldon on November 04, 2008 at 07:36 pm

    Charlie commited the changes required in open_flash_chart.rb so these changes are no longer required if you are running edge OFC Rails plugin.

  10. Giovanni on December 11, 2008 at 06:05 pm

    Thanks very much for your work

    Here is the third example of radar chart from Teethgrinder translated to Ruby: http://ingiroingiro.blogspot.com/2008/12/radar-chart-lines.html .

  11. monk.e.boy on March 16, 2009 at 03:11 pm

    “Looks like a monkey” - damn, I make myself laff sometimes ;-) lolzzz

    monk.e.boy

Leave a comment

Markdown enabled




Search

Sponsor