diff options
| author | Peter Ward <peteraward@gmail.com> | 2009-10-29 20:29:54 +1100 | 
|---|---|---|
| committer | Peter Ward <peteraward@gmail.com> | 2009-10-29 20:29:54 +1100 | 
| commit | 9d65a3fdc49858895dcefecd7b53534c297a25c4 (patch) | |
| tree | 79ff102f5faa1f8c8b82a92a84bcffc96fd82e77 /pngchart.py | |
| parent | cf517eb700405758f091451f8d66fef2a2cf7108 (diff) | |
Added PNG charts.
Diffstat (limited to 'pngchart.py')
| -rw-r--r-- | pngchart.py | 53 | 
1 files changed, 53 insertions, 0 deletions
| diff --git a/pngchart.py b/pngchart.py new file mode 100644 index 0000000..5428718 --- /dev/null +++ b/pngchart.py @@ -0,0 +1,53 @@ +from pngcanvas import PNGCanvas + +try: +    from itertools import izip as zip +except ImportError: +    pass + +class SimpleLineChart(object): +    def __init__(self, width, height, colours=None, legend=None): +        self.canvas = PNGCanvas(width, height) + +        self.width = width +        self.height = height + +        self.colours = colours +        self.legend = legend + +        self.series = [] + +    def add_data(self, series): +        self.series.append(series) + +    def render(self): +        max_width = max(map(len, self.series)) +        max_height = max(map(max, self.series)) +        x_scale = float(self.width) / max_width +        y_scale = float(self.height) / max_height + +        data = zip(self.series, self.colours or [], self.legend or []) +        for series, colour, legend in data: +            colour = int(colour, 16) +            self.canvas.color = ( +                colour>>16 & 0xff, +                colour>>8 & 0xff, +                colour & 0xff, +                0xff, +            ) +            last = None +            for x, y in enumerate(series): +                if y is not None: +                    y = self.height - y * y_scale +                    if last is not None: +                        x *= x_scale +                        self.canvas.line(x - x_scale, last, x, y) +                last = y + +    def download(self, filename): +        self.render() + +        f = open(filename, 'wb') +        f.write(self.canvas.dump()) +        f.close() + | 
