1 # -*- encoding: utf-8 -*-
4 # Copyright (C) 2002-2004 Jörg Lehmann <joergl@users.sourceforge.net>
5 # Copyright (C) 2003-2004 Michael Schindler <m-schindler@users.sourceforge.net>
6 # Copyright (C) 2002-2004 André Wobst <wobsta@users.sourceforge.net>
8 # This file is part of PyX (http://pyx.sourceforge.net/).
10 # PyX is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # PyX is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with PyX; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 from pyx
.graph
.axis
import tick
31 def labels(self
, ticks
):
32 """fill the label attribute of ticks
33 - ticks is a list of instances of tick
34 - for each element of ticks the value of the attribute label is set to
35 a string appropriate to the attributes num and denom of that tick
37 - label attributes of the tick instances are just kept, whenever they
39 - the method might modify the labelattrs attribute of the ticks; be sure
40 to not modify it in-place!"""
44 "a texter creating decimal labels (e.g. '1.234' or even '0.\overline{3}')"
46 __implements__
= _Itexter
48 def __init__(self
, prefix
="", infix
="", suffix
="", equalprecision
=0,
49 decimalsep
=".", thousandsep
="", thousandthpartsep
="",
50 plus
="", minus
="-", period
=r
"\overline{%s}",
51 labelattrs
=[text
.mathmode
]):
52 r
"""initializes the instance
53 - prefix, infix, and suffix (strings) are added at the begin,
54 immediately after the minus, and at the end of the label,
56 - decimalsep, thousandsep, and thousandthpartsep (strings)
57 are used as separators
58 - plus or minus (string) is inserted for non-negative or negative numbers
59 - period (string) is taken as a format string generating a period;
60 it has to contain exactly one string insert operators "%s" for the
61 period; usually it should be r"\overline{%s}"
62 - labelattrs is a list of attributes to be added to the label attributes
63 given in the painter"""
67 self
.equalprecision
= equalprecision
68 self
.decimalsep
= decimalsep
69 self
.thousandsep
= thousandsep
70 self
.thousandthpartsep
= thousandthpartsep
74 self
.labelattrs
= labelattrs
76 def labels(self
, ticks
):
80 if tick
.label
is None and tick
.labellevel
is not None:
81 labeledticks
.append(tick
)
82 m
, n
= tick
.num
, tick
.denom
85 quotient
, remainder
= divmod(m
, n
)
86 quotient
= str(quotient
)
87 if len(self
.thousandsep
):
91 tick
.label
+= quotient
[i
]
92 if not ((l
-i
-1) % 3) and l
> i
+1:
93 tick
.label
+= self
.thousandsep
97 tick
.label
+= self
.decimalsep
99 tick
.temp_decprecision
= 0
101 tick
.temp_decprecision
+= 1
102 if remainder
in oldremainders
:
103 tick
.temp_decprecision
= None
104 periodstart
= len(tick
.label
) - (len(oldremainders
) - oldremainders
.index(remainder
))
105 tick
.label
= tick
.label
[:periodstart
] + self
.period
% tick
.label
[periodstart
:]
107 oldremainders
+= [remainder
]
109 quotient
, remainder
= divmod(remainder
, n
)
110 if not ((tick
.temp_decprecision
- 1) % 3) and tick
.temp_decprecision
> 1:
111 tick
.label
+= self
.thousandthpartsep
112 tick
.label
+= str(quotient
)
113 if maxdecprecision
< tick
.temp_decprecision
:
114 maxdecprecision
= tick
.temp_decprecision
115 if self
.equalprecision
:
116 for tick
in labeledticks
:
117 if tick
.temp_decprecision
is not None:
118 if tick
.temp_decprecision
== 0 and maxdecprecision
> 0:
119 tick
.label
+= self
.decimalsep
120 for i
in range(tick
.temp_decprecision
, maxdecprecision
):
121 if not ((i
- 1) % 3) and i
> 1:
122 tick
.label
+= self
.thousandthpartsep
124 for tick
in labeledticks
:
125 if tick
.num
* tick
.denom
< 0:
126 plusminus
= self
.minus
128 plusminus
= self
.plus
129 tick
.label
= "%s%s%s%s%s" % (self
.prefix
, plusminus
, self
.infix
, tick
.label
, self
.suffix
)
130 tick
.labelattrs
= tick
.labelattrs
+ self
.labelattrs
132 # del tick.temp_decprecision # we've inserted this temporary variable ... and do not care any longer about it
136 "a texter creating labels with exponentials (e.g. '2\cdot10^5')"
138 __implements__
= _Itexter
140 def __init__(self
, plus
="", minus
="-",
141 mantissaexp
=r
"{{%s}\cdot10^{%s}}",
144 nomantissaexp
=r
"{10^{%s}}",
145 minusnomantissaexp
=r
"{-10^{%s}}",
146 mantissamin
=tick
.rational((1, 1)), mantissamax
=tick
.rational((10L, 1)),
147 skipmantissa1
=0, skipallmantissa1
=1,
148 mantissatexter
=decimal()):
149 r
"""initializes the instance
150 - plus or minus (string) is inserted for non-negative or negative exponents
151 - mantissaexp (string) is taken as a format string generating the exponent;
152 it has to contain exactly two string insert operators "%s" --
153 the first for the mantissa and the second for the exponent;
154 examples are r"{{%s}\cdot10^{%s}}" and r"{{%s}{\rm e}{%s}}"
155 - skipexp0 (string) is taken as a format string used for exponent 0;
156 exactly one string insert operators "%s" for the mantissa;
157 None turns off the special handling of exponent 0;
158 an example is r"{%s}"
159 - skipexp1 (string) is taken as a format string used for exponent 1;
160 exactly one string insert operators "%s" for the mantissa;
161 None turns off the special handling of exponent 1;
162 an example is r"{{%s}\cdot10}"
163 - nomantissaexp (string) is taken as a format string generating the exponent
164 when the mantissa is one and should be skipped; it has to contain
165 exactly one string insert operators "%s" for the exponent;
166 an examples is r"{10^{%s}}"
167 - minusnomantissaexp (string) is taken as a format string generating the exponent
168 when the mantissa is minus one and should be skipped; it has to contain
169 exactly one string insert operators "%s" for the exponent;
170 None turns off the special handling of mantissa -1;
171 an examples is r"{-10^{%s}}"
172 - mantissamin and mantissamax are the minimum and maximum of the mantissa;
173 they are rational instances greater than zero and mantissamin < mantissamax;
174 the sign of the tick is ignored here
175 - skipmantissa1 (boolean) turns on skipping of any mantissa equals one
176 (and minus when minusnomantissaexp is set)
177 - skipallmantissa1 (boolean) as above, but all mantissas must be 1 (or -1)
178 - mantissatexter is the texter for the mantissa
179 - the skipping of a mantissa is stronger than the skipping of an exponent"""
182 self
.mantissaexp
= mantissaexp
183 self
.skipexp0
= skipexp0
184 self
.skipexp1
= skipexp1
185 self
.nomantissaexp
= nomantissaexp
186 self
.minusnomantissaexp
= minusnomantissaexp
187 self
.mantissamin
= mantissamin
188 self
.mantissamax
= mantissamax
189 self
.mantissamindivmax
= self
.mantissamin
/ self
.mantissamax
190 self
.mantissamaxdivmin
= self
.mantissamax
/ self
.mantissamin
191 self
.skipmantissa1
= skipmantissa1
192 self
.skipallmantissa1
= skipallmantissa1
193 self
.mantissatexter
= mantissatexter
195 def labels(self
, ticks
):
198 if tick
.label
is None and tick
.labellevel
is not None:
199 tick
.temp_orgnum
, tick
.temp_orgdenom
= tick
.num
, tick
.denom
200 labeledticks
.append(tick
)
203 while abs(tick
) >= self
.mantissamax
:
205 x
= tick
* self
.mantissamindivmax
206 tick
.num
, tick
.denom
= x
.num
, x
.denom
207 while abs(tick
) < self
.mantissamin
:
209 x
= tick
* self
.mantissamaxdivmin
210 tick
.num
, tick
.denom
= x
.num
, x
.denom
211 if tick
.temp_exp
< 0:
212 tick
.temp_exp
= "%s%i" % (self
.minus
, -tick
.temp_exp
)
214 tick
.temp_exp
= "%s%i" % (self
.plus
, tick
.temp_exp
)
215 self
.mantissatexter
.labels(labeledticks
)
216 if self
.minusnomantissaexp
is not None:
217 allmantissa1
= len(labeledticks
) == len([tick
for tick
in labeledticks
if abs(tick
.num
) == abs(tick
.denom
)])
219 allmantissa1
= len(labeledticks
) == len([tick
for tick
in labeledticks
if tick
.num
== tick
.denom
])
220 for tick
in labeledticks
:
221 if (self
.skipallmantissa1
and allmantissa1
or
222 (self
.skipmantissa1
and (tick
.num
== tick
.denom
or
223 (tick
.num
== -tick
.denom
and self
.minusnomantissaexp
is not None)))):
224 if tick
.num
== tick
.denom
:
225 tick
.label
= self
.nomantissaexp
% tick
.temp_exp
227 tick
.label
= self
.minusnomantissaexp
% tick
.temp_exp
229 if tick
.temp_exp
== "0" and self
.skipexp0
is not None:
230 tick
.label
= self
.skipexp0
% tick
.label
231 elif tick
.temp_exp
== "1" and self
.skipexp1
is not None:
232 tick
.label
= self
.skipexp1
% tick
.label
234 tick
.label
= self
.mantissaexp
% (tick
.label
, tick
.temp_exp
)
235 tick
.num
, tick
.denom
= tick
.temp_orgnum
, tick
.temp_orgdenom
237 # del tick.temp_orgnum # we've inserted those temporary variables ... and do not care any longer about them
238 # del tick.temp_orgdenom
243 "a texter creating decimal or exponential labels"
245 __implements__
= _Itexter
247 def __init__(self
, smallestdecimal
=tick
.rational((1, 1000)),
248 biggestdecimal
=tick
.rational((9999, 1)),
251 exponential
=exponential()):
252 """initializes the instance
253 - smallestdecimal and biggestdecimal are the smallest and
254 biggest decimal values, where the decimal texter should be used;
255 they are rational instances; the sign of the tick is ignored here;
256 a tick at zero is considered for the decimal texter as well
257 - equaldecision (boolean) uses decimal texter or exponential texter
258 globaly (set) or for each tick separately (unset)
259 - decimal and exponential are texters to be used"""
260 self
.smallestdecimal
= smallestdecimal
261 self
.biggestdecimal
= biggestdecimal
262 self
.equaldecision
= equaldecision
263 self
.decimal
= decimal
264 self
.exponential
= exponential
266 def labels(self
, ticks
):
270 if tick
.label
is None and tick
.labellevel
is not None:
271 if not tick
.num
or (abs(tick
) >= self
.smallestdecimal
and abs(tick
) <= self
.biggestdecimal
):
272 decticks
.append(tick
)
274 expticks
.append(tick
)
275 if self
.equaldecision
:
277 self
.exponential
.labels(ticks
)
279 self
.decimal
.labels(ticks
)
281 for tick
in decticks
:
282 self
.decimal
.labels([tick
])
283 for tick
in expticks
:
284 self
.exponential
.labels([tick
])
288 "a texter creating rational labels (e.g. 'a/b' or even 'a \over b')"
289 # XXX: we use divmod here to be more expicit
291 __implements__
= _Itexter
293 def __init__(self
, prefix
="", infix
="", suffix
="",
294 numprefix
="", numinfix
="", numsuffix
="",
295 denomprefix
="", denominfix
="", denomsuffix
="",
296 plus
="", minus
="-", minuspos
=0, over
=r
"{{%s}\over{%s}}",
297 equaldenom
=0, skip1
=1, skipnum0
=1, skipnum1
=1, skipdenom1
=1,
298 labelattrs
=[text
.mathmode
]):
299 r
"""initializes the instance
300 - prefix, infix, and suffix (strings) are added at the begin,
301 immediately after the minus, and at the end of the label,
303 - prefixnum, infixnum, and suffixnum (strings) are added
304 to the labels numerator correspondingly
305 - prefixdenom, infixdenom, and suffixdenom (strings) are added
306 to the labels denominator correspondingly
307 - plus or minus (string) is inserted for non-negative or negative numbers
308 - minuspos is an integer, which determines the position, where the
309 plus or minus sign has to be placed; the following values are allowed:
310 1 - writes the plus or minus in front of the numerator
311 0 - writes the plus or minus in front of the hole fraction
312 -1 - writes the plus or minus in front of the denominator
313 - over (string) is taken as a format string generating the
314 fraction bar; it has to contain exactly two string insert
315 operators "%s" -- the first for the numerator and the second
316 for the denominator; by far the most common examples are
317 r"{{%s}\over{%s}}" and "{{%s}/{%s}}"
318 - usually the numerator and denominator are canceled; however,
319 when equaldenom is set, the least common multiple of all
321 - skip1 (boolean) just prints the prefix, the plus or minus,
322 the infix and the suffix, when the value is plus or minus one
323 and at least one of prefix, infix and the suffix is present
324 - skipnum0 (boolean) just prints a zero instead of
325 the hole fraction, when the numerator is zero;
326 no prefixes, infixes, and suffixes are taken into account
327 - skipnum1 (boolean) just prints the numprefix, the plus or minus,
328 the numinfix and the numsuffix, when the num value is plus or minus one
329 and at least one of numprefix, numinfix and the numsuffix is present
330 - skipdenom1 (boolean) just prints the numerator instead of
331 the hole fraction, when the denominator is one and none of the parameters
332 denomprefix, denominfix and denomsuffix are set and minuspos is not -1 or the
334 - labelattrs is a list of attributes for a texrunners text method;
335 None is considered as an empty list; labelattrs might be changed
336 in the painter as well"""
340 self
.numprefix
= numprefix
341 self
.numinfix
= numinfix
342 self
.numsuffix
= numsuffix
343 self
.denomprefix
= denomprefix
344 self
.denominfix
= denominfix
345 self
.denomsuffix
= denomsuffix
348 self
.minuspos
= minuspos
350 self
.equaldenom
= equaldenom
352 self
.skipnum0
= skipnum0
353 self
.skipnum1
= skipnum1
354 self
.skipdenom1
= skipdenom1
355 self
.labelattrs
= labelattrs
358 """returns the greates common divisor of all elements in n
359 - the elements of n must be non-negative integers
360 - return None if the number of elements is zero
361 - the greates common divisor is not affected when some
362 of the elements are zero, but it becomes zero when
363 all elements are zero"""
369 i
, (dummy
, j
) = j
, divmod(i
, j
)
374 res
= self
.gcd(res
, i
)
378 """returns the least common multiple of all elements in n
379 - the elements of n must be non-negative integers
380 - return None if the number of elements is zero
381 - the least common multiple is zero when some of the
386 res
= divmod(res
* i
, self
.gcd(res
, i
))[0]
389 def labels(self
, ticks
):
392 if tick
.label
is None and tick
.labellevel
is not None:
393 labeledticks
.append(tick
)
394 tick
.temp_rationalnum
= tick
.num
395 tick
.temp_rationaldenom
= tick
.denom
396 tick
.temp_rationalminus
= 1
397 if tick
.temp_rationalnum
< 0:
398 tick
.temp_rationalminus
= -tick
.temp_rationalminus
399 tick
.temp_rationalnum
= -tick
.temp_rationalnum
400 if tick
.temp_rationaldenom
< 0:
401 tick
.temp_rationalminus
= -tick
.temp_rationalminus
402 tick
.temp_rationaldenom
= -tick
.temp_rationaldenom
403 gcd
= self
.gcd(tick
.temp_rationalnum
, tick
.temp_rationaldenom
)
404 (tick
.temp_rationalnum
, dummy1
), (tick
.temp_rationaldenom
, dummy2
) = divmod(tick
.temp_rationalnum
, gcd
), divmod(tick
.temp_rationaldenom
, gcd
)
406 equaldenom
= self
.lcm(*[tick
.temp_rationaldenom
for tick
in ticks
if tick
.label
is None])
407 if equaldenom
is not None:
408 for tick
in labeledticks
:
409 factor
, dummy
= divmod(equaldenom
, tick
.temp_rationaldenom
)
410 tick
.temp_rationalnum
, tick
.temp_rationaldenom
= factor
* tick
.temp_rationalnum
, factor
* tick
.temp_rationaldenom
411 for tick
in labeledticks
:
412 rationalminus
= rationalnumminus
= rationaldenomminus
= ""
413 if tick
.temp_rationalminus
== -1:
414 plusminus
= self
.minus
416 plusminus
= self
.plus
417 if self
.minuspos
== 0:
418 rationalminus
= plusminus
419 elif self
.minuspos
== 1:
420 rationalnumminus
= plusminus
421 elif self
.minuspos
== -1:
422 rationaldenomminus
= plusminus
424 raise RuntimeError("invalid minuspos")
425 if self
.skipnum0
and tick
.temp_rationalnum
== 0:
427 elif (self
.skip1
and self
.skipdenom1
and tick
.temp_rationalnum
== 1 and tick
.temp_rationaldenom
== 1 and
428 (len(self
.prefix
) or len(self
.infix
) or len(self
.suffix
)) and
429 not len(rationalnumminus
) and not len(self
.numprefix
) and not len(self
.numinfix
) and not len(self
.numsuffix
) and
430 not len(rationaldenomminus
) and not len(self
.denomprefix
) and not len(self
.denominfix
) and not len(self
.denomsuffix
)):
431 tick
.label
= "%s%s%s%s" % (self
.prefix
, rationalminus
, self
.infix
, self
.suffix
)
433 if self
.skipnum1
and tick
.temp_rationalnum
== 1 and (len(self
.numprefix
) or len(self
.numinfix
) or len(self
.numsuffix
)):
434 tick
.temp_rationalnum
= "%s%s%s%s" % (self
.numprefix
, rationalnumminus
, self
.numinfix
, self
.numsuffix
)
436 tick
.temp_rationalnum
= "%s%s%s%i%s" % (self
.numprefix
, rationalnumminus
, self
.numinfix
, tick
.temp_rationalnum
, self
.numsuffix
)
437 if self
.skipdenom1
and tick
.temp_rationaldenom
== 1 and not len(rationaldenomminus
) and not len(self
.denomprefix
) and not len(self
.denominfix
) and not len(self
.denomsuffix
):
438 rational
= tick
.temp_rationalnum
440 tick
.temp_rationaldenom
= "%s%s%s%i%s" % (self
.denomprefix
, rationaldenomminus
, self
.denominfix
, tick
.temp_rationaldenom
, self
.denomsuffix
)
441 rational
= self
.over
% (tick
.temp_rationalnum
, tick
.temp_rationaldenom
)
442 tick
.label
= "%s%s%s%s%s" % (self
.prefix
, rationalminus
, self
.infix
, rational
, self
.suffix
)
443 tick
.labelattrs
= tick
.labelattrs
+ self
.labelattrs
445 # del tick.temp_rationalnum # we've inserted those temporary variables ... and do not care any longer about them
446 # del tick.temp_rationaldenom
447 # del tick.temp_rationalminus