fix title=None + graph key
[PyX.git] / pyx / graph / key.py
blobcab2b06e31256e0161d0f8d12fd3b5eb87ef0242
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 from pyx import box, canvas, text, unit
29 class key:
31 defaulttextattrs = [text.vshift.mathaxis]
33 def __init__(self, dist="0.2 cm", pos="tr", hinside=1, vinside=1, hdist="0.6 cm", vdist="0.4 cm",
34 symbolwidth="0.5 cm", symbolheight="0.25 cm", symbolspace="0.2 cm",
35 textattrs=[]):
36 self.dist_str = dist
37 self.pos = pos
38 self.hinside = hinside
39 self.vinside = vinside
40 self.hdist_str = hdist
41 self.vdist_str = vdist
42 self.symbolwidth_str = symbolwidth
43 self.symbolheight_str = symbolheight
44 self.symbolspace_str = symbolspace
45 self.textattrs = textattrs
46 if self.pos in ("tr", "rt"):
47 self.right = 1
48 self.top = 1
49 elif self.pos in ("br", "rb"):
50 self.right = 1
51 self.top = 0
52 elif self.pos in ("tl", "lt"):
53 self.right = 0
54 self.top = 1
55 elif self.pos in ("bl", "lb"):
56 self.right = 0
57 self.top = 0
58 else:
59 raise RuntimeError("invalid pos attribute")
61 def paint(self, plotdata):
62 "creates the layout of the key"
63 c = canvas.canvas()
64 self.dist_pt = unit.topt(unit.length(self.dist_str, default_type="v"))
65 self.hdist_pt = unit.topt(unit.length(self.hdist_str, default_type="v"))
66 self.vdist_pt = unit.topt(unit.length(self.vdist_str, default_type="v"))
67 self.symbolwidth_pt = unit.topt(unit.length(self.symbolwidth_str, default_type="v"))
68 self.symbolheight_pt = unit.topt(unit.length(self.symbolheight_str, default_type="v"))
69 self.symbolspace_pt = unit.topt(unit.length(self.symbolspace_str, default_type="v"))
70 titles = []
71 for plotdat in plotdata:
72 if plotdat.title is not None:
73 titles.append(c.texrunner.text_pt(0, 0, plotdat.title, self.defaulttextattrs + self.textattrs))
74 box.tile_pt(titles, self.dist_pt, 0, -1)
75 box.linealignequal_pt(titles, self.symbolwidth_pt + self.symbolspace_pt, 1, 0)
76 for plotdat, title in zip(plotdata, titles):
77 plotdat.style.key_pt(c, 0, -0.5 * self.symbolheight_pt + title.center[1],
78 self.symbolwidth_pt, self.symbolheight_pt, plotdat)
79 c.insert(title)
80 return c