EYODF : Reading chart using gchartrb
EYODF, for those who do not know stands for Eat Your Own Dog Food. Today, I decided to use gchartrb to plot a line chart of the number of books I have read since Jan 2006, an information that I maintain quite religiously using my del.icio.us ‘read’ tag.
It was an enlightening experience, using the stuff that I built myself. Apart from a few things that I noticed were inelegant, I also discovered a few bugs. But first the code. If you want to use the code, you will need the gchartrb and rubilicious gems installed.
require 'rubygems'
require 'rubilicious'
require 'google_chart'
# Retrieve from delicious
r = Rubilicious.new('deepakjois', 'amistupid')
books = r.all('read')
# Create a monthwise breakdown of books read since 2006
books_grouped = []
[2006,2007].each do |year|
12.times do |month|
count = books.select {|b| b['time'].year == year and b['time'].month == month+1 }.size
books_grouped << [ "#{Date::ABBR_MONTHNAMES[month+1]}" ,count]
end
end
# Create chart
lc = GoogleChart::LineChart.new('600x250', 'Reading Chart 2006-Present', false)
lc.show_legend = false
lc.data_encoding = :text # more granular
data_series = books_grouped.collect{ |b| b.last } # numerical data
labels = books_grouped.collect{ |b| b.first } # labels
lc.data "", data_series, 'ff0000'
lc.axis :x, :labels => labels,
:color => '000000' # Months
lc.axis :x, :labels => [2006, 2007],
:positions => [0, (12/data_series.size.to_f) * 100],
:color => '000000',
:font_size => 12 ,
:alignment => :left # Year
lc.axis :y, :labels => (0..data_series.max).to_a,
:color => '000000' # Number of books
lc.fill :chart, :solid, :color => "ffffff"
lc.fill :background, :solid, :color => 'ffffff'
lc.grid :y_step => 100/(data_series.max.to_f), :x_step => 0, :length_segment => 2, :length_blank => 1
# Adding small black circles as shape markers
data_series.size.times do |i|
lc.shape_marker :circle, :color => '000000', :data_set_index => 0, :data_point_index => i, :pixel_size => 5
end
puts lc.to_url(:chts => '000000,20') # Set title font size and color manually due to bug in gchartrb
This code led to the chart below
I discovered a couple of bugs in gchartrb, apart from a couple of features I missed out due to not reading the API carefully enough.
- The
title_colorandtitle_font_sizeattributes do not work as advertised. Need to fix that ASAP. As a workaround, I have manually added those parameters while calling theto_urlmethod. - I did not realise that the shape markers and the range markers use the same param name, i.e.
chm. If I try to add both using gchartrb, one of them gets overwritten by the other.
Now off to fix those bugs, and implement those features.
That looks really cool, much clearer than grokking gnuplot manuals :)
hi there, wana draw some cool and awesome Sparkline chart then chk this out visifire an amazing chart controller powered by silverlight offered under open source just for free
Post a Comment