src/reportlab/graphics/charts/textlabels.py
author rptlab
Tue, 30 Apr 2013 14:28:14 +0100
branchpy33
changeset 3723 99aa837b6703
parent 3721 0c93dd8ff567
child 4056 be1be82d76fa
permissions -rw-r--r--
second stage of port to Python 3.3; working hello world
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3617
ae5744e97c42 reportlab: copyright date changes
robin
parents: 3368
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2012
817
8c3a399effda License text changes
rgbecker
parents: 737
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2200
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/graphics/charts/textlabels.py
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2200
diff changeset
     4
__version__=''' $Id$ '''
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
     5
import string
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
     6
909
a5ee7d2bdb17 Extracted validator functions into lib.validators.
dinu_gherman
parents: 817
diff changeset
     7
from reportlab.lib import colors
2716
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2698
diff changeset
     8
from reportlab.lib.utils import simpleSplit, _simpleSplit
1245
31de349cfc17 Changed to isNoneOrNumberPair
rgbecker
parents: 1243
diff changeset
     9
from reportlab.lib.validators import isNumber, isNumberOrNone, OneOf, isColorOrNone, isString, \
3645
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
    10
        isTextAnchor, isBoxAnchor, isBoolean, NoneOr, isInstanceOf, isNoneOrString, isNoneOrCallable
948
2531b35adf85 New AttrMap emplacement
rgbecker
parents: 932
diff changeset
    11
from reportlab.lib.attrmap import *
3628
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
    12
from reportlab.pdfbase.pdfmetrics import stringWidth, getAscentDescent
909
a5ee7d2bdb17 Extracted validator functions into lib.validators.
dinu_gherman
parents: 817
diff changeset
    13
from reportlab.graphics.shapes import Drawing, Group, Circle, Rect, String, STATE_DEFAULTS
1328
f8cb7553c9f5 added _text2Path & stroked Labels
rgbecker
parents: 1284
diff changeset
    14
from reportlab.graphics.shapes import _PATH_OP_ARG_COUNT, _PATH_OP_NAMES, definePath
1247
6f7061d9dd0c Working LabelOffset usage, still need variable styles
rgbecker
parents: 1245
diff changeset
    15
from reportlab.graphics.widgetbase import Widget, PropHolder
3368
afa025c34493 reportlab: new base font mechanism more fully applied
rgbecker
parents: 3308
diff changeset
    16
from reportlab.graphics.shapes import _baseGFontName
909
a5ee7d2bdb17 Extracted validator functions into lib.validators.
dinu_gherman
parents: 817
diff changeset
    17
1328
f8cb7553c9f5 added _text2Path & stroked Labels
rgbecker
parents: 1284
diff changeset
    18
_gs = None
2173
34351e616e7c Allow autox/autoy boxanchor
rgbecker
parents: 1951
diff changeset
    19
_A2BA=  {
34351e616e7c Allow autox/autoy boxanchor
rgbecker
parents: 1951
diff changeset
    20
        'x': {0:'n', 45:'ne', 90:'e', 135:'se', 180:'s', 225:'sw', 270:'w', 315: 'nw', -45: 'nw'},
34351e616e7c Allow autox/autoy boxanchor
rgbecker
parents: 1951
diff changeset
    21
        'y': {0:'e', 45:'se', 90:'s', 135:'sw', 180:'w', 225:'nw', 270:'n', 315: 'ne', -45: 'ne'},
34351e616e7c Allow autox/autoy boxanchor
rgbecker
parents: 1951
diff changeset
    22
        }
1328
f8cb7553c9f5 added _text2Path & stroked Labels
rgbecker
parents: 1284
diff changeset
    23
1341
99e79b000f30 Improved _text2Path, added _text2PathDescription
rgbecker
parents: 1328
diff changeset
    24
def _pathNumTrunc(n):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    25
    if int(n)==n: return int(n)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    26
    return round(n,5)
1341
99e79b000f30 Improved _text2Path, added _text2PathDescription
rgbecker
parents: 1328
diff changeset
    27
