From: Michael S. <m-s...@us...> - 2004-09-24 07:15:58
|
Hello, On 23.09.04, Andrea Riciputi wrote: > How can I get a box around the plot keys or around a piece of text that > I write onto a plot? The second task is easy, because the text is a canvas itself and provides you with a bbox() method. The first one is not easy, because the key is no canvas and the information about its position is only known to the method graph.graphxy.dokey(). Maybe that is an issue for Andre: Wouldn't it be reasonable to let the key be a canvas that is first transformed and afterwards inserted into the graph? The following example will do. (Note that you can enlarge the bboxes if you wish to) -------------------------------------------------- from pyx import * from pyx.graph import axis,data,key # first define your own class to overwrite graph.graphxy.dokey() class mygraphxy(graph.graphxy): def dokey(self): self.dolayout() if not self.removedomethod(self.dokey): return if self.key is not None: c = self.key.paint(self.plotitems) bbox = c.bbox() def parentchildalign(pmin, pmax, cmin, cmax, pos, dist, inside): ppos = pmin+0.5*(cmax-cmin)+dist+pos*(pmax-pmin-cmax+cmin-2*dist) cpos = 0.5*(cmin+cmax)+(1-inside)*(1-2*pos)*(cmax-cmin+2*dist) return ppos-cpos x = parentchildalign(self.xpos_pt, self.xpos_pt+self.width_pt, bbox.llx_pt, bbox.urx_pt, self.key.hpos, unit.topt(self.key.hdist), self.key.hinside) y = parentchildalign(self.ypos_pt, self.ypos_pt+self.height_pt, bbox.lly_pt, bbox.ury_pt, self.key.vpos, unit.topt(self.key.vdist), self.key.vinside) self.insert(c, [trafo.translate_pt(x, y)]) # ONLY THIS LINE IS DIFFERENT: self.stroke(c.bbox().rect(), [trafo.translate_pt(x, y)]) h = mygraphxy(width=10, key=graph.key.key(pos="tr")) h.plot(data.function("y=sin(x)", min=0, max=5)) h.finish() t = h.text(5, 5, r"hello") h.stroke(t.bbox().enlarged(0.2).rect()) h.writetofile("foo.eps", paperformat="a4") -------------------------------------------------- Michael. -- "A mathematician is a device for turning coffee into theorems" Paul Erdös. |