/+junk/pygooglechart-py3k

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/pygooglechart-py3k
7 by gak
version bump, added BadContentTypeException, added a few examples, added COPYING licence, code is more PEP8 friendly, download() checks for content type and raises on bad http codes, add_data returns the index of the dataset, Line doesn't allow being an instance.
1
#!/usr/bin/env python
2
3
import os
4
import sys
5
import math
6
7
ROOT = os.path.dirname(os.path.abspath(__file__))
8
sys.path.insert(0, os.path.join(ROOT, '..'))
9
46 by gak
- Added stripes and grid line chart to examples
10
from pygooglechart import Chart
7 by gak
version bump, added BadContentTypeException, added a few examples, added COPYING licence, code is more PEP8 friendly, download() checks for content type and raises on bad http codes, add_data returns the index of the dataset, Line doesn't allow being an instance.
11
from pygooglechart import SimpleLineChart
12
from pygooglechart import XYLineChart
22 by gak
- pygooglechart.py converted to unix line breaks
13
from pygooglechart import SparkLineChart
45 by gak
- Added filled line graph to examples
14
from pygooglechart import Axis
7 by gak
version bump, added BadContentTypeException, added a few examples, added COPYING licence, code is more PEP8 friendly, download() checks for content type and raises on bad http codes, add_data returns the index of the dataset, Line doesn't allow being an instance.
15
16
import settings
17
import helper
18
19
def simple_random():
19 by gak
Added automatic scaling patch from Trent Mick
20
    chart = SimpleLineChart(settings.width, settings.height, y_range=(0, 100))
7 by gak
version bump, added BadContentTypeException, added a few examples, added COPYING licence, code is more PEP8 friendly, download() checks for content type and raises on bad http codes, add_data returns the index of the dataset, Line doesn't allow being an instance.
21
    chart.add_data(helper.random_data())
22
    chart.download('line-simple-random.png')
23
24
def xy_random():
22 by gak
- pygooglechart.py converted to unix line breaks
25
    chart = XYLineChart(settings.width, settings.height,
19 by gak
Added automatic scaling patch from Trent Mick
26
                        x_range=(0, 100), y_range=(0, 100))
7 by gak
version bump, added BadContentTypeException, added a few examples, added COPYING licence, code is more PEP8 friendly, download() checks for content type and raises on bad http codes, add_data returns the index of the dataset, Line doesn't allow being an instance.
27
    chart.add_data(helper.random_data())
28
    chart.add_data(helper.random_data())
29
    chart.download('line-xy-random.png')
30
31
def xy_rect():
19 by gak
Added automatic scaling patch from Trent Mick
32
    chart = XYLineChart(settings.width, settings.height,
33
                        x_range=(0, 100), y_range=(0, 100))
15 by gak
Added more examples
34
    chart.add_data([10, 90, 90, 10, 10])
35
    chart.add_data([10, 10, 90, 90, 10])
7 by gak
version bump, added BadContentTypeException, added a few examples, added COPYING licence, code is more PEP8 friendly, download() checks for content type and raises on bad http codes, add_data returns the index of the dataset, Line doesn't allow being an instance.
36
    chart.download('line-xy-rect.png')
37
38
def xy_circle():
19 by gak
Added automatic scaling patch from Trent Mick
39
    chart = XYLineChart(settings.width, settings.height,
40
                        x_range=(0, 100), y_range=(0, 100))
7 by gak
version bump, added BadContentTypeException, added a few examples, added COPYING licence, code is more PEP8 friendly, download() checks for content type and raises on bad http codes, add_data returns the index of the dataset, Line doesn't allow being an instance.
41
    steps = 40
42
    xradius = 25
43
    yradius = 45
44
    xmid = 50
45
    ymid = 50
46
    xlist = []
47
    ylist = []
48
    for angle in xrange(0, steps + 1):
49
        angle = float(angle) / steps * math.pi * 2
50
        xlist.append(math.cos(angle) * xradius + xmid)
51
        ylist.append(math.sin(angle) * yradius + ymid)
52
    chart.add_data(xlist)
53
    chart.add_data(ylist)
54
    chart.download('line-xy-circle.png')
55
22 by gak
- pygooglechart.py converted to unix line breaks
56
def sparklines():
57
    chart = SparkLineChart(settings.width, settings.height)
