File: legends.py

package info (click to toggle)
python-reportlab 1.20debian-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,068 kB
  • ctags: 5,801
  • sloc: python: 53,293; xml: 1,494; makefile: 85
file content (486 lines) | stat: -rw-r--r-- 18,195 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#Copyright ReportLab Europe Ltd. 2000-2004
#see license.txt for license details
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/graphics/charts/legends.py
"""This will be a collection of legends to be used with charts.
"""
__version__=''' $Id: legends.py 2385 2004-06-17 15:26:05Z rgbecker $ '''

import string, copy

from reportlab.lib import colors
from reportlab.lib.validators import isNumber, OneOf, isString, isColorOrNone, isNumberOrNone, isListOfNumbersOrNone
from reportlab.lib.attrmap import *
from reportlab.pdfbase.pdfmetrics import stringWidth, getFont
from reportlab.graphics.widgetbase import Widget
from reportlab.graphics.shapes import Drawing, Group, String, Rect, Line, STATE_DEFAULTS


class Legend(Widget):
    """A simple legend containing rectangular swatches and strings.

    The swatches are filled rectangles whenever the respective
    color object in 'colorNamePairs' is a subclass of Color in
    reportlab.lib.colors. Otherwise the object passed instead is
    assumed to have 'x', 'y', 'width' and 'height' attributes.
    A legend then tries to set them or catches any error. This
    lets you plug-in any widget you like as a replacement for
    the default rectangular swatches.

    Strings can be nicely aligned left or right to the swatches.
    """

    _attrMap = AttrMap(
        x = AttrMapValue(isNumber, desc="x-coordinate of upper-left reference point"),
        y = AttrMapValue(isNumber, desc="y-coordinate of upper-left reference point"),
        deltax = AttrMapValue(isNumberOrNone, desc="x-distance between neighbouring swatches"),
        deltay = AttrMapValue(isNumberOrNone, desc="y-distance between neighbouring swatches"),
        dxTextSpace = AttrMapValue(isNumber, desc="Distance between swatch rectangle and text"),
        autoXPadding = AttrMapValue(isNumber, desc="x Padding between columns if deltax=None"),
        autoYPadding = AttrMapValue(isNumber, desc="y Padding between rows if deltay=None"),
        dx = AttrMapValue(isNumber, desc="Width of swatch rectangle"),
        dy = AttrMapValue(isNumber, desc="Height of swatch rectangle"),
        columnMaximum = AttrMapValue(isNumber, desc="Max. number of items per column"),
        alignment = AttrMapValue(OneOf("left", "right"), desc="Alignment of text with respect to swatches"),
        colorNamePairs = AttrMapValue(None, desc="List of color/name tuples (color can also be widget)"),
        fontName = AttrMapValue(isString, desc="Font name of the strings"),
        fontSize = AttrMapValue(isNumber, desc="Font size of the strings"),
        fillColor = AttrMapValue(isColorOrNone, desc=""),
        strokeColor = AttrMapValue(isColorOrNone, desc="Border color of the swatches"),
        strokeWidth = AttrMapValue(isNumber, desc="Width of the border color of the swatches"),
        callout = AttrMapValue(None, desc="a user callout(self,g,x,y,(color,text))"),
       )

    def __init__(self):
        # Upper-left reference point.
        self.x = 0
        self.y = 0

        # Alginment of text with respect to swatches.
        self.alignment = "left"

        # x- and y-distances between neighbouring swatches.
        self.deltax = 75
        self.deltay = 20
        self.autoXPadding = 5
        self.autoYPadding = 2

        # Size of swatch rectangle.
        self.dx = 10
        self.dy = 10

        # Distance between swatch rectangle and text.
        self.dxTextSpace = 10

        # Max. number of items per column.
        self.columnMaximum = 3

        # Color/name pairs.
        self.colorNamePairs = [ (colors.red, "red"),
                                (colors.blue, "blue"),
                                (colors.green, "green"),
                                (colors.pink, "pink"),
                                (colors.yellow, "yellow") ]

        # Font name and size of the labels.
        self.fontName = STATE_DEFAULTS['fontName']
        self.fontSize = STATE_DEFAULTS['fontSize']
        self.fillColor = STATE_DEFAULTS['fillColor']
        self.strokeColor = STATE_DEFAULTS['strokeColor']
        self.strokeWidth = STATE_DEFAULTS['strokeWidth']

    def _calculateMaxWidth(self, colorNamePairs):
        "Calculate the maximum width of some given strings."
        m = 0
        for t in map(lambda p:str(p[1]),colorNamePairs):
            if t:
                for s in string.split(t,'\n'):
                    m = max(m,stringWidth(s, self.fontName, self.fontSize))
        return m


    def _calcHeight(self):
        deltay = self.deltay
        dy = self.dy
        thisy = upperlefty = self.y - dy
        ascent=getFont(self.fontName).face.ascent/1000.
        if ascent==0: ascent=0.718 # default (from helvetica)
        leading = self.fontSize*1.2
        columnCount = 0
        count = 0
        lowy = upperlefty
        for unused, name in colorNamePairs:
            T = string.split(name and str(name) or '','\n')
            S = []
            # thisy+dy/2 = y+leading/2
            y = thisy+(dy-ascent)*0.5-leading
            newy = thisy-max(deltay,len(S)*leading)
            lowy = min(y,newy)
            if count == columnMaximum-1:
                count = 0
                thisy = upperlefty
                columnCount = columnCount + 1
            else:
                thisy = newy
                count = count+1
        return upperlefty - lowy

    def draw(self):
        g = Group()
        colorNamePairs = self.colorNamePairs
        thisx = upperleftx = self.x
        thisy = upperlefty = self.y - self.dy
        dx, dy, alignment, columnMaximum = self.dx, self.dy, self.alignment, self.columnMaximum
        deltax, deltay, dxTextSpace = self.deltax, self.deltay, self.dxTextSpace
        fontName, fontSize, fillColor = self.fontName, self.fontSize, self.fillColor
        strokeWidth, strokeColor = self.strokeWidth, self.strokeColor
        leading = fontSize*1.2
        if not deltay:
            deltay = max(dy,leading)+self.autoYPadding
        if not deltax:
            maxWidth = self._calculateMaxWidth(colorNamePairs)
            deltax = maxWidth+dx+dxTextSpace+self.autoXPadding
        else:
            if alignment=='left': maxWidth = self._calculateMaxWidth(colorNamePairs)

        def gAdd(t,g=g,fontName=fontName,fontSize=fontSize,fillColor=fillColor):
            t.fontName = fontName
            t.fontSize = fontSize
            t.fillColor = fillColor
            return g.add(t)

        ascent=getFont(fontName).face.ascent/1000.
        if ascent==0: ascent=0.718 # default (from helvetica)
        ascent=ascent*fontSize # normalize

        columnCount = 0
        count = 0
        callout = getattr(self,'callout',None)
        for col, name in colorNamePairs:
            T = string.split(name and str(name) or '','\n')
            S = []
            # thisy+dy/2 = y+leading/2
            y = thisy+(dy-ascent)*0.5
            if callout: callout(self,g,thisx,y,colorNamePairs[count])
            if alignment == "left":
                for t in T:
                    # align text to left
                    s = String(thisx+maxWidth,y,t)
                    s.textAnchor = "end"
                    S.append(s)
                    y = y-leading
                x = thisx+maxWidth+dxTextSpace
            elif alignment == "right":
                for t in T:
                    # align text to right
                    s = String(thisx+dx+dxTextSpace, y, t)
                    s.textAnchor = "start"
                    S.append(s)
                    y = y-leading
                x = thisx
            else:
                raise ValueError, "bad alignment"

            # Make a 'normal' color swatch...
            if isinstance(col, colors.Color):
                r = Rect(x, thisy, dx, dy)
                r.fillColor = col
                r.strokeColor = strokeColor
                r.strokeWidth = strokeWidth
                g.add(r)
            else:
                #try and see if we should do better.
                try:
                    c = copy.deepcopy(col)
                    c.x = x
                    c.y = thisy
                    c.width = dx
                    c.height = dy
                    g.add(c)
                except:
                    pass

            map(gAdd,S)

            if count%columnMaximum == columnMaximum-1:
                thisx = thisx+deltax
                thisy = upperlefty
                columnCount = columnCount + 1
            else:
                thisy = thisy-max(deltay,len(S)*leading)
            count = count+1

        return g


