/+junk/pygooglechart-py3k

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/pygooglechart-py3k

« back to all changes in this revision

Viewing changes to pygooglechart.py

  • Committer: gak
  • Date: 2007-12-13 23:04:28 UTC
  • Revision ID: git-v1:ad594da292ccb166e3762f89a80662a9f0c41ca3
Refactored axis to allow multiple axis labels

Show diffs side-by-side

added added

removed removed

Lines of Context:
160
160
    RIGHT = 'r'
161
161
    TYPES = (BOTTOM, TOP, LEFT, RIGHT)
162
162
 
163
 
    def __init__(self, axis, **kw):
164
 
        assert(axis in Axis.TYPES)
 
163
    def __init__(self, axis_index, axis_type, **kw):
 
164
        assert(axis_type in Axis.TYPES)
165
165
        self.has_style = False
166
 
        self.index = None
 
166
        self.axis_index = axis_index
 
167
        self.axis_type = axis_type
167
168
        self.positions = None
168
169
 
169
 
    def set_index(self, index):
170
 
        self.index = index
 
170
    def set_index(self, axis_index):
 
171
        self.axis_index = axis_index
171
172
 
172
173
    def set_positions(self, positions):
173
174
        self.positions = positions
181
182
 
182
183
    def style_to_url(self):
183
184
        bits = []
184
 
        bits.append(str(self.index))
 
185
        bits.append(str(self.axis_index))
185
186
        bits.append(self.colour)
186
187
        if self.font_size is not None:
187
188
            bits.append(str(self.font_size))
191
192
 
192
193
    def positions_to_url(self):
193
194
        bits = []
194
 
        bits.append(str(self.index))
 
195
        bits.append(str(self.axis_index))
195
196
        bits += [str(a) for a in self.positions]
196
197
        return ','.join(bits)
197
198
 
198
199
 
199
200
class LabelAxis(Axis):
200
201
 
201
 
    def __init__(self, axis, values, **kwargs):
202
 
        Axis.__init__(self, axis, **kwargs)
 
202
    def __init__(self, axis_index, axis_type, values, **kwargs):
 
203
        Axis.__init__(self, axis_index, axis_type, **kwargs)
203
204
        self.values = [str(a) for a in values]
204
205
 
205
206
    def __repr__(self):
206
 
        return '%i:|%s' % (self.index, '|'.join(self.values))
 
207
        return '%i:|%s' % (self.axis_index, '|'.join(self.values))
207
208
 
208
209
 
209
210
class RangeAxis(Axis):
210
211
 
211
 
    def __init__(self, axis, low, high, **kwargs):
212
 
        Axis.__init__(self, axis, **kwargs)
 
212
    def __init__(self, axis_index, axis_type, low, high, **kwargs):
 
213
        Axis.__init__(self, axis_index, axis_type, **kwargs)
213
214
        self.low = low
214
215
        self.high = high
215
216
 
216
217
    def __repr__(self):
217
 
        return '%i,%s,%s' % (self.index, self.low, self.high)
 
218
        return '%i,%s,%s' % (self.axis_index, self.low, self.high)
218
219
 
219
220
# Chart Classes
220
221
# -----------------------------------------------------------------------------
252
253
            Chart.BACKGROUND: None,
253
254
            Chart.CHART: None,
254
255
        }
255
 
        self.axis = {
256
 
            Axis.TOP: None,
257
 
            Axis.BOTTOM: None,
258
 
            Axis.LEFT: None,
259
 
            Axis.RIGHT: None,
260
 
        }
 
256
#        self.axis = {
 
257
#            Axis.TOP: None,
 
258
#            Axis.BOTTOM: None,
 
259
#            Axis.LEFT: None,
 
260
#            Axis.RIGHT: None,
 
261
#        }
 
262
        self.axis = []
261
263
        self.markers = []
262
264
 
263
265
    # URL generation
409
411
    # Axis Labels
410
412
    # -------------------------------------------------------------------------
411
413
 
412
 
    def set_axis_labels(self, axis, values):
413
 
        assert(axis in Axis.TYPES)
414
 
        self.axis[axis] = LabelAxis(axis, values)