58
    chart.add_data(helper.random_data())
59
    chart.download('line-sparkline.png')
60
45 by gak
- Added filled line graph to examples
61
def fill():
62
46 by gak
- Added stripes and grid line chart to examples
63
    # Set the vertical range from 0 to 50
45 by gak
- Added filled line graph to examples
64
    max_y = 50
65
    chart = SimpleLineChart(200, 125, y_range=[0, max_y])
66
67
    # First value is the highest Y value. Two of them are needed to be
68
    # plottable.
69
    chart.add_data([max_y] * 2)
70
71
    # 3 sets of real data
72
    chart.add_data([28, 30, 31, 33, 35, 36, 42, 48, 43, 37, 32, 24, 28])
73
    chart.add_data([16, 18, 18, 21, 23, 23, 29, 36, 31, 25, 20, 12, 17])
74
    chart.add_data([7, 9, 9, 12, 14, 14, 20, 27, 21, 15, 10, 3, 7])
75
76
    # Last value is the lowest in the Y axis.
77
    chart.add_data([0] * 2)
78
79
    # Black lines
80
    chart.set_colours(['000000'] * 5)
81
82
    # Filled colours
83
    # from the top to the first real data
84
    chart.add_fill_range('76A4FB', 0, 1)
85
86
    # Between the 3 data values
87
    chart.add_fill_range('224499', 1, 2)
88
    chart.add_fill_range('FF0000', 2, 3)
49 by gak
- Added QR codes example
89
45 by gak
- Added filled line graph to examples
90
    # from the last real data to the
91
    chart.add_fill_range('80C65A', 3, 4)
92
93
    # Some axis data
94
    chart.set_axis_labels(Axis.LEFT, ['', max_y / 2, max_y])
95
    chart.set_axis_labels(Axis.BOTTOM, ['Sep', 'Oct', 'Nov', 'Dec'])
96
97
    chart.download('line-fill.png')
98
46 by gak
- Added stripes and grid line chart to examples
99
def stripes():
100
    
101
    # Set the vertical range from 0 to 100
102
    max_y = 100
103
104
    # Chart size of 200x125 pixels and specifying the range for the Y axis
105
    chart = SimpleLineChart(200, 125, y_range=[0, max_y])
106
107
    # Add the chart data
108
    data = [
109
        32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37,
110
        37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62, 62, 60, 55,
111
        55, 52, 47, 44, 44, 40, 40, 37, 34, 34, 32, 32, 32, 31, 32
112
    ]
113
    chart.add_data(data)
114
    
115
    # Set the line colour to blue
116
    chart.set_colours(['0000FF'])
117
118
    # Set the vertical stripes
119
    chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2)
120
49 by gak
- Added QR codes example
121
    # Set the horizontal dotted lines
46 by gak
- Added stripes and grid line chart to examples
122
    chart.set_grid(0, 25, 5, 5)
123
124
    # The Y axis labels contains 0 to 100 skipping every 25, but remove the
125
    # first number because it's obvious and gets in the way of the first X
126
    # label.
127
    left_axis = range(0, max_y + 1, 25)
128
    left_axis[0] = ''
129
    chart.set_axis_labels(Axis.LEFT, left_axis)
130
131
    # X axis labels
132
    chart.set_axis_labels(Axis.BOTTOM, \
133
        ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'])
134
135
    chart.download('line-stripes.png')
136
7 by gak
version bump, added BadContentTypeException, added a few examples, added COPYING licence, code is more PEP8 friendly, download() checks for content type and raises on bad http codes, add_data returns the index of the dataset, Line doesn't allow being an instance.
137
def main():
138
    simple_random()
139
    xy_random()
140
    xy_rect()
141
    xy_circle()
22 by gak
- pygooglechart.py converted to unix line breaks
142
    sparklines()
45 by gak
- Added filled line graph to examples
143
    fill()
46 by gak
- Added stripes and grid line chart to examples
144
    stripes()
7 by gak
version bump, added BadContentTypeException, added a few examples, added COPYING licence, code is more PEP8 friendly, download() checks for content type and raises on bad http codes, add_data returns the index of the dataset, Line doesn't allow being an instance.
145
146
if __name__ == '__main__':
147
    main()
148