ofc-demo.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. import cherrypy
  14. from string import Template
  15. import datetime
  16. import ofc2
  17. from ofc2_element import Line, Bar, BarStack
  18. xhtml_template = """
  19. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  20. "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  21. <html xmlns="http://www.w3.org/1999/xhtml">
  22. <script type="text/javascript" src="/static/swfobject.js"></script>
  23. <script type="text/javascript">
  24. $js
  25. </script>
  26. <head>
  27. <title>$title</title>
  28. </head>
  29. <body>
  30. $body
  31. </body>
  32. </html>
  33. """
  34. jstpl = Template('swfobject.embedSWF("/static/open-flash-chart.swf","$title", "$width", "$height", "$flash_ver", "expressInstall.swf", {"data-file": "$data_src"});\n')
  35. dvtpl = Template('<h1>$title</h1><div id="$title"></div><br/>\n')
  36. class Chart(object):
  37. type = ''
  38. title = 'chart'
  39. width=400
  40. height=200
  41. flash_ver='9.0.0'
  42. data_src = None
  43. chart = None
  44. def index(self):
  45. return self.chart.encode()
  46. index.exposed = True
  47. def __init__(self, type, name):
  48. self.title = name
  49. self.data_src='/'+name
  50. def get_js(self):
  51. return jstpl.substitute(title=self.title, width=self.width, height=self.height, flash_ver = self.flash_ver, data_src = self.data_src)
  52. def get_div(self):
  53. return dvtpl.substitute(title=self.title)
  54. class BarChart(Chart):
  55. def __init__(self, type, name):
  56. Chart.__init__(self, type, name)
  57. # create the bar element and set its values
  58. element = Bar(values=[9,8,7,6,5,4,3,2,1])
  59. # create the chart and set its title
  60. self.chart = ofc2.open_flash_chart(title=str(datetime.datetime.now()))
  61. self.chart.add_element(element)
  62. class BarStackChart(Chart):
  63. def __init__(self, type, name):
  64. Chart.__init__(self, type, name)
  65. # create the bar element and set its values
  66. element = BarStack(values=[ [ 2.5, 5 ], [ 7.5 ], [ 5, { 'val': 5, 'colour': '#ff0000' } ], [ 2, 2, 2, 2, { "val": 2, 'colour': '#ff00ff' } ] ])
  67. # create the chart and set its title
  68. self.chart = ofc2.open_flash_chart(title=str(datetime.datetime.now()))
  69. self.chart.set_y_axis(min=0, max=14, steps=7)
  70. self.chart.set_x_axis(labels=['a', 'b', 'c', 'd'])
  71. self.chart.add_element(element)
  72. class LineChart(Chart):
  73. def __init__(self, type, name):
  74. Chart.__init__(self, type, name)
  75. # create the bar element and set its values
  76. element = Line(values=[9,8,7,6,5,4,3,2,1])
  77. # create the chart and set its title
  78. self.chart = ofc2.open_flash_chart(title=str(datetime.datetime.now()))
  79. self.chart.add_element(element)
  80. class OFC2Demo(object):
  81. tpl = Template(xhtml_template)
  82. swfobjs = ''
  83. divs = ''
  84. linechart = LineChart('line_chart', 'linechart') # var name must be the same as the second param
  85. barchart = BarChart('bar_chart', 'barchart') # var name must be the same as the second param
  86. barstackchart = BarStackChart('bar_stack', 'barstackchart') # var name must be the same as the second param
  87. def index(self):
  88. self.load_charts()
  89. response = self.tpl.substitute(title='Open Flash Charts 2 - Python Library Demo', js=self.swfobjs, body=self.divs)
  90. return response
  91. def load_charts(self):
  92. self.swfobjs = ''
  93. self.divs = ''
  94. self.add_chart(self.linechart)
  95. self.add_chart(self.barchart)
  96. self.add_chart(self.barstackchart)
  97. def add_chart(self, chart):
  98. self.swfobjs += chart.get_js()
  99. self.divs += chart.get_div()
  100. # Expose methods
  101. index.exposed = True
  102. cherrypy.quickstart(OFC2Demo(), '/', 'cherrypy.conf')