From da38f7cf8942eeb90c7dab2f3a7415fd40a0350e Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Wed, 19 Sep 2012 16:15:23 +0200 Subject: [PATCH] Polish cabaret --- demo/cabaret/__init__.py | 7 +++--- demo/cabaret/static/css.css | 3 ++- demo/cabaret/static/js.js | 33 ++++++++++++++++++++++------- demo/cabaret/templates/index.jinja2 | 26 +++++++++++++++++------ pygal/config.py | 2 -- pygal/interpolate.py | 7 ++++-- 6 files changed, 55 insertions(+), 23 deletions(-) diff --git a/demo/cabaret/__init__.py b/demo/cabaret/__init__.py index a6c9dd0..a7d5dcf 100644 --- a/demo/cabaret/__init__.py +++ b/demo/cabaret/__init__.py @@ -20,6 +20,7 @@ from flask import Flask, render_template, request from pygal import CHARTS_BY_NAME from pygal.graph import CHARTS_NAMES from pygal.config import CONFIG_ITEMS +from pygal.interpolate import KINDS from pygal.style import styles from json import loads @@ -33,7 +34,7 @@ def create_app(): def index(): return render_template( 'index.jinja2', charts_names=CHARTS_NAMES, configs=CONFIG_ITEMS, - styles_names=styles.keys()) + interpolations=KINDS, styles_names=styles.keys()) @app.route("/svg", methods=('POST',)) def svg(): @@ -47,8 +48,8 @@ def create_app(): if value: config[item.name] = item.coerce(value) chart = CHARTS_BY_NAME[values['type']](**config) - for title, vals in loads(values['vals']).items(): - chart.add(title, vals) + for serie in loads(values['vals'])['vals']: + chart.add(serie[0], serie[1]) return chart.render_response() return app diff --git a/demo/cabaret/static/css.css b/demo/cabaret/static/css.css index 7fd84db..d938ab3 100644 --- a/demo/cabaret/static/css.css +++ b/demo/cabaret/static/css.css @@ -13,6 +13,7 @@ figure { figure svg { width: 100%; + height: 0%; background: none; } @@ -23,7 +24,7 @@ figure svg { } .side { - min-width: 400px; + width: 375px !important; } label { diff --git a/demo/cabaret/static/js.js b/demo/cabaret/static/js.js index b73a817..ed5c006 100644 --- a/demo/cabaret/static/js.js +++ b/demo/cabaret/static/js.js @@ -1,10 +1,10 @@ function resend() { var $fig = $('figure'), - // $embed = $fig.find('embed'), type = $('#type').val(), - data = $('#data').val(), style = $('#style').val(), - opts = {}; + interpolation = $('#interpolation').val(), + opts = {}, + vals = []; $('.c-opts').each(function() { var $this = $(this), val = $this.val(); @@ -14,16 +14,25 @@ function resend() { opts[$this.attr('id').replace('c-', '')] = val; } }); + if(interpolation) { + opts['interpolate'] = interpolation; + } + $('.data .controls').each(function () { + var label = $(this).find('.serie-label').val(), + values = $(this).find('.serie-value').val(); + vals.push([label, values.split(',').map(function (v) { return parseFloat(v); })]); + }); $.ajax({ url: '/svg', type: 'POST', data: { type: type, style: style, - vals: '{' + data + '}', + vals: JSON.stringify({vals: vals}), opts: JSON.stringify(opts) }, - dataType: 'html' + dataType: 'html', + traditional: true }).done(function (data) { // $fig.find('div').get(0).innerHTML = data; $fig.find('div').html(data); @@ -35,11 +44,19 @@ function resend() { } $(function () { - $('#type').on('change', resend); + $('#type').add('#style').add('#interpolation').on('change', resend); $('#data').on('input', resend); - $('#style').on('change', resend); $('.c-opts:not([type=checkbox])').on('input', resend); $('.c-opts[type=checkbox]').on('change', resend); - $('div.tt').tooltip(); + $('div.tt').tooltip({ placement: 'top' }); + $('.control-group.data').on('click keypress', '.btn.rem', function () { + if($('.data .controls').length > 1) { + $(this).closest('.controls').remove(); + } + }); + $('.control-group.data').on('click keypress', '.btn.add', function () { + $(this).siblings('.controls').last().clone().insertBefore($(this)).find('input').val(''); + }); + $('.control-group.data').on('input', 'input', resend); resend(); }); diff --git a/demo/cabaret/templates/index.jinja2 b/demo/cabaret/templates/index.jinja2 index c0da29c..71a2704 100644 --- a/demo/cabaret/templates/index.jinja2 +++ b/demo/cabaret/templates/index.jinja2 @@ -23,14 +23,14 @@ -
+
-
- +
+ + +
+
@@ -43,12 +43,24 @@
+ +
+ +
+ +
+
{% for group, keys in configs | groupby('category') %}
{{ group }} - {% for key in keys if key.name not in ['js', 'css', 'style'] %} + {% for key in keys if key.name not in ['js', 'css', 'style', 'interpolate'] %} {% set doc = 'title="' + key.doc + '
' + key.subdoc + '"' %}
{% if key.is_boolean %} diff --git a/pygal/config.py b/pygal/config.py index 8816e7c..eca7f92 100644 --- a/pygal/config.py +++ b/pygal/config.py @@ -99,9 +99,7 @@ class Config(object): "It can be an absolute file path or an external link", str) - ############ Look ############ - title = Key( None, str, "Look", "Graph title.", "Leave it to None to disable title.") diff --git a/pygal/interpolate.py b/pygal/interpolate.py index bf47727..8711a69 100644 --- a/pygal/interpolate.py +++ b/pygal/interpolate.py @@ -28,10 +28,13 @@ try: except ImportError: pass +KINDS = ['cubic', 'univariate', 'quadratic', 'slinear', 'nearest', 'zero'] + def interpolation(x, y, kind): """Make the interpolation function""" - assert scipy != None, 'You must have scipy installed to use interpolation' + assert scipy is not None, ( + 'You must have scipy installed to use interpolation') order = None if len(y) < len(x): x = x[:len(y)] @@ -42,7 +45,7 @@ def interpolation(x, y, kind): return ident if isinstance(kind, int): order = kind - elif kind in ['zero', 'slinear', 'quadratic', 'cubic', 'univariate']: + elif kind in KINDS: order = {'nearest': 0, 'zero': 0, 'slinear': 1, 'quadratic': 2, 'cubic': 3, 'univariate': 3}[kind] if order and len(x) <= order: