/+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-05-03 04:23:51 UTC
  • Revision ID: git-v1:1f4e14367f685fad4e70b381063c5ca195704615
- Initial "grammar" code
- New exception types: AbstractClassException and UnknownChartType
- More unit tests
- Removed tests from within pygooglechart.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""
2
 
PyGoogleChart - A complete Python wrapper for the Google Chart API
 
2
pygooglechart - A complete Python wrapper for the Google Chart API
3
3
 
4
4
http://pygooglechart.slowchop.com/
5
5
 
31
31
# -----------------------------------------------------------------------------
32
32
 
33
33
__version__ = '0.2.1'
 
34
__author__ = 'Gerald Kaszuba'
34
35
 
35
36
reo_colour = re.compile('^([A-Fa-f0-9]{2,2}){3,4}$')
36
37
 
37
 
 
38
38
def _check_colour(colour):
39
39
    if not reo_colour.match(colour):
40
40
        raise InvalidParametersException('Colours need to be in ' \
69
69
    pass
70
70
 
71
71
 
 
72
class AbstractClassException(PyGoogleChartException):
 
73
    pass
 
74
 
 
75
 
 
76
class UnknownChartType(PyGoogleChartException):
 
77
    pass
 
78
 
 
79
 
72
80
# Data Classes
73
81
# -----------------------------------------------------------------------------
74
82
 
76
84
class Data(object):
77
85
 
78
86
    def __init__(self, data):
79
 
        assert(type(self) != Data)  # This is an abstract class
 
87
        if type(self) == Data:
 
88
            raise AbstractClassException('This is an abstract class')
80
89
        self.data = data
81
90
 
82
91
    @classmethod
279
288
 
280
289
    def __init__(self, width, height, title=None, legend=None, colours=None,
281
290
                 auto_scale=True, x_range=None, y_range=None):
282
 
        assert(type(self) != Chart)  # This is an abstract class
 
291
        if type(self) == Chart:
 
292
            raise AbstractClassException('This is an abstract class')
283
293
        assert(isinstance(width, int))
284
294
        assert(isinstance(height, int))
285
295
        self.width = width
674
684
class LineChart(Chart):
675
685
 
676
686
    def __init__(self, *args, **kwargs):
677
 
        assert(type(self) != LineChart)  # This is an abstract class
 
687
        if type(self) == LineChart:
 
688
            raise AbstractClassException('This is an abstract class')
678
689
        Chart.__init__(self, *args, **kwargs)
679
690
 
680
691
 
712
723
class BarChart(Chart):
713
724
 
714
725
    def __init__(self, *args, **kwargs):
715
 
        assert(type(self) != BarChart)  # This is an abstract class
 
726
        if type(self) == BarChart:
 
727
            raise AbstractClassException('This is an abstract class')
716
728
        Chart.__init__(self, *args, **kwargs)
717
729
        self.bar_width = None
718
730
        self.zero_lines = {}
757
769
class GroupedBarChart(BarChart):
758
770
 
759
771
    def __init__(self, *args, **kwargs):
760
 
        assert(type(self) != GroupedBarChart)  # This is an abstract class
 
772
        if type(self) == GroupedBarChart:
 
773
            raise AbstractClassException('This is an abstract class')
761
774
        BarChart.__init__(self, *args, **kwargs)
762
775
        self.bar_spacing = None
763
776
        self.group_spacing = None
813
826
class PieChart(Chart):
814
827
 
815
828
    def __init__(self, *args, **kwargs):
816
 
        assert(type(self) != PieChart)  # This is an abstract class
 
829
        if type(self) == PieChart:
 
830
            raise AbstractClassException('This is an abstract class')
817
831
        Chart.__init__(self, *args, **kwargs)
818
832
        self.pie_labels = []
819
833
 
895
909
        return 'cht=gom'
896
910
 
897
911
 
898
 
def test():
899
 
    chart = PieChart2D(320, 200)
900
 
    chart = ScatterChart(320, 200)
901
 
    chart = SimpleLineChart(320, 200)
902
 
    chart = GroupedVerticalBarChart(320, 200)
903
 
#    chart = SplineRadarChart(500, 500)
904
 
#    chart = MapChart(440, 220)
905
 
#    chart = GoogleOMeterChart(440, 220, x_range=(0, 100))
906
 
    sine_data = [math.sin(float(a) / math.pi) * 100 for a in xrange(100)]
907
 
    random_data = [random.random() * 100 for a in xrange(100)]
908
 
    random_data2 = [random.random() * 50 for a in xrange(100)]
909
 
#    chart.set_bar_width(50)
910
 
#    chart.set_bar_spacing(0)
911
 
    chart.add_data(sine_data)
912
 
    chart.add_data(random_data)
913
 
#    chart.set_zero_line(1, .5)
914
 
#    chart.add_data(random_data2)
915
 
#    chart.set_line_style(0, thickness=5)
916
 
#    chart.set_line_style(1, thickness=2, line_segment=10, blank_segment=5)
917
 
#    chart.set_title('heloooo weeee')
918
 
#    chart.set_legend(('sine wave', 'random * x'))
919
 
    chart.set_colours(('ee2000', 'DDDDAA', 'fF03f2'))
920
 
#    chart.fill_solid(Chart.ALPHA, '123456')
921
 
#    chart.fill_linear_gradient(Chart.ALPHA, 20, '004070', 1, '300040', 0,
922
 
#        'aabbcc55', 0.5)
923
 
#    chart.fill_linear_stripes(Chart.CHART, 20, '204070', .2, '300040', .2,
924
 
#        'aabbcc00', 0.2)
925
 
#    axis_left_index = chart.set_axis_range(Axis.LEFT, 0, 10)
926
 
#    axis_right_index = chart.set_axis_range(Axis.RIGHT, 5, 30)
927
 
#    axis_bottom_index = chart.set_axis_labels(Axis.BOTTOM, [1, 25, 95])
928
 
#    chart.set_axis_positions(axis_bottom_index, [1, 25, 95])
929
 
#    chart.set_axis_style(axis_bottom_index, '003050', 15)
930
 
 
931
 
#    chart.set_pie_labels(('apples', 'oranges', 'bananas'))
932
 
 
933
 
#    chart.set_grid(10, 10)
934
 
#    for a in xrange(0, 100, 10):
935
 
#        chart.add_marker(1, a, 'a', 'AACA20', 10)
936
 
 
937
 
#    chart.add_horizontal_range('00A020', .2, .5)
938
 
#    chart.add_vertical_range('00c030', .2, .4)
939
 
 
940
 
#    chart.add_fill_simple('303030A0')
941
 
 
942
 
#    chart.set_codes(['AU', 'AT', 'US'])
943
 
#    chart.add_data([1,2,3])
944
 
#    chart.set_colours(('EEEEEE', '000000', '00FF00'))
945
 
 
946
 
#    chart.add_data([50,75])
947
 
#    chart.set_pie_labels(('apples', 'oranges'))
948
 
 
949
 
    url = chart.get_url()
950
 
    print url
951
 
 
952
 
    chart.download('test.png')
953
 
 
954
 
    if 1:
955
 
        data = urllib.urlopen(chart.get_url()).read()
956
 
        open('meh.png', 'wb').write(data)
957
 
        os.system('eog meh.png')
958
 
 
959
 
 
960
 
if __name__ == '__main__':
961
 
    test()
 
912
class ChartGrammar(object):
 
913
 
 
914
    def __init__(self, grammar):
 
915
        self.grammar = grammar
 
916
        self.chart = self.create_chart_instance()
 
917
 
 
918
    @staticmethod
 
919
    def get_possible_chart_types():
 
920
        possible_charts = []
 
921
        for cls_name in globals():
 
922
            if not cls_name.endswith('Chart'):
 
923
                continue
 
924
            cls = globals()[cls_name]
 
925
            # Check if it is an abstract class
 
926
            try:
 
927
                cls(1, 1)
 
928
            except AbstractClassException:
 
929
                continue
 
930
            # Strip off "Class"
 
931
            possible_charts.append(cls_name[:-5])
 
932
        return possible_charts
 
933
 
 
934
    def create_chart_instance(self):
 
935
        assert('w' in grammar)  # width is required
 
936
        assert('h' in grammar)  # height is required
 
937
        assert('type' in grammar)  # type is required
 
938
        types = ChartGrammar.get_possible_chart_types()
 
939
        if grammar['type'] not in types:
 
940
            raise UnknownChartType('%s is an unknown chart type. Possible '
 
941
                'chart types are %s' % (grammar['type'], ','.join(types)))
 
942
 
 
943
    def download(self):
 
944
        pass
 
945