Thursday, August 30, 2012

Ruby charts

Managers love charts and statistics. Few open-source test automation tools are having any built-in tools for it. If you are using Ruby for scripting, then one of the easiest way to create charts is to use the googlecharts gem. It is using Google Charts API for creating charts.

You can install googlecharts gem with following command:

gem install googlecharts

Here is an example on how to use it:

require 'gchart'

g = Gchart.bar(:data => [5, 10, 20, 15],
:bar_colors => 'DDFFAA,5BFFA5,FFCDCD,FFADAD',
:stacked => true,
:size => '666x450',
:bg => 'f8f8ff',
:bar_width_and_spacing => '50,12,24',
:legend => ['1-Low','2-Medium','3-High'],
:max_value => 50,
:axis_with_labels => 'x,y',
:axis_labels => [['New','Open','Closed'],[5,10,15,20,25],
:title => "Defects Summary Graph - by Status group by Severity" )


gcharturl = g.split('?')[0]
gchartpostdata = g.split('?')[1]
url = URI.parse(gcharturl)

ht = Net::HTTP.new(url.host, url.port)
ht.start
res = ht.request_post(gcharturl, gchartpostdata)
ht.finish

open("summary.png", "wb") { |file|
file.write(res.body)
}


... and it would create an image close to this one:


A couple of things to notice. 

  1. The Gchart function call will return the URL to the graph. You can access it using GET, but it won't work if the URL is longer than 1024 characters . That's why I used POST to get the image data.
  2. The chart can contain max. 300000 pixels, so if you specify a size larger than that, then creating the image will fail. 

With Ruby you can easily access MySQL database. So test results can be easily saved to database and the data can be used to create nice graphs for the management to love!

If you haven't used Ruby with MySQL, here's simple instructions:

  1. Install mysql gem with command: gem install mysql
  2. Make sure that libmysql.dll exist on your PC and that it is accessible (in a folder in PATH). Also the version need to match with the gem version. More information about it can be found from here.
How to use it e.g.


require 'mysql'

con = Mysql.new(myserver.com, 'myuser', 'mypwd', 'mydb') 
con.query("UPDATE xxx SET yyy=1 WHERE 1=1")
con.close 


More information about googlecharts:

 
 

No comments:

Post a Comment