demo.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. import cherrypy
  13. import socket
  14. import math
  15. import openFlashChart
  16. from openFlashChart_varieties import (Line,
  17. Line_Dot,
  18. Line_Hollow,
  19. Bar,
  20. Bar_Filled,
  21. Bar_Glass,
  22. Bar_3d,
  23. Bar_Sketch,
  24. HBar,
  25. Bar_Stack,
  26. Area_Line,
  27. Area_Hollow,
  28. Pie,
  29. Scatter,
  30. Scatter_Line)
  31. from openFlashChart_varieties import (dot_value,
  32. hbar_value,
  33. bar_value,
  34. bar_3d_value,
  35. bar_glass_value,
  36. bar_sketch_value,
  37. bar_stack_value,
  38. pie_value,
  39. scatter_value,
  40. x_axis_labels,
  41. x_axis_label)
  42. class OFC:
  43. @cherrypy.expose
  44. def index(self):
  45. graphs = []
  46. # Line Charts
  47. graphs.append(openFlashChart.flashHTML('100%', '400', '/line', '/flashes/'))
  48. graphs.append(openFlashChart.flashHTML('100%', '400', '/line_dot', '/flashes/'))
  49. graphs.append(openFlashChart.flashHTML('100%', '400', '/line_hollow', '/flashes/'))
  50. # Bar Charts
  51. graphs.append(openFlashChart.flashHTML('100%', '400', '/bar', '/flashes/'))
  52. graphs.append(openFlashChart.flashHTML('100%', '400', '/bar_filled', '/flashes/'))
  53. graphs.append(openFlashChart.flashHTML('100%', '400', '/bar_glass', '/flashes/'))
  54. graphs.append(openFlashChart.flashHTML('100%', '400', '/bar_3d', '/flashes/'))
  55. graphs.append(openFlashChart.flashHTML('100%', '400', '/bar_sketch', '/flashes/'))
  56. graphs.append(openFlashChart.flashHTML('100%', '400', '/hbar', '/flashes/'))
  57. graphs.append(openFlashChart.flashHTML('100%', '400', '/bar_stack', '/flashes/'))
  58. # Area Charts
  59. graphs.append(openFlashChart.flashHTML('100%', '400', '/area_line', '/flashes/'))
  60. graphs.append(openFlashChart.flashHTML('100%', '400', '/area_hollow', '/flashes/'))
  61. # Pie Chart
  62. graphs.append(openFlashChart.flashHTML('100%', '400', '/pie', '/flashes/'))
  63. # Scatter Charts
  64. graphs.append(openFlashChart.flashHTML('100%', '400', '/scatter', '/flashes/'))
  65. graphs.append(openFlashChart.flashHTML('100%', '400', '/scatter_line', '/flashes/'))
  66. # Radar Charts
  67. graphs.append(openFlashChart.flashHTML('100%', '400', '/radar', '/flashes/'))
  68. # Testing Chart
  69. graphs.append(openFlashChart.flashHTML('100%', '400', '/test', '/flashes/'))
  70. graphs.append(self.source("html_snippet.html"))
  71. return self.source("OFC.htm") %({"chart": "<br/><br/><br/><br/>".join(graphs)})
  72. # Line Charts
  73. @cherrypy.expose
  74. def line(self):
  75. plot1 = Line(text = "line1", fontsize = 20, values = range(0,10))
  76. plot2 = Line(text = "line2", fontsize = 06, values = range(10,0, -1))
  77. plot3 = Line(text = "line3", fontsize = 12, values = range(-5,5))
  78. plot1.set_line_style(4, 3)
  79. plot2.set_line_style(5, 5)
  80. plot3.set_line_style(4, 8)
  81. plot1.set_colour('#D4C345')
  82. plot2.set_colour('#C95653')
  83. plot3.set_colour('#8084FF')
  84. chart = openFlashChart.template("Line chart")
  85. chart.set_y_axis(min = -6, max = 10)
  86. chart.add_element(plot1)
  87. chart.add_element(plot2)
  88. chart.add_element(plot3)
  89. return chart.encode()
  90. @cherrypy.expose
  91. def line_dot(self):
  92. plot1 = Line_Dot(values = [math.sin(float(x)/10) * 1.9 + 4 for x in range(0, 62, 2)])
  93. plot2 = Line_Dot(values = [math.sin(float(x)/10) * 1.9 + 7 for x in range(0, 62, 2)])
  94. plot3 = Line_Dot(values = [math.sin(float(x)/10) * 1.9 + 10 for x in range(0, 62, 2)])
  95. plot1.set_halo_size(0)
  96. plot2.set_halo_size(1)
  97. plot3.set_halo_size(3)
  98. plot1.set_width(1)
  99. plot2.set_width(2)
  100. plot3.set_width(6)
  101. plot1.set_dot_size(4)
  102. plot2.set_dot_size(4)
  103. plot3.set_dot_size(6)
  104. chart = openFlashChart.template("Line_Dot chart")
  105. chart.set_y_axis(min = 0, max = 15, steps = 5)
  106. chart.add_element(plot1)
  107. chart.add_element(plot2)
  108. chart.add_element(plot3)
  109. return chart.encode()
  110. @cherrypy.expose
  111. def line_hollow(self):
  112. plot1 = Line_Hollow(values = [math.sin(float(x)/10) * 1.9 + 4 for x in range(0, 62, 2)])
  113. plot2 = Line_Hollow(values = [math.sin(float(x)/10) * 1.9 + 7 for x in range(0, 62, 2)])
  114. plot3 = Line_Hollow(values = [math.sin(float(x)/10) * 1.9 + 10 for x in range(0, 62, 2)])
  115. plot1.set_halo_size(3)
  116. plot2.set_halo_size(1)
  117. plot3.set_halo_size(0)
  118. plot1.set_width(1)
  119. plot2.set_width(2)
  120. plot3.set_width(3)
  121. plot1.set_dot_size(4)
  122. plot2.set_dot_size(4)
  123. plot3.set_dot_size(6)
  124. chart = openFlashChart.template("Line_Hollow chart")
  125. chart.set_y_axis(min = 0, max = 15, steps = 5)
  126. chart.add_element(plot1)
  127. chart.add_element(plot2)
  128. chart.add_element(plot3)
  129. return chart.encode()
  130. # Bar Charts
  131. @cherrypy.expose
  132. def bar(self):
  133. plot = Bar(text = "bar1", values = range(9, 0, -1))
  134. chart = openFlashChart.template("Bar chart")
  135. chart.add_element(plot)
  136. return chart.encode()
  137. @cherrypy.expose
  138. def bar_filled(self):
  139. plot = Bar_Filled(values = range(9, 0, -1) + [bar_value((5, 3), '#AAAAAA', 'Special:<br>Top = #top#<br>Bottom = #bottom#')], colour = '#E2D66A', outline = '#577261')
  140. chart = openFlashChart.template("Bar_Filled chart",)
  141. chart.add_element(plot)
  142. chart.set_bg_colour('#FFFFFF')
  143. return chart.encode()
  144. @cherrypy.expose
  145. def bar_glass(self):
  146. plot = Bar_Glass(values = range(-5, 5, 2) + [bar_glass_value(5, '#333333', 'Special:<br>Top = #top#<br>Bottom = #bottom#')])
  147. chart = openFlashChart.template("Bar_Glass chart")
  148. chart.set_y_axis(min = -6, max = 6)
  149. chart.add_element(plot)
  150. return chart.encode()
  151. @cherrypy.expose
  152. def bar_3d(self):
  153. plot = Bar_3d(values = range(-8, 8, 2) + [bar_3d_value(5, '#333333', 'Special:<br>Top = #top#<br>Bottom = #bottom#')])
  154. plot.set_colour('#D54C78')
  155. chart = openFlashChart.template("Bar_3d chart")
  156. chart.set_y_axis(min = -8, max = 8)
  157. chart.set_x_axis(colour = '#909090', three_d = 5, labels = list('qp#m^fur'))
  158. chart.add_element(plot)
  159. return chart.encode()
  160. @cherrypy.expose
  161. def bar_sketch(self):
  162. plot1 = Bar_Sketch(values = range(10, 0, -1) + [bar_sketch_value(5, '#333333', 'Special:<br>Top = #top#')], colour = '#81AC00', outline = '#567300')
  163. plot2 = Bar_Sketch(values = [bar_sketch_value(5, '#333333', 'Special:<br>Top = #top#')] + range(10, 0, -1), colour = '#81ACFF', outline = '#5673FF')
  164. chart = openFlashChart.template("Bar_Sketch chart", style = '{color: #567300; font-size: 14px}')
  165. chart.add_element(plot1)
  166. chart.add_element(plot2)
  167. return chart.encode()
  168. @cherrypy.expose
  169. def hbar(self):
  170. months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  171. plot = HBar(colour = '#86BBEF')
  172. plot.set_tooltip('Months: #val#')
  173. plot.append_values(hbar_value((0, 4), colour = '#909090'))
  174. plot.append_values(hbar_value((4, 8), colour = '#909009'))
  175. plot.append_values(hbar_value((8, 11), tooltip = '#left# to #right#<br>%s to %s (#val# months)' %(months[8], months[11])))
  176. chart = openFlashChart.template("HBar chart")
  177. chart.set_x_axis(offset = False, labels = x_axis_labels(labels = months))
  178. chart.set_y_axis(offset = True, labels = ['one', 'two', 'three'])
  179. chart.add_element(plot)
  180. return chart.encode()
  181. @cherrypy.expose
  182. def bar_stack(self):
  183. plot = Bar_Stack(colours = ['#C4D318', '#50284A', '#7D7B6A'])
  184. plot.set_tooltip('X label [#x_label#], Value [#val#]<br>Total [#total#]')
  185. plot.append_keys('#C4D318', 'job1', 13)
  186. plot.append_keys('#50284A', 'job2', 13)
  187. plot.append_keys('#7D7B6A', 'job3', 13)
  188. plot.append_keys('#ff0000', 'job4', 13)
  189. plot.append_keys('#ff00ff', 'job5', 13)
  190. plot.append_stack([2.5, 5, 2.5])
  191. plot.append_stack([2.5, 5, 1.25, 1.25])
  192. plot.append_stack([5, bar_stack_value(5, '#ff0000')])
  193. plot.append_stack([2, 2, 2, 2, bar_stack_value(2, '#ff00ff')])
  194. chart = openFlashChart.template("Bar_Stack chart", style = '{font-size: 20px; color: #F24062; text-align: center;}')
  195. chart.set_tooltip(behaviour = 'hover')
  196. chart.set_x_axis(labels = x_axis_labels(labels = ['Winter', 'Spring', 'Summer', 'Autumn']))
  197. chart.set_y_axis(min = 0, max = 14, steps = 2)
  198. chart.add_element(plot)
  199. return chart.encode()
  200. # Area charts
  201. @cherrypy.expose
  202. def area_line(self):
  203. 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)])
  204. plot.set_halo_size(1)
  205. plot.set_width(2)
  206. plot.set_dot_size(4)
  207. chart = openFlashChart.template("Area_Line chart")
  208. chart.set_y_axis(min = -2, max = 2, steps = 2, offset = True)
  209. chart.set_x_axis(labels = x_axis_labels(labels = ['%d' %i for i in range(0, 62, 2)], steps = 4, rotate = 'vertical'), steps = 2)
  210. chart.add_element(plot)
  211. return chart.encode()
  212. @cherrypy.expose
  213. def area_hollow(self):
  214. 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)])
  215. plot.set_halo_size(1)
  216. plot.set_width(2)
  217. plot.set_dot_size(4)
  218. chart = openFlashChart.template("Area_Hollow chart")
  219. chart.set_y_axis(min = -2, max = 2, steps = 2, offset = False)
  220. chart.set_x_axis(labels = x_axis_labels(rotate = 'diagonal'), steps = 2)
  221. chart.add_element(plot)
  222. return chart.encode()
  223. # Pie chart
  224. @cherrypy.expose
  225. def pie(self):
  226. 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')
  227. plot.set_tooltip('#val# of #total#<br>#percent# of 100%')
  228. plot.set_gradient_fill(True)
  229. plot.set_on_click('plot1')
  230. plot.set_no_labels(False)
  231. chart = openFlashChart.template("Pie chart")
  232. chart.add_element(plot)
  233. return chart.encode()
  234. # Scatter charts
  235. @cherrypy.expose
  236. def scatter(self):
  237. radians = [math.radians(degree) for degree in xrange(0, 360, 5)]
  238. values = [scatter_value(('%.2f' %math.sin(radian), '%.2f' %math.cos(radian))) for radian in radians]
  239. plot1 = Scatter(colour = '#FFD600', values = [scatter_value((0, 0))])
  240. plot2 = Scatter(colour = '#D600FF', values = values)
  241. plot1.set_dot_size(10)
  242. plot2.set_dot_size(3)
  243. chart = openFlashChart.template("Scatter chart")
  244. chart.set_x_axis(min = -2, max = 3)
  245. chart.set_y_axis(min = -2, max = 2)
  246. chart.add_element(plot1)
  247. chart.add_element(plot2)
  248. return chart.encode()
  249. @cherrypy.expose
  250. def scatter_line(self):
  251. from random import randint
  252. x_values = [0]
  253. while x_values[-1] < 25:
  254. x_values.append(x_values[-1] + float(randint(5, 15))/10)
  255. values = [scatter_value((x, float(randint(-15, 15))/10)) for x in x_values]
  256. plot = Scatter_Line(colour = '#FFD600', values = values)
  257. plot.set_dot_size(3)
  258. chart = openFlashChart.template("Scatter_Line chart")
  259. chart.set_x_axis(min = 0, max = 25)
  260. chart.set_y_axis(min = -10, max = 10)
  261. chart.add_element(plot)
  262. return chart.encode()
  263. # Radar Charts
  264. @cherrypy.expose
  265. def radar(self):
  266. plot = Area_Hollow(colour = '#45909F', fill = '#45909F', fill_alpha = 0.4, values = [3, 4, 5, 4, 3, 3, 2.5])
  267. plot.set_width(1)
  268. plot.set_dot_size(4)
  269. plot.set_halo_size(1)
  270. plot.set_loop()
  271. chart = openFlashChart.template("Radar chart")
  272. chart.set_bg_colour('#DFFFEC')
  273. chart.set_radar_axis(max = 5, colour = '#EFD1EF', grid_colour = '#EFD1EF', labels = list('012345'), spoke_labels = list('1234567'))
  274. chart.set_tooltip(behaviour = 'proximity')
  275. chart.add_element(plot)
  276. return chart.encode()
  277. # Testing Graph
  278. @cherrypy.expose
  279. def test(self):
  280. plot1 = Line(text = "line1", fontsize = 20, values = [None, 5, 1, 2, 4, None, None, 2, 7, 5])
  281. plot2 = Line(text = "line2", fontsize = 12, values = range(-4, 7, 1))
  282. 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])
  283. plot1.set_tooltip('Title1:<br>Amount = #val#')
  284. plot2.set_tooltip('Title2:<br>Value = #val#')
  285. plot3.set_tooltip('Title3:<br>Height = #val#')
  286. plot1.set_on_click('plot1')
  287. plot2.set_on_click('plot2')
  288. plot1.set_line_style(4, 3)
  289. plot2.set_line_style(4, 8)
  290. plot1.set_colour('#D4C345')
  291. plot2.set_colour('#8084FF')
  292. plot3.set_colour('#FF84FF')
  293. chart = openFlashChart.template("Testing chart", style = '{font-size: 40px; font-family: Times New Roman; color: #A2ACBA; text-align: right;}')
  294. 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))
  295. chart.set_y_axis(stroke = 5, colour = '#1E33FF', tick_length = 15, grid_colour = '#090305', offset = True, steps = 4, min = -6)
  296. chart.set_y_axis_right(stroke = 5, colour = '#44FF22', tick_length = 20, grid_colour = '#55ff55', offset = True, steps = 1)
  297. chart.set_x_legend("x-axis legend", style = '{font-size: 20px; color: #778877}')
  298. chart.set_y_legend("y-axis legend", style = '{font-size: 22px; color: #778877}')
  299. 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;}')
  300. chart.add_element(plot1)
  301. chart.add_element(plot2)
  302. chart.add_element(plot3)
  303. return chart.encode()
  304. @cherrypy.expose
  305. def ajax(self, count):
  306. if int(count) % 3 is 0:
  307. plot = Line_Dot(text = "line1", fontsize = 20, values = [None, 5, dot_value(1, '#D02020', '#val#<br>Text'), 2, 4, None, None, 2, 7, 5])
  308. elif int(count) % 3 is 1:
  309. plot = Line(text = "line2", fontsize = 12, values = range(-4, 7, 1))
  310. else:
  311. 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])
  312. plot.set_tooltip('Title1:<br>Amount = #val#')
  313. plot.set_on_click('plot1')
  314. plot.set_line_style(4, 3)
  315. plot.set_colour('#D4C345')
  316. chart = openFlashChart.template("Testing chart: %s" %count, style = '{font-size: 40px; font-family: Times New Roman; color: #A2ACBA; text-align: right;}')
  317. 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))
  318. chart.set_y_axis(stroke = 5, colour = '#1E33FF', tick_length = 15, grid_colour = '#090305', offset = True, steps = 4, min = -6)
  319. chart.set_y_axis_right(stroke = 5, colour = '#44FF22', tick_length = 20, grid_colour = '#55ff55', offset = True, steps = 1)
  320. chart.set_x_legend("x-axis legend", style = '{font-size: 20px; color: #778877}')
  321. chart.set_y_legend("y-axis legend", style = '{font-size: 22px; color: #778877}')
  322. 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;}')
  323. chart.add_element(plot)
  324. return chart.encode()
  325. def source(self, filename):
  326. """Opens a file specified by the file/pathname in read-only"""
  327. file = open(filename, 'r')
  328. result = file.read()
  329. file.close()
  330. return result
  331. @cherrypy.expose
  332. def flashes(self, filename):
  333. cherrypy.response.headers['Content-Type'] = "application/x-shockwave-flash"
  334. cherrypy.response.headers['Expires'] = "Tue, 01 Dec 2009 12:00:00 GMT"
  335. cherrypy.response.headers['Cache-Control'] = "Public"
  336. return open(filename)
  337. cherrypy.server.socket_host = socket.gethostbyname(socket.gethostname())
  338. cherrypy.quickstart(OFC(), config = 'serverconfig.conf')