src/reportlab/graphics/charts/areas.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4370 823a8c33ce43
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
1692
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
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/areas.py
3032
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
     4
4252
fe660f227cac changes for release 3.3.0
robin
parents: 4116
diff changeset
     5
__version__='3.3.0'
3032
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
     6
__doc__='''This module defines a Area mixin classes'''
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
     7
1692
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
     8
from reportlab.lib.validators import isNumber, isColor, isColorOrNone, isNoneOrShape
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
     9
from reportlab.graphics.widgetbase import Widget
2034
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    10
from reportlab.graphics.shapes import Rect, Group, Line, Polygon
1692
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    11
from reportlab.lib.attrmap import AttrMap, AttrMapValue
4116
cf49463fc067 Fix a bunch of undefined names. Mostly typos or missing imports.
Matthew Duggan <mgithub@guarana.org>
parents: 3617
diff changeset
    12
from reportlab.lib.colors import grey
1692
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    13
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    14
class PlotArea(Widget):
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    15
    "Abstract base class representing a chart's plot area, pretty unusable by itself."
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    16
    _attrMap = AttrMap(
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    17
        x = AttrMapValue(isNumber, desc='X position of the lower-left corner of the chart.'),
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    18
        y = AttrMapValue(isNumber, desc='Y position of the lower-left corner of the chart.'),
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    19
        width = AttrMapValue(isNumber, desc='Width of the chart.'),
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    20
        height = AttrMapValue(isNumber, desc='Height of the chart.'),
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    21
        strokeColor = AttrMapValue(isColorOrNone, desc='Color of the plot area border.'),
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    22
        strokeWidth = AttrMapValue(isNumber, desc='Width plot area border.'),
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    23
        fillColor = AttrMapValue(isColorOrNone, desc='Color of the plot area interior.'),
3269
87f51df3bc0b attributes changed to advanced usage level
meitham
parents: 3032
diff changeset
    24
        background = AttrMapValue(isNoneOrShape, desc='Handle to background object e.g. Rect(0,0,width,height).'),
1692
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    25
        debug = AttrMapValue(isNumber, desc='Used only for debugging.'),
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    26
        )
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    27
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    28
    def __init__(self):
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    29
        self.x = 20
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    30
        self.y = 10
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    31
        self.height = 85
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    32
        self.width = 180
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    33
        self.strokeColor = None
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    34
        self.strokeWidth = 1
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    35
        self.fillColor = None
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    36
        self.background = None
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    37
        self.debug = 0
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    38
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    39
    def makeBackground(self):
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    40
        if self.background is not None:
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    41
            BG = self.background
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    42
            if isinstance(BG,Group):
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    43
                g = BG
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    44
                for bg in g.contents:
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    45
                    bg.x = self.x
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    46
                    bg.y = self.y
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    47
                    bg.width = self.width
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    48
                    bg.height = self.height
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    49
            else:
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    50
                g = Group()
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    51
                if type(BG) not in (type(()),type([])): BG=(BG,)
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    52
                for bg in BG:
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    53
                    bg.x = self.x
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    54
                    bg.y = self.y
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    55
                    bg.width = self.width
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    56
                    bg.height = self.height
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    57
                    g.add(bg)
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    58
            return g
dc56f53c8322 Added areas.PlotArea for chart plot areas
rgbecker
parents:
diff changeset
    59
        else:
1796
04a7f2c96dda Minor saving
rgbecker
parents: 1692
diff changeset
    60
            strokeColor,strokeWidth,fillColor=self.strokeColor, self.strokeWidth, self.fillColor
04a7f2c96dda Minor saving
rgbecker
parents: 1692
diff changeset
    61
            if (strokeWidth and strokeColor) or fillColor:
04a7f2c96dda Minor saving
rgbecker
parents: 1692
diff changeset
    62
                g = Group()
