arct to arc/arcn conversion has been fixed
[PyX/mjg.git] / pyx / graph / key.py
blob99f66c7f00b2b414ed0b9d20835e33b20e70cd53
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, trafo, 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=[], columns=1, columndist=0.5*unit.v_cm,
37 border=0.3*unit.v_cm, keyattrs=None):
38 self.dist = dist
39 self.hinside = hinside
40 self.vinside = vinside
41 self.hdist = hdist
42 self.vdist = vdist
43 self.symbolwidth = symbolwidth
44 self.symbolheight = symbolheight
45 self.symbolspace = symbolspace
46 self.textattrs = textattrs
47 self.columns = columns
48 self.columndist = columndist
49 self.border = border
50 self.keyattrs = keyattrs
51 if pos is not None:
52 if vpos is not None or hpos is not None:
53 raise ValueError("either specify pos or a combination of hpos, vpos")
54 for poslist, hpos, vpos in [(["tr", "rt"], 1, 1),
55 (["tc", "ct"], 0.5, 1),
56 (["tl", "lt"], 0, 1),
57 (["mr", "rm"], 1, 0.5),
58 (["mc", "cm"], 0.5, 0.5),
59 (["ml", "lm"], 0, 0.5),
60 (["br", "rb"], 1, 0),
61 (["bc", "cb"], 0.5, 0),
62 (["bl", "lb"], 0, 0)]:
63 if pos in poslist:
64 self.hpos = hpos
65 self.vpos = vpos
66 break
67 else:
68 raise ValueError("invalid pos")
69 else:
70 if vpos is None or hpos is None:
71 raise ValueError("either specify pos or a combination of hpos, vpos")
72 self.hpos = hpos
73 self.vpos = vpos
75 def paintcolumn(self, plotitems):
76 "creates the layout of a key column"
77 c = canvas.canvas()
78 self.dist_pt = unit.topt(self.dist)
79 self.hdist_pt = unit.topt(self.hdist)
80 self.vdist_pt = unit.topt(self.vdist)
81 self.symbolwidth_pt = unit.topt(self.symbolwidth)
82 self.symbolheight_pt = unit.topt(self.symbolheight)
83 self.symbolspace_pt = unit.topt(self.symbolspace)
84 titleboxes = []
85 for plotitem in plotitems:
86 titlebox = c.texrunner.text_pt(0, 0, plotitem.title, self.defaulttextattrs + self.textattrs)
87 titlebox.plotitem = plotitem
88 titleboxes.append(titlebox)
89 dy_pt = box.tile_pt(titleboxes, self.dist_pt, 0, -1)
90 box.linealignequal_pt(titleboxes, self.symbolwidth_pt + self.symbolspace_pt, 1, 0)
91 y_pt = -0.5 * self.symbolheight_pt + titleboxes[0].center[1]
92 for titlebox in titleboxes:
93 titlebox.plotitem.key_pt(c, 0, y_pt, self.symbolwidth_pt, self.symbolheight_pt)
94 y_pt -= dy_pt
95 for titlebox in titleboxes:
96 c.insert(titlebox)
97 return c
99 def paint(self, plotitems):
100 "creates the layout of the key"
101 columndist_pt = unit.topt(self.columndist)
102 c = canvas.canvas()
103 plotitems = [plotitem for plotitem in plotitems if plotitem.title is not None]
104 itemspercolumn = (len(plotitems) + self.columns - 1) / self.columns # integer division
105 x_pt = 0
106 while plotitems:
107 subc = self.paintcolumn(plotitems[:itemspercolumn])
108 c.insert(subc, [trafo.translate_pt(x_pt, 0)])
109 x_pt += unit.topt(subc.bbox().width()) + columndist_pt
110 del plotitems[:itemspercolumn]
111 if self.keyattrs is not None:
112 newc = canvas.canvas()
113 newc.draw(c.bbox().enlarged(self.border).path(), self.keyattrs)
114 newc.insert(c)
115 c = newc
116 return c