Browse Source

Fix show_major_dots + coverage

pull/130/head
Florian Mounier 11 years ago
parent
commit
118bad0093
  1. 4
      demo/moulinrouge/tests.py
  2. 44
      pygal/graph/base.py
  3. 44
      pygal/graph/graph.py
  4. 7
      pygal/graph/histogram.py
  5. 28
      pygal/graph/line.py
  6. 33
      pygal/test/test_histogram.py
  7. 27
      pygal/test/test_line.py

4
demo/moulinrouge/tests.py

@ -429,12 +429,12 @@ def get_test_routes(app):
@app.route('/test/major_dots')
def test_major_dots():
line = Line(x_labels_major_every=3, show_only_major_dots=True)
line = Line(x_labels_major_count=2, show_only_major_dots=True)
line.add('test', range(12))
line.x_labels = [
'lol', 'lol1', 'lol2', 'lol3', 'lol4', 'lol5',
'lol6', 'lol7', 'lol8', 'lol9', 'lol10', 'lol11']
line.x_labels_major = ['lol3']
# line.x_labels_major = ['lol3']
return line.render_response()
@app.route('/test/x_major_labels/<chart>')

44
pygal/graph/base.py

@ -26,7 +26,7 @@ from pygal.view import Margin, Box
from pygal.util import (
get_text_box, get_texts_box, cut, rad, humanize, truncate, split_title)
from pygal.svg import Svg
from pygal.util import cached_property
from pygal.util import cached_property, majorize
from math import sin, cos, sqrt
@ -227,6 +227,48 @@ class BaseGraph(object):
"""Getter for the number of series"""
return len(self.all_series)
@cached_property
def _x_major_labels(self):
"""Getter for the x major label"""
if self.x_labels_major:
return self.x_labels_major
if self.x_labels_major_every:
return [self._x_labels[i][0] for i in range(
0, len(self._x_labels), self.x_labels_major_every)]
if self.x_labels_major_count:
label_count = len(self._x_labels)
major_count = self.x_labels_major_count
if (major_count >= label_count):
return [label[0] for label in self._x_labels]
return [self._x_labels[
int(i * (label_count - 1) / (major_count - 1))][0]
for i in range(major_count)]
return []
@cached_property
def _y_major_labels(self):
"""Getter for the y major label"""
if self.y_labels_major:
return self.y_labels_major
if self.y_labels_major_every:
return [self._y_labels[i][1] for i in range(
0, len(self._y_labels), self.y_labels_major_every)]
if self.y_labels_major_count:
label_count = len(self._y_labels)
major_count = self.y_labels_major_count
if (major_count >= label_count):
return [label[1] for label in self._y_labels]
return [self._y_labels[
int(i * (label_count - 1) / (major_count - 1))][1]
for i in range(major_count)]
return majorize(
cut(self._y_labels, 1)
)
def _draw(self):
"""Draw all the things"""
self._compute()

