root/tags/0.5.0/docs/examples.rst

Revision 145, 5.5 kB (checked in by lgs, 3 years ago)

Import docs created by Adam Przywecki

Line 
1.. index:: examples
2.. _examples:
3
4********
5Examples
6********
7
8The most common use cases of PyCha are saving the chart to a file and embedding
9the chart in an application.
10
11.. index:: save chart as png
12.. _save-chart-to-file:
13
14Ex1: Save Chart to File
15=======================
16
17The files for this examples are `precip.py <examples/precip.py>`_ and `barchart.py
18<examples/barchart.py>`_.
19
20precip.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
54barchart.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       
118Let's say we want to graph the monthly precipitations of Tainan, Taiwan, and
119Paris, France. We can store the data in a Python file called ``precip.py`` so that
120we can refer to it as ``precip.Tainan`` and ``precip.Paris``.
121
122In ``barchart.py``, we first import the necessary modules.
123
124Our program supports a commandline argument for the file name::
125
126    import sys
127
128PyCha depends on Cairo::
129
130    import cairo
131
132We want a bar chart::
133
134    import pycha.bar
135
136We need our data::
137
138    import precip
139
140We 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
145Our 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           
152We 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
184Notice that the :ref:`axis:x:ticks option <xticks>` sets the labels for the
185x-axis ticks.
186
187The legend is hidden by default, so we unhide it. The ``legend:position`` option is
188used to place the legend in the desired location.
189
190We now create the chart and render it::
191
192    chart = chartFactory(surface, options)
193
194    chart.addDataset(dataSet)
195    chart.render()
196   
197The final step is to save the chart as a png image file::
198
199    surface.write_to_png(output)
200
201We 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       
211Running the program from the command line with:
212
213``python barchart.py``
214
215produces the beautiful charts shown below.
216
217Vertical bar chart:
218
219.. image:: examples/vbarchart-ex1.png
220
221Horizontal bar chart:
222
223.. image:: examples/hbarchart-ex1.png
224
225Reason 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
230Ex2: Embed Chart in GTK App
231===========================
232
233Coming soon. Please refer to ``Chavier`` in the source tree for an example.
234
235
Note: See TracBrowser for help on using the browser.