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


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


XML







Today's Page Hits: 38