root/examples/demo.py @ 55:28c00ff71169

Revision 55:28c00ff71169, 3.4 kB (checked in by Nicolas ?vrard <nicoe@…>, 5 years ago)

Added vbar and hbar charts

Line 
1import operator
2from cStringIO import StringIO
3import relatorio
4
5class Invoice(dict):
6
7    @property
8    def total(self):
9        return sum(l['amount'] for l in self['lines'])
10
11    @property
12    def vat(self):
13        return self.total * 0.21
14
15
16repos = relatorio.ReportRepository()
17repos.add_report(Invoice, 'application/vnd.oasis.opendocument.text',
18                 'basic.odt', report_name='basic')
19repos.add_report(Invoice, 'application/vnd.oasis.opendocument.text',
20                 'invoice.odt', report_name='complicated')
21repos.add_report(Invoice, 'application/vnd.oasis.opendocument.spreadsheet',
22                 'pivot.ods', report_name='pivot')
23repos.add_report(Invoice, 'application/vnd.oasis.opendocument.presentation',
24                 'presentation.odp', report_name='presentation')
25repos.add_report(Invoice, 'application/pdf', 'basic.tex',
26                 report_name='ConTeXt')
27repos.add_report(Invoice, 'image/png', 'pie_chart', report_name='pie')
28repos.add_report(Invoice, 'image/png', 'vbar_chart', report_name='vbar')
29repos.add_report(Invoice, 'image/png', 'hbar_chart', report_name='hbar')
30
31inv = Invoice(customer={'name': 'John Bonham',
32                        'address': {'street': 'Smirnov street',
33                                    'zip': 1000,
34                                    'city': 'Montreux'}},
35              lines=[{'item': {'name': 'Vodka 70cl',
36                               'reference': 'VDKA-001',
37                               'price': 10.34},
38                      'quantity': 7,
39                      'amount': 7*10.34},
40                     {'item': {'name': 'Cognac 70cl',
41                               'reference': 'CGNC-067',
42                               'price': 13.46},
43                      'quantity': 12,
44                      'amount': 12*13.46},
45                     {'item': {'name': 'Sparkling water 25cl',
46                               'reference': 'WATR-007',
47                               'price': 4},
48                      'quantity': 1,
49                      'amount': 4},
50                     {'item': {'name': 'Good customer',
51                               'reference': 'BONM-001',
52                               'price': -20},
53                      'quantity': 1,
54                      'amount': -20},
55                    ],
56              id='MZY-20080703',
57              status='late',
58              trombine=(file('bouteille.png', 'r'), 'image/png'))
59
60
61# ODT
62basic_report, _ = repos.reports[Invoice]['basic']
63file('bonham_basic.odt', 'w').write(basic_report(inv).render().getvalue())
64report, _ = repos.reports[Invoice]['complicated']
65file('bonham_complicated.odt', 'w').write(report(inv).render().getvalue())
66
67# ODS
68ods_report, _ = repos.reports[Invoice]['pivot']
69file('bonham_pivot.ods', 'w').write(ods_report(inv).render().getvalue())
70
71# ODP
72odp_report, _ = repos.reports[Invoice]['presentation']
73file('bonham_presentation.odp', 'w').write(odp_report(inv).render().getvalue())
74
75#PDF
76#pdf_report, _ = repos.reports[Invoice]['ConTeXt']
77#file('bonham_basic.pdf', 'w').write(pdf_report(inv).render().getvalue())
78
79#Image
80pie_report, _ = repos.reports[Invoice]['pie']
81file('pie.png', 'w').write(pie_report(inv).render().getvalue())
82hbar_report, _ = repos.reports[Invoice]['hbar']
83file('hbar.png', 'w').write(hbar_report(inv).render().getvalue())
84vbar_report, _ = repos.reports[Invoice]['vbar']
85file('vbar.png', 'w').write(vbar_report(inv).render().getvalue())
Note: See TracBrowser for help on using the browser.