Example #1
0
    def _makeProtoLabel(self):
        "Return a label prototype for further modification."

        protoLabel = Label()
        protoLabel.dx = 0
        protoLabel.dy = 0
        protoLabel.boxStrokeWidth = 0.1
        protoLabel.boxStrokeColor = colors.black
        protoLabel.boxFillColor = colors.yellow
        # protoLabel.text = 'Hello World!' # Does not work as expected.

        return protoLabel
    def _makeProtoLabel(self):
        "Return a label prototype for further modification."

        protoLabel = Label()
        protoLabel.dx = 0
        protoLabel.dy = 0
        protoLabel.boxStrokeWidth = 0.1
        protoLabel.boxStrokeColor = colors.black
        protoLabel.boxFillColor = colors.yellow
        # protoLabel.text = 'Hello World!' # Does not work as expected.

        return protoLabel
from reportlab.graphics import shapes
from reportlab.graphics.charts.textlabels import Label

d = Drawing(200, 100)

# mark the origin of the label
d.add(Circle(100,90, 5, fillColor=colors.green))

lab = Label()
lab.setOrigin(100,90)
lab.boxAnchor = 'ne'
lab.angle = 45
lab.dx = 0
lab.dy = -20
lab.boxStrokeColor = colors.green
lab.setText('Some\nMulti-Line\nLabel')

d.add(lab)

draw(d, 'Label example')



disc("""
In the drawing above, the label is defined relative to the green blob.
The text box should have its north-east corner ten points down from
the origin, and be rotated by 45 degrees about that corner.
""")

disc("""
Example #4
0
    def draw_hex(self):
        print 'Reporting Hex'
        story = []
        for p in self.world.packets:
            pl,t = p.build_ps()
            XSTART = 0
            XDSTART = 210
            y = 0.0
            XMUL= 100.0
            YMUL = 10.0
            larg = 16
            YDUMP = PAGE_HEIGHT*0.80/YMUL - 10
            YDUMP = 0
            canvas = shapes.Drawing(500, 100)
            # canvas.add(shapes.Rect(0,0, 500, PAGE_HEIGHT, fillColor=colors.yellow))
            backcolor=colgen(0.6, 0.8, 1.0)
            forecolor=colgen(0.2, 0.5, 0.8)
            def hexstr(x):
                s = []
                for c in x:
                    s.append("%02x" % ord(c))
                return " ".join(s)

            my_y = 0
            shift = 0
            last_x = 0
            while t:
                bkcol = backcolor.next()
                proto,fields = t.pop()
                l = Label()
                l.setText(proto.name)
                l.boxAnchor = 'w'
                l.boxStrokeColor = colors.gray
                bc = colors.Color(bkcol[0], bkcol[1], bkcol[2] )
                l.boxFillColor = bc
                l.setOrigin(XSTART, (YDUMP-y)*YMUL)
                canvas.add(l)
                my_y = y
                for fname, fval, fdump in fields:
                    y += 1.5
                    col = forecolor.next()
                    l = Label()
                    l.boxAnchor = 'w'
                    l.setText(fname.name)
                    l.setOrigin(XSTART + (0.1 * XMUL), (YDUMP-y)*YMUL)
                    canvas.add(l)
                    
                    if fval is not None:
                        if len(fval) > 24:
                            fval = fval[:21]+"..."
                    else:
                        fval=""
                    
                    l = Label()
                    l.setText(fval)
                    xlabel, ylabel = XSTART+(1.5*XMUL), (YDUMP-y)*YMUL
                    l.setOrigin(xlabel, ylabel)
                    l.boxStrokeWidth = 2
                    l.boxAnchor = 'e'
                    canvas.add(l)
                    
                    first = True
                    while fdump:
                        dmp, fdump = fdump[:larg-shift],fdump[larg-shift:]
                        l = Label()
                        l.boxAnchor = 'w'
                        l.fontName = 'Courier'
                        l.boxFillColor = colors.Color(bkcol[0], bkcol[1], bkcol[2])
                        l.boxStrokeColor = colors.Color(col[0], col[1], col[2])
                        l.boxStrokeWidth = 2
                        xdump, ydump = XDSTART+ last_x * 0.06*XMUL, (YDUMP-my_y)*YMUL
                        l.setOrigin(xdump, ydump)
                        h = hexstr(dmp)
                        l.setText(h)
                        canvas.add(l)
                        if first:
                            link = shapes.Line(xdump, ydump, xlabel, ylabel, strokeColor=colors.Color(col[0], col[1], col[2]), strokeWidth=1)
                            canvas.add(link)
                            first = False
                        shift += len(dmp)
                        last_x += len(h) +1
                        if shift >= larg:
                            shift = 0
                            last_x = 0
                            my_y += 2
                y += 2
            scale = 0.7
            canvas.shift(0, y * YMUL*scale)
            canvas.height = min(y * YMUL *scale , PAGE_HEIGHT*0.80)
            canvas.scale(scale, scale)
            # para = Paragraph('<a name="hex%d"/>'%p.number + \
            # '<a href="#summary%d">(%d) %s</a>' % (p.number, p.number, p.description), styles['Normal'])
            # story.append([[para, Spacer(1,10), canvas]])
            para = Paragraph('<a href="#summary%d">(%d) %s</a>' % (p.number, p.number, p.description), styles['Normal'])
            story.append([[para, Spacer(1,10), canvas]])

        t = Table(story)
        t.setStyle(TableStyle([
                        ('INNERGRID', (0,0), (-1,-1), 0.1, colors.black), 
                        ('BOX', (0,0), (-1,-1), 0.1, colors.black), 
                        ]))
        return [t]