graph arrow style and various style/data/graph related fixes
[PyX/mjg.git] / pyx / graph / graph.py
blob6dd0ab75f0df9ca9237f7d7a75e847fd268c66b9
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2002-2004 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2003-2004 Michael Schindler <m-schindler@users.sourceforge.net>
7 # Copyright (C) 2002-2004 André Wobst <wobsta@users.sourceforge.net>
9 # This file is part of PyX (http://pyx.sourceforge.net/).
11 # PyX is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # PyX is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with PyX; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 import math, re, string
27 from pyx import canvas, path, trafo, unit
28 from pyx.graph.axis import painter, axis
31 goldenmean = 0.5 * (math.sqrt(5) + 1)
34 class lineaxispos:
35 """an axispos linear along a line with a fix direction for the ticks"""
37 def __init__(self, convert, x1, y1, x2, y2, fixtickdirection):
38 """initializes the instance
39 - only the convert method is needed from the axis
40 - x1, y1, x2, y2 are PyX lengths (start and end position of the line)
41 - fixtickdirection is a tuple tick direction (fixed along the line)"""
42 self.convert = convert
43 self.x1 = x1
44 self.y1 = y1
45 self.x2 = x2
46 self.y2 = y2
47 self.x1_pt = unit.topt(x1)
48 self.y1_pt = unit.topt(y1)
49 self.x2_pt = unit.topt(x2)
50 self.y2_pt = unit.topt(y2)
51 self.fixtickdirection = fixtickdirection
53 def vbasepath(self, v1=None, v2=None):
54 if v1 is None:
55 v1 = 0
56 if v2 is None:
57 v2 = 1
58 return path.line_pt((1-v1)*self.x1_pt+v1*self.x2_pt,
59 (1-v1)*self.y1_pt+v1*self.y2_pt,
60 (1-v2)*self.x1_pt+v2*self.x2_pt,
61 (1-v2)*self.y1_pt+v2*self.y2_pt)
63 def basepath(self, x1=None, x2=None):
64 if x1 is None:
65 v1 = 0
66 else:
67 v1 = self.convert(x1)
68 if x2 is None:
69 v2 = 1
70 else:
71 v2 = self.convert(x2)
72 return path.line_pt((1-v1)*self.x1_pt+v1*self.x2_pt,
73 (1-v1)*self.y1_pt+v1*self.y2_pt,
74 (1-v2)*self.x1_pt+v2*self.x2_pt,
75 (1-v2)*self.y1_pt+v2*self.y2_pt)
77 def gridpath(self, x):
78 raise RuntimeError("gridpath not available")
80 def vgridpath(self, v):
81 raise RuntimeError("gridpath not available")
83 def vtickpoint_pt(self, v):
84 return (1-v)*self.x1_pt+v*self.x2_pt, (1-v)*self.y1_pt+v*self.y2_pt
86 def vtickpoint(self, v):
87 return (1-v)*self.x1+v*self.x2, (1-v)*self.y1+v*self.y2
89 def tickpoint_pt(self, x):
90 v = self.convert(x)
91 return (1-v)*self.x1_pt+v*self.x2_pt, (1-v)*self.y1_pt+v*self.y2_pt
93 def tickpoint(self, x):
94 v = self.convert(x)
95 return (1-v)*self.x1+v*self.x2, (1-v)*self.y1+v*self.y2
97 def tickdirection(self, x):
98 return self.fixtickdirection
100 def vtickdirection(self, v):
101 return self.fixtickdirection
104 class lineaxisposlinegrid(lineaxispos):
105 """an axispos linear along a line with a fix direction for the ticks
106 with support for grid lines for a rectangular graphs"""
108 __implements__ = painter._Iaxispos
110 def __init__(self, convert, x1, y1, x2, y2, fixtickdirection, startgridlength, endgridlength):
111 """initializes the instance
112 - only the convert method is needed from the axis
113 - x1, y1, x2, y2 are PyX lengths (start and end position of the line)
114 - fixtickdirection is a tuple tick direction (fixed along the line)
115 - startgridlength and endgridlength are PyX lengths for the starting
116 and end point of the grid, respectively; the gridpath is a line along
117 the fixtickdirection"""
118 lineaxispos.__init__(self, convert, x1, y1, x2, y2, fixtickdirection)
119 self.startgridlength = startgridlength
120 self.endgridlength = endgridlength
121 self.startgridlength_pt = unit.topt(self.startgridlength)
122 self.endgridlength_pt = unit.topt(self.endgridlength)
124 def gridpath(self, x):
125 v = self.convert(x)
126 return path.line_pt((1-v)*self.x1_pt+v*self.x2_pt+self.fixtickdirection[0]*self.startgridlength_pt,
127 (1-v)*self.y1_pt+v*self.y2_pt+self.fixtickdirection[1]*self.startgridlength_pt,
128 (1-v)*self.x1_pt+v*self.x2_pt+self.fixtickdirection[0]*self.endgridlength_pt,
129 (1-v)*self.y1_pt+v*self.y2_pt+self.fixtickdirection[1]*self.endgridlength_pt)
131 def vgridpath(self, v):
132 return path.line_pt((1-v)*self.x1_pt+v*self.x2_pt+self.fixtickdirection[0]*self.startgridlength_pt,
133 (1-v)*self.y1_pt+v*self.y2_pt+self.fixtickdirection[1]*self.startgridlength_pt,
134 (1-v)*self.x1_pt+v*self.x2_pt+self.fixtickdirection[0]*self.endgridlength_pt,
135 (1-v)*self.y1_pt+v*self.y2_pt+self.fixtickdirection[1]*self.endgridlength_pt)
138 class graphxy(canvas.canvas):
140 def plot(self, data, styles=None):
141 if self.haslayout:
142 raise RuntimeError("layout setup was already performed")
143 try:
144 for d in data:
145 pass
146 except:
147 usedata = [data]
148 else:
149 usedata = data
150 if styles is None:
151 for d in usedata:
152 if styles is None:
153 styles = d.getdefaultstyles()
154 elif styles != d.getdefaultstyles():
155 raise RuntimeError("defaultstyles differ")
156 for d in usedata:
157 d.setstyles(self, styles)
158 self.plotdata.append(d)
159 return data
161 def pos_pt(self, x, y, xaxis=None, yaxis=None):
162 if xaxis is None:
163 xaxis = self.axes["x"]
164 if yaxis is None:
165 yaxis = self.axes["y"]
166 return self.xpos_pt + xaxis.convert(x)*self.width_pt, self.ypos_pt + yaxis.convert(y)*self.height_pt
168 def pos(self, x, y, xaxis=None, yaxis=None):
169 if xaxis is None:
170 xaxis = self.axes["x"]
171 if yaxis is None:
172 yaxis = self.axes["y"]
173 return self.xpos + xaxis.convert(x)*self.width, self.ypos + yaxis.convert(y)*self.height
175 def vpos_pt(self, vx, vy):
176 return self.xpos_pt + vx*self.width_pt, self.ypos_pt + vy*self.height_pt
178 def vpos(self, vx, vy):
179 return self.xpos + vx*self.width, self.ypos + vy*self.height
181 def vgeodesic(self, vx1, vy1, vx2, vy2):
182 """returns a geodesic path between two points in graph coordinates"""
183 return path.line_pt(self.xpos_pt + vx1*self.width_pt,
184 self.ypos_pt + vy1*self.height_pt,
185 self.xpos_pt + vx2*self.width_pt,
186 self.ypos_pt + vy2*self.height_pt)
188 def vgeodesic_el(self, vx1, vy1, vx2, vy2):
189 """returns a geodesic path element between two points in graph coordinates"""
190 return path.lineto_pt(self.xpos_pt + vx2*self.width_pt,
191 self.ypos_pt + vy2*self.height_pt)
193 def vcap_pt(self, coordinate, length_pt, vx, vy):
194 """returns an error cap path for a given coordinate, lengths and
195 point in graph coordinates"""
196 if coordinate == 0:
197 return path.line_pt(self.xpos_pt + vx*self.width_pt - 0.5*length_pt,
198 self.ypos_pt + vy*self.height_pt,
199 self.xpos_pt + vx*self.width_pt + 0.5*length_pt,
200 self.ypos_pt + vy*self.height_pt)
201 elif coordinate == 1:
202 return path.line_pt(self.xpos_pt + vx*self.width_pt,
203 self.ypos_pt + vy*self.height_pt - 0.5*length_pt,
204 self.xpos_pt + vx*self.width_pt,
205 self.ypos_pt + vy*self.height_pt + 0.5*length_pt)
206 else:
207 raise ValueError("direction invalid")
209 def keynum(self, key):
210 try:
211 while key[0] in string.letters:
212 key = key[1:]
213 return int(key)
214 except IndexError:
215 return 1
217 def removedomethod(self, method):
218 hadmethod = 0
219 while 1:
220 try:
221 self.domethods.remove(method)
222 hadmethod = 1
223 except ValueError:
224 return hadmethod
226 def dolayout(self):
227 if not self.removedomethod(self.dolayout): return
229 # count the usage of styles and perform selects
230 styletotal = {}
231 def stylesid(styles):
232 return ":".join([str(id(style)) for style in styles])
233 for data in self.plotdata:
234 try:
235 styletotal[stylesid(data.styles)] += 1
236 except:
237 styletotal[stylesid(data.styles)] = 1
238 styleindex = {}
239 for data in self.plotdata:
240 try:
241 styleindex[stylesid(data.styles)] += 1
242 except:
243 styleindex[stylesid(data.styles)] = 0
244 data.selectstyles(self, styleindex[stylesid(data.styles)], styletotal[stylesid(data.styles)])
246 # adjust the axes ranges
247 for step in range(3):
248 for data in self.plotdata:
249 data.adjustaxes(self, step)
251 # finish all axes
252 XPattern = re.compile(r"x([2-9]|[1-9][0-9]+)?$")
253 YPattern = re.compile(r"y([2-9]|[1-9][0-9]+)?$")
254 xaxisextents = [0, 0]
255 yaxisextents = [0, 0]
256 needxaxisdist = [0, 0]
257 needyaxisdist = [0, 0]
258 items = list(self.axes.items())
259 items.sort() #TODO: alphabetical sorting breaks for axis numbers bigger than 9
260 # TODO: linked axes are not taken into account (consider x being a link to x2)
261 for key, axis in items:
262 num = self.keynum(key)
263 num2 = 1 - num % 2 # x1 -> 0, x2 -> 1, x3 -> 0, x4 -> 1, ...
264 num3 = 2 * (num % 2) - 1 # x1 -> 1, x2 -> -1, x3 -> 1, x4 -> -1, ...
265 if XPattern.match(key):
266 if needxaxisdist[num2]:
267 xaxisextents[num2] += self.axesdist
268 self.axespos[key] = lineaxisposlinegrid(axis.convert,
269 self.xpos,
270 self.ypos + num2*self.height - num3*xaxisextents[num2],
271 self.xpos + self.width,
272 self.ypos + num2*self.height - num3*xaxisextents[num2],
273 (0, num3),
274 xaxisextents[num2], xaxisextents[num2] + self.height)
275 if num == 1:
276 self.xbasepath = self.axespos[key].basepath
277 self.xvbasepath = self.axespos[key].vbasepath
278 self.xgridpath = self.axespos[key].gridpath
279 self.xvgridpath = self.axespos[key].vgridpath
280 self.xtickpoint_pt = self.axespos[key].tickpoint_pt
281 self.xtickpoint = self.axespos[key].tickpoint
282 self.xvtickpoint_pt = self.axespos[key].vtickpoint_pt
283 self.xvtickpoint = self.axespos[key].tickpoint
284 self.xtickdirection = self.axespos[key].tickdirection
285 self.xvtickdirection = self.axespos[key].vtickdirection
286 elif YPattern.match(key):
287 if needyaxisdist[num2]:
288 yaxisextents[num2] += self.axesdist
289 self.axespos[key] = lineaxisposlinegrid(axis.convert,
290 self.xpos + num2*self.width - num3*yaxisextents[num2],
291 self.ypos,
292 self.xpos + num2*self.width - num3*yaxisextents[num2],
293 self.ypos + self.height,
294 (num3, 0),
295 yaxisextents[num2], yaxisextents[num2] + self.width)
296 if num == 1:
297 self.ybasepath = self.axespos[key].basepath
298 self.yvbasepath = self.axespos[key].vbasepath
299 self.ygridpath = self.axespos[key].gridpath
300 self.yvgridpath = self.axespos[key].vgridpath
301 self.ytickpoint_pt = self.axespos[key].tickpoint_pt
302 self.ytickpoint = self.axespos[key].tickpoint
303 self.yvtickpoint_pt = self.axespos[key].vtickpoint_pt
304 self.yvtickpoint = self.axespos[key].tickpoint
305 self.ytickdirection = self.axespos[key].tickdirection
306 self.yvtickdirection = self.axespos[key].vtickdirection
307 else:
308 raise ValueError("Axis key '%s' not allowed" % key)
309 axis.finish(self.axespos[key])
310 if XPattern.match(key):
311 xaxisextents[num2] += axis.axiscanvas.extent
312 needxaxisdist[num2] = 1
313 if YPattern.match(key):
314 yaxisextents[num2] += axis.axiscanvas.extent
315 needyaxisdist[num2] = 1
316 self.haslayout = 1
318 def dobackground(self):
319 self.dolayout()
320 if not self.removedomethod(self.dobackground): return
321 if self.backgroundattrs is not None:
322 self.draw(path.rect_pt(self.xpos_pt, self.ypos_pt, self.width_pt, self.height_pt),
323 self.backgroundattrs)
325 def doaxes(self):
326 self.dolayout()
327 if not self.removedomethod(self.doaxes): return
328 for axis in self.axes.values():
329 self.insert(axis.axiscanvas)
331 def dodata(self):
332 self.dolayout()
333 if not self.removedomethod(self.dodata): return
334 for data in self.plotdata:
335 data.draw(self)
337 def dokey(self):
338 self.dolayout()
339 if not self.removedomethod(self.dokey): return
340 if self.key is not None:
341 c = self.key.paint(self.plotdata)
342 bbox = c.bbox()
343 def parentchildalign(pmin, pmax, cmin, cmax, pos, dist, inside):
344 ppos = pmin+0.5*(cmax-cmin)+dist+pos*(pmax-pmin-cmax+cmin-2*dist)
345 cpos = 0.5*(cmin+cmax)+(1-inside)*(1-2*pos)*(cmax-cmin+2*dist)
346 return ppos-cpos
347 x = parentchildalign(self.xpos_pt, self.xpos_pt+self.width_pt,
348 bbox.llx_pt, bbox.urx_pt,
349 self.key.hpos, unit.topt(self.key.hdist), self.key.hinside)
350 y = parentchildalign(self.ypos_pt, self.ypos_pt+self.height_pt,
351 bbox.lly_pt, bbox.ury_pt,
352 self.key.vpos, unit.topt(self.key.vdist), self.key.vinside)
353 self.insert(c, [trafo.translate_pt(x, y)])
355 def finish(self):
356 while len(self.domethods):
357 self.domethods[0]()
359 def initwidthheight(self, width, height, ratio):
360 if (width is not None) and (height is None):
361 self.width = width
362 self.height = (1.0/ratio) * self.width
363 elif (height is not None) and (width is None):
364 self.height = height
365 self.width = ratio * self.height
366 else:
367 self.width = width
368 self.height = height
369 self.width_pt = unit.topt(self.width)
370 self.height_pt = unit.topt(self.height)
371 if self.width_pt <= 0: raise ValueError("width <= 0")
372 if self.height_pt <= 0: raise ValueError("height <= 0")
374 def initaxes(self, axes, addlinkaxes=0):
375 for key in ["x", "y"]:
376 if not axes.has_key(key):
377 axes[key] = axis.linear()
378 elif axes[key] is None:
379 del axes[key]
380 if addlinkaxes:
381 if not axes.has_key(key + "2") and axes.has_key(key):
382 axes[key + "2"] = axes[key].createlinkaxis()
383 elif axes[key + "2"] is None:
384 del axes[key + "2"]
385 self.axes = axes
386 self.axesnames = ([], [])
387 for key in axes.keys():
388 if len(key) != 1 and (not key[1:].isdigit() or key[1:] == "1"):
389 raise ValueError("invalid axis count")
390 if key[0] == "x":
391 self.axesnames[0].append(key)
392 elif key[0] == "y":
393 self.axesnames[1].append(key)
394 else:
395 raise ValueError("invalid axis name")
397 def __init__(self, xpos=0, ypos=0, width=None, height=None, ratio=goldenmean,
398 key=None, backgroundattrs=None, axesdist=0.8*unit.v_cm, **axes):
399 canvas.canvas.__init__(self)
400 self.xpos = xpos
401 self.ypos = ypos
402 self.xpos_pt = unit.topt(self.xpos)
403 self.ypos_pt = unit.topt(self.ypos)
404 self.initwidthheight(width, height, ratio)
405 self.initaxes(axes, 1)
406 self.axescanvas = {}
407 self.axespos = {}
408 self.key = key
409 self.backgroundattrs = backgroundattrs
410 self.axesdist = axesdist
411 self.plotdata = []
412 self.domethods = [self.dolayout, self.dobackground, self.doaxes, self.dodata, self.dokey]
413 self.haslayout = 0
414 self.addkeys = []
416 def bbox(self):
417 self.finish()
418 return canvas.canvas.bbox(self)
420 def prolog(self):
421 self.finish()
422 return canvas.canvas.prolog(self)
424 def outputPS(self, file):
425 self.finish()
426 canvas.canvas.outputPS(self, file)
430 # some thoughts, but deferred right now
432 # class graphxyz(graphxy):
434 # axisnames = "x", "y", "z"
436 # def _vxtickpoint(self, axis, v):
437 # return self._vpos(v, axis.vypos, axis.vzpos)
439 # def _vytickpoint(self, axis, v):
440 # return self._vpos(axis.vxpos, v, axis.vzpos)
442 # def _vztickpoint(self, axis, v):
443 # return self._vpos(axis.vxpos, axis.vypos, v)
445 # def vxtickdirection(self, axis, v):
446 # x1, y1 = self._vpos(v, axis.vypos, axis.vzpos)
447 # x2, y2 = self._vpos(v, 0.5, 0)
448 # dx, dy = x1 - x2, y1 - y2
449 # norm = math.hypot(dx, dy)
450 # return dx/norm, dy/norm
452 # def vytickdirection(self, axis, v):
453 # x1, y1 = self._vpos(axis.vxpos, v, axis.vzpos)
454 # x2, y2 = self._vpos(0.5, v, 0)
455 # dx, dy = x1 - x2, y1 - y2
456 # norm = math.hypot(dx, dy)
457 # return dx/norm, dy/norm
459 # def vztickdirection(self, axis, v):
460 # return -1, 0
461 # x1, y1 = self._vpos(axis.vxpos, axis.vypos, v)
462 # x2, y2 = self._vpos(0.5, 0.5, v)
463 # dx, dy = x1 - x2, y1 - y2
464 # norm = math.hypot(dx, dy)
465 # return dx/norm, dy/norm
467 # def _pos(self, x, y, z, xaxis=None, yaxis=None, zaxis=None):
468 # if xaxis is None: xaxis = self.axes["x"]
469 # if yaxis is None: yaxis = self.axes["y"]
470 # if zaxis is None: zaxis = self.axes["z"]
471 # return self._vpos(xaxis.convert(x), yaxis.convert(y), zaxis.convert(z))
473 # def pos(self, x, y, z, xaxis=None, yaxis=None, zaxis=None):
474 # if xaxis is None: xaxis = self.axes["x"]
475 # if yaxis is None: yaxis = self.axes["y"]
476 # if zaxis is None: zaxis = self.axes["z"]
477 # return self.vpos(xaxis.convert(x), yaxis.convert(y), zaxis.convert(z))
479 # def _vpos(self, vx, vy, vz):
480 # x, y, z = (vx - 0.5)*self._depth, (vy - 0.5)*self._width, (vz - 0.5)*self._height
481 # d0 = float(self.a[0]*self.b[1]*(z-self.eye[2])
482 # + self.a[2]*self.b[0]*(y-self.eye[1])
483 # + self.a[1]*self.b[2]*(x-self.eye[0])
484 # - self.a[2]*self.b[1]*(x-self.eye[0])
485 # - self.a[0]*self.b[2]*(y-self.eye[1])
486 # - self.a[1]*self.b[0]*(z-self.eye[2]))
487 # da = (self.eye[0]*self.b[1]*(z-self.eye[2])
488 # + self.eye[2]*self.b[0]*(y-self.eye[1])
489 # + self.eye[1]*self.b[2]*(x-self.eye[0])
490 # - self.eye[2]*self.b[1]*(x-self.eye[0])
491 # - self.eye[0]*self.b[2]*(y-self.eye[1])
492 # - self.eye[1]*self.b[0]*(z-self.eye[2]))
493 # db = (self.a[0]*self.eye[1]*(z-self.eye[2])
494 # + self.a[2]*self.eye[0]*(y-self.eye[1])
495 # + self.a[1]*self.eye[2]*(x-self.eye[0])
496 # - self.a[2]*self.eye[1]*(x-self.eye[0])
497 # - self.a[0]*self.eye[2]*(y-self.eye[1])
498 # - self.a[1]*self.eye[0]*(z-self.eye[2]))
499 # return da/d0 + self._xpos, db/d0 + self._ypos
501 # def vpos(self, vx, vy, vz):
502 # tx, ty = self._vpos(vx, vy, vz)
503 # return unit.t_pt(tx), unit.t_pt(ty)
505 # def xbaseline(self, axis, x1, x2, xaxis=None):
506 # if xaxis is None: xaxis = self.axes["x"]
507 # return self.vxbaseline(axis, xaxis.convert(x1), xaxis.convert(x2))
509 # def ybaseline(self, axis, y1, y2, yaxis=None):
510 # if yaxis is None: yaxis = self.axes["y"]
511 # return self.vybaseline(axis, yaxis.convert(y1), yaxis.convert(y2))
513 # def zbaseline(self, axis, z1, z2, zaxis=None):
514 # if zaxis is None: zaxis = self.axes["z"]
515 # return self.vzbaseline(axis, zaxis.convert(z1), zaxis.convert(z2))
517 # def vxbaseline(self, axis, v1, v2):
518 # return (path._line(*(self._vpos(v1, 0, 0) + self._vpos(v2, 0, 0))) +
519 # path._line(*(self._vpos(v1, 0, 1) + self._vpos(v2, 0, 1))) +
520 # path._line(*(self._vpos(v1, 1, 1) + self._vpos(v2, 1, 1))) +
521 # path._line(*(self._vpos(v1, 1, 0) + self._vpos(v2, 1, 0))))
523 # def vybaseline(self, axis, v1, v2):
524 # return (path._line(*(self._vpos(0, v1, 0) + self._vpos(0, v2, 0))) +
525 # path._line(*(self._vpos(0, v1, 1) + self._vpos(0, v2, 1))) +
526 # path._line(*(self._vpos(1, v1, 1) + self._vpos(1, v2, 1))) +
527 # path._line(*(self._vpos(1, v1, 0) + self._vpos(1, v2, 0))))
529 # def vzbaseline(self, axis, v1, v2):
530 # return (path._line(*(self._vpos(0, 0, v1) + self._vpos(0, 0, v2))) +
531 # path._line(*(self._vpos(0, 1, v1) + self._vpos(0, 1, v2))) +
532 # path._line(*(self._vpos(1, 1, v1) + self._vpos(1, 1, v2))) +
533 # path._line(*(self._vpos(1, 0, v1) + self._vpos(1, 0, v2))))
535 # def xgridpath(self, x, xaxis=None):
536 # assert 0
537 # if xaxis is None: xaxis = self.axes["x"]
538 # v = xaxis.convert(x)
539 # return path._line(self._xpos+v*self._width, self._ypos,
540 # self._xpos+v*self._width, self._ypos+self._height)
542 # def ygridpath(self, y, yaxis=None):
543 # assert 0
544 # if yaxis is None: yaxis = self.axes["y"]
545 # v = yaxis.convert(y)
546 # return path._line(self._xpos, self._ypos+v*self._height,
547 # self._xpos+self._width, self._ypos+v*self._height)
549 # def zgridpath(self, z, zaxis=None):
550 # assert 0
551 # if zaxis is None: zaxis = self.axes["z"]
552 # v = zaxis.convert(z)
553 # return path._line(self._xpos, self._zpos+v*self._height,
554 # self._xpos+self._width, self._zpos+v*self._height)
556 # def vxgridpath(self, v):
557 # return path.path(path._moveto(*self._vpos(v, 0, 0)),
558 # path._lineto(*self._vpos(v, 0, 1)),
559 # path._lineto(*self._vpos(v, 1, 1)),
560 # path._lineto(*self._vpos(v, 1, 0)),
561 # path.closepath())
563 # def vygridpath(self, v):
564 # return path.path(path._moveto(*self._vpos(0, v, 0)),
565 # path._lineto(*self._vpos(0, v, 1)),
566 # path._lineto(*self._vpos(1, v, 1)),
567 # path._lineto(*self._vpos(1, v, 0)),
568 # path.closepath())
570 # def vzgridpath(self, v):
571 # return path.path(path._moveto(*self._vpos(0, 0, v)),
572 # path._lineto(*self._vpos(0, 1, v)),
573 # path._lineto(*self._vpos(1, 1, v)),
574 # path._lineto(*self._vpos(1, 0, v)),
575 # path.closepath())
577 # def _addpos(self, x, y, dx, dy):
578 # assert 0
579 # return x+dx, y+dy
581 # def _connect(self, x1, y1, x2, y2):
582 # assert 0
583 # return path._lineto(x2, y2)
585 # def doaxes(self):
586 # self.dolayout()
587 # if not self.removedomethod(self.doaxes): return
588 # axesdist_pt = unit.topt(self.axesdist)
589 # XPattern = re.compile(r"%s([2-9]|[1-9][0-9]+)?$" % self.axisnames[0])
590 # YPattern = re.compile(r"%s([2-9]|[1-9][0-9]+)?$" % self.axisnames[1])
591 # ZPattern = re.compile(r"%s([2-9]|[1-9][0-9]+)?$" % self.axisnames[2])
592 # items = list(self.axes.items())
593 # items.sort() #TODO: alphabetical sorting breaks for axis numbers bigger than 9
594 # for key, axis in items:
595 # num = self.keynum(key)
596 # num2 = 1 - num % 2 # x1 -> 0, x2 -> 1, x3 -> 0, x4 -> 1, ...
597 # num3 = 1 - 2 * (num % 2) # x1 -> -1, x2 -> 1, x3 -> -1, x4 -> 1, ...
598 # if XPattern.match(key):
599 # axis.vypos = 0
600 # axis.vzpos = 0
601 # axis._vtickpoint = self._vxtickpoint
602 # axis.vgridpath = self.vxgridpath
603 # axis.vbaseline = self.vxbaseline
604 # axis.vtickdirection = self.vxtickdirection
605 # elif YPattern.match(key):
606 # axis.vxpos = 0
607 # axis.vzpos = 0
608 # axis._vtickpoint = self._vytickpoint
609 # axis.vgridpath = self.vygridpath
610 # axis.vbaseline = self.vybaseline
611 # axis.vtickdirection = self.vytickdirection
612 # elif ZPattern.match(key):
613 # axis.vxpos = 0
614 # axis.vypos = 0
615 # axis._vtickpoint = self._vztickpoint
616 # axis.vgridpath = self.vzgridpath
617 # axis.vbaseline = self.vzbaseline
618 # axis.vtickdirection = self.vztickdirection
619 # else:
620 # raise ValueError("Axis key '%s' not allowed" % key)
621 # if axis.painter is not None:
622 # axis.dopaint(self)
623 # # if XPattern.match(key):
624 # # self._xaxisextents[num2] += axis._extent
625 # # needxaxisdist[num2] = 1
626 # # if YPattern.match(key):
627 # # self._yaxisextents[num2] += axis._extent
628 # # needyaxisdist[num2] = 1
630 # def __init__(self, tex, xpos=0, ypos=0, width=None, height=None, depth=None,
631 # phi=30, theta=30, distance=1,
632 # backgroundattrs=None, axesdist=0.8*unit.v_cm, **axes):
633 # canvas.canvas.__init__(self)
634 # self.tex = tex
635 # self.xpos = xpos
636 # self.ypos = ypos
637 # self._xpos = unit.topt(xpos)
638 # self._ypos = unit.topt(ypos)
639 # self._width = unit.topt(width)
640 # self._height = unit.topt(height)
641 # self._depth = unit.topt(depth)
642 # self.width = width
643 # self.height = height
644 # self.depth = depth
645 # if self._width <= 0: raise ValueError("width < 0")
646 # if self._height <= 0: raise ValueError("height < 0")
647 # if self._depth <= 0: raise ValueError("height < 0")
648 # self._distance = distance*math.sqrt(self._width*self._width+
649 # self._height*self._height+
650 # self._depth*self._depth)
651 # phi *= -math.pi/180
652 # theta *= math.pi/180
653 # self.a = (-math.sin(phi), math.cos(phi), 0)
654 # self.b = (-math.cos(phi)*math.sin(theta),
655 # -math.sin(phi)*math.sin(theta),
656 # math.cos(theta))
657 # self.eye = (self._distance*math.cos(phi)*math.cos(theta),
658 # self._distance*math.sin(phi)*math.cos(theta),
659 # self._distance*math.sin(theta))
660 # self.initaxes(axes)
661 # self.axesdist = axesdist
662 # self.backgroundattrs = backgroundattrs
664 # self.data = []
665 # self.domethods = [self.dolayout, self.dobackground, self.doaxes, self.dodata]
666 # self.haslayout = 0
667 # self.defaultstyle = {}
669 # def bbox(self):
670 # self.finish()
671 # return bbox._bbox(self._xpos - 200, self._ypos - 200, self._xpos + 200, self._ypos + 200)