PullMonkey Blog

28 Jul

Open Flash Chart II – multi-bar graph with tooltips

Just got a comment that says tooltips are not working properly, so I decided to do an example for you guys (and gals). This example is based on teethgrinder's tooltip example.

Here is the graph we are after in this example:

More Open Flash Chart II examples.

And here is the code (the controller):
NOTE: You will need the latest plugin and open-flash-chart.swf (as of this article) for the tooltips to register properly.


class TestItController < ApplicationController
  def index
    @graph = open_flash_chart_object(600,300,"/test_it/graph_code")

  def graph_code
    # based on this example - http://teethgrinder.co.uk/open-flash-chart-2/tooltip.php
    title = Title.new("MultiBar Tooltip")

    bar = Bar.new
    bar.values  = [9,8,7,6,5,4,3,2,1]
    bar.tooltip = "Title Bar l<br>val = #val#"
    bar.colour  = '#47092E'

    # NOTE: you can use obj.variable=() or obj.set_variable() interchangeably

    bar2 = Bar.new
    bar2.set_tooltip("Spoon {#val#}<br>Title Bar 2")

    vals = [1,2,3,4]

    tmp = BarValue.new(5)
    tmp.set_tooltip("Spoon {#val#}<br>Title Bar 2<br>Override bar 2 tooltip<br>Special data point")
    vals << tmp
    vals << [6,7,8,9]
    vals = vals.flatten

    bar2.values = vals

    t = Tooltip.new
    t.stroke = 5
    t.colour = '#6E604F'
    t.set_title_style("{font-size: 14px; color: #CC2A43;}")
    t.set_body_style("{font-size: 10px; font-weight: bold; color: #000000;}")

    chart = OpenFlashChart.new
    chart.title = title

    render :text => chart.to_s

And in your view (index.html.erb):


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

Good Luck!

