EYODF : Reading chart using gchartrb On Wednesday, December 19, 2007

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

Reading Chart 2006 – Present

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_color and title_font_size attributes do not work as advertised. Need to fix that ASAP. As a workaround, I have manually added those parameters while calling the to_url method.
  • 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.

Freedom 0 – A story in 5 acts On Friday, December 14, 2007

  1. Once upon a time there was a blogging platform called Movable Type. It was free (as in beer) and it was kickass.
  2. Then one day Movable Type released version 3.0 of their software and created severe restrictions on their licensing
  3. On that day Mark Pilgrim wrote Freedom 0, one of the most inspiring writings I have read since then.
  4. 1307 days later, Movable Type was open sourced
  5. Mark Pilgrim wrote 0+1307, and I fell off my chair laughing.

But seriously, I cannot put it better than how Mark put it in his original post. A couple of samples are below. Read the whole post, it is worth it.

In the long run, the utility of all non-Free software approaches zero. All non-Free software is a dead end.

It’s not about who has a right to make a living (everyone does); it’s not about how nice Ben and Mena are (I’ve met them, they are very nice); and it’s certainly not about eating. I’ve taken the $535 that Movable Type would have cost me, and I’ve donated it to the WordPress developers.

It’s not about money; it’s about freedom.

Plotting a sparklines chart using gchartrb and Google Chart API On Thursday, December 13, 2007

There are two ways to create a sparkline chart using the Google Chart API

  • According to this post on the Google Chart API mailing list, it is possible to plot a sparklines chart using some undocumented parameters
  • Another undocumented feature is the presence of a new chart type called lfi, which is used in this blog post (right at the end under ‘Did someone mention Edward Tufte?’)

Here is some sample code using gchartrb. It uses the custom parameters feature to specify the undocumented part as a hash just before generating the URL.


# Plotting a sparklines chart
sparklines = GoogleChart::LineChart.new('100x50', nil, false)
sparklines.data "Test", [4,3,2,4,6,8,10]
sparklines.show_legend = false
sparklines.axis :x, :labels => [] # Empty labels
sparklines.axis :y, :labels => [] # Empty labels
puts sparklines.to_url(:chxs => "0,000000,10,0,_|1,000000,10,0,_")

Generates the following URL

http://chart.apis.google.com/chart?chxl=0:||1:|&chd=s:YSMYkw9&chxs=0,000000,10,0,_|1,000000,10,0,_&cht=lc&chxt=x,y&chs=100x50

and the following image

Google Chart

You can encapsulate this inside a method and call it repeatedly to generate a sparkline chart.

Once the option is documented, I should be adding it to gchartrb.

gchartrb On Tuesday, December 11, 2007

After a long time, I am finally back to doing some open source coding. I released gchartrb, which is a Ruby API around the recently released Google Chart API.

Here is some sample usage :


# Pie Chart
pc = GoogleChart::PieChart.new('320x200', "Pie Chart",false)
pc.data "Apples", 40
pc.data "Banana", 20
pc.data "Peach", 30
pc.data "Orange", 60
puts pc.to_url

Generates the graph as below

Generated using gchartrb

Visit the site for more info, examples and documentation.

What Makes a Terrorist On Sunday, December 9, 2007

..participation in terrorism is just a special application of the economics of occupational choice. Labor economists are, after all, experts on occupational choice. Some people choose to become doctors or lawyers or economists, and others pursue careers in terrorism.

What Makes a Terrorist by Allan B. Krueger.

Three kids and an iguana On Thursday, December 6, 2007

The one thing that so many of today's cute startups have in common is that all they have is a simple little Ruby-on-Rails Ajax site that has no barriers to entry and doesn't solve any gnarly problems. So many of these companies feel insubstantial and fluffy, because, out of necessity (the whole company is three kids and an iguana), they haven't solved anything difficult yet. Until they do, they won't be solving problems for people. People pay for solutions to their problems.

Joel Spolsky in Where there's muck, there's brass.