Browse Source

Work on solidgauge PR

pull/307/head
Florian Mounier 9 years ago
parent
commit
013d9c7447
  1. 18
      demo/moulinrouge/tests.py
  2. 93
      pygal/graph/solidgauge.py
  3. 3
      pygal/svg.py

18
demo/moulinrouge/tests.py

@ -4,7 +4,7 @@ from pygal import (
Bar, Gauge, Pyramid, Funnel, Dot, StackedBar, StackedLine, XY, Bar, Gauge, Pyramid, Funnel, Dot, StackedBar, StackedLine, XY,
CHARTS_BY_NAME, Config, Line, Histogram, Box, CHARTS_BY_NAME, Config, Line, Histogram, Box,
Pie, Treemap, TimeLine, DateLine, Radar, HorizontalBar, Pie, Treemap, TimeLine, DateLine, Radar, HorizontalBar,
DateTimeLine) DateTimeLine, SolidGauge)
try: try:
from pygal.maps import world from pygal.maps import world
@ -202,17 +202,21 @@ def get_test_routes(app):
] ]
return gauge.render_response() return gauge.render_response()
@app.route('/test/solidguage/') @app.route('/test/solidgauge/')
def test_solidguage(): def test_solidgauge():
gauge = SolidGauge(half_pie=True, gauge = SolidGauge(half_pie=True,
inner_radius=0.70, inner_radius=0.70,
print_values=True, print_values=True,
human_readable=True) human_readable=True)
gauge.title = 'Hello World!' gauge.title = 'Hello World!'
gauge.value_formatter = lambda x: '${}'.format(x.values[0]) if x.title in ['Series 1'] else '{}%'.format(x.values[0]) percent_formatter = lambda x: '{}%'.format(x)
gauge.add('Series 1', [{'value': 225000, 'maxvalue': 1275000}]) dollar_formatter = lambda x: '{}$'.format(x)
gauge.add('Series 2', [{'value': 81, 'maxvalue': 100}]) gauge.value_formatter = percent_formatter
gauge.add('Series 3', [{'value': 3, 'maxvalue': 100}])
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 4', [{'value': 51, 'maxvalue': 100}])
gauge.add('Series 5', [{'value': 79, 'maxvalue': 100}]) gauge.add('Series 5', [{'value': 79, 'maxvalue': 100}])
gauge.add('Series 6', [{'value': 99, 'maxvalue': 100}]) gauge.add('Series 6', [{'value': 99, 'maxvalue': 100}])

93
pygal/graph/solidgauge.py

@ -1,58 +1,75 @@
# -*- coding: utf-8 -*-
# This file is part of pygal
#
from math import pi, sqrt, sin, cos # 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 <http://www.gnu.org/licenses/>.
"""
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.graph.graph import Graph
from pygal.util import alter, decorate from pygal.util import alter, decorate
class SolidGauge(Graph):
""" class SolidGauge(Graph):
Solid Guage
For each series a solid guage is shown on the plot area.
See http://en.wikipedia.org/wiki/#####
"""
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) serie_node = self.svg.serie(serie)
metadata = serie.metadata.get(0) metadata = serie.metadata.get(0) or {}
if 'maxvalue' in metadata.keys(): maxvalue = metadata.get('maxvalue', 100)
maxvalue = metadata['maxvalue']
else:
maxvalue = serie.values[0] * 1.5
if self.half_pie: if self.half_pie:
center = ((current_square[1]*sq_dimensions[0]) - (sq_dimensions[0] / 2.), center = (
(current_square[0]*sq_dimensions[1]) - (sq_dimensions[1] / 4)) (current_square[1]*sq_dimensions[0]) - (sq_dimensions[0] / 2.),
(current_square[0]*sq_dimensions[1]) - (sq_dimensions[1] / 4))
else: else:
center = ((current_square[1]*sq_dimensions[0]) - (sq_dimensions[0] / 2.), center = (
(current_square[0]*sq_dimensions[1]) - (sq_dimensions[1] / 2.)) (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]) radius = min([sq_dimensions[0]/2, sq_dimensions[1]/2])
value = serie.values[0] value = serie.values[0]
ratio = value / maxvalue
ratio = min(value, maxvalue) / maxvalue
if self.half_pie: if self.half_pie:
angle = 2 * pi * ratio / 2 angle = 2 * pi * ratio / 2
endangle = pi / 2 endangle = pi / 2
else: else:
angle = 2 * pi * ratio angle = 2 * pi * ratio
endangle = 2 * pi endangle = 2 * pi
value = self._format(serie) value = self._format(value)
gauge_ = decorate( gauge_ = decorate(
self.svg, self.svg,
self.svg.node(serie_node['plot'], class_="gauge"), self.svg.node(serie_node['plot'], class_="gauge"),
metadata) metadata)
big_radius = radius * .9 big_radius = radius * .9
small_radius = radius * serie.inner_radius small_radius = radius * serie.inner_radius
maxvalue = Maxvalue(serie)
print(maxvalue.values)
alter(self.svg.solidgauge( alter(self.svg.solidgauge(
serie_node, gauge_, big_radius, small_radius, 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): def _compute_x_labels(self):
pass pass
@ -71,7 +88,8 @@ class SolidGauge(Graph):
for index, serie in enumerate(self.series): for index, serie in enumerate(self.series):
current_square = self._current_square(squares, index) 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): def _squares(self):
@ -110,20 +128,5 @@ class SolidGauge(Graph):
current_square[0] += 1 current_square[0] += 1
else: else:
return tuple(current_square) return tuple(current_square)
return print('Something went wrong with the current square assignment.') raise Exception(
'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

3
pygal/svg.py

@ -298,7 +298,8 @@ class Svg(object):
def solidgauge( def solidgauge(
self, serie_node, node, radius, small_radius, 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""" """Draw a solid gauge slice and background slice"""
project = lambda rho, alpha: ( project = lambda rho, alpha: (
rho * sin(-alpha), rho * cos(-alpha)) rho * sin(-alpha), rho * cos(-alpha))

Loading…
Cancel
Save