Python to generate nice looking SVG graph http://pygal.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.8 KiB

13 years ago
# -*- coding: utf-8 -*-
13 years ago
# This file is part of pygal
#
# A python svg graph plotting library
# Copyright © 2012 Kozea
#
# This library is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pygal. If not, see <http://www.gnu.org/licenses/>.
from flask import Flask, render_template, url_for
from moulinrouge.data import labels, series
13 years ago
from logging import getLogger, INFO, DEBUG
import pygal
13 years ago
from pygal.config import Config
from pygal.style import styles
import string
import random
def random_label():
chars = string.letters + string.digits + u' àéèçêâäëï'
return ''.join(
[random.choice(chars)
for i in range(
random.randrange(4, 30))])
def random_value(min=0, max=15):
return random.randrange(min, max, 1)
13 years ago
def create_app():
"""Creates the pygal test web app"""
app = Flask(__name__)
try:
from log_colorizer import make_colored_stream_handler
handler = make_colored_stream_handler()
except ImportError:
from logging import StreamHandler
handler = StreamHandler()
13 years ago
getLogger('werkzeug').addHandler(handler)
getLogger('werkzeug').setLevel(INFO)
getLogger('pygal').addHandler(handler)
getLogger('pygal').setLevel(DEBUG)
@app.route("/")
def index():
return render_template('index.jinja2')
@app.route("/all-<type>-<style>(fill=<fill>).svg")
def all_svg(type, style, fill):
13 years ago
data = random.randrange(1, 10)
order = random.randrange(1, 10)
max = 10 ** order
min = 10 ** random.randrange(0, order)
13 years ago
config = Config()
config.width = 600
config.height = 400
config.fill = fill == 'True'
13 years ago
config.style = styles[style]
if type != 'Pie':
config.x_labels = [random_label() for i in range(data)]
config.title = "%d - %d" % (min, max)
g = getattr(pygal, type)(config)
13 years ago
for i in range(random.randrange(1, 10)):
if type == 'Pie':
values = random_value(min, max)
elif type == 'XY':
values = [(
random_value((-max, min)[random.randrange(0, 2)], max),
random_value((-max, min)[random.randrange(0, 2)], max))
for i in range(data)]
else:
values = [random_value((-max, min)[random.randrange(0, 2)],
max) for i in range(data)]
13 years ago
g.add(random_label(), values)
return g.render_response()
13 years ago
@app.route("/all")
def all():
width, height = 600, 400
svgs = [url_for('all_svg', type=type, style=style, fill=fill)
13 years ago
for style in styles
for fill in (False, True)
for type in ('Bar', 'Line', 'XY', 'Pie', 'StackedBar',
13 years ago
'HorizontalBar', 'HorizontalStackedBar', 'Radar')]
13 years ago
return render_template('svgs.jinja2',
svgs=svgs,
width=width,
height=height)
@app.route("/rotation[<int:angle>].svg")
def rotation_svg(angle):
config = Config()
config.width = 375
config.height = 245
config.x_labels = labels
config.x_label_rotation = angle
g = pygal.Line(config)
for serie, values in series.items():
g.add(serie, values)
g.add(serie, values)
return g.render_response()
13 years ago
@app.route("/rotation")
def rotation():
width, height = 375, 245
svgs = [url_for('rotation_svg', angle=angle)
for angle in range(0, 91, 5)]
return render_template('svgs.jinja2',
svgs=svgs,
width=width,
height=height)
@app.route("/bigline.svg")
def big_line_svg():
g = pygal.Line(600, 400)
13 years ago
g.x_labels = ['a', 'b', 'c', 'd']
g.add('serie', [11, 50, 133, 2])
return g.render_response()
13 years ago
@app.route("/bigline")
def big_line():
width, height = 900, 800
svgs = [url_for('big_line_svg')]
return render_template('svgs.jinja2',
svgs=svgs,
width=width,
height=height)
return app