Creating Graphs in Ruby
I have been working in the Financial services industry for over nine years. In that time I have found that at some or other point you are going to be asked to create a line, bar or pie chart. So today I was investigating how to do this with Ruby. If we are going to be creating charts for a Web application, according to The Ruby Toolbox Chartkick is the favorite. However in my case I just want to create some simple PNGs, so I am going to use Gruff.
Gruff created by Geoffrey Grosenbach has a tag-line of ‘Beautiful graphs for one or multiple datasets. Can be used on websites or in documents.’ So let’s test that out and create some charts.
First we are going to need some test data, I went and pulled down Google’s stock performance for the last six months or so. To get this data just use Yahoo finance they have a nice export to Spreadsheet button that give you the stock information in a CSV. To get to the historical data just hit http://finance.yahoo.com/q/hp?s=GOOG.
So the requirement for today is simple, show the Open, High, Low and Close prices for Google’s stock on a line chart.
Firstly you are going to need the ‘Gruff’ library, if you have a Gemfile then just add the following line to your Gemfile:
Alternatively just install the gem via the command-line:
gem install gruff
Here is the full code listing, we will go through the majors parts below:
In our code, we need two require statements. One will be for the Gruff gem, and another will be for CSV. Since we are reading the stock information from a CSV, we can use Ruby’s built-in library to parse our document.
At line four we assign the contents of our CSV file to a variable called google, since our CSV contains a header on the first line we make sure to let Ruby know that this is the case.
Next is some boilerplate code to create our graph with a width of 600 pixels and we set the header on the chart.
The x-axis of our Line chart is going to be the trading dates. We call the ‘foreach’ method on the CSV and pull out all the trade dates. To get the start date of our dataset we call the ‘min’ method on the array of dates. For the “middle” date, we do some simple Math to get the date that is in the middle of the start and end dates. Lastly for the end date of our dataset we again just call ‘last’ which gets the last element in the array.
Then we are ready to set the labels property on our graph object, we set the property to a Hash of keys and dates. The labels property will then be displayed as our x-axis.
For each line on our Line chart, we need to pass in the label as well as the data series. Since we are looking for Open, High, Low and Close that is the labels we pass through as well as the corresponding data associated.
I go ahead and set some meaningful labels on the x and y axis, thereafter to create the chart we call the write method passing in the path with the file extension to where the image of the chart needs to be save.
All of this will output this image:
Pretty cool right? There wasn’t much involved in getting this working there are numerous other charts available in Gruff, should you be interested I recommend checking out the repository on GitHub and playing with the library.