44
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 (
majorize, truncate, reverse_text_len, get_texts_box, cut, rad, decorate)
truncate, reverse_text_len, get_texts_box, cut, rad, decorate)
from math import sqrt, ceil, cos
from itertools import repeat, chain
@ -142,25 +142,9 @@ class Graph(BaseGraph):
d='M%f %f v%f' % (0, 0, self.view.height),
class_='line')
lastlabel = self._x_labels[-1][0]
if self.x_labels_major:
x_labels_major = self.x_labels_major
elif self.x_labels_major_every:
x_labels_major = [self._x_labels[i][0] for i in range(
0, len(self._x_labels), self.x_labels_major_every)]
elif self.x_labels_major_count:
label_count = len(self._x_labels)
major_count = self.x_labels_major_count
if (major_count >= label_count):
x_labels_major = [label[0] for label in self._x_labels]
else:
x_labels_major = [self._x_labels[
int(i * (label_count - 1) / (major_count - 1))][0]
for i in range(major_count)]
else:
x_labels_major = []
for label, position in self._x_labels:
major = label in x_labels_major
major = label in self._x_major_labels
if not (self.show_minor_x_labels or major):
continue
guides = self.svg.node(axis, class_='guides')
@ -197,7 +181,7 @@ class Graph(BaseGraph):
' always_show' if self.show_x_guides else ''
))
for label, position in self._x_2nd_labels:
major = label in x_labels_major
major = label in self._x_major_labels
if not (self.show_minor_x_labels or major):
continue
# it is needed, to have the same structure as primary axis
@ -230,26 +214,8 @@ class Graph(BaseGraph):
class_='line'
)
if self.y_labels_major:
y_labels_major = self.y_labels_major
elif self.y_labels_major_every:
y_labels_major = [self._y_labels[i][1] for i in range(
0, len(self._y_labels), self.y_labels_major_every)]
elif self.y_labels_major_count:
label_count = len(self._y_labels)
major_count = self.y_labels_major_count
if (major_count >= label_count):
y_labels_major = [label[1] for label in self._y_labels]
else:
y_labels_major = [self._y_labels[
int(i * (label_count - 1) / (major_count - 1))][1]
for i in range(major_count)]
else:
y_labels_major = majorize(
cut(self._y_labels, 1)
)
for label, position in self._y_labels:
major = position in y_labels_major
major = position in self._y_major_labels
if not (self.show_minor_y_labels or major):
continue
guides = self.svg.node(axis, class_='%sguides' % (
@ -285,7 +251,7 @@ class Graph(BaseGraph):
secondary_ax = self.svg.node(
self.nodes['plot'], class_="axis y2")
for label, position in self._y_2nd_labels:
major = position in y_labels_major
major = position in self._y_major_labels
if not (self.show_minor_x_labels or major):
continue
# it is needed, to have the same structure as primary axis

7
pygal/graph/histogram.py

@ -32,13 +32,6 @@ class Histogram(Graph):
_dual = True
_series_margin = 0
@cached_property
def _values(self):
"""Getter for secondary series values (flattened)"""
return [val[0]
for serie in self.series
for val in serie.values
if val[0] is not None]
@cached_property
def _secondary_values(self):

28
pygal/graph/line.py

@ -67,30 +67,14 @@ class Line(Graph):
points = serie.points
view_values = list(map(self.view, points))
if serie.show_dots:
if serie.show_only_major_dots:
major_dots_index = []
if self.x_labels:
if self.x_labels_major:
major_dots_index = []
for major in self.x_labels_major:
start = -1
while True:
try:
index = self.x_labels.index(
major, start + 1)
except ValueError:
break
else:
major_dots_index.append(index)
start = index
elif self.x_labels_major_every:
major_dots_index = range(
0, len(self.x_labels), self.x_labels_major_every)
for i, (x, y) in enumerate(view_values):
if None in (x, y) or (serie.show_only_major_dots
and i not in major_dots_index):
if None in (x, y):
continue
if (serie.show_only_major_dots and
self.x_labels and i < len(self.x_labels) and
self.x_labels[i] not in self._x_major_labels):
continue
metadata = serie.metadata.get(i)
classes = []
if x > self.view.width / 2:

33
pygal/test/test_histogram.py

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
# This file is part of pygal
#
# A python svg graph plotting library
# Copyright © 2012-2014 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/>.
from pygal import Histogram
def test_histogram():
hist = Histogram()
hist.add('1', [
(2, 0, 1),
(4, 1, 3),
(3, 3.5, 5),
(1.5, 5, 10)
])
hist.add('2', [(2, 2, 8)], secondary=True)
q = hist.render_pyquery()
assert len(q('.rect')) == 5

27
pygal/test/test_line.py

@ -94,7 +94,7 @@ def test_not_equal_x_labels():
'7', '8', '9', '10']
def test_only_major_dots():
def test_only_major_dots_every():
line = Line(show_only_major_dots=True, x_labels_major_every=3)
line.add('test', range(12))
line.x_labels = map(str, range(12))
@ -102,6 +102,31 @@ def test_only_major_dots():
assert len(q(".dots")) == 4
def test_only_major_dots_no_labels():
line = Line(show_only_major_dots=True)
line.add('test', range(12))
q = line.render_pyquery()
assert len(q(".dots")) == 12
def test_only_major_dots_count():
line = Line(show_only_major_dots=True)
line.add('test', range(12))
line.x_labels = map(str, range(12))
line.x_labels_major_count = 2
q = line.render_pyquery()
assert len(q(".dots")) == 2
def test_only_major_dots():
line = Line(show_only_major_dots=True,)
line.add('test', range(12))
line.x_labels = map(str, range(12))
line.x_labels_major = ['1', '5', '11']
q = line.render_pyquery()
assert len(q(".dots")) == 3
def test_line_secondary():
line = Line()
rng = [8, 12, 23, 73, 39, 57]

Loading…
Cancel
Save