Skip to content

Commit 8c4ec19

Browse files
committed
Add reverse_direction Gauge config parameter
Refactors #567
1 parent 721a587 commit 8c4ec19

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

docs/documentation/configuration/specific_options.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ half_pie
3030
pie_chart.add('Opera', 2.3)
3131

3232

33+
34+
reverse_direction
35+
-----------------
36+
37+
You can reverse the direction of the gauge by setting ``reverse_direction=True``:
38+
39+
.. pygal-code::
40+
41+
gauge_chart = pygal.Gauge(reverse_direction=True)
42+
gauge_chart.add('Chrome', 8212)
43+
gauge_chart.add('Firefox', 8099)
44+
gauge_chart.add('Opera', 2933)
45+
gauge_chart.add('IE', 41)
46+
47+
3348
inner_radius
3449
------------
3550

docs/documentation/types/gauge.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,18 @@ Gauge chart:
1515
gauge_chart.add('Firefox', 8099)
1616
gauge_chart.add('Opera', 2933)
1717
gauge_chart.add('IE', 41)
18+
19+
Reverse
20+
~~~~~~~
21+
22+
Gauge chart can be reversed:
23+
24+
.. pygal-code:: 600 550
25+
26+
gauge_chart = pygal.Gauge(human_readable=True, reverse_direction=True)
27+
gauge_chart.title = 'DeltaBlue V8 benchmark results'
28+
gauge_chart.range = [0, 10000]
29+
gauge_chart.add('Chrome', 8212)
30+
gauge_chart.add('Firefox', 8099)
31+
gauge_chart.add('Opera', 2933)
32+
gauge_chart.add('IE', 41)

pygal/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ class Config(CommonConfig):
324324

325325
half_pie = Key(False, bool, "Look", "Create a half-pie chart")
326326

327+
reverse_direction = Key(
328+
False, bool, "Look", "Reverse the direction of the gauge"
329+
)
330+
327331
x_labels = Key(
328332
None, list, "Label", "X labels, must have same len than data.",
329333
"Leave it to None to disable x labels display.", str

pygal/graph/gauge.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ def _compute(self):
132132
self.min_ -= 1
133133
self.max_ += 1
134134

135-
self._box.set_polar_box(0, 1, self.min_, self.max_)
135+
if self.reverse_direction:
136+
self._box.set_polar_box(0, 1, self.max_, self.min_)
137+
else:
138+
self._box.set_polar_box(0, 1, self.min_, self.max_)
136139

137140
def _compute_x_labels(self):
138141
pass

pygal/test/test_gauge.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from pygal import Gauge
2+
3+
4+
def test_gauge_reverse_direction():
5+
"""Test that reverse_direction config is respected"""
6+
gauge = Gauge(reverse_direction=True)
7+
assert gauge.config.reverse_direction is True
8+
9+
gauge.add('A', 10)
10+
gauge.range = (0, 10)
11+
gauge.setup()
12+
13+
# When reversed, the polar box should be (0, 1, max, min)
14+
# min=0, max=10
15+
# reversed: (0, 1, 10, 0)
16+
assert gauge._box._tmin == 10
17+
assert gauge._box._tmax == 0
18+
assert gauge._box._rmin == 0
19+
assert gauge._box._rmax == 1
20+
21+
def test_gauge_normal_direction():
22+
"""Test that normal direction works as expected"""
23+
gauge = Gauge()
24+
assert gauge.config.reverse_direction is False
25+
26+
gauge.add('A', 10)
27+
gauge.range = (0, 10)
28+
gauge.setup()
29+
30+
# min=0, max=10
31+
# normal: (0, 1, 0, 10)
32+
assert gauge._box._tmin == 0
33+
assert gauge._box._tmax == 10
34+
assert gauge._box._rmin == 0
35+
assert gauge._box._rmax == 1

0 commit comments

Comments
 (0)