Radar chart example with OFC2
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
endThe view code is as simple as usual (index_radar.hml.erb):
<script type="text/javascript" src="/javascripts/swfobject.js"></script>
<%= @graph %>Posted in Ruby on Rails | 11 comments | atom
Trackbacks
Use the following link to trackback from your own site:
http://harryseldon.thinkosphere.com/trackbacks?article_id=21
-
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...
home
about
archives

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.
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.
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
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.
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’
For info, because of Markdown, you need to put a backslash before the underscores else they mean emphasize like this.
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 :)
You are welcome.
And sure it is a pseudo taken from Asimov’s Foundation series as said in the about page
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.
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 .
“Looks like a monkey” - damn, I make myself laff sometimes ;-) lolzzz
monk.e.boy