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