32 Responses to “Open Flash Chart II – multi-bar graph with tooltips”

  1. By stu on Jul 28, 2008 | Reply

    Thanks! Yep, I was doing it wrong 🙂

    I really appreciate the detailed example.

  2. By monk.e.boy on Jul 28, 2008 | Reply

    Cool 🙂

    I is gunna link to this from the tooltip demo page. I fink you is doing v.v.nice work 🙂

    hats off.


  3. By syl on Jul 28, 2008 | Reply

    I am sorry but for me the tooltips does not work…

  4. By charlie on Jul 28, 2008 | Reply

    @syl – make sure you have the latest plugin and the latest open_flash_chart.swf as of this article.

  5. By Nick on Jul 28, 2008 | Reply

    Actually, I have the same issue as well. I get an error #2032 with the code from your pie chart example.

    Further tracing gives me:

    <filter:code attributes=lang=ruby>
    You have a nil object when you didn’t expect it!
    You might have expected an instance of Array.
    The error occurred while evaluating nil.empty?

    D:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:376:in `new_constants_in’
    D:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:202:in `load_file’
    D:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:94:in `require_or_load’
    D:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:248:in `load_missing_constant’
    D:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:453:in `const_missing’
    D:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:465:in `const_missing’
    D:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:471:in `send’
    D:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:471:in `const_missing’
    vendor/plugins/Open_Flash_Chart.git/lib/open_flash_chart.rb:62:in `tooltip=’

    The offending line seems to be:

    <filter:code attributes=lang=ruby>
    def set_tooltip(tip)
    if tip.is_a?(Tooltip)
    #we have a style for our chart’s tooltips
    @tooltip = tip
    # the user could just use set_tip(tip) or tip=(tip) to just set the text of the tooltip
    @tip = tip
    alias_method "tooltip=", :set_tooltip

    Line 62 is "if tip.is_a?(Tooltip)". Not sure what is happening after this, however.

  6. By charlie on Jul 28, 2008 | Reply

    @Nick – I am unable to reproduce your problem. I used the pie chart code from here – http://pullmonkey.com/2008/7/29/open-flash-chart-ii-pie-chart and everything works fine. Would you mind posting your code, even though it should be identical to the pie chart code?

  7. By syl on Jul 28, 2008 | Reply

    It’s still not working, where can I find the latest plugin for RoR because I took the swf and the javascript file from the official Open Flash Chart website.

  8. By charlie on Jul 28, 2008 | Reply

    @syl – here are the instructions – http://pullmonkey.com/projects/open_flash_chart2, all the latest code is out on github.

  9. By syl on Jul 28, 2008 | Reply

    Is anyone know something about a problem when IE6 load swfobject.js and bug because the file is too much heavy for him?

    When I load the html page IE6 use all the memory and don’t display the page.

  10. By Winston on Jul 28, 2008 | Reply

    For the older version of OFC, I can see from http://teethgrinder.co.uk/open-flash-chart/gallery-area-2.php that it is able to display both x-axis and y-axis values in a tooltip for an Area chart

    May I know if this is doable in OFC2?

    What are the "magical values" which I can use?

    Thank you!

  11. By Syl on Jul 28, 2008 | Reply

    I am sorry but since 3 days I am searching why, when I am loading my Graph on IE6 it crash and my computer swap a lot and crash if I don’t stop iexplore manualy.
    If anyone of you have an idea I am interrested…
    Thank you in advance…


  12. By Syl on Jul 28, 2008 | Reply

    I finaly found why the tooltips doesn’t work with the last "http://github.com/pullmonkey/open_flash_chart&quot; version.

    The tooltip class isn’t load at startup so to enable it you have to put the following line in the file :

    -> RAILS_ROOT/vendor/plugins/open_flash_chart.git/init.rb

    Add this line:

    require ‘tooltip’

    Then restart your application:
    RAILS_ROOT/script/server -d webrick -p 3000

    Here is my init.rb file:

    # Include hook code here
    require ‘open_flash_chart_object’
    require ‘open_flash_chart’
    require ‘bar_base’
    require ‘bar’
    require ‘bar_3d’
    require ‘bar_glass’
    require ‘bar_sketch’
    require ‘bar_stack’
    require ‘h_bar’
    require ‘line_base’
    require ‘line’
    require ‘line_dot’
    require ‘line_hollow’
    require ‘pie’
    require ‘scatter’
    require ‘title’
    require ‘tooltip’
    require ‘x_axis_label’
    require ‘x_axis_labels’
    require ‘x_axis’
    require ‘x_legend’
    require ‘y_axis_base’
    require ‘y_axis’
    require ‘y_axis_right’
    require ‘y_legend’

  13. By Syl on Jul 28, 2008 | Reply

    I finaly found why the tooltips doesn’t work with the last "http://github.com/pullmonkey/open_flash_chart&quot; version.

    The tooltip class isn’t load at startup so to enable it you have to put the following line in the file :

    -> RAILS_ROOT/vendor/plugins/open_flash_chart.git/init.rb

    Add this line:

    require ‘tooltip’

    Then restart your application:
    RAILS_ROOT/script/server -d webrick -p 3000

    Here is my init.rb file:

    # Include hook code here
    require ‘open_flash_chart_object’
    require ‘open_flash_chart’
    require ‘bar_base’
    require ‘bar’
    require ‘bar_3d’
    require ‘bar_glass’
    require ‘bar_sketch’
    require ‘bar_stack’
    require ‘h_bar’
    require ‘line_base’
    require ‘line’
    require ‘line_dot’
    require ‘line_hollow’
    require ‘pie’
    require ‘scatter’
    require ‘title’
    require ‘tooltip’
    require ‘x_axis_label’
    require ‘x_axis_labels’
    require ‘x_axis’
    require ‘x_legend’
    require ‘y_axis_base’
    require ‘y_axis’
    require ‘y_axis_right’
    require ‘y_legend’

  14. By charlie on Jul 28, 2008 | Reply

    @Syl – thank you very much for your troubleshooting. I have corrected this in the git hub release.

  15. By Mahmoud on Jul 28, 2008 | Reply

    Greetings guys,

    I’d like to thank u for the wonderful plugin u worked on..
    I’d appreciate it if anyone can help me with this.

    what are the keys that can be used in the tooltip other than "#val#" in a Bar chart

    I found that the keys for the pie chart. (#val#, #total#, #percent#, #label#) http://teethgrinder.co.uk/open-flash-chart-2/pie-chart.php

    I need the x_label key to show up in the tooltip in a bar chart. but couldn’t get that done yet..

    Thanks in Advance

  16. By charlie on Jul 28, 2008 | Reply

    @Mahmoud – good question. Those details are best left up to teethgrinder (aka Monk.e.boy) to answer. The one you pointed out "#val#" is the only one I recall using with regards to bar charts. I would pose your exact question to Monk.e.boy in his <a href="http://openflashchart.com/forum/">OFC Forums</a> where is has been really quick to reply. If you find out, please drop us a line 🙂

  17. By Dalf on Jul 28, 2008 | Reply

    This example is exactly what I was looking for (thanks you), but it doesn’t work. My version of open flash chart is from 1 month ago.
    I have no error message, the chart doesn’t show up.
    Do you know why ?

    I call the action by using Ajax, and onSuccess :
    x = chart.load(transport.responseText);

    Thanks you in advance

  18. By Dalf on Jul 28, 2008 | Reply

    Ok I could resolve my problem.
    At first, I displayed an empty chart, and then the user can press "refresh" to refresh the chart (and show the one above). It was not working because my first chart had a problem.

    I like Open Flash Chart a lot, continue your very good work 😉

  19. By myrmidonguild on Sep 4, 2009 | Reply

    Thanks for the info

  20. By Helmut on Feb 4, 2010 | Reply

    Hello, i’ve found this really helpful! great plugin! but here is my problem… i have to elements in my chart, a bar and a line, so here is my code:

    def show
    @graph = open_flash_chart_object(520,300,”/dashboard/graph_code”)

    def graph_code
    @bills = current_user.bills.all(:order => ‘previous_meter_reading_date DESC’, :limit => 12).reverse
    bar = BarGlass.new
    bar.values = current_user.consumption_per_day_history.map { |b| b[:value] }
    bar.tooltip = “[#x_label#] {#x#} #key#:#val# kWh/day”
    bar.colour = ‘#89C33E’

    vals = current_user.consumption_per_day_history.map { |b| b[:value] }.reverse

    # prepare tooltip for the line
    dot = HollowDot.new( ‘0x80a033’, ‘Bounces’)
    dot.size = 4
    dot.halo_size = 3
    dot.tooltip = “[#x_label#] {#x#} #key#:#val# kWh/day”

    #set the line for the chart
    line = Line.new
    line.text = “[Friend name]’s consumption”
    line.width = 2
    line.colour = ‘#5E4725’
    line.dot_size = 15
    line.values = vals
    line.default_dot_style = dot

    #set the chart style and chart settings
    title = Title.new(“Electricity consumption, kWh per day”)
    x_axis = XAxis.new
    x_axis.set_range(0, @bills.size, 1) # set the number o
    x_axis.colour = ‘#C0C0C0’

    labels = XAxisLabels.new
    labels.labels = current_user.consumption_per_day_history.map { |b| “#{Date::ABBR_MONTHNAMES[b[:month]]} #{b[:year]}” }
    labels.rotate = 320
    x_axis.labels = labels

    max = 0
    @bills.each do |bill|
    max = bill.consumption_per_day if bill.consumption_per_day > max

    y_axis = YAxis.new
    y_axis.set_inner_bg_colour( ‘#E3F0FD’, ‘#CBD7E6’, 90 )
    y_axis.set_range(0, max, 2)
    y_axis.colour = ‘#C0C0C0’

    # x and y leyends
    x_legend = XLegend.new(‘Month and year’)
    x_legend.set_style(‘{font-size: 14px; color: #89C33E}’)
    y_legend = YLegend.new(“kWh/day”)
    y_legend.set_style(‘{font-size: 20px; color: #89C33E}’)

    t = Tooltip.new
    t.stroke = 4
    t.colour = ‘#89c33e’
    t.set_title_style(“{font-size: 13px; font-weight: bold; color: #89C33E;}”)
    t.set_body_style(“{font-size: 11px; color: #000000;}”)

    chart = OpenFlashChart.new
    chart.title = title
    chart.bg_colour = ‘#FFFFFF’
    chart.x_axis = x_axis
    chart.y_axis = y_axis

    render :text => chart.to_s

    so the both of them look good i can also make difference between the tooltips but when hovering the chart, the line tooltip looks great it displays all the values (i mean #x_label# #x# and #val#) but for the bar i can only get the #val# so don’t know how to get this working… am i doing anything wrong? can you please help me?

  21. By charlie on Feb 4, 2010 | Reply

    Check the JSON output and see if you notice anything strange.
    It all looks good to me, but x_label may not be supported for bar graphs.
    Ask in the open flash chart forums for more information. This plugin makes use of the swf file monkeyboy created.
    Forum – http://forums.openflashchart.com/

  22. By Helmut on Feb 4, 2010 | Reply

    @charlie, hey thanks for your reply! i found a way to make the #x_label# magic value work! i downloaded this file ” OFC2Patches-DZ-Hyperion.zip” from http://www.ofc2dz.com/OFC2/downloads/ofc2Downloads.html and then replaced the open-flash-chart.swf file with the one in the .zip and reloaded my chart and x_label appeared on my tooltip!

    anyway thanks a lot and hope this could be useful for someone!

  23. By wadner on Apr 15, 2010 | Reply


    Can you provide an exposure of your JSON in your examples above? I’m porting this over and it would be very helpful for troubleshooting if you can expose the JSON that is being used by your examples.


  24. By charlie on Apr 18, 2010 | Reply

    You can get to the JSON of any of my examples, just do a “view page source” and look for the “data-file”.
    So for this one it is here – http://pullmonkey.com/projects/open_flash_chart2/multi_bar_tooltip

  25. By vivi on Oct 27, 2010 | Reply

    Hi Charlie, could i use LineValue.new ? I tried to use it for tooltip (as you did in Bar) but got error. Need your help. Thanks

  26. By charlie on Oct 27, 2010 | Reply

    Hey vivi – what error are you getting?

  27. By vivi on Oct 28, 2010 | Reply

    Thanks for your reply. Finally i found the way to solve my problem.

    My error is: for big number such as: 15234123, it dispalys as 15.234,123 for German formatting. It must be 15.234.123.

    So, the solution is: downloaded ”OFC2Patches-DZ-Hyperion.zip” from http://www.ofc2dz.com/OFC2/downloads/ofc2Downloads.html and then replaced the open-flash-chart.swf file with the one in the .zip and reloaded my chart.

  28. By xinghu on Jan 10, 2011 | Reply


    I install the plugin from your github, but I can not display the tooltip box in my chart.

    After I change the open-flash-chart.swf(269.7kb) to http://pullmonkey.com/projects/open-flash-chart.swf(256.9kb) it works!

    But the new swf file can not display the xaxis labels’s visible_steps.

    Could you tell me what to do if I want to see the tooltip and the correct xaxis label ?

    many thanks!

    here is my json:


  29. By res on Mar 1, 2011 | Reply

    Hi I m using ofc2 for my charts. and m havin issues wit tooltips.it continues to hover even wen pointer is not on line or points.please suggest

    Thanks n Regards

  30. By charlie on Mar 2, 2011 | Reply

    Hey Res,

    What swf file version are you using with the plugin? The one provided or a later version?

    – Charlie

  31. By michael on Aug 4, 2012 | Reply

    Started GET “/chars/graph_code” for at 2012-08-04 11:03:43 +0800
    Processing by CharsController#graph_code as */*
    Completed 500 Internal Server Error in 16ms

    NoMethodError (undefined method `returning’ for #):
    lib/myplugin/open_flash_chart/lib/open_flash_chart/base.rb:118:in `method_missing’
    lib/myplugin/open_flash_chart/lib/open_flash_chart/base.rb:40:in `render’
    app/controllers/chars_controller.rb:60:in `graph_code’

    why render :text => chart.to_s go wrong?

  32. By charlie on Aug 4, 2012 | Reply

    Hey Michael – Can you post the code that you see having returning in it? Also, are you using the latest from github?

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