class LineSwatch(Widget):
    """basically a Line with properties added so it can be used in a LineLegend"""
    _attrMap = AttrMap(
        x = AttrMapValue(isNumber, desc="x-coordinate for swatch line start point"),
        y = AttrMapValue(isNumber, desc="y-coordinate for swatch line start point"),
        width = AttrMapValue(isNumber, desc="length of swatch line"),
        height = AttrMapValue(isNumber, desc="used for line strokeWidth"),
        strokeColor = AttrMapValue(isColorOrNone, desc="color of swatch line"),
        strokeDashArray = AttrMapValue(isListOfNumbersOrNone, desc="dash array for swatch line"),
    )

    def __init__(self):
        from reportlab.lib.colors import red
        from reportlab.graphics.shapes import Line
        self.x = 0
        self.y = 0
        self.width  = 20
        self.height = 1
        self.strokeColor = red
        self.strokeDashArray = None

    def draw(self):
        l = Line(self.x,self.y,self.x+self.width,self.y)
        l.strokeColor = self.strokeColor
        l.strokeDashArray  = self.strokeDashArray
        l.strokeWidth = self.height
        return l

class LineLegend(Legend):
    """A subclass of Legend for drawing legends with lines as the
    swatches rather than rectangles. Useful for lineCharts and
    linePlots. Should be similar in all other ways the the standard
    Legend class.
    """

    _attrMap = AttrMap(
        x = AttrMapValue(isNumber, desc="x-coordinate of upper-left reference point"),
        y = AttrMapValue(isNumber, desc="y-coordinate of upper-left reference point"),
        deltax = AttrMapValue(isNumberOrNone, desc="x-distance between neighbouring line-swatches"),
        deltay = AttrMapValue(isNumberOrNone, desc="y-distance between neighbouring line-swatches"),
        dxTextSpace = AttrMapValue(isNumber, desc="Distance between line-swatches and text"),
        autoXPadding = AttrMapValue(isNumber, desc="x Padding between columns if deltax=None"),
        autoYPadding = AttrMapValue(isNumber, desc="y Padding between rows if deltay=None"),
        dx = AttrMapValue(isNumber, desc="Width of line-swatch - ie length of the line"),
        dy = AttrMapValue(isNumber, desc="Height of line-swatch - ie strokeWidth to be used for the line"),
        columnMaximum = AttrMapValue(isNumber, desc="Max. number of items per column"),
        alignment = AttrMapValue(OneOf("left", "right"), desc="Alignment of text with respect to line-swatches"),
        colorNamePairs = AttrMapValue(None, desc="List of color/name tuples (color can also be widget)"),
        fontName = AttrMapValue(isString, desc="Font name of the strings"),
        fontSize = AttrMapValue(isNumber, desc="Font size of the strings"),
        fillColor = AttrMapValue(isColorOrNone, desc=""),
        strokeColor = AttrMapValue(isColorOrNone, desc="Stroke color of the line-swatches"),
        strokeWidth = AttrMapValue(isNumber, desc="Width of the line-swatches"),
        callout = AttrMapValue(None, desc="a user callout(self,g,x,y,(color,text))"),
       )

    def __init__(self):
        Legend.__init__(self)

        # Size of swatch rectangle.
        self.dx = 10 #width of line
        self.dy = 2  #strokeWidth for line

        # Color/name pairs.
        self.colorNamePairs = []
        for col, colName in [ (colors.red, "red"),
                                (colors.blue, "blue"),
                                (colors.green, "green"),
                                (colors.pink, "pink"),
                                (colors.yellow, "yellow") ]:
            l =  LineSwatch()
            l.strokeColor = col
            self.colorNamePairs.append((l, colName))

        # Font name and size of the labels.
        self.fillColor = STATE_DEFAULTS['fillColor']
        self.strokeColor = STATE_DEFAULTS['strokeColor']
        self.strokeWidth = STATE_DEFAULTS['strokeWidth']

    def draw(self):
        g = Group()
        colorNamePairs = self.colorNamePairs
        thisx = upperleftx = self.x
        thisy = upperlefty = self.y - self.dy
        dx, dy, alignment, columnMaximum = self.dx, self.dy, self.alignment, self.columnMaximum
        deltax, deltay, dxTextSpace = self.deltax, self.deltay, self.dxTextSpace
        fontName, fontSize, fillColor = self.fontName, self.fontSize, self.fillColor
        strokeWidth, strokeColor = self.strokeWidth, self.strokeColor
        leading = fontSize*1.2
        if not deltay:
            deltay = max(dy,leading)+self.autoYPadding
        if not deltax:
            maxWidth = self._calculateMaxWidth(colorNamePairs)
            deltax = maxWidth+dx+dxTextSpace+self.autoXPadding
        else:
            if alignment=='left': maxWidth = self._calculateMaxWidth(colorNamePairs)

        def gAdd(t,g=g,fontName=fontName,fontSize=fontSize,fillColor=fillColor):
            t.fontName = fontName
            t.fontSize = fontSize
            t.fillColor = fillColor
            return g.add(t)

        ascent=getFont(fontName).face.ascent/1000.
        if ascent==0: ascent=0.718 # default (from helvetica)
        ascent=ascent*fontSize # normalize

        columnCount = 0
        count = 0
        callout = getattr(self,'callout',None)
        for col, name in colorNamePairs:
            T = string.split(name and str(name) or '','\n')
            S = []
            # thisy+dy/2 = y+leading/2
            y = thisy+(dy-ascent)*0.5
            if callout: callout(self,g,thisx,y,colorNamePairs[count])
            if alignment == "left":
                for t in T:
                    # align text to left
                    s = String(thisx+maxWidth,y,t)
                    s.textAnchor = "end"
                    S.append(s)
                    y = y-leading
                x = thisx+maxWidth+dxTextSpace
            elif alignment == "right":
                for t in T:
                    # align text to right
                    s = String(thisx+dx+dxTextSpace, y, t)
                    s.textAnchor = "start"
                    S.append(s)
                    y = y-leading
                x = thisx
            else:
                raise ValueError, "bad alignment"

            # Make a 'normal' color line-swatch...
            if isinstance(col, colors.Color):
                l =  LineSwatch()
                l.x = x
                l.y = thisy
                l.width = dx
                l.height = dy
                l.strokeColor = col
                g.add(l)
            else:
                #try and see if we should do better.
                try:
                    c = copy.deepcopy(col)
                    c.x = x
                    c.y = thisy
                    c.width = dx
                    c.height = dy
                    g.add(c)
                except:
                    pass

            map(gAdd,S)

            if count%columnMaximum == columnMaximum-1:
                thisx = thisx+deltax
                thisy = upperlefty
                columnCount = columnCount + 1
            else:
                thisy = thisy-max(deltay,len(S)*leading)
            count = count+1

        return g



    def demo(self):
        "Make sample legend."

        d = Drawing(200, 100)

        legend = Legend()
        legend.alignment = 'left'
        legend.x = 0
        legend.y = 100
        legend.dxTextSpace = 5
        items = string.split('red green blue yellow pink black white', ' ')
        items = map(lambda i:(getattr(colors, i), i), items)
        legend.colorNamePairs = items

        d.add(legend, 'legend')

        return d