415
 
 
416
 
    def set_axis_range(self, axis, low, high):
417
 
        assert(axis in Axis.TYPES)
418
 
        self.axis[axis] = RangeAxis(axis, low, high)
419
 
 
420
 
    def set_axis_positions(self, axis, positions):
421
 
        assert(axis in Axis.TYPES)
422
 
        if not self.axis[axis]:
423
 
            raise InvalidParametersException('Please create an axis first')
424
 
        self.axis[axis].set_positions(positions)
425
 
 
426
 
    def set_axis_style(self, axis, colour, font_size=None, alignment=None):
427
 
        assert(axis in Axis.TYPES)
428
 
        if not self.axis[axis]:
429
 
            raise InvalidParametersException('Please create an axis first')
430
 
        self.axis[axis].set_style(colour, font_size, alignment)
 
414
    def set_axis_labels(self, axis_type, values):
 
415
        assert(axis_type in Axis.TYPES)
 
416
        axis_index = len(self.axis)
 
417
        axis = LabelAxis(axis_index, axis_type, values)
 
418
        self.axis.append(axis)
 
419
        return axis_index
 
420
 
 
421
    def set_axis_range(self, axis_type, low, high):
 
422
        assert(axis_type in Axis.TYPES)
 
423
        axis_index = len(self.axis)
 
424
        axis = RangeAxis(axis_index, axis_type, low, high)
 
425
        self.axis.append(axis)
 
426
        return axis_index
 
427
 
 
428
    def set_axis_positions(self, axis_index, positions):
 
429
        try:
 
430
            self.axis[axis_index].set_positions(positions)
 
431
        except IndexError:
 
432
            raise InvalidParametersException('Axis index %i has not been ' \
 
433
                'created' % axis)
 
434
 
 
435
    def set_axis_style(self, axis_index, colour, font_size=None, alignment=None):
 
436
        try:
 
437
            self.axis[axis_index].set_style(colour, font_size, alignment)
 
438
        except IndexError:
 
439
            raise InvalidParametersException('Axis index %i has not been ' \
 
440
                'created' % axis)
431
441
 
432
442
    def axis_to_url(self):
433
443
        available_axis = []
436
446
        positions = []
437
447
        styles = []
438
448
        index = -1
439
 
        for position, axis in self.axis.items():
440
 
            if not axis:
441
 
                continue
442
 
            index += 1
443
 
            axis.set_index(index)
444
 
            available_axis.append(position)
 
449
        for axis in self.axis:
 
450
            available_axis.append(axis.axis_type)
445
451
            if isinstance(axis, RangeAxis):
446
452
                range_axis.append(repr(axis))
447
453
            if isinstance(axis, LabelAxis):
670
676
#        'aabbcc00', 0.5)
671
677
#    chart.fill_linear_stripes(Chart.CHART, 20, '204070', .2, '300040', .2,
672
678
#        'aabbcc00', 0.2)
673
 
    chart.set_axis_range(Axis.LEFT, 0, 10)
674
 
    chart.set_axis_range(Axis.RIGHT, 5, 30)
675
 
    chart.set_axis_labels(Axis.BOTTOM, [1, 25, 95])
676
 
    chart.set_axis_positions(Axis.BOTTOM, [1, 25, 95])
677
 
    chart.set_axis_style(Axis.BOTTOM, 'FFFFFF', 15)
 
679
    axis_left_index = chart.set_axis_range(Axis.LEFT, 0, 10)
 
680
    axis_left_index = chart.set_axis_range(Axis.LEFT, 0, 10)
 
681
    axis_left_index = chart.set_axis_range(Axis.LEFT, 0, 10)
 
682
    axis_right_index = chart.set_axis_range(Axis.RIGHT, 5, 30)
 
683
    axis_bottom_index = chart.set_axis_labels(Axis.BOTTOM, [1, 25, 95])
 
684
    chart.set_axis_positions(axis_bottom_index, [1, 25, 95])
 
685
    chart.set_axis_style(axis_bottom_index, '003050', 15)
678
686
 
679
687
#    chart.set_pie_labels(('apples', 'oranges', 'bananas'))
680
688