Skip to content

Commit

Permalink
* Fix range option for some Charts (Fix Kozea#297 Fix Kozea#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Feb 25, 2016
1 parent a6835fa commit 7430ac7
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 10 deletions.
7 changes: 7 additions & 0 deletions demo/moulinrouge/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ def test_zero_at_34_for(chart, zero=34):
graph.add('2', [73, -14, 10, None, -58, 32, 91])
return graph.render_response()

@app.route('/test/range/<chart>')
def test_range_for(chart):
graph = CHARTS_BY_NAME[chart]()
graph.range = [0, 100]
graph.add('1', [1, 2, 10])
return graph.render_response()

@app.route('/test/fill_with_none/')
def test_fill_with_none():
graph = XY(fill=True)
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Changelog
* Add horizontal line charts (thanks @chartique #301)
* There is now a `formatter` config option to format values as specified. The formatter callable may or may not take `chart`, `serie` and `index` as argument. The default value formatting is now chart dependent and is value_formatter for most graph but could be a combination of value_formatter and x_value_formatter for dual charts.
* The `human_readable` option has been removed. Now you have to use the pygal.formatters.human_readable formatter (value_formatter=human_readable instead of human_readable=True)
* New chart type: SolidGauge (thanks @chartique #295)
* Fix range option for some Charts (#297 #298)

2.1.1
=====
Expand Down
6 changes: 6 additions & 0 deletions pygal/graph/funnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ def _compute(self):
self._box.ymin = -val_max
self._box.ymax = val_max

if self.range and self.range[0] is not None:
self._box.ymin = self.range[0]

if self.range and self.range[1] is not None:
self._box.ymax = self.range[1]

def _compute_x_labels(self):
self._x_labels = list(
zip(self.x_labels and
Expand Down
6 changes: 6 additions & 0 deletions pygal/graph/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,9 @@ def _compute(self):
self._box.xmin, self._box.xmax = xmin, xmax
if yrng:
self._box.ymin, self._box.ymax = ymin, ymax

if self.range and self.range[0] is not None:
self._box.ymin = self.range[0]

if self.range and self.range[1] is not None:
self._box.ymax = self.range[1]
16 changes: 13 additions & 3 deletions pygal/graph/pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,19 @@ def _get_separated_values(self, secondary=False):

def _compute_box(self, positive_vals, negative_vals):
"""Compute Y min and max"""
self._box.ymax = max(max(positive_vals or [self.zero]),
max(negative_vals or [self.zero]))
self._box.ymin = - self._box.ymax
max_ = max(
max(positive_vals or [self.zero]),
max(negative_vals or [self.zero]))

if self.range and self.range[0] is not None:
self._box.ymin = self.range[0]
else:
self._box.ymin = - max_

if self.range and self.range[1] is not None:
self._box.ymax = self.range[1]
else:
self._box.ymax = max_

def _pre_compute_secondary(self, positive_vals, negative_vals):
"""Compute secondary y min and max"""
Expand Down
14 changes: 10 additions & 4 deletions pygal/graph/stackedbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ def _get_separated_values(self, secondary=False):

def _compute_box(self, positive_vals, negative_vals):
"""Compute Y min and max"""
self._box.ymin = negative_vals and min(
min(negative_vals), self.zero) or self.zero
self._box.ymax = positive_vals and max(
max(positive_vals), self.zero) or self.zero
if self.range and self.range[0] is not None:
self._box.ymin = self.range[0]
else:
self._box.ymin = negative_vals and min(
min(negative_vals), self.zero) or self.zero
if self.range and self.range[1] is not None:
self._box.ymax = self.range[1]
else:
self._box.ymax = positive_vals and max(
max(positive_vals), self.zero) or self.zero

def _compute(self):
"""Compute y min and max and y scale and set labels"""
Expand Down
17 changes: 17 additions & 0 deletions pygal/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,23 @@ def test_x_y_title(Chart):
assert len(q('.titles .title')) == 3


def test_range(Chart):
"""Test y label major option"""
if Chart in (
Pie, Treemap, Dot, SolidGauge
) or issubclass(Chart, BaseMap):
return
chart = Chart()
chart.range = (0, 100)
chart.add('', [1, 2, 10])
q = chart.render_pyquery()
axis = map(str, range(0, 101, 10))
if Chart == Radar:
axis = map(str, range(100, -1, -20))
z = 'x' if getattr(chart, 'horizontal', False) or Chart == Gauge else 'y'
assert [t.text for t in q('.axis.%s .guides text' % z)] == list(axis)


def test_x_label_major(Chart):
"""Test x label major option"""
if Chart in (
Expand Down
6 changes: 3 additions & 3 deletions pygal/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def xmin(self):
@xmin.setter
def xmin(self, value):
"""X minimum setter"""
if value:
if value is not None:
self._xmin = value

@property
Expand All @@ -90,7 +90,7 @@ def ymin(self):
@ymin.setter
def ymin(self, value):
"""Y minimum setter"""
if value:
if value is not None:
self._ymin = value

@property
Expand All @@ -101,7 +101,7 @@ def xmax(self):
@xmax.setter
def xmax(self, value):
"""X maximum setter"""
if value:
if value is not None:
self._xmax = value

@property
Expand Down

0 comments on commit 7430ac7

Please sign in to comment.