source: code/Website/python-ofc-library-2/demo.py@ 7878

Last change on this file since 7878 was 7849, checked in by dennisw, 16 years ago
File size: 15.7 KB
Line 
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: Eugene Kin Chee Yip
11# Date: 7 Nov 2008
12
13import cherrypy
14import socket
15import math
16
17import openFlashChart
18from openFlashChart_varieties import (Line,
19 Line_Dot,
20 Line_Hollow,
21 Bar,
22 Bar_Filled,
23 Bar_Glass,
24 Bar_3d,
25 Bar_Sketch,
26 HBar,
27 Bar_Stack,
28 Area_Line,
29 Area_Hollow,
30 Pie,
31 Scatter,
32 Scatter_Line)
33
34from openFlashChart_varieties import (dot_value,
35 hbar_value,
36 bar_value,
37 bar_3d_value,
38 bar_glass_value,
39 bar_sketch_value,
40 bar_stack_value,
41 pie_value,
42 scatter_value,
43 x_axis_labels,
44 x_axis_label)
45
46class OFC:
47
48 @cherrypy.expose
49 def index(self):
50 graphs = []
51
52 # Line Charts
53 graphs.append(openFlashChart.flashHTML('100%', '400', '/line', '/flashes/'))
54 graphs.append(openFlashChart.flashHTML('100%', '400', '/line_dot', '/flashes/'))
55 graphs.append(openFlashChart.flashHTML('100%', '400', '/line_hollow', '/flashes/'))
56
57 # Bar Charts
58 graphs.append(openFlashChart.flashHTML('100%', '400', '/bar', '/flashes/'))
59 graphs.append(openFlashChart.flashHTML('100%', '400', '/bar_filled', '/flashes/'))
60 graphs.append(openFlashChart.flashHTML('100%', '400', '/bar_glass', '/flashes/'))
61 graphs.append(openFlashChart.flashHTML('100%', '400', '/bar_3d', '/flashes/'))
62 graphs.append(openFlashChart.flashHTML('100%', '400', '/bar_sketch', '/flashes/'))
63 graphs.append(openFlashChart.flashHTML('100%', '400', '/hbar', '/flashes/'))
64 graphs.append(openFlashChart.flashHTML('100%', '400', '/bar_stack', '/flashes/'))
65
66 # Area Charts
67 graphs.append(openFlashChart.flashHTML('100%', '400', '/area_line', '/flashes/'))
68 graphs.append(openFlashChart.flashHTML('100%', '400', '/area_hollow', '/flashes/'))
69
70 # Pie Chart
71 graphs.append(openFlashChart.flashHTML('100%', '400', '/pie', '/flashes/'))
72
73 # Scatter Charts
74 graphs.append(openFlashChart.flashHTML('100%', '400', '/scatter', '/flashes/'))
75 graphs.append(openFlashChart.flashHTML('100%', '400', '/scatter_line', '/flashes/'))
76
77 # Radar Charts
78 graphs.append(openFlashChart.flashHTML('100%', '400', '/radar', '/flashes/'))
79
80 # Testing Chart
81 graphs.append(openFlashChart.flashHTML('100%', '400', '/test', '/flashes/'))
82
83 graphs.append(self.source("html_snippet.html"))
84
85 return self.source("OFC.htm") %({"chart": "<br/><br/><br/><br/>".join(graphs)})
86
87
88 # Line Charts
89 @cherrypy.expose
90 def line(self):
91 plot1 = Line(text = "line1", fontsize = 20, values = range(0,10))
92 plot2 = Line(text = "line2", fontsize = 06, values = range(10,0, -1))
93 plot3 = Line(text = "line3", fontsize = 12, values = range(-5,5))
94
95 plot1.set_line_style(4, 3)
96 plot2.set_line_style(5, 5)
97 plot3.set_line_style(4, 8)
98
99 plot1.set_colour('#D4C345')
100 plot2.set_colour('#C95653')
101 plot3.set_colour('#8084FF')
102
103 chart = openFlashChart.template("Line chart")
104 chart.set_y_axis(min = -6, max = 10)
105
106 chart.add_element(plot1)
107 chart.add_element(plot2)
108 chart.add_element(plot3)
109
110 return chart.encode()
111
112 @cherrypy.expose
113 def line_dot(self):
114 plot1 = Line_Dot(values = [math.sin(float(x)/10) * 1.9 + 4 for x in range(0, 62, 2)])
115 plot2 = Line_Dot(values = [math.sin(float(x)/10) * 1.9 + 7 for x in range(0, 62, 2)])
116 plot3 = Line_Dot(values = [math.sin(float(x)/10) * 1.9 + 10 for x in range(0, 62, 2)])
117
118 plot1.set_halo_size(0)
119 plot2.set_halo_size(1)
120 plot3.set_halo_size(3)
121
122 plot1.set_width(1)
123 plot2.set_width(2)
124 plot3.set_width(6)
125
126 plot1.set_dot_size(4)
127 plot2.set_dot_size(4)
128 plot3.set_dot_size(6)
129
130 chart = openFlashChart.template("Line_Dot chart")
131 chart.set_y_axis(min = 0, max = 15, steps = 5)
132 chart.add_element(plot1)
133 chart.add_element(plot2)
134 chart.add_element(plot3)
135
136 return chart.encode()
137
138 @cherrypy.expose
139 def line_hollow(self):
140 plot1 = Line_Hollow(values = [math.sin(float(x)/10) * 1.9 + 4 for x in range(0, 62, 2)])
141 plot2 = Line_Hollow(values = [math.sin(float(x)/10) * 1.9 + 7 for x in range(0, 62, 2)])
142 plot3 = Line_Hollow(values = [math.sin(float(x)/10) * 1.9 + 10 for x in range(0, 62, 2)])
143
144 plot1.set_halo_size(3)
145 plot2.set_halo_size(1)
146 plot3.set_halo_size(0)
147
148 plot1.set_width(1)
149 plot2.set_width(2)
150 plot3.set_width(3)
151
152 plot1.set_dot_size(4)
153 plot2.set_dot_size(4)
154 plot3.set_dot_size(6)
155
156 chart = openFlashChart.template("Line_Hollow chart")
157 chart.set_y_axis(min = 0, max = 15, steps = 5)
158 chart.add_element(plot1)
159 chart.add_element(plot2)
160 chart.add_element(plot3)
161
162 return chart.encode()
163
164
165 # Bar Charts
166 @cherrypy.expose
167 def bar(self):
168 plot = Bar(text = "bar1", values = range(9, 0, -1))
169
170 chart = openFlashChart.template("Bar chart")
171 chart.add_element(plot)
172
173 return chart.encode()
174
175 @cherrypy.expose
176 def bar_filled(self):
177 plot = Bar_Filled(values = range(9, 0, -1) + [bar_value((5, 3), '#AAAAAA', 'Special:<br>Top = #top#<br>Bottom = #bottom#')], colour = '#E2D66A', outline = '#577261')
178
179 chart = openFlashChart.template("Bar_Filled chart",)
180 chart.add_element(plot)
181 chart.set_bg_colour('#FFFFFF')
182
183 return chart.encode()
184
185 @cherrypy.expose
186 def bar_glass(self):
187 plot = Bar_Glass(values = range(-5, 5, 2) + [bar_glass_value(5, '#333333', 'Special:<br>Top = #top#<br>Bottom = #bottom#')])
188
189 chart = openFlashChart.template("Bar_Glass chart")
190 chart.set_y_axis(min = -6, max = 6)
191 chart.add_element(plot)
192
193 return chart.encode()
194
195 @cherrypy.expose
196 def bar_3d(self):
197 plot = Bar_3d(values = range(-8, 8, 2) + [bar_3d_value(5, '#333333', 'Special:<br>Top = #top#<br>Bottom = #bottom#')])
198 plot.set_colour('#D54C78')
199
200 chart = openFlashChart.template("Bar_3d chart")
201 chart.set_y_axis(min = -8, max = 8)
202 chart.set_x_axis(colour = '#909090', three_d = 5, labels = list('qp#m^fur'))
203 chart.add_element(plot)
204
205 return chart.encode()
206
207 @cherrypy.expose
208 def bar_sketch(self):
209 plot1 = Bar_Sketch(values = range(10, 0, -1) + [bar_sketch_value(5, '#333333', 'Special:<br>Top = #top#')], colour = '#81AC00', outline = '#567300')
210 plot2 = Bar_Sketch(values = [bar_sketch_value(5, '#333333', 'Special:<br>Top = #top#')] + range(10, 0, -1), colour = '#81ACFF', outline = '#5673FF')
211
212 chart = openFlashChart.template("Bar_Sketch chart", style = '{color: #567300; font-size: 14px}')
213 chart.add_element(plot1)
214 chart.add_element(plot2)
215
216 return chart.encode()
217
218 @cherrypy.expose
219 def hbar(self):
220 months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
221
222 plot = HBar(colour = '#86BBEF')
223 plot.set_tooltip('Months: #val#')
224 plot.append_values(hbar_value((0, 4), colour = '#909090'))
225 plot.append_values(hbar_value((4, 8), colour = '#909009'))
226 plot.append_values(hbar_value((8, 11), tooltip = '#left# to #right#<br>%s to %s (#val# months)' %(months[8], months[11])))
227
228 chart = openFlashChart.template("HBar chart")
229 chart.set_x_axis(offset = False, labels = x_axis_labels(labels = months))
230 chart.set_y_axis(offset = True, labels = ['one', 'two', 'three'])
231 chart.add_element(plot)
232
233 return chart.encode()
234
235 @cherrypy.expose
236 def bar_stack(self):
237 plot = Bar_Stack(colours = ['#C4D318', '#50284A', '#7D7B6A'])
238 plot.set_tooltip('X label [#x_label#], Value [#val#]<br>Total [#total#]')
239 plot.append_keys('#C4D318', 'job1', 13)
240 plot.append_keys('#50284A', 'job2', 13)
241 plot.append_keys('#7D7B6A', 'job3', 13)
242 plot.append_keys('#ff0000', 'job4', 13)
243 plot.append_keys('#ff00ff', 'job5', 13)
244
245 plot.append_stack([2.5, 5, 2.5])
246 plot.append_stack([2.5, 5, 1.25, 1.25])
247 plot.append_stack([5, bar_stack_value(5, '#ff0000')])
248 plot.append_stack([2, 2, 2, 2, bar_stack_value(2, '#ff00ff')])
249
250 chart = openFlashChart.template("Bar_Stack chart", style = '{font-size: 20px; color: #F24062; text-align: center;}')
251 chart.set_tooltip(behaviour = 'hover')
252 chart.set_x_axis(labels = x_axis_labels(labels = ['Winter', 'Spring', 'Summer', 'Autumn']))
253 chart.set_y_axis(min = 0, max = 14, steps = 2)
254 chart.add_element(plot)
255
256 return chart.encode()
257
258
259 # Area charts
260 @cherrypy.expose
261 def area_line(self):
262 plot = Area_Line(colour = '#C4B86A', fill = '#C4B86A', fill_alpha = 0.7, values = [math.sin(float(x)/10) * 1.9 for x in range(0, 62, 2)])
263 plot.set_halo_size(1)
264 plot.set_width(2)
265 plot.set_dot_size(4)
266
267 chart = openFlashChart.template("Area_Line chart")
268 chart.set_y_axis(min = -2, max = 2, steps = 2, offset = True)
269 chart.set_x_axis(labels = x_axis_labels(labels = ['%d' %i for i in range(0, 62, 2)], steps = 4, rotate = 'vertical'), steps = 2)
270 chart.add_element(plot)
271
272 return chart.encode()
273
274 @cherrypy.expose
275 def area_hollow(self):
276 plot = Area_Hollow(colour = '#838A96', fill = '#E01B49', fill_alpha = 0.4, values = [math.sin(float(x)/10) * 1.9 for x in range(0, 62, 2)])
277 plot.set_halo_size(1)
278 plot.set_width(2)
279 plot.set_dot_size(4)
280
281 chart = openFlashChart.template("Area_Hollow chart")
282 chart.set_y_axis(min = -2, max = 2, steps = 2, offset = False)
283 chart.set_x_axis(labels = x_axis_labels(rotate = 'diagonal'), steps = 2)
284 chart.add_element(plot)
285
286 return chart.encode()
287
288
289 # Pie chart
290 @cherrypy.expose
291 def pie(self):
292 plot = Pie(start_angle = 35, animate = True, values = [2, 3, pie_value(6.5, ('hello (6.5)', '#FF33C9', 24))], colours = ['#D01F3C', '#356AA0', '#C79810'], label_colour = '#432BAF')
293 plot.set_tooltip('#val# of #total#<br>#percent# of 100%')
294 plot.set_gradient_fill(True)
295 plot.set_on_click('plot1')
296 plot.set_no_labels(False)
297
298 chart = openFlashChart.template("Pie chart")
299 chart.add_element(plot)
300
301 return chart.encode()
302
303
304 # Scatter charts
305 @cherrypy.expose
306 def scatter(self):
307 radians = [math.radians(degree) for degree in xrange(0, 360, 5)]
308 values = [scatter_value(('%.2f' %math.sin(radian), '%.2f' %math.cos(radian))) for radian in radians]
309
310 plot1 = Scatter(colour = '#FFD600', values = [scatter_value((0, 0))])
311 plot2 = Scatter(colour = '#D600FF', values = values)
312
313 plot1.set_dot_size(10)
314 plot2.set_dot_size(3)
315
316 chart = openFlashChart.template("Scatter chart")
317 chart.set_x_axis(min = -2, max = 3)
318 chart.set_y_axis(min = -2, max = 2)
319 chart.add_element(plot1)
320 chart.add_element(plot2)
321
322 return chart.encode()
323
324 @cherrypy.expose
325 def scatter_line(self):
326 from random import randint
327
328 x_values = [0]
329 while x_values[-1] < 25:
330 x_values.append(x_values[-1] + float(randint(5, 15))/10)
331
332 values = [scatter_value((x, float(randint(-15, 15))/10)) for x in x_values]
333
334 plot = Scatter_Line(colour = '#FFD600', values = values)
335 plot.set_dot_size(3)
336
337 chart = openFlashChart.template("Scatter_Line chart")
338 chart.set_x_axis(min = 0, max = 25)
339 chart.set_y_axis(min = -10, max = 10)
340 chart.add_element(plot)
341
342 return chart.encode()
343
344
345 # Radar Charts
346 @cherrypy.expose
347 def radar(self):
348 plot = Area_Hollow(colour = '#45909F', fill = '#45909F', fill_alpha = 0.4, values = [3, 4, 5, 4, 3, 3, 2.5])
349 plot.set_width(1)
350 plot.set_dot_size(4)
351 plot.set_halo_size(1)
352 plot.set_loop()
353
354 chart = openFlashChart.template("Radar chart")
355 chart.set_bg_colour('#DFFFEC')
356 chart.set_radar_axis(max = 5, colour = '#EFD1EF', grid_colour = '#EFD1EF', labels = list('012345'), spoke_labels = list('1234567'))
357 chart.set_tooltip(behaviour = 'proximity')
358 chart.add_element(plot)
359
360 return chart.encode()
361
362
363 # Testing Graph
364 @cherrypy.expose
365 def test(self):
366 plot1 = Line(text = "line1", fontsize = 20, values = [None, 5, 1, 2, 4, None, None, 2, 7, 5])
367 plot2 = Line(text = "line2", fontsize = 12, values = range(-4, 7, 1))
368 plot3 = Bar_Glass(text = "bar1", values = [4, None, -4, 3, bar_glass_value((5, -2), '#333333', 'Special:<br>Top = #top#<br>Bottom = #bottom#'), 7, None, None, -5, 5])
369
370 plot1.set_tooltip('Title1:<br>Amount = #val#')
371 plot2.set_tooltip('Title2:<br>Value = #val#')
372 plot3.set_tooltip('Title3:<br>Height = #val#')
373
374 plot1.set_on_click('plot1')
375 plot2.set_on_click('plot2')
376
377 plot1.set_line_style(4, 3)
378 plot2.set_line_style(4, 8)
379
380 plot1.set_colour('#D4C345')
381 plot2.set_colour('#8084FF')
382 plot3.set_colour('#FF84FF')
383
384 chart = openFlashChart.template("Testing chart", style = '{font-size: 40px; font-family: Times New Roman; color: #A2ACBA; text-align: right;}')
385
386 chart.set_x_axis(stroke = 10, colour = '#165132', tick_height = 30, grid_colour = '#AAEE00', offset = True, steps = 2, labels = x_axis_labels(labels = list('sfwertr56w') + [x_axis_label('custom!!', '#2683CF', 24, 'diagonal')], steps = 2))
387 chart.set_y_axis(stroke = 5, colour = '#1E33FF', tick_length = 15, grid_colour = '#090305', offset = True, steps = 4, min = -6)
388 chart.set_y_axis_right(stroke = 5, colour = '#44FF22', tick_length = 20, grid_colour = '#55ff55', offset = True, steps = 1)
389
390 chart.set_x_legend("x-axis legend", style = '{font-size: 20px; color: #778877}')
391 chart.set_y_legend("y-axis legend", style = '{font-size: 22px; color: #778877}')
392
393 chart.set_tooltip(shadow = True, stroke = 4, colour = '#909090', bg_colour = '#FAFAFA', title_style = '{font-size: 14px; color: #CC2A43;}', body_style = '{font-size: 10px; font-weight: bold; color: #000000;}')
394
395 chart.add_element(plot1)
396 chart.add_element(plot2)
397 chart.add_element(plot3)
398
399 return chart.encode()
400
401 @cherrypy.expose
402 def ajax(self, count):
403 if int(count) % 3 is 0:
404 plot = Line_Dot(text = "line1", fontsize = 20, values = [None, 5, dot_value(1, '#D02020', '#val#<br>Text'), 2, 4, None, None, 2, 7, 5])
405 elif int(count) % 3 is 1:
406 plot = Line(text = "line2", fontsize = 12, values = range(-4, 7, 1))
407 else:
408 plot = Bar_Glass(text = "bar1", values = [4, None, -4, 3, bar_glass_value((5, -2), '#333333', 'Special:<br>Top = #top#<br>Bottom = #bottom#'), 7, None, None, -5, 5])
409
410 plot.set_tooltip('Title1:<br>Amount = #val#')
411 plot.set_on_click('plot1')
412 plot.set_line_style(4, 3)
413
414 plot.set_colour('#D4C345')
415
416 chart = openFlashChart.template("Testing chart: %s" %count, style = '{font-size: 40px; font-family: Times New Roman; color: #A2ACBA; text-align: right;}')
417
418 chart.set_x_axis(stroke = 10, colour = '#165132', tick_height = 30, grid_colour = '#AAEE00', offset = True, steps = 2, labels = x_axis_labels(labels = list('sfwertr56w') + [x_axis_label('custom!!', '#2683CF', 24, 210)], steps = 2))
419 chart.set_y_axis(stroke = 5, colour = '#1E33FF', tick_length = 15, grid_colour = '#090305', offset = True, steps = 4, min = -6)
420 chart.set_y_axis_right(stroke = 5, colour = '#44FF22', tick_length = 20, grid_colour = '#55ff55', offset = True, steps = 1)
421
422 chart.set_x_legend("x-axis legend", style = '{font-size: 20px; color: #778877}')
423 chart.set_y_legend("y-axis legend", style = '{font-size: 22px; color: #778877}')
424
425 chart.set_tooltip(shadow = True, stroke = 4, colour = '#909090', bg_colour = '#FAFAFA', title_style = '{font-size: 14px; color: #CC2A43;}', body_style = '{font-size: 10px; font-weight: bold; color: #000000;}')
426
427 chart.add_element(plot)
428
429 return chart.encode()
430
431
432
433
434
435 def source(self, filename):
436 """Opens a file specified by the file/pathname in read-only"""
437 file = open(filename, 'r')
438 result = file.read()
439 file.close()
440 return result
441
442 @cherrypy.expose
443 def flashes(self, filename):
444 cherrypy.response.headers['Content-Type'] = "application/x-shockwave-flash"
445 cherrypy.response.headers['Expires'] = "Tue, 01 Dec 2009 12:00:00 GMT"
446 cherrypy.response.headers['Cache-Control'] = "Public"
447 return open(filename)
448
449
450cherrypy.server.socket_host = socket.gethostbyname(socket.gethostname())
451cherrypy.quickstart(OFC(), config = 'serverconfig.conf')
452
Note: See TracBrowser for help on using the repository browser.