/+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: 2008-08-23 03:58:12 UTC
  • Revision ID: git-v1:ba1b973bb024ebdb56a33bae68acd2a929abafca
 - Added "colours within series" option to chart. (chco=xxx|xxx) (Steve Brandt)
 - Added legend positioning (chdlp) (Steve Brandt)
 - Added line styles (chm=D) (Steve Brandt)
 - Bugfix, replace "1" with "0" in add_horizontal_range and add_vertical_range (incorrect syntax for Google) (Steve Brandt)
 - Better clipping checks with tests working now

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
def _reset_warnings():
48
48
    """Helper function to reset all warnings. Used by the unit tests."""
49
49
    globals()['__warningregistry__'] = None
50
 
#def _warn(message):
51
 
#    warnings.warn_explicit(msg, warnings.UserWarning,
52
50
 
53
51
 
54
52
# Exception Classes
117
115
    def scale_value(cls, value, range):
118
116
        scaled = cls.int_scale_value(value, range)
119
117
        clipped = cls.clip_value(scaled)
 
118
        Data.check_clip(scaled, clipped)
 
119
        return clipped
 
120
 
 
121
    @staticmethod
 
122
    def check_clip(scaled, clipped):
120
123
        if clipped != scaled:
121
124
            warnings.warn('One or more of of your data points has been '
122
125
                'clipped because it is out of range.')
123
 
        return clipped
124
126
 
125
127
 
126
128
class SimpleData(Data):
168
170
        # map index
169
171
        scaled = cls.float_scale_value(value, range)
170
172
        clipped = cls.clip_value(scaled)
 
173
        Data.check_clip(scaled, clipped)
171
174
        return clipped
172
175
 
173
176
 
288
291
    LINEAR_STRIPES = 'ls'
289
292
 
290
293
    def __init__(self, width, height, title=None, legend=None, colours=None,
291
 
            auto_scale=True, x_range=None, y_range=None):
 
294
            auto_scale=True, x_range=None, y_range=None,
 
295
            colours_within_series=None):
292
296
        if type(self) == Chart:
293
297
            raise AbstractClassException('This is an abstract class')
294
298
        assert(isinstance(width, int))
298
302
        self.data = []
299
303
        self.set_title(title)
300
304
        self.set_legend(legend)
 
305
        self.set_legend_position(None)
301
306
        self.set_colours(colours)
 
307
        self.set_colours_within_series(colours_within_series)
302
308
 
303
309
        # Data for scaling.
304
310
        self.auto_scale = auto_scale  # Whether to automatically scale data
320
326
        }
321
327
        self.axis = []
322
328
        self.markers = []
 
329
        self.trendline_data = []
 
330
        self.trendline_style = None
323
331
        self.line_styles = {}
324
332
        self.grid = None
325
333
 
341
349
            url_bits.append('chtt=%s' % self.title)
342
350
        if self.legend:
343
351
            url_bits.append('chdl=%s' % '|'.join(self.legend))
 
352
        if self.legend_position:
 
353
            url_bits.append('chdlp=%s' % (self.legend_position))
344
354
        if self.colours:
345
 
            url_bits.append('chco=%s' % ','.join(self.colours))
 
355
            url_bits.append('chco=%s' % ','.join(self.colours))            
 
356
        if self.colours_within_series:
 
357
            url_bits.append('chco=%s' % '|'.join(self.colours_within_series))
346
358
        ret = self.fill_to_url()
347
359
        if ret:
348
360
            url_bits.append(ret)
349
361
        ret = self.axis_to_url()
350
362
        if ret:
351
 
            url_bits.append(ret)
 
363
            url_bits.append(ret)                    
 
364
        if self.trendline_data: 
 
365
            if not self.trendline_style: self.set_trendline_style()
 
366
            url_bits.append('ewtr=0,%s,%.1f' % (self.trendline_colour, self.trendline_thickness))
 
367
            url_bits.append(self.trendline_data_to_url(data_class=data_class))            
352
368
        if self.markers:
353
 
            url_bits.append(self.markers_to_url())
 
369
            url_bits.append(self.markers_to_url())        
354
370
        if self.line_styles:
355
371
            style = []
356
372
            for index in xrange(max(self.line_styles) + 1):
394
410
        else:
395
411
            self.legend = None
396
412
 
 
413
    def set_legend_position(self, legend_position):
 
414
        if legend_position:
 
415
            self.legend_position = urllib.quote(legend_position)
 
416
        else:    
 
417
            self.legend_position = None
 
418
 
397
419
    # Chart colours
398
420
    # -------------------------------------------------------------------------
399
421
 
407
429
                _check_colour(col)
408
430
        self.colours = colours
409
431
 
 
432
    def set_colours_within_series(self, colours):
 
433
        # colours needs to be a list, tuple or None
 
434
        assert(isinstance(colours, list) or isinstance(colours, tuple) or
 
435
            colours is None)
 
436
        # make sure the colours are in the right format
 
437
        if colours:
 
438
            for col in colours:
 
439
                _check_colour(col)
 
440
        self.colours_within_series = colours        
 
441
 
410
442
    # Background/Chart colours
411
443
    # -------------------------------------------------------------------------
412
444
 
626
658
    # Markers, Ranges and Fill area (chm)
627
659
    # -------------------------------------------------------------------------
628
660
 
629
 
    def markers_to_url(self):
 
661
    def markers_to_url(self):        
630
662
        return 'chm=%s' % '|'.join([','.join(a) for a in self.markers])
631
663
 
632
664
    def add_marker(self, index, point, marker_type, colour, size, priority=0):
634
666
            str(size), str(priority)))
635
667
 
636
668
    def add_horizontal_range(self, colour, start, stop):
637
 
        self.markers.append(('r', colour, '1', str(start), str(stop)))
 
669
        self.markers.append(('r', colour, '0', str(start), str(stop)))
 
670
 
 
671
    def add_data_line(self, colour, data_set, size, priority=0):
 
672
        self.markers.append(('D', colour, str(data_set), '0', str(size), str(priority)))
 
673
 
 
674
    def add_marker_text(self, string, colour, data_set, data_point, size, priority=0):
 
675
        self.markers.append((str(string), colour, str(data_set), str(data_point), str(size), str(priority)))        
638
676
 
639
677
    def add_vertical_range(self, colour, start, stop):
640
 
        self.markers.append(('R', colour, '1', str(start), str(stop)))
 
678
        self.markers.append(('R', colour, '0', str(start), str(stop)))
641
679
 
642
680
    def add_fill_range(self, colour, index_start, index_end):
643
681
        self.markers.append(('b', colour, str(index_start), str(index_end), \
941
979
 
942
980
    def parse_data(self, data):
943
981
        self.chart.data = data
944
 
        print self.chart.data
945
982
 
946
983
    @staticmethod
947
984
    def get_possible_chart_types():