Posts Tagged ‘Ruby’

Easy Charts in FXRuby with the Google Chart API

InfoQ has just published an article written by Matthew Bass that introduces the Google Chart API and the gchartrb library, which you can use to programmatically generate URLs for use with Google Chart. It’s very easy to use this library to generate charts in your FXRuby applications. First, install the gchartrb gem:

$ sudo gem install gchartrb
You’ll need to poke around the gchartrb documentation to decide just what kinds of charts are supported; for this example, we’ll just use a bar chart example from Matthew’s article:

def bar_chart'600x200', 'My Chart', :vertical) do |bc| 'Trend 1', [5,4,3,1,3,5], '0000ff' 'Trend 2', [1,2,3,4,5,6], 'ff0000' 'Trend 3', [6,5,4,4,5,6], '00ff00'

The toescapedurl method for the BarChart object returns a URL from which we can retrieve the chart image data in PNG format. We can in turn use that data to construct an FXPNGImage, and place it inside an FXImageFrame:, nil, :opts => LAYOUT_FILL) do |f|
  f.image =, open(bar_chart.to_escaped_url, "rb").read)

The result is a nice little bar chart, as shown here:


For reference, here’s the complete program:

Ruby and Rails Resources

I’ve just added a new page of links to Ruby and Rails resources. It’s primarily intended for software developers who are new to Ruby and want to get up to speed as quickly as possible. Please leave any suggested additions in the comments.