2034
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    63
                _3d_dy = getattr(self,'_3d_dy',None)
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    64
                x = self.x
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    65
                y = self.y
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    66
                h = self.height
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    67
                w = self.width
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    68
                if _3d_dy is not None:
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    69
                    _3d_dx = self._3d_dx
2054
08302f68d88e Factorise axes a bit and make them more 3d aware
rgbecker
parents: 2034
diff changeset
    70
                    if fillColor and not strokeColor:
2034
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    71
                        from reportlab.lib.colors import Blacker
2054
08302f68d88e Factorise axes a bit and make them more 3d aware
rgbecker
parents: 2034
diff changeset
    72
                        c = Blacker(fillColor, getattr(self,'_3d_blacken',0.7))
08302f68d88e Factorise axes a bit and make them more 3d aware
rgbecker
parents: 2034
diff changeset
    73
                    else:
08302f68d88e Factorise axes a bit and make them more 3d aware
rgbecker
parents: 2034
diff changeset
    74
                        c = strokeColor
08302f68d88e Factorise axes a bit and make them more 3d aware
rgbecker
parents: 2034
diff changeset
    75
                    if not strokeWidth: strokeWidth = 0.5
08302f68d88e Factorise axes a bit and make them more 3d aware
rgbecker
parents: 2034
diff changeset
    76
                    if fillColor or strokeColor or c:
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2077
diff changeset
    77
                        bg = Polygon([x,y,x,y+h,x+_3d_dx,y+h+_3d_dy,x+w+_3d_dx,y+h+_3d_dy,x+w+_3d_dx,y+_3d_dy,x+w,y],
2054
08302f68d88e Factorise axes a bit and make them more 3d aware
rgbecker
parents: 2034
diff changeset
    78
                            strokeColor=strokeColor or c or grey, strokeWidth=strokeWidth, fillColor=fillColor)
2034
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    79
                        g.add(bg)
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    80
                        g.add(Line(x,y,x+_3d_dx,y+_3d_dy, strokeWidth=0.5, strokeColor=c))
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    81
                        g.add(Line(x+_3d_dx,y+_3d_dy, x+_3d_dx,y+h+_3d_dy,strokeWidth=0.5, strokeColor=c))
2073
39ee29211c8a Added dark floor in Plot Areas for use in 3D column charts
johnprecedo
parents: 2054
diff changeset
    82
                        fc = Blacker(c, getattr(self,'_3d_blacken',0.8))
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2077
diff changeset
    83
                        g.add(Polygon([x,y,x+_3d_dx,y+_3d_dy,x+w+_3d_dx,y+_3d_dy,x+w,y],
2073
39ee29211c8a Added dark floor in Plot Areas for use in 3D column charts
johnprecedo
parents: 2054
diff changeset
    84
                            strokeColor=strokeColor or c or grey, strokeWidth=strokeWidth, fillColor=fc))
2034
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    85
                        bg = Line(x+_3d_dx,y+_3d_dy, x+w+_3d_dx,y+_3d_dy,strokeWidth=0.5, strokeColor=c)
2054
08302f68d88e Factorise axes a bit and make them more 3d aware
rgbecker
parents: 2034
diff changeset
    86
                    else:
08302f68d88e Factorise axes a bit and make them more 3d aware
rgbecker
parents: 2034
diff changeset
    87
                        bg = None
2034
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    88
                else:
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    89
                    bg = Rect(x, y, w, h,
9c187d86543c Added _3d options for use with 3d charts
rgbecker
parents: 1796
diff changeset
    90
                        strokeColor=strokeColor, strokeWidth=strokeWidth, fillColor=fillColor)
2054
08302f68d88e Factorise axes a bit and make them more 3d aware
rgbecker
parents: 2034
diff changeset
    91
                if bg: g.add(bg)
1796
04a7f2c96dda Minor saving
rgbecker
parents: 1692
diff changeset
    92
                return g
04a7f2c96dda Minor saving
rgbecker
parents: 1692
diff changeset
    93
            else:
04a7f2c96dda Minor saving
rgbecker
parents: 1692
diff changeset
    94
                return None