view MoinMoin/stats/ @ 995:b6ae650aec74

stats package whitespace-only and style cleanup
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Wed, 19 Jul 2006 11:30:12 +0200
parents 84dc5d29721a
children 0194beaf511e
line wrap: on
line source

# -*- coding: iso-8859-1 -*-
    MoinMoin - Charts

    This is a wrapper for the "gdchart" module.

        import random
        c = Chart()
        c.addData(ChartData([random.gauss(0, 5.0) for i in range(20)], color='blue'))
        c.option(title = 'gdchart Demo')
        c.draw(Chart.GDC_LINE, (600, 300), 'test.gif')

    @copyright: 2002-2004 by Jürgen Hermann <>
    @license: GNU GPL, see COPYING for details.

import gdchart
from MoinMoin.util.web import Color

class ChartData:
    """ Data set for one line in a chart, including
        properties like the color of that line.
    def __init__(self, data, color='black'):
        Create a data set.
        @param data: tuple / list of numbers
        @param color: rendering color (triple, '#RRGGBB' or color name)
        """ = data
        self.color = color

class Chart:
    """ Wrapper for "gdchart".

        All GDC* constants are available as class attributes.

    DEFAULTS = gdchart.option()

    def __init__(self):
        # Get a copy of the default options
        self.options = self.DEFAULTS.copy()
        self.datasets = []


    def addData(self, data):

    def option(self, **args):
        # Save option values in the object's dictionary.

    def draw(self, style, size, output, labels=None):
        args = []
        colors = []
        for dataset in self.datasets:
            if isinstance(dataset, ChartData):

        # Default for X axis labels (numbered 1..n)
        if not labels: labels = map(str, range(1, len(args[0])+1))

        # set colors for the data sets
        if colors: self.option(set_color=map(int, map(Color, colors)))

        # pass options to gdchart and render the chart

        # limit label length in order to workaround bugs in gdchart
        labels = [x[:32] for x in labels]
        gdchart.chart(*((style, size, output, labels) + tuple(args)))

# copy GDC constants to Chart's namespace
for key, val in vars(gdchart).items():
    if key.startswith('GDC'):
        setattr(Chart, key, val)

if __name__ == "__main__":
    import os, sys, random
    c = Chart()
    c.addData(ChartData([random.randrange(0, i+1) for i in range(20)], color='green'))
    c.addData(ChartData([random.gauss(30, 5.0) for i in range(20)], color='blue'))
        title='gdchart Demo',
        xtitle='X axis',
        ytitle='random values',
    c.draw(Chart.GDC_LINE, (600, 300), 'test.gif')
    if sys.platform == "win32":
        os.system("explorer test.gif")
        os.system("display test.gif &")