PullMonkey Blog

04 Jan

Open Flash Chart Plugin for Ruby on Rails

Update: Version 2 of Open Flash Chart is available. Examples for version 2 are here.

I just finished converting the latest version of Open Flash Chart over to ruby, for use with rails.

Check out what I can do now :) Like the google analytics graphs.

Scatter Chart

width="500" height="300" id="ie_chart_17" align="middle">
type="application/x-shockwave-flash" pluginpage="http://ww.macromedia.com/go/getflashplayer" id="chart_17"/>

View Source Code

More examples can be found on my Open Flash Chart Projects page.

Don't forget to check out the original - http://teethgrinder.co.uk/open-flash-chart/.

32 Responses to “Open Flash Chart Plugin for Ruby on Rails”

  1. By Christian von Kleist on Jan 4, 2008 | Reply

    Awesome! Thanks for the great code. You should start a RubyForge project for it and maintain it as a gem! :)

  2. By charlie on Jan 4, 2008 | Reply

    Thanks for the support, I will see what I can do.

  3. By sam stein on Jan 4, 2008 | Reply

    hi,<br/><br/><br/>just started using the plugin and it’s great, wondering if there is a way to change the y axis labels to strings instead of nums. maybe like:<br/><br/> g.set_y_label_min(0)<br/> g.set_y_label_max(26)<br/> g.set_y_label_steps(26)<br/> g.set_y_labels(['a', ..., 'z'])<br/><br/>thanks, sam<br/><br/><br/>

  4. By charlie on Jan 4, 2008 | Reply

    Hmm. That is a good question. I will have to test this functionality, I think that generally the y labels are set based on the data values. Although, I am worried that the flash object may not know how to handle it. The flash object is designed and maintained by the people over here (the php version) - http://teethgrinder.co.uk/open-flash-chart/index.php. We will see though :) I hope so, that would be great.

  5. By andrew on Jan 4, 2008 | Reply

    how do you set the bg_colour ?<br/><br/>ive tried everything.<br/>g.bg_colour(’#E2E6E0′)<br/>g.bg_colour = ‘#E2E6E0′<br/>g.bg_color = ‘#E2E6E0′<br/><br/>seems to always have error and not show graph?<br/><br/>

  6. By charlie on Jan 4, 2008 | Reply

    Try g.set_bg_color(’…’).

  7. By Vapor on Jan 4, 2008 | Reply

    I get these errors in log…<br/><br/>Processing BugController#js (for at 2008-01-15 11:40:13) [GET]<br/> Session ID: 7adf1a6b4745f6a2c381fbc270da38c8<br/> Parameters: {"format"=>"js", "action"=>"js", "id"=>"ofc", "controller"=>"post"}<br/><br/>and…<br/><br/>ActionController::RoutingError (no route found to match "/post/open-flash-chart.swf" with {:method=>:get}):

  8. By charlie on Jan 4, 2008 | Reply

    @Vapor - you want open-flash-chart.swf to wind up like this /open-flash-chart.swf, not /post/open-flash-chart.swf, in the open flash chart object call, take out the post parts. If that does not work, post your code here or over at http://railsforum.com and I will take a look.

  9. By Martin M on Jan 4, 2008 | Reply

    I found a problem with the labels for the x-axis. Plus signs (+) are not shown in the labels, nor in tooltips containing #x_label#<br/><br/>Steps to reproduce: g.set_x_labels(['Hello+World'])<br/><br/>I don’t know if this is a problem with the Rails plugin or with Open Flash Chart itself, but I thought I’d begin by posting about it here.

  10. By charlie on Jan 4, 2008 | Reply

    Thank you, I will take a look.

  11. By jackster on Jan 4, 2008 | Reply

    Your graphs are the best!<br/>Thanks for all your hard work and for always helping who ever you can through the simplest to the toughest of problems.<br/><br/>jackster

  12. By Newb on Jan 4, 2008 | Reply

    Help, I followed the instructions for graph 2, when I try to view I get this.<br/><br/>Please help<br/><br/>&tool_tip=x%3A%23x_label%23%3Cbr%3Ey%3A%23val%23& &x_label_style=10,#9933cc,0& &x_legend=Something,20,#000000& &title=Scatter Chart,{font-size: 26px;}& &x_offset=false& &y_label_style=10,#9933cc& &x_axis_steps=1& &y_ticks=5,10,10& &scatter=3,#736aff,My Dots,12& &values=[6,3,4],[5,1,14],[15,-5,15],[18,-4,12],[24,1,13],[-3,-1,7],[-1,-4,14],[11,-1,8],[17,2,4],[4,4,13],[6,3,4],[9,1,7],[12,1,16],[15,-5,7],[0,4,7],[23,0,14],[12,4,13],[-1,0,12],[2,1,15],[22,1,16],[6,-5,18],[22,-5,18],[3,-3,8],[-2,-1,11],[14,-3,17],[17,-5,16],[23,-3,9],[4,-5,16],[7,0,15],[13,-4,16]& &x_min=-5& &x_max=25& &y_min=-5& &y_max=5&

  13. By charlie on Jan 4, 2008 | Reply

    Newb: you are calling the data method. For graph two, there are two methods/actions in the controller, one for grabbing the data and the other for displaying the data. two() presents the data as text, view() displays the graph. So, it would seem that you are going to the url like this: http://domain.com/two, when what you really want is to go to http://domain.com/view. Give that a shot and let me know.

  14. By newb on Jan 4, 2008 | Reply

    Charlie,<br/><br/>I used the /view and now I get a blank box?<br/><br/>It says "Click here to active and use this control", I clicked, installed abobe flash, but the graph does not display. I still get a blank box.<br/><br/>I tried with 2 browsers and hit it remotly from 2 other computers. The weird part is that I can see the graphs on your page, but not mine.<br/><br/>Here is my controller:<br/>class Dbop1Controller < ApplicationController<br/>def view<br/> @graph = open_flash_chart_object(500,250, ‘/projects/open_flash_chart/one’, false, ‘/projects/’)<br/>end<br/>def one<br/> g = Graph.new<br/> g.title("Spoon Sales", ‘{font-size: 26px;}’)<br/> g.set_data([0,0,33,16,9,11,30,48,47,49,16,49])<br/> g.set_x_labels(%w(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec))<br/> g.set_y_max(60)<br/> g.set_y_label_steps(6)<br/> render :text => g.render<br/>end<br/><br/>end<br/><br/>Here is the contents of the view:<br/><%= @graph %><br/><br/>I hope I am not being too much of a bother to you. I really love the graphs!!!<br/><br/>

  15. By Newb on Jan 4, 2008 | Reply

    Charlie,<br/><br/>I tail -f the develpment log and I get this error:<br/><br/>ActionController::RoutingError (No route matches "/projects/open-flash-chart.swf" with {:method=>:get}):<br/><br/>Can you confirm where the open-flash-chart.swf file is to be copied.<br/><br/>Thanks

  16. By Flavio on Jan 4, 2008 | Reply

    Hi, thank you for your great job!<br/>I would like to know how can I fix the ie "click to activate this control" problem.<br/>After reading <a href=http://teethgrinder.co.uk/open-flash-chart/tutorial-2.php">this </a> tutorial I tried the syntax:<br/>@graph = open_flash_chart_object(500,250, ‘/projects/open_flash_chart/scatter’, true, ‘/projects/’)<br/><br/>but nothing changed.<br/><br/>Can you help me?

  17. By charlie on Jan 4, 2008 | Reply

    Flavio - as far as the open_flash_chart_object syntax, did you update your plugin? The click to activate is an IE thing, and is really lame. I have not found a solution for it, but am looking for suggestions. I work primarily in linux and therefore in firefox, sorry I can not be of more help.

  18. By charlie on Jan 4, 2008 | Reply

    Newb - sorry, the examples contain /projects, you can remove the /projects part and you should have something like this in your view:<br/>def view @graph = open_flash_chart_object(500,250, ‘/open_flash_chart/one’, false, ‘/’)

  19. By Chap on Jan 4, 2008 | Reply

    Beautiful stuff.<br/><br/>Question. I’m trying to insert a short, custom comment about each value into the tool tips.<br/><br/>This doesn’t work. comments = [New Info, Data is scarce here, Something else] and then g.set_tool_tip(comments)<br/><br/>It looks like the way that Open Chart access extra information in the tool tips is the #tip# variable. But I’m not sure how to designate a custom string into #tip# for each data point.<br/><br/>How do you assign this?

  20. By charlie on Jan 4, 2008 | Reply

    Chap, I am behind .. sorry - seems that version 1.9.7 was released Jan 28th - http://teethgrinder.co.uk/open-flash-chart/download.php&lt;br/&gt;I will get on it and let you know when the update is available, and I will also post code that does what you are looking to do. When it is available you will want to take a look at this - http://teethgrinder.co.uk/open-flash-chart/gallery-tool-tip-line.php

  21. By charlie on Jan 4, 2008 | Reply

    Chap - I have updated to 1.9.7, here is how to do customizable tool tips - http://pullmonkey.com/projects/open_flash_chart/view_source_code/extra_tt

  22. By Flavio on Jan 4, 2008 | Reply

    Thank for your reply Charlie, what do you mean with: "did you update your plugin?"<br/>I’ve installed your ruby plugin by the 1st of February following your <a href="http://pullmonkey.com/projects/open_flash_chart">instructions</a>.<br/>I never checked out the <a href="http://teethgrinder.co.uk/open-flash-chart/">Open Flash Chart Project</a> version, should I?<br/>I’ll try with your latest version.

  23. By charlie on Jan 4, 2008 | Reply

    Flavio - Try the latest version, but also one more thing I noticed is that you have /projects in there for the url and the base :), that is what I have to have because my open flash chart stuff is under projects, but I leave it in there so people know what those parameters are used for. So do this instead - open_flash_chart_object(500,250, ‘/open_flash_chart/scatter’, true, ‘/’)

  24. By thommy on Jan 4, 2008 | Reply

    I’ve found a bug concerning the link functionality. I’m not sure if it’s fixed yet. Here is my fix:<br/>@links.each do |link|<br/> if num == 1<br/> temp << format_output(output_type, ‘links’, link)<br/> else<br/> temp << format_output(output_type, "links_#{num}", link)<br/> end<br/> num += 1<br/>end<br/><br/>Just replaced ‘data’ with ‘link’.<br/>By the way: Nice plugin, thanx for your good work.

  25. By charlie on Jan 4, 2008 | Reply

    Wow - Teach me to copy and paste from data values to links :) I have fixed it and it is in subversion. Thank you!

  26. By Alvin on Jan 4, 2008 | Reply

    Hi, I’ve tried to make it work, but no luck, the webbrowser didn’t display anything, any ideas?

    Here is my controller. (rails version 1.2.5)

    class GraphsController < ApplicationController
    def view
    @graph = open_flash_chart_object(500,250, ‘/graphs/bar_chart’, true, ‘/’)
    def bar_chart
    bar = BarOutline.new(50, ‘#9933CC’, ‘#8010A0′)
    bar.key("Page VIEWS", 10)

    10.times do |t|
    bar.data << rand(7) + 3

    g = Graph.new
    g.title("BAR CHART", "{font-size: 15px;}")

    g.data_sets << bar

    g.set_x_labels(%w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct))

    g.set_x_label_style(10, ‘#9933CC’, 0,2)
    g.set_y_legend("OPENF LADF", 12, "#736AFF")
    render :text => g.render

  27. By charlie on Jan 4, 2008 | Reply

    @Alvin - looks good from first glance, some questions:
    1) If you view page source, do you see anything related to open flash chart that may just not be rendering?
    2) Are you using firefox or IE or something else? Any javascript error messages?
    3) Do you have the open_flash_chart.swf inside your public directory.
    4) What does http://your_domain.com/graphs/bar_chart display? You may have to view source to find out.

    Let me know.

  28. By Botti on Jan 4, 2008 | Reply

    @alvin & @charlie

    The output from /graph/view the said scenario was:
    <script type="text/javascript" src="/javascripts/swfobject.js"></script>
    <div id="flashcontent"></div>
    <script type="text/javascript">
    var so = new SWFObject("/open-flash-chart.swf", "chart","500","250", "9", "#FFFFFF");
    so.addVariable("data", "%2Freport%2Fbar_chart");
    so.addParam("allowScriptAccess", "sameDomain");
    <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"
    width="500" height="250" id="ie_chart" align="middle">
    <param name="allowScriptAccess" value="sameDomain" />
    <param name="movie" value="/open-flash-chart.swf?500&height=250&data=%2Freport%2Fbar_chart" />
    <param name="quality" value="high" />
    <param name="bgcolor" value="#FFFFFF" />
    <embed src="/open-flash-chart.swf?data=%2Freport%2Fbar_chart" quality="high" bgcolor="#FFFFFF" width="500" height="250" name="chart" align="middle" allowScriptAccess="sameDomain"
    type="application/x-shockwave-flash" pluginpage="http://ww.macromedia.com/go/getflashplayer" id="chart"/>

    I removed the <noscript> tags from lib/open_flash_chart.rb and the graph now displays fine.

  29. By Botti on Jan 4, 2008 | Reply

    (the source in my previous comment is clipped at the top)

    If you edit lib/open_flash_chart.rb (comment out line 825 and 840), remember to restart your environment for the changes to take effect.

  30. By charlie on Jan 4, 2008 | Reply

    @Botti and @alvin -
    You can just set the use_swfobject to false via:
    open_flash_chart_object(500,250, ‘<url>’, false, ‘/’)
    The params are as follows - (width, height, url, use_swfobject, base). If you have use_swfobject set to true, then you need to include the swfobject.js file. So instead of modifying the plugin you can just set use_swfobject to false and it will only display the object :)

  31. By Botti on Jan 4, 2008 | Reply

    Ah, d’oh. Was unclear about the params, but should have figured out the bit about swfobject js ;) Thanks.

  32. By leethal on Jan 4, 2008 | Reply

    Github github github github github github github github github!

Sorry, comments for this entry are closed at this time.