/+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: 2009-03-15 08:30:28 UTC
  • Revision ID: git-v1:33085bb9ee79265f2d97b0024c1b3bf33db09836
 - Version bump to 0.3.0
 - Fixed GPL date
 - Fixed line 80 overruns

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
import os
25
25
import urllib
26
 
import urllib.request, urllib.error
 
26
import urllib2
27
27
import math
28
28
import random
29
29
import re
284
284
    of the chart. legend requires a list that corresponds to datasets.
285
285
    """
286
286
 
287
 
    BASE_URL = 'http://chart.apis.google.com/chart'
 
287
    BASE_URL = 'http://chart.apis.google.com/chart?'
288
288
    BACKGROUND = 'bg'
289
289
    CHART = 'c'
290
290
    ALPHA = 'a'
332
332
        self.markers = []
333
333
        self.line_styles = {}
334
334
        self.grid = None
335
 
        self.title_colour = None
336
 
        self.title_font_size = None
337
335
 
338
336
    # URL generation
339
337
    # -------------------------------------------------------------------------
340
 
        
 
338
 
341
339
    def get_url(self, data_class=None):
342
 
        return self.BASE_URL + '?' + self.get_url_extension(data_class)
343
 
    
344
 
    def get_url_extension(self, data_class=None):
345
340
        url_bits = self.get_url_bits(data_class=data_class)
346
 
        return '&'.join(url_bits)
 
341
        return self.BASE_URL + '&'.join(url_bits)
347
342
 
348
343
    def get_url_bits(self, data_class=None):
349
344
        url_bits = []
375
370
            url_bits.append(self.markers_to_url())        
376
371
        if self.line_styles:
377
372
            style = []
378
 
            for index in range(max(self.line_styles) + 1):
 
373
            for index in xrange(max(self.line_styles) + 1):
379
374
                if index in self.line_styles:
380
375
                    values = self.line_styles[index]
381
376
                else:
389
384
    # Downloading
390
385
    # -------------------------------------------------------------------------
391
386
 
392
 
    def download(self, file_name, use_post=True):
393
 
        if use_post:
394
 
            opener = urllib.request.urlopen(self.BASE_URL, self.get_url_extension())
395
 
        else:
396
 
            opener = urllib.request.urlopen(self.get_url())
 
387
    def download(self, file_name):
 
388
        opener = urllib2.urlopen(self.get_url())
397
389
 
398
390
        if opener.headers['content-type'] != 'image/png':
399
391
            raise BadContentTypeException('Server responded with a ' \
406
398
 
407
399
    def set_title(self, title):
408
400
        if title:
409
 
            self.title = urllib.parse.quote(title)
 
401
            self.title = urllib.quote(title)
410
402
        else:
411
403
            self.title = None
412
404
 
413
 
    def set_title_style(self, colour=None, font_size=None):
 
405
    def set_title_style(self, colour, font_size):
414
406
        if not colour is None:
415
407
            _check_colour(colour)
416
 
        if not colour and not font_size:
417
 
            return
418
 
        self.title_colour = colour or '333333'
419
 
        self.title_font_size = font_size or 13.5
 
408
        self.title_colour = colour
 
409
        self.title_font_size = font_size
420
410
 
421
411
    def set_legend(self, legend):
422
412
        """legend needs to be a list, tuple or None"""
423
413
        assert(isinstance(legend, list) or isinstance(legend, tuple) or
424
414
            legend is None)
425
415
        if legend:
426
 
            self.legend = [urllib.parse.quote(a) for a in legend]
 
416
            self.legend = [urllib.quote(a) for a in legend]
427
417
        else:
428
418
            self.legend = None
429
419
 
430
420
    def set_legend_position(self, legend_position):
431
421
        if legend_position:
432
 
            self.legend_position = urllib.parse.quote(legend_position)
 
422
            self.legend_position = urllib.quote(legend_position)
433
423
        else:    
434
424
            self.legend_position = None
435
425
 
470
460
        assert(angle >= 0 and angle <= 90)
471
461
        assert(len(args) % 2 == 0)
472
462
        args = list(args)  # args is probably a tuple and we need to mutate
473
 
        for a in range(int(len(args) / 2)):
 
463
        for a in xrange(int(len(args) / 2)):
474
464
            col = args[a * 2]
475
465
            offset = args[a * 2 + 1]
476
466
            _check_colour(col)
626
616
 
627
617
    def set_axis_labels(self, axis_type, values):
628
618
        assert(axis_type in Axis.TYPES)
629
 
        values = [urllib.parse.quote(str(a)) for a in values]
 
619
        values = [urllib.quote(str(a)) for a in values]
630
620
        axis_index = len(self.axis)
631
621
        axis = LabelAxis(axis_index, axis_type, values)
632
622
        self.axis.append(axis)
812
802
            url_bits.append('chbh=%i' % self.bar_width)
813
803
        zero_line = []
814
804
        if self.zero_lines:
815
 
            for index in range(max(self.zero_lines) + 1):
 
805
            for index in xrange(max(self.zero_lines) + 1):
816
806
                if index in self.zero_lines:
817
807
                    zero_line.append(str(self.zero_lines[index]))
818
808
                else:
906
896
                (self.__class__.__name__))
907
897
 
908
898
    def set_pie_labels(self, labels):
909
 
        self.pie_labels = [urllib.parse.quote(a) for a in labels]
 
899
        self.pie_labels = [urllib.quote(a) for a in labels]
910
900
 
911
901
    def get_url_bits(self, data_class=None):
912
902
        url_bits = Chart.get_url_bits(self, data_class=data_class)
1077
1067
    def data_to_url(self, data_class=None):
1078
1068
        if not self.data:
1079
1069
            raise NoDataGivenException()
1080
 
        return 'chl=%s' % urllib.parse.quote(self.data[0])
 
1070
        return 'chl=%s' % urllib.quote(self.data[0])
1081
1071
 
1082
1072
    def get_url_bits(self, data_class=None):
1083
1073
        url_bits = Chart.get_url_bits(self, data_class=data_class)