/+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-05 11:26:48 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20110105112648-1wsbve4v794tikh4
added a new README and renamed the old README.markdown to README.old....

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'
337
337
 
338
338
    # URL generation
339
339
    # -------------------------------------------------------------------------
340
 
 
 
340
        
341
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):
342
345
        url_bits = self.get_url_bits(data_class=data_class)
343
 
        return self.BASE_URL + '&'.join(url_bits)
 
346
        return '&'.join(url_bits)
344
347
 
345
348
    def get_url_bits(self, data_class=None):
346
349
        url_bits = []
372
375
            url_bits.append(self.markers_to_url())        
373
376
        if self.line_styles:
374
377
            style = []
375
 
            for index in xrange(max(self.line_styles) + 1):
 
378
            for index in range(max(self.line_styles) + 1):
376
379
                if index in self.line_styles:
377
380
                    values = self.line_styles[index]
378
381
                else:
386
389
    # Downloading
387
390
    # -------------------------------------------------------------------------
388
391
 
389
 
    def download(self, file_name):
390
 
        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())
391
397
 
392
398
        if opener.headers['content-type'] != 'image/png':
393
399
            raise BadContentTypeException('Server responded with a ' \
400
406
 
401
407
    def set_title(self, title):
402
408
        if title:
403
 
            self.title = urllib.quote(title)
 
409
            self.title = urllib.parse.quote(title)
404
410
        else:
405
411
            self.title = None
406
412
 
417
423
        assert(isinstance(legend, list) or isinstance(legend, tuple) or
418
424
            legend is None)
419
425
        if legend:
420
 
            self.legend = [urllib.quote(a) for a in legend]
 
426
            self.legend = [urllib.parse.quote(a) for a in legend]
421
427
        else:
422
428
            self.legend = None
423
429
 
424
430
    def set_legend_position(self, legend_position):
425
431
        if legend_position:
426
 
            self.legend_position = urllib.quote(legend_position)
 
432
            self.legend_position = urllib.parse.quote(legend_position)
427
433
        else:    
428
434
            self.legend_position = None
429
435
 
464
470
        assert(angle >= 0 and angle <= 90)
465
471
        assert(len(args) % 2 == 0)
466
472
        args = list(args)  # args is probably a tuple and we need to mutate
467
 
        for a in xrange(int(len(args) / 2)):
 
473
        for a in range(int(len(args) / 2)):
468
474
            col = args[a * 2]
469
475
            offset = args[a * 2 + 1]
470
476
            _check_colour(col)
620
626
 
621
627
    def set_axis_labels(self, axis_type, values):
622
628
        assert(axis_type in Axis.TYPES)
623
 
        values = [urllib.quote(str(a)) for a in values]
 
629
        values = [urllib.parse.quote(str(a)) for a in values]
624
630
        axis_index = len(self.axis)
625
631
        axis = LabelAxis(axis_index, axis_type, values)
626
632
        self.axis.append(axis)
806
812
            url_bits.append('chbh=%i' % self.bar_width)
807
813
        zero_line = []
808
814
        if self.zero_lines:
809
 
            for index in xrange(max(self.zero_lines) + 1):
 
815
            for index in range(max(self.zero_lines) + 1):
810
816
                if index in self.zero_lines:
811
817
                    zero_line.append(str(self.zero_lines[index]))
812
818
                else:
900
906
                (self.__class__.__name__))
901
907
 
902
908
    def set_pie_labels(self, labels):
903
 
        self.pie_labels = [urllib.quote(a) for a in labels]
 
909
        self.pie_labels = [urllib.parse.quote(a) for a in labels]
904
910
 
905
911
    def get_url_bits(self, data_class=None):
906
912
        url_bits = Chart.get_url_bits(self, data_class=data_class)
1071
1077
    def data_to_url(self, data_class=None):
1072
1078
        if not self.data:
1073
1079
            raise NoDataGivenException()
1074
 
        return 'chl=%s' % urllib.quote(self.data[0])
 
1080
        return 'chl=%s' % urllib.parse.quote(self.data[0])
1075
1081
 
1076
1082
    def get_url_bits(self, data_class=None):
1077
1083
        url_bits = Chart.get_url_bits(self, data_class=data_class)