Browse Source

Fix log

pull/8/head
Florian Mounier 12 years ago
parent
commit
7c30891274
  1. 2
      demo/moulinrouge/tests.py
  2. 2
      pygal/graph/bar.py
  3. 4
      pygal/graph/graph.py
  4. 4
      pygal/graph/horizontal.py
  5. 14
      pygal/graph/stackedbar.py
  6. 44
      pygal/view.py

2
demo/moulinrouge/tests.py

@ -132,6 +132,8 @@ def get_test_routes(app):
else:
graph.add('1', [.1, 10, .001, 1000000])
graph.add('2', [.234, 243, 2, 2981379, 1231])
graph.x_labels = ('a', 'b', 'c', 'd', 'e')
graph.x_label_rotation = 45
return graph.render_response()
@app.route('/test/zero_at_34/<chart>')

2
pygal/graph/bar.py

@ -62,7 +62,7 @@ class Bar(Graph):
"""Draw a bar graph for a serie"""
bars = self.svg.node(serie_node['plot'], class_="bars")
for i, (x, y) in enumerate(serie.points):
if None in (x, y):
if None in (x, y) or (self.logarithmic and y <= 0):
continue
metadata = serie.metadata.get(i)

4
pygal/graph/graph.py

@ -24,7 +24,7 @@ Commmon graphing functions
from __future__ import division
from pygal.interpolate import interpolation
from pygal.graph.base import BaseGraph
from pygal.view import View, XLogView, YLogView, XYLogView, HorizontalView
from pygal.view import View, LogView, XYLogView
from pygal.util import is_major, truncate, reverse_text_len
from math import isnan, pi, sqrt, ceil
@ -51,7 +51,7 @@ class Graph(BaseGraph):
if self.__class__.__name__ == 'XY':
view_class = XYLogView
else:
view_class = YLogView
view_class = LogView
else:
view_class = View

4
pygal/graph/horizontal.py

@ -21,7 +21,7 @@ Horizontal graph base
"""
from pygal.graph.graph import Graph
from pygal.view import HorizontalView, XLogView
from pygal.view import HorizontalView, HorizontalLogView
class HorizontalGraph(Graph):
@ -42,7 +42,7 @@ class HorizontalGraph(Graph):
def _set_view(self):
"""Assign a view to current graph"""
if self.logarithmic:
view_class = XLogView
view_class = HorizontalLogView
else:
view_class = HorizontalView

14
pygal/graph/stackedbar.py

@ -44,6 +44,10 @@ class StackedBar(Bar):
if val is not None and val < self.zero])
for vals in transposed]
if self.logarithmic:
positive_vals = filter(lambda x: x > 0, positive_vals)
negative_vals = filter(lambda x: x > 0, negative_vals)
positive_vals = positive_vals or [self.zero]
negative_vals = negative_vals or [self.zero]
@ -68,13 +72,13 @@ class StackedBar(Bar):
self.negative_cumulation = [0] * self._len
self.positive_cumulation = [0] * self._len
def _bar(self, parent, x, val, index, i, zero):
cumulation = (self.negative_cumulation if val < self.zero else
def _bar(self, parent, x, y, index, i, zero):
cumulation = (self.negative_cumulation if y < self.zero else
self.positive_cumulation)
zero = cumulation[i]
cumulation[i] = zero + val
cumulation[i] = zero + y
if zero == 0:
zero = self.zero
val -= self.zero
y -= self.zero
return super(StackedBar, self)._bar(
parent, x, zero + val, index, i, zero, False)
parent, x, zero + y, index, i, zero, False)

44
pygal/view.py

@ -150,7 +150,7 @@ class PolarView(View):
(rho * cos(theta), rho * sin(theta)))
class YLogView(View):
class LogView(View):
"""Logarithmic projection """
# Do not want to call the parent here
# pylint: disable-msg=W0231
@ -158,8 +158,6 @@ class YLogView(View):
self.width = width
self.height = height
self.box = box
self.ymin = self.box.ymin
self.ymax = self.box.ymax
self.log10_ymax = log10(self.box.ymax)
self.log10_ymin = log10(self.box.ymin)
self.box.fix(False)
@ -182,8 +180,6 @@ class XLogView(View):
self.width = width
self.height = height
self.box = box
self.xmin = self.box.xmin
self.xmax = self.box.xmax
self.log10_xmax = log10(self.box.xmax)
self.log10_xmin = log10(self.box.xmin)
self.box.fix(False)
@ -193,22 +189,48 @@ class XLogView(View):
"""Project x"""
if x is None or x <= 0 or self.log10_xmax - self.log10_xmin == 0:
return None
return (self.width - self.width *
return (self.width *
(log10(x) - self.log10_xmin)
/ (self.log10_xmax - self.log10_xmin))
class XYLogView(XLogView, YLogView):
class XYLogView(XLogView, LogView):
def __init__(self, width, height, box):
self.width = width
self.height = height
self.box = box
self.xmin = self.box.xmin
self.xmax = self.box.xmax
self.ymin = self.box.ymin
self.ymax = self.box.ymax
self.log10_ymax = log10(self.box.ymax)
self.log10_ymin = log10(self.box.ymin)
self.log10_xmax = log10(self.box.xmax)
self.log10_xmin = log10(self.box.xmin)
self.box.fix(False)
class HorizontalLogView(XLogView):
"""Logarithmic projection """
# Do not want to call the parent here
# pylint: disable-msg=W0231
def __init__(self, width, height, box):
self._force_vertical = None
self.width = width
self.height = height
self.box = box
self.log10_xmax = log10(self.box.ymax)
self.log10_xmin = log10(self.box.ymin)
self.box.fix(False)
self.box.swap()
def x(self, x):
"""Project x"""
if x is None:
return None
if self._force_vertical:
return super(HorizontalLogView, self).x(x)
return super(XLogView, self).y(x)
def y(self, y):
if y is None:
return None
if self._force_vertical:
return super(XLogView, self).y(y)
return super(HorizontalLogView, self).x(y)

Loading…
Cancel
Save