ofc2.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 cjson
  14. class Title(dict):
  15. def __init__(self, title, style=None):
  16. self['text'] = title
  17. self.set_style(style)
  18. def set_style(self, style):
  19. if style:
  20. self['style'] = style
  21. class y_legend(Title):
  22. pass
  23. class x_legend(Title):
  24. pass
  25. ##########################################
  26. # axis classes
  27. class axis(dict):
  28. def __init__(self, stroke=None, tick_height=None, colour=None, grid_colour=None, steps=None):
  29. self.set_stroke(stroke)
  30. self.set_tick_height(tick_height)
  31. self.set_colour(colour)
  32. self.set_grid_colour(grid_colour)
  33. self.set_steps(steps)
  34. def set_stroke(self, stroke):
  35. if stroke:
  36. self['stroke'] = stroke
  37. def set_tick_height(self, tick_height):
  38. if tick_height:
  39. self['tick_height'] = tick_height
  40. def set_colour(self, colour):
  41. if colour:
  42. self['colour'] = colour
  43. def set_grid_colour(self, grid_colour):
  44. if grid_colour:
  45. self['grid_colour'] = grid_colour
  46. def set_steps(self, steps):
  47. if steps:
  48. self['steps'] = steps
  49. class x_axis(axis):
  50. def __init__(self, stroke=None, tick_height=None, colour=None, grid_colour=None, labels=None, steps=None):
  51. axis.__init__(self, stroke, tick_height, colour, grid_colour, steps)
  52. self.set_labels(labels)
  53. self['orientation'] = 2
  54. def set_labels(self, labels):
  55. if labels:
  56. self['labels'] = labels
  57. class y_axis(axis):
  58. def __init__(self, stroke=None, tick_height=None, colour=None, grid_colour=None, offset=None, max=None, min=None, steps=None):
  59. axis.__init__(self, stroke, tick_height, colour, grid_colour, steps)
  60. self.set_offset(offset)
  61. self.set_max(max)
  62. self.set_min(min)
  63. def set_offset(self, offset):
  64. if offset:
  65. self['offset'] = offset
  66. def set_max(self, max):
  67. if max:
  68. self['max'] = max
  69. def set_min(self, min):
  70. if min:
  71. self['min'] = min
  72. ##########################################
  73. # open_flash_chart class
  74. class tooltip(dict):
  75. def __init__(self, shadow=None, stroke=None, colour=None, bg_colour=None, title_style=None, body_style=None):
  76. self.set_shadow(shadow)
  77. self.set_stroke(stroke)
  78. self.set_colour(colour)
  79. self.set_background(bg_colour)
  80. self.set_title(title_style)
  81. self.set_body(body_style)
  82. def set_shadow(self, shadow):
  83. if shadow:
  84. self['shadow'] = shadow
  85. def set_stroke(self, stroke):
  86. if stroke:
  87. self['stroke'] = stroke
  88. def set_colour(self, colour):
  89. if colour:
  90. self['colour'] = colour
  91. def set_background(self, background):
  92. if background:
  93. self['background'] = background
  94. def set_title(self, title):
  95. if title:
  96. self['title'] = title
  97. def set_body(self, body):
  98. if body:
  99. self['body'] = body
  100. ##########################################
  101. # open_flash_chart class
  102. class open_flash_chart(dict):
  103. def __init__(self, title, style=None):
  104. self['title'] = Title(title, style)
  105. def set_x_legend(self, legend):
  106. self['x_legend'] = x_legend(legend)
  107. def set_y_legend(self, legend):
  108. self['y_legend'] = y_legend(legend)
  109. def set_x_axis(self, stroke=None, tick_height=None, colour=None, grid_colour=None, labels=None, steps=None):
  110. self['x_axis'] = x_axis(stroke, tick_height, colour, grid_colour, labels, steps)
  111. def set_y_axis(self, stroke=None, tick_height=None, colour=None, grid_colour=None, offset=None, max=None, min=None, steps=None):
  112. self['y_axis'] = y_axis(stroke, tick_height, colour, grid_colour, offset, max, min, steps)
  113. def set_y_axis_right(self, stroke=None, tick_height=None, colour=None, grid_colour=None, offset=None, max=None, min=None, steps=None):
  114. self['y_axis_right'] = y_axis(stroke, tick_height, colour, grid_colour, offset, max, min, steps)
  115. def set_bg_colour(self, colour):
  116. self['bg_colour'] = colour
  117. def set_tooltip(self, shadow=None, stroke=None, colour=None, bg_colour=None, title_style=None, body_style=None):
  118. self['tooltip'] = tooltip(shadow, stroke, colour, bg_colour, title_style, body_style)
  119. def add_element(self, element):
  120. try:
  121. self['elements'].append(element)
  122. except:
  123. self['elements'] = [element]
  124. def encode(self):
  125. return cjson.encode(self)
  126. #ofc = open_flash_chart('Example JSON')
  127. #ofc.set_y_legend('Example Y Legend')
  128. #ofc.set_x_legend('Example X Legend')
  129. #ofc.set_x_axis(1, 1, '#ff0000', '#00ff00', ['sun', 'mon', 'tue'])
  130. #ofc.set_x_axis(labels=['sun', 'mon', 'tue'])
  131. #print cjson.encode(ofc)