1 | # This program is free software; you can redistribute it and/or modify
|
---|
2 | # it under the terms of the GNU General Public License as published by
|
---|
3 | # the Free Software Foundation; version 2 of the License.
|
---|
4 | #
|
---|
5 | # This program is distributed in the hope that it will be useful,
|
---|
6 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
7 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
8 | # GNU General Public License for more details.
|
---|
9 | #
|
---|
10 | # Author: Emanuel Fonseca
|
---|
11 | # Email: emdfonseca<at>gmail<dot>com
|
---|
12 | # Date: 25 August 2008
|
---|
13 |
|
---|
14 | import cherrypy
|
---|
15 | from string import Template
|
---|
16 |
|
---|
17 | import datetime
|
---|
18 |
|
---|
19 | import ofc2
|
---|
20 | from ofc2_element import Line, Bar, BarStack
|
---|
21 |
|
---|
22 | xhtml_template = """
|
---|
23 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
|
---|
24 | "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
---|
25 | <html xmlns="http://www.w3.org/1999/xhtml">
|
---|
26 | <script type="text/javascript" src="/static/swfobject.js"></script>
|
---|
27 | <script type="text/javascript">
|
---|
28 | $js
|
---|
29 | </script>
|
---|
30 | <head>
|
---|
31 |
|
---|
32 | <title>$title</title>
|
---|
33 |
|
---|
34 | </head>
|
---|
35 |
|
---|
36 | <body>
|
---|
37 | $body
|
---|
38 | </body>
|
---|
39 |
|
---|
40 | </html>
|
---|
41 | """
|
---|
42 |
|
---|
43 | jstpl = Template('swfobject.embedSWF("/static/open-flash-chart.swf","$title", "$width", "$height", "$flash_ver", "expressInstall.swf", {"data-file": "$data_src"});\n')
|
---|
44 | dvtpl = Template('<h1>$title</h1><div id="$title"></div><br/>\n')
|
---|
45 |
|
---|
46 | class Chart(object):
|
---|
47 | type = ''
|
---|
48 | title = 'chart'
|
---|
49 |
|
---|
50 | width=400
|
---|
51 | height=200
|
---|
52 | flash_ver='9.0.0'
|
---|
53 |
|
---|
54 | data_src = None
|
---|
55 |
|
---|
56 | chart = None
|
---|
57 |
|
---|
58 | def index(self):
|
---|
59 | return self.chart.encode()
|
---|
60 | index.exposed = True
|
---|
61 |
|
---|
62 | def __init__(self, type, name):
|
---|
63 | self.title = name
|
---|
64 | self.data_src='/'+name
|
---|
65 |
|
---|
66 | def get_js(self):
|
---|
67 | return jstpl.substitute(title=self.title, width=self.width, height=self.height, flash_ver = self.flash_ver, data_src = self.data_src)
|
---|
68 |
|
---|
69 | def get_div(self):
|
---|
70 | return dvtpl.substitute(title=self.title)
|
---|
71 |
|
---|
72 | class BarChart(Chart):
|
---|
73 | def __init__(self, type, name):
|
---|
74 | Chart.__init__(self, type, name)
|
---|
75 |
|
---|
76 | # create the bar element and set its values
|
---|
77 | element = Bar(values=[9,8,7,6,5,4,3,2,1])
|
---|
78 |
|
---|
79 | # create the chart and set its title
|
---|
80 | self.chart = ofc2.open_flash_chart(title=str(datetime.datetime.now()))
|
---|
81 | self.chart.add_element(element)
|
---|
82 |
|
---|
83 | class BarStackChart(Chart):
|
---|
84 | def __init__(self, type, name):
|
---|
85 | Chart.__init__(self, type, name)
|
---|
86 |
|
---|
87 | # create the bar element and set its values
|
---|
88 | element = BarStack(values=[ [ 2.5, 5 ], [ 7.5 ], [ 5, { 'val': 5, 'colour': '#ff0000' } ], [ 2, 2, 2, 2, { "val": 2, 'colour': '#ff00ff' } ] ])
|
---|
89 |
|
---|
90 | # create the chart and set its title
|
---|
91 | self.chart = ofc2.open_flash_chart(title=str(datetime.datetime.now()))
|
---|
92 | self.chart.set_y_axis(min=0, max=14, steps=7)
|
---|
93 | self.chart.set_x_axis(labels=['a', 'b', 'c', 'd'])
|
---|
94 | self.chart.add_element(element)
|
---|
95 |
|
---|
96 | class LineChart(Chart):
|
---|
97 | def __init__(self, type, name):
|
---|
98 | Chart.__init__(self, type, name)
|
---|
99 |
|
---|
100 | # create the bar element and set its values
|
---|
101 | element = Line(values=[9,8,7,6,5,4,3,2,1])
|
---|
102 |
|
---|
103 | # create the chart and set its title
|
---|
104 | self.chart = ofc2.open_flash_chart(title=str(datetime.datetime.now()))
|
---|
105 | self.chart.add_element(element)
|
---|
106 |
|
---|
107 | class OFC2Demo(object):
|
---|
108 | tpl = Template(xhtml_template)
|
---|
109 | swfobjs = ''
|
---|
110 | divs = ''
|
---|
111 |
|
---|
112 | linechart = LineChart('line_chart', 'linechart') # var name must be the same as the second param
|
---|
113 | barchart = BarChart('bar_chart', 'barchart') # var name must be the same as the second param
|
---|
114 | barstackchart = BarStackChart('bar_stack', 'barstackchart') # var name must be the same as the second param
|
---|
115 |
|
---|
116 | def index(self):
|
---|
117 | self.load_charts()
|
---|
118 | response = self.tpl.substitute(title='Open Flash Charts 2 - Python Library Demo', js=self.swfobjs, body=self.divs)
|
---|
119 | return response
|
---|
120 |
|
---|
121 | def load_charts(self):
|
---|
122 | self.swfobjs = ''
|
---|
123 | self.divs = ''
|
---|
124 | self.add_chart(self.linechart)
|
---|
125 | self.add_chart(self.barchart)
|
---|
126 | self.add_chart(self.barstackchart)
|
---|
127 |
|
---|
128 | def add_chart(self, chart):
|
---|
129 | self.swfobjs += chart.get_js()
|
---|
130 | self.divs += chart.get_div()
|
---|
131 |
|
---|
132 | # Expose methods
|
---|
133 | index.exposed = True
|
---|
134 |
|
---|
135 | cherrypy.quickstart(OFC2Demo(), '/', 'cherrypy.conf')
|
---|
136 |
|
---|