From 013d9c74475b24a9aa488f593eb01a943f35a4cf Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Tue, 23 Feb 2016 16:54:10 +0100 Subject: [PATCH] Work on solidgauge PR --- demo/moulinrouge/tests.py | 18 +++++--- pygal/graph/solidgauge.py | 93 ++++++++++++++++++++------------------- pygal/svg.py | 3 +- 3 files changed, 61 insertions(+), 53 deletions(-) diff --git a/demo/moulinrouge/tests.py b/demo/moulinrouge/tests.py index fac7f7b..93b2da4 100644 --- a/demo/moulinrouge/tests.py +++ b/demo/moulinrouge/tests.py @@ -4,7 +4,7 @@ from pygal import ( Bar, Gauge, Pyramid, Funnel, Dot, StackedBar, StackedLine, XY, CHARTS_BY_NAME, Config, Line, Histogram, Box, Pie, Treemap, TimeLine, DateLine, Radar, HorizontalBar, - DateTimeLine) + DateTimeLine, SolidGauge) try: from pygal.maps import world @@ -202,17 +202,21 @@ def get_test_routes(app): ] return gauge.render_response() - @app.route('/test/solidguage/') - def test_solidguage(): + @app.route('/test/solidgauge/') + def test_solidgauge(): gauge = SolidGauge(half_pie=True, inner_radius=0.70, print_values=True, human_readable=True) gauge.title = 'Hello World!' - gauge.value_formatter = lambda x: '${}'.format(x.values[0]) if x.title in ['Series 1'] else '{}%'.format(x.values[0]) - gauge.add('Series 1', [{'value': 225000, 'maxvalue': 1275000}]) - gauge.add('Series 2', [{'value': 81, 'maxvalue': 100}]) - gauge.add('Series 3', [{'value': 3, 'maxvalue': 100}]) + percent_formatter = lambda x: '{}%'.format(x) + dollar_formatter = lambda x: '{}$'.format(x) + gauge.value_formatter = percent_formatter + + gauge.add('Series 1', [{'value': 225000, 'maxvalue': 1275000}], + value_formatter=dollar_formatter) + gauge.add('Series 2', [{'value': 110, 'maxvalue': 100}]) + gauge.add('Series 3', [{'value': 3}]) gauge.add('Series 4', [{'value': 51, 'maxvalue': 100}]) gauge.add('Series 5', [{'value': 79, 'maxvalue': 100}]) gauge.add('Series 6', [{'value': 99, 'maxvalue': 100}]) diff --git a/pygal/graph/solidgauge.py b/pygal/graph/solidgauge.py index 65cddfa..e55488b 100644 --- a/pygal/graph/solidgauge.py +++ b/pygal/graph/solidgauge.py @@ -1,58 +1,75 @@ - - - -from math import pi, sqrt, sin, cos +# -*- coding: utf-8 -*- +# This file is part of pygal +# +# A python svg graph plotting library +# Copyright © 2012-2015 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 . + + +""" +Solid Guage +For each series a solid guage is shown on the plot area. +""" +from __future__ import division + + +from math import pi, sqrt from pygal.graph.graph import Graph from pygal.util import alter, decorate -class SolidGauge(Graph): - """ - Solid Guage - For each series a solid guage is shown on the plot area. - - See http://en.wikipedia.org/wiki/##### - """ +class SolidGauge(Graph): - def gaugify(self, serie, startangle, squares, sq_dimensions, current_square): + def gaugify( + self, serie, startangle, squares, sq_dimensions, current_square): serie_node = self.svg.serie(serie) - metadata = serie.metadata.get(0) - if 'maxvalue' in metadata.keys(): - maxvalue = metadata['maxvalue'] - else: - maxvalue = serie.values[0] * 1.5 + metadata = serie.metadata.get(0) or {} + maxvalue = metadata.get('maxvalue', 100) if self.half_pie: - center = ((current_square[1]*sq_dimensions[0]) - (sq_dimensions[0] / 2.), - (current_square[0]*sq_dimensions[1]) - (sq_dimensions[1] / 4)) + center = ( + (current_square[1]*sq_dimensions[0]) - (sq_dimensions[0] / 2.), + (current_square[0]*sq_dimensions[1]) - (sq_dimensions[1] / 4)) else: - center = ((current_square[1]*sq_dimensions[0]) - (sq_dimensions[0] / 2.), - (current_square[0]*sq_dimensions[1]) - (sq_dimensions[1] / 2.)) + center = ( + (current_square[1]*sq_dimensions[0]) - (sq_dimensions[0] / 2.), + (current_square[0]*sq_dimensions[1]) - (sq_dimensions[1] / 2.)) radius = min([sq_dimensions[0]/2, sq_dimensions[1]/2]) value = serie.values[0] - ratio = value / maxvalue + + ratio = min(value, maxvalue) / maxvalue if self.half_pie: angle = 2 * pi * ratio / 2 endangle = pi / 2 else: angle = 2 * pi * ratio endangle = 2 * pi - value = self._format(serie) + value = self._format(value) gauge_ = decorate( self.svg, self.svg.node(serie_node['plot'], class_="gauge"), metadata) - big_radius = radius * .9 small_radius = radius * serie.inner_radius - maxvalue = Maxvalue(serie) - print(maxvalue.values) alter(self.svg.solidgauge( serie_node, gauge_, big_radius, small_radius, - angle, startangle, center, value, 0, metadata, self.half_pie, endangle, self._format(maxvalue)), metadata) + angle, startangle, center, value, 0, metadata, + self.half_pie, endangle, self._format(maxvalue)), metadata) def _compute_x_labels(self): pass @@ -71,7 +88,8 @@ class SolidGauge(Graph): for index, serie in enumerate(self.series): current_square = self._current_square(squares, index) - self.gaugify(serie, startangle, squares, sq_dimensions, current_square) + self.gaugify( + serie, startangle, squares, sq_dimensions, current_square) def _squares(self): @@ -110,20 +128,5 @@ class SolidGauge(Graph): current_square[0] += 1 else: return tuple(current_square) - return print('Something went wrong with the current square assignment.') - -class Maxvalue(SolidGauge): - - def __init__(self, serie): - self.serie = serie - - @property - def values(self): - try: - return [self.serie.metadata[0]['maxvalue']] - except: - return [self.serie.values[0] * 1.5] - - @property - def title(self): - return self.serie.title + raise Exception( + 'Something went wrong with the current square assignment.') diff --git a/pygal/svg.py b/pygal/svg.py index 2b5264d..1187a9f 100644 --- a/pygal/svg.py +++ b/pygal/svg.py @@ -298,7 +298,8 @@ class Svg(object): def solidgauge( self, serie_node, node, radius, small_radius, - angle, start_angle, center, val, i, metadata, half_pie, endangle, maxvalue): + angle, start_angle, center, val, i, metadata, half_pie, endangle, + maxvalue): """Draw a solid gauge slice and background slice""" project = lambda rho, alpha: ( rho * sin(-alpha), rho * cos(-alpha))