99e79b000f30 Improved _text2Path, added _text2PathDescription
rgbecker
parents: 1328
diff changeset
    28
def _processGlyph(G, truncate=1, pathReverse=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    29
    O = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    30
    P = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    31
    R = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    32
    for g in G+(('end',),):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    33
        op = g[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    34
        if O and op in ['moveTo', 'moveToClosed','end']:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    35
            if O[0]=='moveToClosed':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    36
                O = O[1:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    37
                if pathReverse:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3648
diff changeset
    38
                    for i in range(0,len(P),2):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    39
                        P[i+1], P[i] = P[i:i+2]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    40
                    P.reverse()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    41
                    O.reverse()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    42
                O.insert(0,'moveTo')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    43
                O.append('closePath')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    44
            i = 0
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3648
diff changeset
    45
            if truncate: P = list(map(_pathNumTrunc,P))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    46
            for o in O:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    47
                j = i + _PATH_OP_ARG_COUNT[_PATH_OP_NAMES.index(o)]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    48
                if o=='closePath':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    49
                    R.append(o)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    50
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    51
                    R.append((o,)+ tuple(P[i:j]))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    52
                i = j
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    53
            O = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    54
            P = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    55
        O.append(op)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    56
        P.extend(g[1:])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    57
    return R
1328
f8cb7553c9f5 added _text2Path & stroked Labels
rgbecker
parents: 1284
diff changeset
    58
3368
afa025c34493 reportlab: new base font mechanism more fully applied
rgbecker
parents: 3308
diff changeset
    59
def _text2PathDescription(text, x=0, y=0, fontName=_baseGFontName, fontSize=1000,
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    60
                            anchor='start', truncate=1, pathReverse=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    61
    global _gs
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    62
    if not _gs:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    63
        import _renderPM
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    64
        _gs = _renderPM.gstate(1,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    65
    from reportlab.graphics import renderPM
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    66
    renderPM._setFont(_gs,fontName,fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    67
    P = []
2868
4bcd244d8c3c textlabels.py: fix undefined variable bug reported by riek@zem.uni-bonn.de
rgbecker
parents: 2716
diff changeset
    68
    if not anchor=='start':
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    69
        textLen = stringWidth(text, fontName,fontSize)
2868
4bcd244d8c3c textlabels.py: fix undefined variable bug reported by riek@zem.uni-bonn.de
rgbecker
parents: 2716
diff changeset
    70
        if anchor=='end':
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    71
            x = x-textLen
2868
4bcd244d8c3c textlabels.py: fix undefined variable bug reported by riek@zem.uni-bonn.de
rgbecker
parents: 2716
diff changeset
    72
        elif anchor=='middle':
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    73
            x = x - textLen/2.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    74
    for g in _gs._stringPath(text,x,y):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    75
        P.extend(_processGlyph(g,truncate=truncate,pathReverse=pathReverse))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    76
    return P
1341
99e79b000f30 Improved _text2Path, added _text2PathDescription
rgbecker
parents: 1328
diff changeset
    77
3368
afa025c34493 reportlab: new base font mechanism more fully applied
rgbecker
parents: 3308
diff changeset
    78
def _text2Path(text, x=0, y=0, fontName=_baseGFontName, fontSize=1000,
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    79
                anchor='start', truncate=1, pathReverse=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    80
    return definePath(_text2PathDescription(text,x=x,y=y,fontName=fontName,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    81
                    fontSize=fontSize,anchor=anchor,truncate=truncate,pathReverse=pathReverse))
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
    82
1807
b7c571a2e141 Minor enhancements to SpiderChart/Label
rgbecker
parents: 1798
diff changeset
    83
_BA2TA={'w':'start','nw':'start','sw':'start','e':'end', 'ne': 'end', 'se':'end', 'n':'middle','s':'middle','c':'middle'}
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
    84
class Label(Widget):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    85
    """A text label to attach to something else, such as a chart axis.
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
    86
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    87
    This allows you to specify an offset, angle and many anchor
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    88
    properties relative to the label's origin.  It allows, for example,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    89
    angled multiline axis labels.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    90
    """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    91
    # fairly straight port of Robin Becker's textbox.py to new widgets
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    92
    # framework.
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
    93
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
    94
    _attrMap = AttrMap(
3271
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
    95
        x = AttrMapValue(isNumber,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
    96
        y = AttrMapValue(isNumber,desc=''),
3308
500d4e7a9ab5 label help_text corrected
meitham
parents: 3271
diff changeset
    97
        dx = AttrMapValue(isNumber,desc='delta x - offset'),
500d4e7a9ab5 label help_text corrected
meitham
parents: 3271
diff changeset
    98
        dy = AttrMapValue(isNumber,desc='delta y - offset'),
3271
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
    99
        angle = AttrMapValue(isNumber,desc='angle of label: default (0), 90 is vertical, 180 is upside down, etc'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   100
        boxAnchor = AttrMapValue(isBoxAnchor,desc='anchoring point of the label'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   101
        boxStrokeColor = AttrMapValue(isColorOrNone,desc='border color of the box'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   102
        boxStrokeWidth = AttrMapValue(isNumber,desc='border width'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   103
        boxFillColor = AttrMapValue(isColorOrNone,desc='the filling color of the box'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   104
        boxTarget = AttrMapValue(OneOf('normal','anti','lo','hi'),desc="one of ('normal','anti','lo','hi')"),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   105
        fillColor = AttrMapValue(isColorOrNone,desc='label text color'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   106
        strokeColor = AttrMapValue(isColorOrNone,desc='label text border color'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   107
        strokeWidth = AttrMapValue(isNumber,desc='label text border width'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   108
        text = AttrMapValue(isString,desc='the actual text to display'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   109
        fontName = AttrMapValue(isString,desc='the name of the font used'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   110
        fontSize = AttrMapValue(isNumber,desc='the size of the font'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   111
        leading = AttrMapValue(isNumberOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   112
        width = AttrMapValue(isNumberOrNone,desc='the width of the label'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   113
        maxWidth = AttrMapValue(isNumberOrNone,desc='maximum width the label can grow to'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   114
        height = AttrMapValue(isNumberOrNone,desc='the height of the text'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   115
        textAnchor = AttrMapValue(isTextAnchor,desc='the anchoring point of the text inside the label'),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   116
        visible = AttrMapValue(isBoolean,desc="True if the label is to be drawn"),
3271
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   117
        topPadding = AttrMapValue(isNumber,desc='padding at top of box'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   118
        leftPadding = AttrMapValue(isNumber,desc='padding at left of box'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   119
        rightPadding = AttrMapValue(isNumber,desc='padding at right of box'),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   120
        bottomPadding = AttrMapValue(isNumber,desc='padding at bottom of box'),
3628
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   121
        useAscentDescent = AttrMapValue(isBoolean,desc="If True then the font's Ascent & Descent will be used to compute default heights and baseline."),
3645
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   122
        customDrawChanger = AttrMapValue(isNoneOrCallable,desc="An instance of CustomDrawChanger to modify the behavior at draw time", _advancedUsage=1),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   123
        )
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   124
2362
338ddcc1b3a5 textlabels.py: added keyword arguments
rgbecker
parents: 2332
diff changeset
   125
    def __init__(self,**kw):
2363
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   126
        self._setKeywords(**kw)
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   127
        self._setKeywords(
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   128
                _text = 'Multi-Line\nString',
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   129
                boxAnchor = 'c',
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   130
                angle = 0,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   131
                x = 0,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   132
                y = 0,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   133
                dx = 0,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   134
                dy = 0,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   135
                topPadding = 0,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   136
                leftPadding = 0,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   137
                rightPadding = 0,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   138
                bottomPadding = 0,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   139
                boxStrokeWidth = 0.5,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   140
                boxStrokeColor = None,
2511
83406bfe8569 merge 2643:2644 from stable textlabels.py, barcharts.py: added boxTarget support
rgbecker
parents: 2363
diff changeset
   141
                boxTarget = 'normal',
2363
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   142
                strokeColor = None,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   143
                boxFillColor = None,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   144
                leading = None,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   145
                width = None,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   146
                maxWidth = None,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   147
                height = None,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   148
                fillColor = STATE_DEFAULTS['fillColor'],
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   149
                fontName = STATE_DEFAULTS['fontName'],
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   150
                fontSize = STATE_DEFAULTS['fontSize'],
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   151
                strokeWidth = 0.1,
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   152
                textAnchor = 'start',
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   153
                visible = 1,
3628
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   154
                useAscentDescent = False,
2363
d541aeb182a1 remove 2.2 isms
rgbecker
parents: 2362
diff changeset
   155
                )
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   156
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   157
    def setText(self, text):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   158
        """Set the text property.  May contain embedded newline characters.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   159
        Called by the containing chart or axis."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   160
        self._text = text
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   161
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   162
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   163
    def setOrigin(self, x, y):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   164
        """Set the origin.  This would be the tick mark or bar top relative to
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   165
        which it is defined.  Called by the containing chart or axis."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   166
        self.x = x
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   167
        self.y = y
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   168
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   169
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   170
    def demo(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   171
        """This shows a label positioned with its top right corner
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   172
        at the top centre of the drawing, and rotated 45 degrees."""
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   173
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   174
        d = Drawing(200, 100)
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   175
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   176
        # mark the origin of the label
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   177
        d.add(Circle(100,90, 5, fillColor=colors.green))
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   178
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   179
        lab = Label()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   180
        lab.setOrigin(100,90)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   181
        lab.boxAnchor = 'ne'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   182
        lab.angle = 45
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   183
        lab.dx = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   184
        lab.dy = -20
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   185
        lab.boxStrokeColor = colors.green
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   186
        lab.setText('Another\nMulti-Line\nString')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   187
        d.add(lab)
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   188
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   189
        return d
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   190
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   191
    def _getBoxAnchor(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   192
        '''hook for allowing special box anchor effects'''
2173
34351e616e7c Allow autox/autoy boxanchor
rgbecker
parents: 1951
diff changeset
   193
        ba = self.boxAnchor
34351e616e7c Allow autox/autoy boxanchor
rgbecker
parents: 1951
diff changeset
   194
        if ba in ('autox', 'autoy'):
34351e616e7c Allow autox/autoy boxanchor
rgbecker
parents: 1951
diff changeset
   195
            angle = self.angle
34351e616e7c Allow autox/autoy boxanchor
rgbecker
parents: 1951
diff changeset
   196
            na = (int((angle%360)/45.)*45)%360
34351e616e7c Allow autox/autoy boxanchor
rgbecker
parents: 1951
diff changeset
   197
            if not (na % 90): # we have a right angle case
34351e616e7c Allow autox/autoy boxanchor
rgbecker
parents: 1951
diff changeset
   198
                da = (angle - na) % 360
34351e616e7c Allow autox/autoy boxanchor
rgbecker
parents: 1951
diff changeset
   199
                if abs(da)>5:
34351e616e7c Allow autox/autoy boxanchor
rgbecker
parents: 1951
diff changeset
   200
                    na = na + (da>0 and 45 or -45)
34351e616e7c Allow autox/autoy boxanchor
rgbecker
parents: 1951
diff changeset
   201
            ba = _A2BA[ba[-1]][na]
34351e616e7c Allow autox/autoy boxanchor
rgbecker
parents: 1951
diff changeset
   202
        return ba
1263
20e63f4a39f4 Added maxWidth and auto splitting
rgbecker
parents: 1251
diff changeset
   203
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   204
    def computeSize(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   205
        # the thing will draw in its own coordinate system
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   206
        self._lineWidths = []
1951
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   207
        topPadding = self.topPadding
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   208
        leftPadding = self.leftPadding
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   209
        rightPadding = self.rightPadding
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   210
        bottomPadding = self.bottomPadding
2716
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2698
diff changeset
   211
        self._lines = simpleSplit(self._text,self.fontName,self.fontSize,self.maxWidth)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   212
        if not self.width:
2926
bd9b2e9f21b4 textlabels.py: fix bug found by Martin Tate
rgbecker
parents: 2868
diff changeset
   213
            self._width = leftPadding+rightPadding
bd9b2e9f21b4 textlabels.py: fix bug found by Martin Tate
rgbecker
parents: 2868
diff changeset
   214
            if self._lines:
bd9b2e9f21b4 textlabels.py: fix bug found by Martin Tate
rgbecker
parents: 2868
diff changeset
   215
                self._lineWidths = [stringWidth(line,self.fontName,self.fontSize) for line in self._lines]
bd9b2e9f21b4 textlabels.py: fix bug found by Martin Tate
rgbecker
parents: 2868
diff changeset
   216
                self._width += max(self._lineWidths)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   217
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   218
            self._width = self.width
3628
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   219
        if self.useAscentDescent:
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   220
            self._ascent, self._descent = getAscentDescent(self.fontName,self.fontSize)
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   221
            self._baselineRatio = self._ascent/(self._ascent-self._descent)
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   222
        else:
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   223
            self._baselineRatio = 1/1.2
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   224
        if self.leading:
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   225
            self._leading = self.leading
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   226
        elif self.useAscentDescent:
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   227
            self._leading = self._ascent - self._descent
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   228
        else:
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   229
            self._leading = self.fontSize*1.2
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   230
        self._height = self.height or (self._leading*len(self._lines) + topPadding + bottomPadding)
1951
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   231
        self._ewidth = (self._width-leftPadding-rightPadding)
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   232
        self._eheight = (self._height-topPadding-bottomPadding)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   233
        boxAnchor = self._getBoxAnchor()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   234
        if boxAnchor in ['n','ne','nw']:
1951
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   235
            self._top = -topPadding
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   236
        elif boxAnchor in ['s','sw','se']:
1951
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   237
            self._top = self._height-topPadding
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   238
        else:
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2173
diff changeset
   239
            self._top = 0.5*self._eheight
1951
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   240
        self._bottom = self._top - self._eheight
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   241
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   242
        if boxAnchor in ['ne','e','se']:
1951
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   243
            self._left = leftPadding - self._width
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   244
        elif boxAnchor in ['nw','w','sw']:
1951
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   245
            self._left = leftPadding
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   246
        else:
1951
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   247
            self._left = -self._ewidth*0.5
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   248
        self._right = self._left+self._ewidth
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   249
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   250
    def _getTextAnchor(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   251
        '''This can be overridden to allow special effects'''
1807
b7c571a2e141 Minor enhancements to SpiderChart/Label
rgbecker
parents: 1798
diff changeset
   252
        ta = self.textAnchor
2173
34351e616e7c Allow autox/autoy boxanchor
rgbecker
parents: 1951
diff changeset
   253
        if ta=='boxauto': ta = _BA2TA[self._getBoxAnchor()]
1807
b7c571a2e141 Minor enhancements to SpiderChart/Label
rgbecker
parents: 1798
diff changeset
   254
        return ta
1251
84228daac0a8 Flip mode bar labels
rgbecker
parents: 1247
diff changeset
   255
3645
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   256
    def _rawDraw(self):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   257
        _text = self._text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   258
        self._text = _text or ''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   259
        self.computeSize()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   260
        self._text = _text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   261
        g = Group()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   262
        g.translate(self.x + self.dx, self.y + self.dy)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   263
        g.rotate(self.angle)
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   264
3628
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   265
        y = self._top - self._leading*self._baselineRatio
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   266
        textAnchor = self._getTextAnchor()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   267
        if textAnchor == 'start':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   268
            x = self._left
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   269
        elif textAnchor == 'middle':
1951
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   270
            x = self._left + self._ewidth*0.5
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   271
        else:
1951
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   272
            x = self._right
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   273
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   274
        # paint box behind text just in case they
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   275
        # fill it
1798
fb9411feaa17 Minor efficiency saving
rgbecker
parents: 1683
diff changeset
   276
        if self.boxFillColor or (self.boxStrokeColor and self.boxStrokeWidth):
1951
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   277
            g.add(Rect( self._left-self.leftPadding,
35815d13a326 Added padding to Label
rgbecker
parents: 1808
diff changeset
   278
                        self._bottom-self.bottomPadding,
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   279
                        self._width,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   280
                        self._height,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   281
                        strokeColor=self.boxStrokeColor,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   282
                        strokeWidth=self.boxStrokeWidth,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   283
                        fillColor=self.boxFillColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   284
                        )
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   285
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   286
        fillColor, fontName, fontSize = self.fillColor, self.fontName, self.fontSize
3628
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   287
        strokeColor, strokeWidth, leading = self.strokeColor, self.strokeWidth, self._leading
3635
5e6f12790777 more support for passing stuff through to SVG
rgbecker
parents: 3628
diff changeset
   288
        svgAttrs=getattr(self,'_svgAttrs',{})
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   289
        if strokeColor:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   290
            for line in self._lines:
3646
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   291
                s = _text2Path(line, x, y, fontName, fontSize, textAnchor)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   292
                s.fillColor = fillColor
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   293
                s.strokeColor = strokeColor
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   294
                s.strokeWidth = strokeWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   295
                g.add(s)
3628
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   296
                y -= leading
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   297
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   298
            for line in self._lines:
3635
5e6f12790777 more support for passing stuff through to SVG
rgbecker
parents: 3628
diff changeset
   299
                s = String(x, y, line, _svgAttrs=svgAttrs)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   300
                s.textAnchor = textAnchor
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   301
                s.fontName = fontName
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   302
                s.fontSize = fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   303
                s.fillColor = fillColor
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   304
                g.add(s)
3628
4843650935df textlabels.py: add useAscentDescent option to Label class
rgbecker
parents: 3617
diff changeset
   305
                y -= leading
737
8f0e58918da9 Initial checkin, replacing previous trailing digit filenames.
dinu_gherman
parents:
diff changeset
   306
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   307
        return g
1203
25258d23982f Improved barchart labelling, added BarChartLabel
rgbecker
parents: 1202
diff changeset
   308
3645
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   309
    def draw(self):
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   310
        customDrawChanger = getattr(self,'customDrawChanger',None)
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   311
        if customDrawChanger:
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   312
            customDrawChanger(True,self)
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   313
            try:
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   314
                return self._rawDraw()
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   315
            finally:
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   316
                customDrawChanger(False,self)
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   317
        else:
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   318
            return self._rawDraw()
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   319
1453
53b7dda76ffd Changes needed for labeldecorator
rgbecker
parents: 1341
diff changeset
   320
class LabelDecorator:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   321
    _attrMap = AttrMap(
3271
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   322
        x = AttrMapValue(isNumberOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   323
        y = AttrMapValue(isNumberOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   324
        dx = AttrMapValue(isNumberOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   325
        dy = AttrMapValue(isNumberOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   326
        angle = AttrMapValue(isNumberOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   327
        boxAnchor = AttrMapValue(isBoxAnchor,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   328
        boxStrokeColor = AttrMapValue(isColorOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   329
        boxStrokeWidth = AttrMapValue(isNumberOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   330
        boxFillColor = AttrMapValue(isColorOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   331
        fillColor = AttrMapValue(isColorOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   332
        strokeColor = AttrMapValue(isColorOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   333
        strokeWidth = AttrMapValue(isNumberOrNone),desc='',
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   334
        fontName = AttrMapValue(isNoneOrString,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   335
        fontSize = AttrMapValue(isNumberOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   336
        leading = AttrMapValue(isNumberOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   337
        width = AttrMapValue(isNumberOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   338
        maxWidth = AttrMapValue(isNumberOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   339
        height = AttrMapValue(isNumberOrNone,desc=''),
c14b161b62bf attributes help messages
meitham
parents: 3220
diff changeset
   340
        textAnchor = AttrMapValue(isTextAnchor,desc=''),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   341
        visible = AttrMapValue(isBoolean,desc="True if the label is to be drawn"),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   342
        )
1453
53b7dda76ffd Changes needed for labeldecorator
rgbecker
parents: 1341
diff changeset
   343
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   344
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   345
        self.textAnchor = 'start'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   346
        self.boxAnchor = 'w'
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   347
        for a in self._attrMap.keys():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   348
            if not hasattr(self,a): setattr(self,a,None)
1453
53b7dda76ffd Changes needed for labeldecorator
rgbecker
parents: 1341
diff changeset
   349
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   350
    def decorate(self,l,L):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   351
        chart,g,rowNo,colNo,x,y,width,height,x00,y00,x0,y0 = l._callOutInfo
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   352
        L.setText(chart.categoryAxis.categoryNames[colNo])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   353
        g.add(L)
1453
53b7dda76ffd Changes needed for labeldecorator
rgbecker
parents: 1341
diff changeset
   354
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   355
    def __call__(self,l):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   356
        from copy import deepcopy
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   357
        L = Label()
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   358
        for a,v in self.__dict__.items():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   359
            if v is None: v = getattr(l,a,None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   360
            setattr(L,a,v)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   361
        self.decorate(l,L)
1453
53b7dda76ffd Changes needed for labeldecorator
rgbecker
parents: 1341
diff changeset
   362
1251
84228daac0a8 Flip mode bar labels
rgbecker
parents: 1247
diff changeset
   363
isOffsetMode=OneOf('high','low','bar','axis')
1247
6f7061d9dd0c Working LabelOffset usage, still need variable styles
rgbecker
parents: 1245
diff changeset
   364
class LabelOffset(PropHolder):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   365
    _attrMap = AttrMap(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   366
                posMode = AttrMapValue(isOffsetMode,desc="Where to base +ve offset"),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   367
                pos = AttrMapValue(isNumber,desc='Value for positive elements'),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   368
                negMode = AttrMapValue(isOffsetMode,desc="Where to base -ve offset"),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   369
                neg = AttrMapValue(isNumber,desc='Value for negative elements'),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   370
                )
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   371
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   372
        self.posMode=self.negMode='axis'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   373
        self.pos = self.neg = 0
1247
6f7061d9dd0c Working LabelOffset usage, still need variable styles
rgbecker
parents: 1245
diff changeset
   374
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   375
    def _getValue(self, chart, val):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   376
        flipXY = chart._flipXY
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   377
        A = chart.categoryAxis
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   378
        jA = A.joinAxis
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   379
        if val>=0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   380
            mode = self.posMode
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   381
            delta = self.pos
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   382
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   383
            mode = self.negMode
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   384
            delta = self.neg
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   385
        if flipXY:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   386
            v = A._x
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   387
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   388
            v = A._y
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   389
        if jA:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   390
            if flipXY:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   391
                _v = jA._x
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   392
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   393
                _v = jA._y
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   394
            if mode=='high':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   395
                v = _v + jA._length
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   396
            elif mode=='low':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   397
                v = _v
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   398
            elif mode=='bar':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   399
                v = _v+val
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   400
        return v+delta
1247
6f7061d9dd0c Working LabelOffset usage, still need variable styles
rgbecker
parents: 1245
diff changeset
   401
1284
cc913e2cea24 Syncing with on-site changes and adding NA_Label
rgbecker
parents: 1263
diff changeset
   402
NoneOrInstanceOfLabelOffset=NoneOr(isInstanceOf(LabelOffset))
1247
6f7061d9dd0c Working LabelOffset usage, still need variable styles
rgbecker
parents: 1245
diff changeset
   403
3646
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   404
class PMVLabel(Label):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   405
    _attrMap = AttrMap(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   406
        BASE=Label,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   407
        )
1203
25258d23982f Improved barchart labelling, added BarChartLabel
rgbecker
parents: 1202
diff changeset
   408
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   409
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   410
        Label.__init__(self)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   411
        self._pmv = 0
1251
84228daac0a8 Flip mode bar labels
rgbecker
parents: 1247
diff changeset
   412
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   413
    def _getBoxAnchor(self):
3648
29290d936f9c textlabels.py: call super methods in PMVLabel _get Box/Text Anchor
robin
parents: 3646
diff changeset
   414
        a = Label._getBoxAnchor(self)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   415
        if self._pmv<0: a = {'nw':'se','n':'s','ne':'sw','w':'e','c':'c','e':'w','sw':'ne','s':'n','se':'nw'}[a]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   416
        return a
1251
84228daac0a8 Flip mode bar labels
rgbecker
parents: 1247
diff changeset
   417
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   418
    def _getTextAnchor(self):
3648
29290d936f9c textlabels.py: call super methods in PMVLabel _get Box/Text Anchor
robin
parents: 3646
diff changeset
   419
        a = Label._getTextAnchor(self)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   420
        if self._pmv<0: a = {'start':'end', 'middle':'middle', 'end':'start'}[a]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   421
        return a
1284
cc913e2cea24 Syncing with on-site changes and adding NA_Label
rgbecker
parents: 1263
diff changeset
   422
3646
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   423
class BarChartLabel(PMVLabel):
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   424
    """
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   425
    An extended Label allowing for nudging, lines visibility etc
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   426
    """
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   427
    _attrMap = AttrMap(
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   428
        BASE=PMVLabel,
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   429
        lineStrokeWidth = AttrMapValue(isNumberOrNone, desc="Non-zero for a drawn line"),
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   430
        lineStrokeColor = AttrMapValue(isColorOrNone, desc="Color for a drawn line"),
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   431
        fixedEnd = AttrMapValue(NoneOrInstanceOfLabelOffset, desc="None or fixed draw ends +/-"),
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   432
        fixedStart = AttrMapValue(NoneOrInstanceOfLabelOffset, desc="None or fixed draw starts +/-"),
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   433
        nudge = AttrMapValue(isNumber, desc="Non-zero sign dependent nudge"),
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   434
        )
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   435
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   436
    def __init__(self):
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   437
        PMVLabel.__init__(self)
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   438
        self.lineStrokeWidth = 0
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   439
        self.lineStrokeColor = None
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   440
        self.fixedStart = self.fixedEnd = None
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   441
        self.nudge = 0
a57aaae1144d textlabels.py: fix call of text2path, add PMVLabel
robin
parents: 3645
diff changeset
   442
1284
cc913e2cea24 Syncing with on-site changes and adding NA_Label
rgbecker
parents: 1263
diff changeset
   443
class NA_Label(BarChartLabel):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   444
    """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   445
    An extended Label allowing for nudging, lines visibility etc
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   446
    """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   447
    _attrMap = AttrMap(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   448
        BASE=BarChartLabel,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   449
        text = AttrMapValue(isNoneOrString, desc="Text to be used for N/A values"),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   450
        )
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   451
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   452
        BarChartLabel.__init__(self)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1558
diff changeset
   453
        self.text = 'n/a'
1798
fb9411feaa17 Minor efficiency saving
rgbecker
parents: 1683
diff changeset
   454
NoneOrInstanceOfNA_Label=NoneOr(isInstanceOf(NA_Label))
3645
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   455
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   456
from reportlab.graphics.charts.utils import CustomDrawChanger
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   457
class RedNegativeChanger(CustomDrawChanger):
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   458
    def __init__(self,fillColor=colors.red):
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   459
        CustomDrawChanger.__init__(self)
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   460
        self.fillColor = fillColor
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   461
    def _changer(self,obj):
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   462
        R = {}
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   463
        if obj._text.startswith('-'):
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   464
            R['fillColor'] = obj.fillColor
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   465
            obj.fillColor = self.fillColor
388ffb1a42df graphics/charts: added CustomDrawChanger class
robin
parents: 3635
diff changeset
   466
        return R