Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 147 lines (123 sloc) 4.089 kb
be82b9f @paradoxxxzero Set license
paradoxxxzero authored
1 # -*- coding: utf-8 -*-
2 # This file is part of pygal
3 #
4 # A python svg graph plotting library
5 # Copyright © 2012 Kozea
6 #
7 # This library is free software: you can redistribute it and/or modify it under
8 # the terms of the GNU Lesser General Public License as published by the Free
9 # Software Foundation, either version 3 of the License, or (at your option) any
10 # later version.
11 #
12 # This library is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 # details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with pygal. If not, see <http://www.gnu.org/licenses/>.
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
19 """
20 Projection and bounding helpers
21 """
22
4e3d585 @paradoxxxzero Compatibility with python 2.6 and 3.2. Yay!
paradoxxxzero authored
23 from __future__ import division
51e82fc @paradoxxxzero No data + logarithmic
paradoxxxzero authored
24 from math import sin, cos, log10
80f39a1 @paradoxxxzero Big refactor and add radar graph
paradoxxxzero authored
25
26
69edbfc @paradoxxxzero Add pie and xy
paradoxxxzero authored
27 class Margin(object):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
28 """Graph margin"""
69edbfc @paradoxxxzero Add pie and xy
paradoxxxzero authored
29 def __init__(self, top, right, bottom, left):
30 self.top = top
31 self.right = right
32 self.bottom = bottom
33 self.left = left
34
35 @property
36 def x(self):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
37 """Helper for total x margin"""
69edbfc @paradoxxxzero Add pie and xy
paradoxxxzero authored
38 return self.left + self.right
39
40 @property
41 def y(self):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
42 """Helper for total y margin"""
69edbfc @paradoxxxzero Add pie and xy
paradoxxxzero authored
43 return self.top + self.bottom
5913e43 @paradoxxxzero Here comes the lines
paradoxxxzero authored
44
45
46 class Box(object):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
47 """Chart boundings"""
a1ec68e @paradoxxxzero Pylint and interpolation refactoring
paradoxxxzero authored
48 margin = .02
b0fe46c @paradoxxxzero Add 2% margin
paradoxxxzero authored
49
b964163 @paradoxxxzero Rewrite of drawing pattern
paradoxxxzero authored
50 def __init__(self):
51 self.xmin = self.ymin = 0
52 self.xmax = self.ymax = 1
53
54 @property
55 def width(self):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
56 """Helper for box width"""
b964163 @paradoxxxzero Rewrite of drawing pattern
paradoxxxzero authored
57 return self.xmax - self.xmin
58
59 @property
60 def height(self):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
61 """Helper for box height"""
b964163 @paradoxxxzero Rewrite of drawing pattern
paradoxxxzero authored
62 return self.ymax - self.ymin
63
8f0da62 @paradoxxxzero Add horizontal graphs
paradoxxxzero authored
64 def swap(self):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
65 """Return the box (for horizontal qraphs)"""
8f0da62 @paradoxxxzero Add horizontal graphs
paradoxxxzero authored
66 self.xmin, self.ymin = self.ymin, self.xmin
67 self.xmax, self.ymax = self.ymax, self.xmax
68
15985f1 @paradoxxxzero Fix margin in log scale
paradoxxxzero authored
69 def fix(self, with_margin=True):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
70 """Correct box when no values and take margin in account"""
b964163 @paradoxxxzero Rewrite of drawing pattern
paradoxxxzero authored
71 if not self.width:
72 self.xmax = self.xmin + 1
73 if not self.height:
74 self.ymin -= .5
75 self.ymax = self.ymin + 1
a1ec68e @paradoxxxzero Pylint and interpolation refactoring
paradoxxxzero authored
76 xmargin = self.margin * self.width
b0fe46c @paradoxxxzero Add 2% margin
paradoxxxzero authored
77 self.xmin -= xmargin
78 self.xmax += xmargin
15985f1 @paradoxxxzero Fix margin in log scale
paradoxxxzero authored
79 if with_margin:
a1ec68e @paradoxxxzero Pylint and interpolation refactoring
paradoxxxzero authored
80 ymargin = self.margin * self.height
15985f1 @paradoxxxzero Fix margin in log scale
paradoxxxzero authored
81 self.ymin -= ymargin
82 self.ymax += ymargin
5913e43 @paradoxxxzero Here comes the lines
paradoxxxzero authored
83
84
85 class View(object):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
86 """Projection base class"""
b964163 @paradoxxxzero Rewrite of drawing pattern
paradoxxxzero authored
87 def __init__(self, width, height, box):
5913e43 @paradoxxxzero Here comes the lines
paradoxxxzero authored
88 self.width = width
89 self.height = height
b964163 @paradoxxxzero Rewrite of drawing pattern
paradoxxxzero authored
90 self.box = box
b0fe46c @paradoxxxzero Add 2% margin
paradoxxxzero authored
91 self.box.fix()
5913e43 @paradoxxxzero Here comes the lines
paradoxxxzero authored
92
93 def x(self, x):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
94 """Project x"""
b004099 @paradoxxxzero Support different series array sizes
paradoxxxzero authored
95 if x == None:
96 return None
4e3d585 @paradoxxxzero Compatibility with python 2.6 and 3.2. Yay!
paradoxxxzero authored
97 return self.width * (x - self.box.xmin) / self.box.width
5913e43 @paradoxxxzero Here comes the lines
paradoxxxzero authored
98
99 def y(self, y):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
100 """Project y"""
b004099 @paradoxxxzero Support different series array sizes
paradoxxxzero authored
101 if y == None:
102 return None
5913e43 @paradoxxxzero Here comes the lines
paradoxxxzero authored
103 return (self.height - self.height *
4e3d585 @paradoxxxzero Compatibility with python 2.6 and 3.2. Yay!
paradoxxxzero authored
104 (y - self.box.ymin) / self.box.height)
5913e43 @paradoxxxzero Here comes the lines
paradoxxxzero authored
105
106 def __call__(self, xy):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
107 """Project x and y"""
5913e43 @paradoxxxzero Here comes the lines
paradoxxxzero authored
108 x, y = xy
7f25b50 @paradoxxxzero Handle one point
paradoxxxzero authored
109 return (self.x(x), self.y(y))
80f39a1 @paradoxxxzero Big refactor and add radar graph
paradoxxxzero authored
110
111
112 class PolarView(View):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
113 """Polar projection for pie like graphs"""
114
82e506f @paradoxxxzero Many many things, noticeably interpolation
paradoxxxzero authored
115 def __call__(self, rhotheta):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
116 """Project rho and theta"""
b004099 @paradoxxxzero Support different series array sizes
paradoxxxzero authored
117 if None in rhotheta:
118 return None, None
82e506f @paradoxxxzero Many many things, noticeably interpolation
paradoxxxzero authored
119 rho, theta = rhotheta
120 rho = max(rho, 0)
80f39a1 @paradoxxxzero Big refactor and add radar graph
paradoxxxzero authored
121 return super(PolarView, self).__call__(
82e506f @paradoxxxzero Many many things, noticeably interpolation
paradoxxxzero authored
122 (rho * cos(theta), rho * sin(theta)))
51e82fc @paradoxxxzero No data + logarithmic
paradoxxxzero authored
123
124
125 class LogView(View):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
126 """Logarithmic projection """
127 # Do not want to call the parent here
128 # pylint: disable-msg=W0231
51e82fc @paradoxxxzero No data + logarithmic
paradoxxxzero authored
129 def __init__(self, width, height, box):
130 self.width = width
131 self.height = height
132 self.box = box
133 self.ymin = self.box.ymin
134 self.ymax = self.box.ymax
135 self.log10_ymax = log10(self.box.ymax)
136 self.log10_ymin = log10(self.box.ymin)
15985f1 @paradoxxxzero Fix margin in log scale
paradoxxxzero authored
137 self.box.fix(False)
51e82fc @paradoxxxzero No data + logarithmic
paradoxxxzero authored
138
e61551e @paradoxxxzero More pylint
paradoxxxzero authored
139 # pylint: enable-msg=W0231
51e82fc @paradoxxxzero No data + logarithmic
paradoxxxzero authored
140 def y(self, y):
9f15334 @paradoxxxzero Pylinting
paradoxxxzero authored
141 """Project y"""
6d9bcd5 @paradoxxxzero Work on log scale + minor fixes
paradoxxxzero authored
142 if y == None or y <= 0:
b004099 @paradoxxxzero Support different series array sizes
paradoxxxzero authored
143 return None
51e82fc @paradoxxxzero No data + logarithmic
paradoxxxzero authored
144 return (self.height - self.height *
145 (log10(y) - self.log10_ymin)
4e3d585 @paradoxxxzero Compatibility with python 2.6 and 3.2. Yay!
paradoxxxzero authored
146 / (self.log10_ymax - self.log10_ymin))
Something went wrong with that request. Please try again.