- adjustments to the new graph data+style handling
[PyX/mjg.git] / pyx / graph / key.py
blob86ff7e65cb9c7a7c4ed2d8d5d2b052e2940838ea
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*unit.v_cm, pos="tr", hpos=None, vpos=None,
34 hinside=1, vinside=1, hdist=0.6*unit.v_cm, vdist=0.4*unit.v_cm,
35 symbolwidth=0.5*unit.v_cm, symbolheight=0.25*unit.v_cm, symbolspace=0.2*unit.v_cm,
36 textattrs=[]):
37 self.dist = dist
38 self.hinside = hinside
39 self.vinside = vinside
40 self.hdist = hdist
41 self.vdist = vdist
42 self.symbolwidth = symbolwidth
43 self.symbolheight = symbolheight
44 self.symbolspace = symbolspace
45 self.textattrs = textattrs
46 if pos is not None:
47 if vpos is not None or hpos is not None:
48 raise ValueError("either specify pos or a combination of hpos, vpos")
49 for poslist, hpos, vpos in [(["tr", "rt"], 1, 1),
50 (["tc", "ct"], 0.5, 1),
51 (["tl", "lt"], 0, 1),
52 (["mr", "rm"], 1, 0.5),
53 (["mc", "cm"], 0.5, 0.5),
54 (["ml", "lm"], 0, 0.5),
55 (["br", "rb"], 1, 0),
56 (["bc", "cb"], 0.5, 0),
57 (["bl", "lb"], 0, 0)]:
58 if pos in poslist:
59 self.hpos = hpos
60 self.vpos = vpos
61 break
62 else:
63 raise ValueError("invalid pos")
64 else:
65 if vpos is None or hpos is None:
66 raise ValueError("either specify pos or a combination of hpos, vpos")
67 self.hpos = hpos
68 self.vpos = vpos
70 def paint(self, plotitems):
71 "creates the layout of the key"
72 plotitems = [plotitem for plotitem in plotitems if plotitem.gettitle() is not None]
73 c = canvas.canvas()
74 self.dist_pt = unit.topt(self.dist)
75 self.hdist_pt = unit.topt(self.hdist)
76 self.vdist_pt = unit.topt(self.vdist)
77 self.symbolwidth_pt = unit.topt(self.symbolwidth)
78 self.symbolheight_pt = unit.topt(self.symbolheight)
79 self.symbolspace_pt = unit.topt(self.symbolspace)
80 titleboxes = []
81 for plotitem in plotitems:
82 titlebox = c.texrunner.text_pt(0, 0, plotitem.gettitle(), self.defaulttextattrs + self.textattrs)
83 titlebox.plotitem = plotitem
84 titleboxes.append(titlebox)
85 dy_pt = box.tile_pt(titleboxes, self.dist_pt, 0, -1)
86 box.linealignequal_pt(titleboxes, self.symbolwidth_pt + self.symbolspace_pt, 1, 0)
87 y_pt = -0.5 * self.symbolheight_pt + titleboxes[0].center[1]
88 for titlebox in titleboxes:
89 titlebox.plotitem.key_pt(c, 0, y_pt, self.symbolwidth_pt, self.symbolheight_pt)
90 y_pt -= dy_pt
91 for titlebox in titleboxes:
92 c.insert(titlebox)
93 return c