/+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: Gustav Hartvigsson
  • Date: 2011-01-03 21:57:12 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20110103215712-1yeiw9tl7oiwh8w1
forgot the the the images in the examples folder...

Show diffs side-by-side

added added

removed removed

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