Dondo Land

Bryan Donovan's Weblog

All | General | Music | Rails

20071024 Wednesday October 24, 2007

 Adding an Open Flash Chart to a Ruby/Rails application

If you want to add a simple line graph to a Rails application but for one reason or another don't want to/can't use RMagick et al, a relatively simple way to do it is with the Open Flash Chart plugin. It includes a Ruby plugin in the zip archive called "ruby-ofc-library-pullmonkey". You just need to copy that to your Rails app's vendor/plugin directory and rename it to "open_flash_chart". Then you'll be able to make a graph like this in no time:

Open Flash Chart converted to JPG
Here's the code:
require 'open_flash_chart'
class AuditsController < ApplicationController

  def drilldown
    @audits = Audit.find_for_drilldown(params)  # or find(:all), etc.
    #merge the drilldown_graph action and this controller with other params (such as date range)
    url = url_for(params.merge({'controller' => 'audits', 'action' => 'drilldown_graph'}))
    @graph = OpenFlashChart.swf_object(250,150,url)
  end

  def drilldown_graph
    @audits = Audit.find_for_drilldown(params)
    ofc = OpenFlashChart.new
    ofc.title("My Title", "{font-size: 16px;}")
    ofc.set_data(@audits.map{|a| a.percent.to_f})
    ofc.line_hollow(2,3,'#C06600', 'Audit Score', 12)
    ofc.set_x_labels(@audits.map{|a| a.audit_date.strftime('%m/%d')})
    ofc.set_x_label_style(9, '0x000000', 0, 3)
    ofc.x_axis_color('0x999999','0xe3e3e3')
    ofc.set_y_max(100)
    ofc.set_y_min(50)
    ofc.set_bg_color('#ffffff')
    ofc.set_y_label_steps(5)
    ofc.set_y_label_style(1, "#000000")
    ofc.y_axis_color('0x999999','0xe3e3e3')
    render :text => ofc.render
  end
end

# View (drilldown.rhtml)

<%= @graph %>



(2008-02-02 14:37:42.0/2007-10-24 09:38:42.0) Permalink Comments [4]
Trackback: http://blogs.sun.com/bdonovan/entry/adding_an_openflash_chart_to

Trackback URL: http://blogs.sun.com/bdonovan/entry/adding_an_openflash_chart_to
Comments:

Hi,

Thanks for the chart plug. One thing, all colours are HTML colours so, '0x990099' can be replaced with '#990099' to avoid confusion.

cheers!

monk.e.boy

Posted by monk.e.boy on November 27, 2007 at 11:49 AM PST #

good point.. I don't know why I have some prefixed with "0x" and others with "#"

Posted by Bryan Donovan on November 27, 2007 at 12:48 PM PST #

[Trackback] It is entirely possible and easy enough to create all kinds of charts and graphs using only PHP along with the GD library. These days PHP is generally compiled with the GD library enabled so it is highly likely that you already have access to it. It i...

Posted by Edmonds Commerce Blog on May 14, 2008 at 01:48 AM PDT #

hey guys want to draw some amazing charts the look out for "visifire" i found it as really amazing charting component powered by silverlight offered under open source just for free

Posted by simhardy on June 16, 2008 at 05:54 AM PDT #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed

« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today


XML







Today's Page Hits: 9