Browse Source

Commit of empty majorize function with tests

pull/98/head
Florian Mounier 11 years ago
parent
commit
5ed5a1da1c
  1. 4
      pygal/config.py
  2. 12
      pygal/graph/graph.py
  3. 7
      pygal/graph/radar.py
  4. 19
      pygal/test/test_util.py
  5. 6
      pygal/util.py

4
pygal/config.py

@ -204,6 +204,10 @@ class Config(object):
"You can specify explicit y labels",
"Must be a list of numbers", float)
y_labels_use_major = Key(
True, bool, "Label",
"Whether to promote some labels majors.")
show_y_labels = Key(
True, bool, "Label", "Set to false to hide y-labels")

12
pygal/graph/graph.py

@ -26,7 +26,7 @@ from pygal.interpolate import INTERPOLATIONS
from pygal.graph.base import BaseGraph
from pygal.view import View, LogView, XYLogView
from pygal.util import (
is_major, truncate, reverse_text_len, get_texts_box, cut, rad, decorate)
majorize, truncate, reverse_text_len, get_texts_box, cut, rad, decorate)
from math import sqrt, ceil, cos
from itertools import repeat, chain
@ -228,8 +228,11 @@ class Graph(BaseGraph):
d='M%f %f h%f' % (0, self.view.height, self.view.width),
class_='line'
)
majors = majorize(
cut(self._y_labels, 1)
) if self.y_labels_use_major else []
for label, position in self._y_labels:
major = is_major(position)
major = position in majors
guides = self.svg.node(axis, class_='%sguides' % (
'logarithmic ' if self.logarithmic else ''
))
@ -262,8 +265,11 @@ class Graph(BaseGraph):
if self._y_2nd_labels:
secondary_ax = self.svg.node(
self.nodes['plot'], class_="axis y2")
majors = majorize(
cut(self._y_2nd_labels, 1)
) if self.y_labels_use_major else []
for label, position in self._y_2nd_labels:
major = is_major(position)
major = position in majors
# it is needed, to have the same structure as primary axis
guides = self.svg.node(secondary_ax, class_='guides')
x = self.view.width + 5

7
pygal/graph/radar.py

@ -25,7 +25,7 @@ from __future__ import division
from pygal.graph.line import Line
from pygal.adapters import positive, none_to_zero
from pygal.view import PolarView, PolarLogView
from pygal.util import deg, cached_property, compute_scale, is_major
from pygal.util import deg, cached_property, compute_scale, majorize, cut
from math import cos, pi
@ -118,9 +118,12 @@ class Radar(Line):
return
axis = self.svg.node(self.nodes['plot'], class_="axis y web")
majors = majorize(
cut(self._y_labels, 1)
) if self.y_labels_use_major else []
for label, r in reversed(self._y_labels):
major = is_major(r)
major = r in majors
guides = self.svg.node(axis, class_='guides')
self.svg.line(
guides, [self.view((r, theta)) for theta in self.x_pos],

19
pygal/test/test_util.py

@ -19,7 +19,7 @@
from pygal._compat import u
from pygal.util import (
round_to_int, round_to_float, _swap_curly, template, humanize,
is_major, truncate, minify_css)
truncate, minify_css, majorize)
from pytest import raises
@ -108,13 +108,6 @@ def test_humanize():
assert humanize(-.000000042) == '-42n'
def test_is_major():
for n in (0, 1, 1000, 10., 0.1, 0.000001, -10, -.001000, -100.):
assert is_major(n)
for n in (2, 10002., 100000.0003, -200, -0.0005):
assert not is_major(n)
def test_truncate():
assert truncate('1234567890', 50) == '1234567890'
assert truncate('1234567890', 5) == u('1234…')
@ -144,3 +137,13 @@ def test_minify_css():
assert minify_css(css) == (
'.title{font-family:sans;font-size:12}'
'.legends .legend text{font-family:monospace;font-size:14}')
def test_major():
assert majorize((0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1)) == [0, .5, 1]
assert majorize((0, .2, .4, .6, .8, 1)) == [0, 1]
assert majorize((0, .2, .4, .6, .8, 1, 1.2, 1.4, 1.6, 1.8, 2)) == [0, 1, 2]
assert majorize((0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120)) == [0, 50, 100]
assert majorize((0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36)) == [0, 10, 20, 30]
assert majorize((0, 1, 2, 3, 4, 5)) == [0, 5]
assert majorize((1, 10, 100, 1000, 10000, 100000)) == []

6
pygal/util.py

@ -47,9 +47,9 @@ def humanize(number):
human_readable[int(order) - int(order > 0)])
def is_major(number):
"""Returns True if number is a round order: 1, 100, 0.001"""
return not number or 10 ** floor(log10(abs(number))) == abs(number)
def majorize(s):
"""Filter s sequence te return only major considered numbers"""
return []
def round_to_int(number, precision):

Loading…
Cancel
Save