def sample1c():
    "Make sample legend."

    d = Drawing(200, 100)

    legend = Legend()
    legend.alignment = 'right'
    legend.x = 0
    legend.y = 100
    legend.dxTextSpace = 5
    items = string.split('red green blue yellow pink black white', ' ')
    items = map(lambda i:(getattr(colors, i), i), items)
    legend.colorNamePairs = items

    d.add(legend, 'legend')

    return d


def sample2c():
    "Make sample legend."

    d = Drawing(200, 100)

    legend = Legend()
    legend.alignment = 'right'
    legend.x = 20
    legend.y = 90
    legend.deltax = 60
    legend.dxTextSpace = 10
    legend.columnMaximum = 4
    items = string.split('red green blue yellow pink black white', ' ')
    items = map(lambda i:(getattr(colors, i), i), items)
    legend.colorNamePairs = items

    d.add(legend, 'legend')

    return d

def sample3():
    "Make sample legend with line swatches."

    d = Drawing(200, 100)

    legend = LineLegend()
    legend.alignment = 'right'
    legend.x = 20
    legend.y = 90
    legend.deltax = 60
    legend.dxTextSpace = 10
    legend.columnMaximum = 4
    items = string.split('red green blue yellow pink black white', ' ')
    items = map(lambda i:(getattr(colors, i), i), items)
    legend.colorNamePairs = items
    d.add(legend, 'legend')

    return d


def sample3a():
    "Make sample legend with line swatches and dasharrays on the lines."

    d = Drawing(200, 100)

    legend = LineLegend()
    legend.alignment = 'right'
    legend.x = 20
    legend.y = 90
    legend.deltax = 60
    legend.dxTextSpace = 10
    legend.columnMaximum = 4
    items = string.split('red green blue yellow pink black white', ' ')
    darrays = ([2,1], [2,5], [2,2,5,5], [1,2,3,4], [4,2,3,4], [1,2,3,4,5,6], [1])
    cnp = []
    for i in range(0, len(items)):
        l =  LineSwatch()
        l.strokeColor = getattr(colors, items[i])
        l.strokeDashArray = darrays[i]
        cnp.append((l, items[i]))
    legend.colorNamePairs = cnp
    d.add(legend, 'legend')

    return d