Digital Drip - The Tech Blog of Jon Gaudette
Blog  /  Ruby Histogram Chart
Ruby

Ruby Histogram Chart

Posted on: August 21, 2009

I’m working on a new project that involves creating dynamic charts of the number of steps and number of steps walked at a moderate pace. I’ve chosen Ruby on Rails as my language and framework, since it’s something I’ve wanted to learn for a while and it just “seemed right”.

When trying to create this charts, I wanted to do it in a native ruby library, as shelling out to a separate program is far from pragmatic. I was excited then to see that there is a ruby binding for gnuplot, the de facto Unix command line plotting tool. After many frustrating hours spent trying to convert my gnuplot syntax of a histogram into ruby_gnuplot, I finally figured it out. Hopefully others can save time and learn from my frustration.

Here’s the chart I’m trying to make:

Plot showing stacked row histogram

To start off with gnuplot, let’s take a look at our data. For a steps.dat file I have the following:

date steps mod_steps
2009-08-09 1936 8178
2009-08-08 880 13844
2009-08-07 1424 15572 
2009-08-06 1242 21451
2009-08-05 1666 14160
2009-08-04 2322 20286
2009-08-03 1442 16653
2009-08-02 478 15529
2009-08-01 2561 13055
2009-07-31 1012 1043
2009-07-30 1452 12383
2009-07-29 1283 15452
2009-07-28 1099 8858

The following gnuplot script will create a rowstacked histogram of this data, and output it to ‘steps.png’
set terminal png transparent nocrop enhanced
set output 'steps.png'
set boxwidth 0.2 absolute
set style fill solid 1.00 border -1
set style histogram rowstacked
set style data histograms
set xtics border in scale 1,0.5 nomirror rotate -45  offset character 0, 0, 0
set title "Testing"
plot 'steps.dat' using 3:xtic(1) t 'moderate', '' using 2 t 'normal'

Here is a function that, using the ruby_gnuplot gem, will create an identical chart. Note that instead of using a data file, we have three arrays (x, y1, and y2) which hold our x-axis, y1-axis, and y2-axis. The trick here, and where the endless hours of searching the documentation came, is whereas in straight gnuplot we can use one plot command, in ruby_gnuploot we need to split this up into two plot calls.
 def genGnu
    Gnuplot.open do |gp|
      Gnuplot::Plot.new( gp ) do |plot|

plot.terminal “png transparent nocrop enhanced size 800 600” plot.output “public/myPlot.png” plot.boxwidth “0.2 absolute” plot.style “fill solid 1.00 border -1” plot.style “histogram rowstacked” plot.style “data histograms” plot.xtics “border in scale 1,0.5 nomirror rotate -45 offset character 0, 0, 0” plot.title "Steps for " + @user.name x = [] y1 = [] y2 = [] @steps = @steps.sort_by { |step| step[‘rec_date’] } @steps.each do |s| x.push(s.rec_date) y1.push(s.steps – s.mod_steps) y2.push(s.mod_steps) end plot.data << Gnuplot::DataSet.new( [x, y2] ) do |ds| ds.using = “2:xtic(1) t ‘moderate’” end plot.data << Gnuplot::DataSet.new( [x, y1] ) do |ds| ds.using = “2:xtic(1) t ‘normal’” end end end end

Comments are Disabled