/+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 22:35:59 UTC
  • Revision ID: git-v1:a7ad1afe3f08b7de8b3a3cbdc7165d4f88ddbe92
version bump, added BadContentTypeException, added a few examples, added COPYING licence, code is more PEP8 friendly, download() checks for content type and raises on bad http codes, add_data returns the index of the dataset, Line doesn't allow being an instance.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
# Helper variables and functions
31
31
# -----------------------------------------------------------------------------
32
32
 
33
 
__version__ = '0.1.1'
34
 
 
35
33
reo_colour = re.compile('^([A-Fa-f0-9]{2,2}){3,4}$')
36
34
 
37
35
 
162
160
    RIGHT = 'r'
163
161
    TYPES = (BOTTOM, TOP, LEFT, RIGHT)
164
162
 
165
 
    def __init__(self, axis_index, axis_type, **kw):
166
 
        assert(axis_type in Axis.TYPES)
 
163
    def __init__(self, axis, **kw):
 
164
        assert(axis in Axis.TYPES)
167
165
        self.has_style = False
168
 
        self.axis_index = axis_index
169
 
        self.axis_type = axis_type
 
166
        self.index = None
170
167
        self.positions = None
171
168
 
172
 
    def set_index(self, axis_index):
173
 
        self.axis_index = axis_index
 
169
    def set_index(self, index):
 
170
        self.index = index
174
171
 
175
172
    def set_positions(self, positions):
176
173
        self.positions = positions
184
181
 
185
182
    def style_to_url(self):
186
183
        bits = []
187
 
        bits.append(str(self.axis_index))
 
184
        bits.append(str(self.index))
188
185
        bits.append(self.colour)
189
186
        if self.font_size is not None:
190
187
            bits.append(str(self.font_size))
194
191
 
195
192
    def positions_to_url(self):
196
193
        bits = []
197
 
        bits.append(str(self.axis_index))
 
194
        bits.append(str(self.index))
198
195
        bits += [str(a) for a in self.positions]
199
196
        return ','.join(bits)
200
197
 
201
198
 
202
199
class LabelAxis(Axis):
203
200
 
204
 
    def __init__(self, axis_index, axis_type, values, **kwargs):
205
 
        Axis.__init__(self, axis_index, axis_type, **kwargs)
 
201
    def __init__(self, axis, values, **kwargs):
 
202
        Axis.__init__(self, axis, **kwargs)
206
203
        self.values = [str(a) for a in values]
207
204
 
208
205
    def __repr__(self):
209
 
        return '%i:|%s' % (self.axis_index, '|'.join(self.values))
 
206
        return '%i:|%s' % (self.index, '|'.join(self.values))
210
207
 
211
208
 
212
209
class RangeAxis(Axis):
213
210
 
214
 
    def __init__(self, axis_index, axis_type, low, high, **kwargs):
215
 
        Axis.__init__(self, axis_index, axis_type, **kwargs)
 
211
    def __init__(self, axis, low, high, **kwargs):
 
212
        Axis.__init__(self, axis, **kwargs)
216
213
        self.low = low
217
214
        self.high = high
218
215
 
219
216
    def __repr__(self):
220
 
        return '%i,%s,%s' % (self.axis_index, self.low, self.high)
 
217
        return '%i,%s,%s' % (self.index, self.low, self.high)
221
218
 
222
219
# Chart Classes
223
220
# -----------------------------------------------------------------------------
255
252
            Chart.BACKGROUND: None,
256
253
            Chart.CHART: None,
257
254
        }
258
 
#        self.axis = {
259
 
#            Axis.TOP: None,
260
 
#            Axis.BOTTOM: None,
261
 
#            Axis.LEFT: None,
262
 
#            Axis.RIGHT: None,
263
 
#        }
264
 
        self.axis = []
 
255
        self.axis = {
 
256
            Axis.TOP: None,
 
257
            Axis.BOTTOM: None,
 
258
            Axis.LEFT: None,
 
259
            Axis.RIGHT: None,
 
260
        }
265
261
        self.markers = []
266
262
 
267
263
    # URL generation
413
409
    # Axis Labels
414
410
    # -------------------------------------------------------------------------
415
411
 
416
 
    def set_axis_labels(self, axis_type, values):
417
 
        assert(axis_type in Axis.TYPES)
418
 
        values = [ urllib.quote(a) for a in values ]
419
 
        axis_index = len(self.axis)
420
 
        axis = LabelAxis(axis_index, axis_type, values)
421
 
        self.axis.append(axis)
422
 
        return axis_index
423
 
 
424
 
    def set_axis_range(self, axis_type, low, high):
425
 
        assert(axis_type in Axis.TYPES)
426
 
        axis_index = len(self.axis)
427
 
        axis = RangeAxis(axis_index, axis_type, low, high)
428
 
        self.axis.append(axis)
429
 
        return axis_index
430
 
 
431
 
    def set_axis_positions(self, axis_index, positions):
432
 
        try:
433
 
            self.axis[axis_index].set_positions(positions)
434
 
        except IndexError:
435
 
            raise InvalidParametersException('Axis index %i has not been ' \
436
 
                'created' % axis)
437
 
 
438
 
    def set_axis_style(self, axis_index, colour, font_size=None, \
439
 
            alignment=None):
440
 
        try:
441
 
            self.axis[axis_index].set_style(colour, font_size, alignment)
442
 
        except IndexError:
443
 
            raise InvalidParametersException('Axis index %i has not been ' \
444
 
                'created' % axis)
 
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)
445
431
 
446
432
    def axis_to_url(self):
447
433
        available_axis = []
450
436
        positions = []
451
437
        styles = []
452
438
        index = -1
453
 
        for axis in self.axis:
454
 
            available_axis.append(axis.axis_type)
 
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)
455
445
            if isinstance(axis, RangeAxis):
456
446
                range_axis.append(repr(axis))
457
447
            if isinstance(axis, LabelAxis):
680
670
#        'aabbcc00', 0.5)
681
671
#    chart.fill_linear_stripes(Chart.CHART, 20, '204070', .2, '300040', .2,
682
672
#        'aabbcc00', 0.2)
683
 
    axis_left_index = chart.set_axis_range(Axis.LEFT, 0, 10)
684
 
    axis_left_index = chart.set_axis_range(Axis.LEFT, 0, 10)
685
 
    axis_left_index = chart.set_axis_range(Axis.LEFT, 0, 10)
686
 
    axis_right_index = chart.set_axis_range(Axis.RIGHT, 5, 30)
687
 
    axis_bottom_index = chart.set_axis_labels(Axis.BOTTOM, [1, 25, 95])
688
 
    chart.set_axis_positions(axis_bottom_index, [1, 25, 95])
689
 
    chart.set_axis_style(axis_bottom_index, '003050', 15)
 
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)
690
678
 
691
679
#    chart.set_pie_labels(('apples', 'oranges', 'bananas'))
692
680