| 1 | .. index:: examples |
|---|
| 2 | .. _examples: |
|---|
| 3 | |
|---|
| 4 | ******** |
|---|
| 5 | Examples |
|---|
| 6 | ******** |
|---|
| 7 | |
|---|
| 8 | The most common use cases of PyCha are saving the chart to a file and embedding |
|---|
| 9 | the chart in an application. |
|---|
| 10 | |
|---|
| 11 | .. index:: save chart as png |
|---|
| 12 | .. _save-chart-to-file: |
|---|
| 13 | |
|---|
| 14 | Ex1: Save Chart to File |
|---|
| 15 | ======================= |
|---|
| 16 | |
|---|
| 17 | The files for this examples are `precip.py <examples/precip.py>`_ and `barchart.py |
|---|
| 18 | <examples/barchart.py>`_. |
|---|
| 19 | |
|---|
| 20 | precip.py |
|---|
| 21 | |
|---|
| 22 | :: |
|---|
| 23 | |
|---|
| 24 | Tainan = ( |
|---|
| 25 | ('Jan', 32.7), |
|---|
| 26 | ('Feb', 9.5), |
|---|
| 27 | ('Mar', 25.5), |
|---|
| 28 | ('Apr', 13.7), |
|---|
| 29 | ('May', 41.5), |
|---|
| 30 | ('Jun', 782.2), |
|---|
| 31 | ('Jul', 526.9), |
|---|
| 32 | ('Aug', 84.5), |
|---|
| 33 | ('Sep', 356.5), |
|---|
| 34 | ('Oct', 33.0), |
|---|
| 35 | ('Nov', 42.9), |
|---|
| 36 | ('Dec', 1.1), |
|---|
| 37 | ) |
|---|
| 38 | |
|---|
| 39 | Paris = ( |
|---|
| 40 | ('Jan', 54), |
|---|
| 41 | ('Feb', 46), |
|---|
| 42 | ('Mar', 54), |
|---|
| 43 | ('Apr', 47), |
|---|
| 44 | ('May', 63), |
|---|
| 45 | ('Jun', 58), |
|---|
| 46 | ('Jul', 54), |
|---|
| 47 | ('Aug', 52), |
|---|
| 48 | ('Sep', 54), |
|---|
| 49 | ('Oct', 56), |
|---|
| 50 | ('Nov', 56), |
|---|
| 51 | ('Dec', 56), |
|---|
| 52 | ) |
|---|
| 53 | |
|---|
| 54 | barchart.py |
|---|
| 55 | |
|---|
| 56 | :: |
|---|
| 57 | |
|---|
| 58 | import sys |
|---|
| 59 | |
|---|
| 60 | import cairo |
|---|
| 61 | |
|---|
| 62 | import pycha.bar |
|---|
| 63 | |
|---|
| 64 | import precip |
|---|
| 65 | |
|---|
| 66 | def barChart(output, chartFactory): |
|---|
| 67 | surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 300) |
|---|
| 68 | |
|---|
| 69 | dataSet = ( |
|---|
| 70 | ('Tainan', [(i, l[1]) for i, l in enumerate(precip.Tainan)]), |
|---|
| 71 | ('Paris', [(i, l[1]) for i, l in enumerate(precip.Paris)]), |
|---|
| 72 | ) |
|---|
| 73 | |
|---|
| 74 | options = { |
|---|
| 75 | 'axis': { |
|---|
| 76 | 'x': { |
|---|
| 77 | 'ticks': [dict(v=i, label=l[0]) for i, l in enumerate(precip.Tainan)], |
|---|
| 78 | 'label': 'Month', |
|---|
| 79 | 'rotate': 25, |
|---|
| 80 | }, |
|---|
| 81 | 'y': { |
|---|
| 82 | 'tickCount': 4, |
|---|
| 83 | 'rotate': 25, |
|---|
| 84 | 'label': 'Precipitation (mm)' |
|---|
| 85 | } |
|---|
| 86 | }, |
|---|
| 87 | 'background': { |
|---|
| 88 | 'chartColor': '#d8e7ec', |
|---|
| 89 | 'baseColor': '#efebe7', |
|---|
| 90 | 'lineColor': '#444444' |
|---|
| 91 | }, |
|---|
| 92 | 'colorScheme': '#6eafc1', |
|---|
| 93 | 'legend': { |
|---|
| 94 | 'hide': False, |
|---|
| 95 | 'position': {'top': 5, 'left': 5}, |
|---|
| 96 | }, |
|---|
| 97 | 'padding': { |
|---|
| 98 | 'left': 135, |
|---|
| 99 | 'bottom': 55, |
|---|
| 100 | }, |
|---|
| 101 | 'title': 'Monthly Precipitation' |
|---|
| 102 | } |
|---|
| 103 | chart = chartFactory(surface, options) |
|---|
| 104 | |
|---|
| 105 | chart.addDataset(dataSet) |
|---|
| 106 | chart.render() |
|---|
| 107 | |
|---|
| 108 | surface.write_to_png(output) |
|---|
| 109 | |
|---|
| 110 | if __name__ == '__main__': |
|---|
| 111 | if len(sys.argv) > 1: |
|---|
| 112 | output = sys.argv[1] |
|---|
| 113 | else: |
|---|
| 114 | output = 'barchart.png' |
|---|
| 115 | barChart('v' + output, pycha.bar.VerticalBarChart) |
|---|
| 116 | barChart('h' + output, pycha.bar.HorizontalBarChart) |
|---|
| 117 | |
|---|
| 118 | Let's say we want to graph the monthly precipitations of Tainan, Taiwan, and |
|---|
| 119 | Paris, France. We can store the data in a Python file called ``precip.py`` so that |
|---|
| 120 | we can refer to it as ``precip.Tainan`` and ``precip.Paris``. |
|---|
| 121 | |
|---|
| 122 | In ``barchart.py``, we first import the necessary modules. |
|---|
| 123 | |
|---|
| 124 | Our program supports a commandline argument for the file name:: |
|---|
| 125 | |
|---|
| 126 | import sys |
|---|
| 127 | |
|---|
| 128 | PyCha depends on Cairo:: |
|---|
| 129 | |
|---|
| 130 | import cairo |
|---|
| 131 | |
|---|
| 132 | We want a bar chart:: |
|---|
| 133 | |
|---|
| 134 | import pycha.bar |
|---|
| 135 | |
|---|
| 136 | We need our data:: |
|---|
| 137 | |
|---|
| 138 | import precip |
|---|
| 139 | |
|---|
| 140 | We next define a class called barChart that takes in the output filename and the |
|---|
| 141 | :ref:`chart type <pycha-chart-types>`:: |
|---|
| 142 | |
|---|
| 143 | def barChart(output, chartFactory): |
|---|
| 144 | |
|---|
| 145 | Our data needs to be formatted for PyCha:: |
|---|
| 146 | |
|---|
| 147 | dataSet = ( |
|---|
| 148 | ('Tainan', [(i, l[1]) for i, l in enumerate(precip.Tainan)]), |
|---|
| 149 | ('Paris', [(i, l[1]) for i, l in enumerate(precip.Paris)]), |
|---|
| 150 | ) |
|---|
| 151 | |
|---|
| 152 | We next set our options:: |
|---|
| 153 | |
|---|
| 154 | options = { |
|---|
| 155 | 'axis': { |
|---|
| 156 | 'x': { |
|---|
| 157 | 'ticks': [dict(v=i, label=l[0]) for i, l in enumerate(precip.Tainan)], |
|---|
| 158 | 'label': 'Month', |
|---|
| 159 | 'rotate': 25, |
|---|
| 160 | }, |
|---|
| 161 | 'y': { |
|---|
| 162 | 'tickCount': 4, |
|---|
| 163 | 'rotate': 25, |
|---|
| 164 | 'label': 'Precipitation (mm)' |
|---|
| 165 | } |
|---|
| 166 | }, |
|---|
| 167 | 'background': { |
|---|
| 168 | 'chartColor': '#d8e7ec', |
|---|
| 169 | 'baseColor': '#efebe7', |
|---|
| 170 | 'lineColor': '#444444' |
|---|
| 171 | }, |
|---|
| 172 | 'colorScheme': '#6eafc1', |
|---|
| 173 | 'legend': { |
|---|
| 174 | 'hide': False, |
|---|
| 175 | 'position': {'top': 5, 'left': 5}, |
|---|
| 176 | }, |
|---|
| 177 | 'padding': { |
|---|
| 178 | 'left': 135, |
|---|
| 179 | 'bottom': 55, |
|---|
| 180 | }, |
|---|
| 181 | 'title': 'Monthly Precipitation' |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | Notice that the :ref:`axis:x:ticks option <xticks>` sets the labels for the |
|---|
| 185 | x-axis ticks. |
|---|
| 186 | |
|---|
| 187 | The legend is hidden by default, so we unhide it. The ``legend:position`` option is |
|---|
| 188 | used to place the legend in the desired location. |
|---|
| 189 | |
|---|
| 190 | We now create the chart and render it:: |
|---|
| 191 | |
|---|
| 192 | chart = chartFactory(surface, options) |
|---|
| 193 | |
|---|
| 194 | chart.addDataset(dataSet) |
|---|
| 195 | chart.render() |
|---|
| 196 | |
|---|
| 197 | The final step is to save the chart as a png image file:: |
|---|
| 198 | |
|---|
| 199 | surface.write_to_png(output) |
|---|
| 200 | |
|---|
| 201 | We initiate the program with:: |
|---|
| 202 | |
|---|
| 203 | if __name__ == '__main__': |
|---|
| 204 | if len(sys.argv) > 1: |
|---|
| 205 | output = sys.argv[1] |
|---|
| 206 | else: |
|---|
| 207 | output = 'barchart.png' |
|---|
| 208 | barChart('v' + output, pycha.bar.VerticalBarChart) |
|---|
| 209 | barChart('h' + output, pycha.bar.HorizontalBarChart) |
|---|
| 210 | |
|---|
| 211 | Running the program from the command line with: |
|---|
| 212 | |
|---|
| 213 | ``python barchart.py`` |
|---|
| 214 | |
|---|
| 215 | produces the beautiful charts shown below. |
|---|
| 216 | |
|---|
| 217 | Vertical bar chart: |
|---|
| 218 | |
|---|
| 219 | .. image:: examples/vbarchart-ex1.png |
|---|
| 220 | |
|---|
| 221 | Horizontal bar chart: |
|---|
| 222 | |
|---|
| 223 | .. image:: examples/hbarchart-ex1.png |
|---|
| 224 | |
|---|
| 225 | Reason for all the rain in the summer in Tainan? Typhoons. |
|---|
| 226 | |
|---|
| 227 | .. index:: embed chart in GTK app |
|---|
| 228 | .. _embed-chart-in-gtk-app: |
|---|
| 229 | |
|---|
| 230 | Ex2: Embed Chart in GTK App |
|---|
| 231 | =========================== |
|---|
| 232 | |
|---|
| 233 | Coming soon. Please refer to ``Chavier`` in the source tree for an example. |
|---|
| 234 | |
|---|
| 235 | |
|---|