Browse Source

Trying to fix scale

pull/8/head
Florian Mounier 13 years ago
parent
commit
1001cdeff5
  1. 11
      demo/moulinrouge/__init__.py
  2. 4
      pygal/bar.py
  3. 20
      pygal/base.py
  4. 5
      pygal/config.py
  5. 4
      pygal/test/test_bar.py
  6. 12
      pygal/test/test_line.py
  7. 1
      pygal/util.py

11
demo/moulinrouge/__init__.py

@ -9,6 +9,7 @@ from pygal.bar import Bar
from pygal.config import Config from pygal.config import Config
from pygal.style import styles from pygal.style import styles
# from pygal.pie import Pie # from pygal.pie import Pie
import math
import string import string
import random import random
@ -21,8 +22,8 @@ def random_label():
random.randrange(4, 30))]) random.randrange(4, 30))])
def random_value(): def random_value(min=0, max=15):
return random.randrange(0, 15, 1) return random.randrange(min, max, 1)
# def generate_vbar(**opts): # def generate_vbar(**opts):
@ -50,11 +51,15 @@ def create_app():
@app.route("/all-<type>-<style>.svg") @app.route("/all-<type>-<style>.svg")
def all_svg(type, style): def all_svg(type, style):
data = random.randrange(1, 10) data = random.randrange(1, 10)
order = random.randrange(1, 10)
max = 10 ** order
min = 10 ** random.randrange(0, order)
config = Config() config = Config()
config.width = 600 config.width = 600
config.height = 400 config.height = 400
config.style = styles[style] config.style = styles[style]
config.x_labels = [random_label() for i in range(data)] config.x_labels = [random_label() for i in range(data)]
config.title = "%d - %d" % (min, max)
if type == 'bar': if type == 'bar':
g = Bar(config) g = Bar(config)
# elif type == 'hbar': # elif type == 'hbar':
@ -68,7 +73,7 @@ def create_app():
return return
for i in range(random.randrange(1, 10)): for i in range(random.randrange(1, 10)):
values = [random_value() for i in range(data)] values = [random_value(min, max) for i in range(data)]
g.add(random_label(), values) g.add(random_label(), values)
return Response(g.render(), mimetype='image/svg+xml') return Response(g.render(), mimetype='image/svg+xml')

4
pygal/bar.py

@ -5,8 +5,8 @@ class Bar(BaseGraph):
"""Bar graph""" """Bar graph"""
def _draw(self): def _draw(self):
ymin, ymax = 0, max([ vals = [val for serie in self.series for val in serie.values]
val for serie in self.series for val in serie.values]) ymin, ymax = min(min(vals), 0), max(vals)
x_step = len(self.series[0].values) x_step = len(self.series[0].values)
x_pos = [x / float(x_step) for x in range(x_step + 1) x_pos = [x / float(x_step) for x in range(x_step + 1)
] if x_step > 1 else [0, 1] # Center if only one value ] if x_step > 1 else [0, 1] # Center if only one value

20
pygal/base.py

@ -1,7 +1,8 @@
from pygal import Serie, Label, Margin from pygal import Serie, Margin
from pygal.util import round_to_scale from pygal.util import round_to_scale
from pygal.svg import Svg from pygal.svg import Svg
from pygal.config import Config from pygal.config import Config
import math
class BaseGraph(object): class BaseGraph(object):
@ -19,14 +20,19 @@ class BaseGraph(object):
return object.__getattribute__(self, attr) return object.__getattribute__(self, attr)
def _y_pos(self, ymin, ymax): def _y_pos(self, ymin, ymax):
step = (ymax - ymin) / float(self.max_scale_step) order = round(math.log10(ymax)) - 1
position = ymin if (ymax - ymin) / float(10 ** order) < 4:
if not step: order -= 1
return [position] step = 10 ** order
positions = set() positions = set()
while position < (ymax + step): position = round_to_scale(ymin, step)
positions.add(round_to_scale(position, self.scale)) while position < ymax:
rounded = round_to_scale(position, self.scale)
if ymin <= rounded <= ymax:
positions.add(rounded)
position += step position += step
if not positions:
return [ymin]
return positions return positions
def _compute_margin(self, x_labels, y_labels): def _compute_margin(self, x_labels, y_labels):

5
pygal/config.py

@ -5,10 +5,13 @@ class Config(object):
width = 800 width = 800
height = 600 height = 600
scale = 1 scale = 1
max_scale_step = 20 max_scale_step = 10
base_css = None base_css = None
style = DefaultStyle style = DefaultStyle
label_font_size = 12 label_font_size = 12
x_labels = None x_labels = None
y_labels = None y_labels = None
title = None title = None
def __init__(self, **kwargs):
self.__dict__.update(kwargs)

4
pygal/test/test_bar.py

@ -4,8 +4,8 @@ from math import cos, sin
def test_simple_bar(): def test_simple_bar():
bar = Bar() bar = Bar()
rng = [12, 3, 30, 4, 40, 10, 9, 2] rng = [12, 3, 30, -5, 40, 10, 9, 2]
bar.add('test1', rng) bar.add('test1', rng)
bar.x_labels = map(str, rng) bar.x_labels = map(str, rng)
bar.title = "Bar test" bar.title = "Bar test"
bar.render() bar._in_browser()

12
pygal/test/test_line.py

@ -1,9 +1,10 @@
from pygal.line import Line from pygal.line import Line
from pygal.config import Config
from math import cos, sin from math import cos, sin
def test_simple_line(): def test_simple_line():
line = Line() line = Line(Config(scale=.5))
rng = range(-30, 31, 5) rng = range(-30, 31, 5)
line.add('test1', [cos(x / 10.) for x in rng]) line.add('test1', [cos(x / 10.) for x in rng])
line.add('test2', [sin(x / 10.) for x in rng]) line.add('test2', [sin(x / 10.) for x in rng])
@ -13,6 +14,15 @@ def test_simple_line():
line._in_browser() line._in_browser()
def test_line():
line = Line()
rng = [8, 12, 23, 73, 39, 57]
line.add('-_-', rng)
line.x_labels = map(str, rng)
line.title = "cos sin and cos - sin"
line._in_browser()
def test_one_dot(): def test_one_dot():
line = Line() line = Line()
line.add('one dot', [12]) line.add('one dot', [12])

1
pygal/util.py

@ -3,6 +3,7 @@ from math import floor
def round_to_int(number, precision): def round_to_int(number, precision):
precision = int(precision)
rounded = (int(number) + precision / 2) / precision * precision rounded = (int(number) + precision / 2) / precision * precision
return rounded return rounded

Loading…
Cancel
Save