| 1 | .. contents:: |
|---|
| 2 | |
|---|
| 3 | ===== |
|---|
| 4 | PyCha |
|---|
| 5 | ===== |
|---|
| 6 | |
|---|
| 7 | Pycha is a very simple Python package for drawing charts using the great |
|---|
| 8 | `Cairo <http://www.cairographics.org/>`_ library. Its goals are: |
|---|
| 9 | |
|---|
| 10 | * Lightweight |
|---|
| 11 | |
|---|
| 12 | * Simple to use |
|---|
| 13 | |
|---|
| 14 | * Nice looking with default values |
|---|
| 15 | |
|---|
| 16 | * Customization |
|---|
| 17 | |
|---|
| 18 | It won't try to draw any possible chart on earth but draw the most common ones |
|---|
| 19 | nicely. There are some other options you may want to look at like |
|---|
| 20 | `pyCairoChart <http://bettercom.de/de/pycairochart>`_. |
|---|
| 21 | |
|---|
| 22 | Pycha is based on `Plotr <http://solutoire.com/plotr/>`_ which is based on |
|---|
| 23 | `PlotKit <http://www.liquidx.net/plotkit/>`_. Both libraries are written in |
|---|
| 24 | JavaScript and are great for client web programming. I needed the same for the |
|---|
| 25 | server side so that's the reason I ported Plotr to Python. Now we can deliver |
|---|
| 26 | charts to people with JavaScript disabled or embed them in PDF reports. |
|---|
| 27 | |
|---|
| 28 | Pycha is distributed under the terms of the `GNU Lesser General Public License |
|---|
| 29 | <http://www.gnu.org/licenses/lgpl.html>`_. |
|---|
| 30 | |
|---|
| 31 | Documentation |
|---|
| 32 | ============= |
|---|
| 33 | |
|---|
| 34 | Installation |
|---|
| 35 | ------------ |
|---|
| 36 | |
|---|
| 37 | Pycha needs PyCairo to works since it uses the Cairo graphics library. If you |
|---|
| 38 | use Linux you will probably already have it installed so you don't have to do |
|---|
| 39 | anything. If you use Windows these are the recommended steps for installing |
|---|
| 40 | PyCairo: |
|---|
| 41 | |
|---|
| 42 | 1. Grab the latest PyCairo Windows installer from |
|---|
| 43 | http://ftp.gnome.org/pub/GNOME/binaries/win32/pycairo/ You need to use the |
|---|
| 44 | one that matches your Python version so take the one ending in -py2.4.exe |
|---|
| 45 | for Python 2.4 or the one ending in -py2.5.exe for Python 2.5 |
|---|
| 46 | 2. Install it in your Python environment (just follow the installation |
|---|
| 47 | program instructions) |
|---|
| 48 | 3. Put the Cairo dlls inside the pycairo directory inside your site-packages |
|---|
| 49 | directory or anywhere in your path. You can find the dlls at |
|---|
| 50 | http://www.gimp.org/%7Etml/gimp/win32/downloads.html Go there and download |
|---|
| 51 | the following packages: |
|---|
| 52 | |
|---|
| 53 | 1. cairo.zip. You just need the libcairo-2.dll file inside that zip |
|---|
| 54 | 2. libpng.zip. You just need the libpng13.dll file inside that zip |
|---|
| 55 | 3. zlib.zip. You just need the zlib1.dll file inside that zip |
|---|
| 56 | |
|---|
| 57 | Pycha is distributed as a Python Egg so is quite easy to install. You just need |
|---|
| 58 | to type the following command: |
|---|
| 59 | |
|---|
| 60 | easy_install pycha |
|---|
| 61 | |
|---|
| 62 | And Easy Install will go to the Cheeseshop and grab the last pycha for you. If |
|---|
| 63 | will also install it for you at no extra cost :-) |
|---|
| 64 | |
|---|
| 65 | Tutorial |
|---|
| 66 | -------- |
|---|
| 67 | |
|---|
| 68 | Using pycha is quite simple. You always follow the same 5 simple steps: |
|---|
| 69 | |
|---|
| 70 | 1. Create a Cairo surface to draw the chart on |
|---|
| 71 | 2. Build a list of data sets from which your chart will be created |
|---|
| 72 | 3. Customize the chart options. |
|---|
| 73 | 4. Create the chart, add the datasets and render it |
|---|
| 74 | 5. Save the results into a file or do whatever you want with the Cairo |
|---|
| 75 | surface |
|---|
| 76 | |
|---|
| 77 | To create the Cairo surface you just need to say the type of surface and its |
|---|
| 78 | dimensions: |
|---|
| 79 | |
|---|
| 80 | import cairo |
|---|
| 81 | width, height = (500, 400) |
|---|
| 82 | surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) |
|---|
| 83 | |
|---|
| 84 | Then you should create your data set querying a database or any other data |
|---|
| 85 | source:: |
|---|
| 86 | |
|---|
| 87 | dataSet = ( |
|---|
| 88 | ('dataSet 1', ((0, 1), (1, 3), (2, 2.5))), |
|---|
| 89 | ('dataSet 2', ((0, 2), (1, 4), (2, 3))), |
|---|
| 90 | ('dataSet 3', ((0, 5), (1, 1), (2, 0.5))), |
|---|
| 91 | ) |
|---|
| 92 | |
|---|
| 93 | As you can see, each data set is a tuple where the first element is the name of |
|---|
| 94 | the data set and the second is another tuple composed by points. Each point is a |
|---|
| 95 | two-elements tuple, the first one is the x value and the second the y value. |
|---|
| 96 | |
|---|
| 97 | Not every chart uses all the information of a data set. For example, the Pie |
|---|
| 98 | chart only uses the first point of each dataset and it only uses the y value of |
|---|
| 99 | the point. |
|---|
| 100 | |
|---|
| 101 | Now you may want to specify some options so the chart can be customize changing |
|---|
| 102 | its defaults values. To see the defaults you can check the |
|---|
| 103 | pycha.chart.Chart.__init__ method in the source code. You can use regular |
|---|
| 104 | dictionaries to define your options. For example, imagine you want to hide the |
|---|
| 105 | legend and use a different color for the background:: |
|---|
| 106 | |
|---|
| 107 | options = { |
|---|
| 108 | 'legend': {'hide': True}, |
|---|
| 109 | 'background': {'color': '#f0f0f0'}, |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | Now we are ready to instantiate the chart, add the data set and render it:: |
|---|
| 113 | |
|---|
| 114 | import pycha.bar |
|---|
| 115 | chart = pycha.bar.VerticalBarChart(surface, options) |
|---|
| 116 | chart.addDataset(dataSet) |
|---|
| 117 | chart.render() |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | Right now you can choose among 4 different kind of charts: |
|---|
| 121 | |
|---|
| 122 | * Pie Charts (pycha.pie.PieChart) |
|---|
| 123 | * Vertical Bar Charts (pycha.bar.VerticalBarChart) |
|---|
| 124 | * Horizontal Bar Charts (pycha.bar.HorizontalBarChart) |
|---|
| 125 | * Line Charts (pycha.bar.LineChart) |
|---|
| 126 | * Scatterplot Charts (pycha.scatter.ScatterplotChart) |
|---|
| 127 | |
|---|
| 128 | Finally you can write the surface to a graphic file or anything you want using |
|---|
| 129 | the cairo library:: |
|---|
| 130 | |
|---|
| 131 | surface.write_to_png('output.png') |
|---|
| 132 | |
|---|
| 133 | That's it! You can see more examples in the examples directory of the source |
|---|
| 134 | code. |
|---|
| 135 | |
|---|
| 136 | Development |
|---|
| 137 | ----------- |
|---|
| 138 | |
|---|
| 139 | You can get the last bleeding edge version of pycha by getting a checkout of |
|---|
| 140 | the subversion repository:: |
|---|
| 141 | |
|---|
| 142 | svn co http://www.lorenzogil.com/svn/pycha/trunk pycha |
|---|
| 143 | |
|---|