| 1 | .. index:: tutorial |
|---|
| 2 | .. _tutorial: |
|---|
| 3 | |
|---|
| 4 | ******** |
|---|
| 5 | Tutorial |
|---|
| 6 | ******** |
|---|
| 7 | |
|---|
| 8 | Using PyCha is quite simple. You always follow the same 5 simple steps: |
|---|
| 9 | |
|---|
| 10 | 1. Create a Cairo surface to draw the chart on. |
|---|
| 11 | 2. Build a list of data sets from which your chart will be created. |
|---|
| 12 | 3. Customize the chart options. |
|---|
| 13 | 4. Create the chart, add the datasets, and render it. |
|---|
| 14 | 5. Save the results into a file or do whatever you want with the Cairo surface. |
|---|
| 15 | |
|---|
| 16 | .. index:: create cairo surface |
|---|
| 17 | .. _creating-cairo-surface: |
|---|
| 18 | |
|---|
| 19 | Creating a Cairo Surface |
|---|
| 20 | ======================== |
|---|
| 21 | |
|---|
| 22 | Detailed information on the Cairo library can be found on their `website |
|---|
| 23 | <http://www.cairographics.org/>`_. |
|---|
| 24 | |
|---|
| 25 | To create the Cairo surface, you need to specify the type of surface and its |
|---|
| 26 | dimensions:: |
|---|
| 27 | |
|---|
| 28 | import cairo |
|---|
| 29 | width, height = (500, 400) |
|---|
| 30 | surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) |
|---|
| 31 | |
|---|
| 32 | .. index:: build dataset |
|---|
| 33 | .. _building-datasets: |
|---|
| 34 | |
|---|
| 35 | Building Datasets |
|---|
| 36 | ================= |
|---|
| 37 | |
|---|
| 38 | PyCha requires a dataset in the form:: |
|---|
| 39 | |
|---|
| 40 | dataSet = ( |
|---|
| 41 | ('dataSet 1', ((0, 1), (1, 3), (2, 2.5))), |
|---|
| 42 | ('dataSet 2', ((0, 2), (1, 4), (2, 3))), |
|---|
| 43 | ('dataSet 3', ((0, 5), (1, 1), (2, 0.5))), |
|---|
| 44 | ) |
|---|
| 45 | |
|---|
| 46 | Each data set is a tuple, of which the first element is the name of the data set |
|---|
| 47 | and the second is a tuple of two points (x, y). |
|---|
| 48 | |
|---|
| 49 | .. index:: customize options |
|---|
| 50 | .. _customizing-options: |
|---|
| 51 | |
|---|
| 52 | Customizing Options |
|---|
| 53 | =================== |
|---|
| 54 | |
|---|
| 55 | Please refer to :ref:`changing-defaults`. |
|---|
| 56 | |
|---|
| 57 | .. index:: render chart |
|---|
| 58 | .. _creating-rendering-chart: |
|---|
| 59 | |
|---|
| 60 | Create and Render Chart |
|---|
| 61 | ======================= |
|---|
| 62 | |
|---|
| 63 | Now we are ready to instantiate the chart, add the dataset, and render it:: |
|---|
| 64 | |
|---|
| 65 | import pycha.bar |
|---|
| 66 | chart = pycha.bar.VerticalBarChart(surface, options) |
|---|
| 67 | chart.addDataset(dataSet) |
|---|
| 68 | chart.render() |
|---|
| 69 | |
|---|
| 70 | Please see :ref:`PyCha chart types <pycha-chart-types>` for supported outputs. |
|---|
| 71 | |
|---|
| 72 | .. _saving-results: |
|---|
| 73 | |
|---|
| 74 | Saving Results |
|---|
| 75 | ============== |
|---|
| 76 | |
|---|
| 77 | Finally, you can write the surface to a file using the Cairo library:: |
|---|
| 78 | |
|---|
| 79 | surface.write_to_png('output.png') |
|---|
| 80 | |
|---|
| 81 | That's it! Please see :ref:`save-chart-to-file` and :ref:`embed-chart-in-gtk-app` |
|---|
| 82 | for examples. |
|---|
| 83 | |
|---|