places projects software writing photos

leif > software >

Histogram CGI Script

This little hack of a script generates a histogram image (PNG format) for data sets given as CGI parameters.

Download

Get the script here : histogram.py. Rename it to histogram.cgi.

Install and configure

To install, put the script somewhere in your web server's file space, and enable the web server to execute the new installation as a CGI script. You'll need to have Python (version 2.1 or later) and the Python Imaging Library installed.

If you really trust this software, then you might just want to install it in your web server's /cgi-bin/ directory, which is probably already configured for CGI script execution. Before you do this, however, note that this script is distributed under the terms of the GNU GPL and hence comes with NO WARRANTY.

If you install the script somewhere else, you'll likely need to do some additional configuration. With Apache, for example, if you installed the script in a web directory called /scripts/python, then you'll need to add some lines to your server's httpd.conf file as follows :

  <Location /scripts/python>
    AddHandler cgi-script .cgi
    Options ExecCGI
  </Location>

The descriptions in the next section assume the script is located at the URL http://foo.net/bar/histogram.cgi.

Use

This script will generate a histogram containing all multiple-value data types passed as parameters. For example, to generate a histogram with values of 1, 2, 3, and 1, call the script like this :

  http://foo.net/bar/histogram.cgi?q=1&q=2&q=3&q=1

This URL will produce an image like this :

And to draw a histogram with another data set, just append the points for second data set, using a different name for the second data set :

  http://foo.net/bar/histogram.cgi?q=1&q=2&q=3&q=1&e=1.1&e=1.5&e=3&e=2.3

This URL will produce an image like this :

You can display up to 100 data sets on one histogram, though that is easily extensible (just extend the length of the series_colors list in the script).

The names of the data sets are ignored. At some remote point in the future there could conceivably be support for automatically drawing a legend, and then the data set names will be taken into account. Don't count on it, though !

A couple of final notes :

Parameters

The most essential script parameters, as indicated above, are the ones you make up : just assign a label to each data set, and pass each data point in the data set as a parameter with the data set's label.

There are some other parameters available to customize the output a bit.

There are currently three bar types available :

Also note that there aren't parameters available for configuring the colors of the data sets. Too bad, I'm just tired at this point, and I like the default colors. :-) I hope you can get some use out of this script regardless.

Contact

Leif Johnson : <leif@ambient.2y.net>