Tuesday, April 15, 2008

Drawing Chart options.

Pretty chart drawing is a very attractive feature in any software. Why chart is nice? Human perceives 3D space, but human communication is 1D space (Linear!) We speak in just one direction. We read in one direction. But, mixing one segment of information with another is not natural communication method. Remember your history class. For comparative topics between other nations along side with time line, it requires some table. Primitive 2D communication. And if table can be quantified, Chart can come into play for visualizing the quantity. Enhanced 2D communication.

Okay, my tool of choice is python.
First option. Raw rendering using Xlib or GTK or Qt4. Unless they support widgets to play with, rendering with fundamental components (Point, Line, Rectangle, Circle) is painful. It is not an option but making life painful.
Next option. PyChart. Free library to generate image of a chart. Eh~~ okay to use, but not pretty. If I am drawing something, the fancier the better!
Then, Chart Director from advsofteng.com. Prettier, and free if their credit lable can be bearable. To remove this label, I need to purchase the license. Fortunately, their pricing makes very sense. However, not free.
ReportLab can be another option. ReportLab is a perfect tool for PDF generation, but not very good for rendering sophisticated graphs. And its output format is only PDF.
Gnuplot is another good one. Especially, for time series, gnuplot is one of the best. But, it is a mathematical graph. If I am drawing certain shape, and it does not meet mathematical property, it is not good. And gnuplot speaks its own language. Need to learn it from the documentation.

Google Chart API is my choice. And they begin to support geographical map chart. How nice is it. Limitation of map chart is the size. I can not create bigger map than size of 440x220. Other than that, Google Chart API wins all other options. Wait... Other libraries don't even mention map support. Google API already won on it.

But~~ it is web-request. No problem. Python has great urllib2 library.
Here is my sample code.

import urllib2

# If you are behind proxy to go through,
my_proxy = urllib2.ProxyHandler( {"http" : "http://proxy_host:proxy_port"} )
opener = urllib2.build_opener(my_proxy)

api_url = 'http://chart.apis.google.com/chart'
chart_property = dict(cht='p3', chd='t:30,20,50', chs='250x100', chl='Hello|World|Raymond')

def make_request( api_url, chart_property ):
  return api_url + "?" + '&'.join( [ "%s=%s" % (k,v) for k,v in chart_property.items() ] )

png = urllib2.urlopen(make_request(api_url, chart_property)).read()
fh = open('test.png','w')
print >> fh, png


jetxee said...

In fact, if you do not need to generate charts for the web in realtime, almost any other Python library is better.

My favourite is Matplotlib.

There are also PyX, Gnuplot.py (Python interface to gnuplot), and PyNGL.

Check also this list. Google Charts are by far not the best.

Raymond said...

I ran into Matplotlib sometime after this post. And, didn't know about others. Especially Gnuplot.py looks very interesting. I appreciate your good piece of info.