Reportlab生成PDF文件

雷达图(Radar Chart)又被叫做蜘蛛网图,适用于显示三个或更多的维度的变量。雷达图是以在同一点开始的轴上显示的三个或更多个变量的二维图表的形式来显示多元数据的方法,其中轴的相对位置和角度通常是无意义的。

先做一个基础的雷达图:

对应的图表数据(data属性)是:

[(125, 180, 200), (100, 150, 180)]

图表的标签(labels)是:

['North', 'South', 'Central']

如果数据和标签改为:

[[10, 12, 14, 16, 14, 12], [6, 8, 10, 12, 9, 15], [7, 8, 17, 4, 12, 8]]

['U', 'V', 'W', 'X', 'Y', 'Z']

则图表变为:

蛛网可以填充起来:

        self.chart.strands[0].fillColor  = color01
        self.chart.strands[1].fillColor  = color02

填充效果:

 

轴线样式可以自定:

        self.chart.spokes.strokeDashArray = (2, 2)

图表为:

图表标签的排列顺序可以从顺时针转为逆时针:

        self.chart.direction = 'anticlockwise'

对比下面设置前后的图表:

蛛形图默认的起始角度为0,正北,旋转方向为顺时针。可以对图表进行旋转,如果旋转30度:

        self.chart.startAngle = 30

旋转前:

旋转后:

可以设置每张蛛网在轴线上的节点符号:

        self.chart.strands.symbol = "FilledDiamond"
        self.chart.strands.symbolSize = 3

也可以单独设置某一个节点符号:

        self.chart.strands[1].symbol = makeMarker("Circle")
        self.chart.strands[1].symbol.strokeWidth = 0.5
        self.chart.strands[1].symbol.fillColor = colors.yellow

代码和注释:

# RadarChart雷达图 or SpiderChart蜘蛛图
from reportlab.graphics.charts.legends import Legend
from reportlab.graphics.samples.excelcolors import *
from reportlab.graphics.charts.spider import SpiderChart
from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin
from reportlab.graphics.charts.textlabels import Label
from reportlab.lib import colors

class radarChart_radar(_DrawingEditorMixin,Drawing):
    def __init__(self,width=200,height=150,*args,**kw):
        Drawing.__init__(self,width,height,*args,**kw)
        self._add(self,SpiderChart(),name='chart',validate=None,desc="The main chart")
        self.chart.width      = 90
        self.chart.height     = 90
        self.chart.x          = 45
        self.chart.y          = 25
        # 雷达图多边形线条的颜色
        self.chart.strands[0].strokeColor= color01
        self.chart.strands[1].strokeColor= color02
        self.chart.strands[2].strokeColor= color03
        self.chart.strands[3].strokeColor= color04
        self.chart.strands[4].strokeColor= color05
        self.chart.strands[5].strokeColor= color06
        self.chart.strands[6].strokeColor= color07
        self.chart.strands[7].strokeColor= color08
        self.chart.strands[8].strokeColor= color09
        self.chart.strands[9].strokeColor= color10
        # 多边形内的填充色
        # self.chart.strands[0].fillColor  = color01
        # self.chart.strands[1].fillColor  = color02
        # self.chart.strands[2].fillColor  = color03
        # self.chart.strands[3].fillColor  = color04
        # self.chart.strands[4].fillColor  = color05
        # self.chart.strands[5].fillColor  = color06
        # self.chart.strands[6].fillColor  = color07
        # self.chart.strands[7].fillColor  = color08
        # self.chart.strands[8].fillColor  = color09
        # self.chart.strands[9].fillColor  = color10
        # 多边形线条的宽度
        self.chart.strands.strokeWidth   = 1
        # 轴条的样式
        # self.chart.spokes.strokeDashArray = (2, 2)

        # 图表的背景色
        self.chart.fillColor             = backgroundGrey
        # 图表代表的数据
        self.chart.data                  = [(125, 180, 200), (100, 150, 180)]
        # 标签内容
        self.chart.labels = ['North', 'South', 'Central']
        #
        # self.chart.data = [[10, 12, 14, 16, 14, 12], [6, 8, 10, 12, 9, 15], [7, 8, 17, 4, 12, 8]]
        # self.chart.labels = ['U', 'V', 'W', 'X', 'Y', 'Z']

        # 图表标签排列方向
        # self.chart.direction = 'anticlockwise'

        # 起始角度,默认为0,正北,旋转方向为顺时针
        # self.chart.startAngle = 30

        # 每张蛛网在轴线上的节点符号
        # self.chart.strands.symbol = "FilledDiamond"
        # self.chart.strands.symbolSize = 3
        # 单独设置某一蛛网跟轴线的节点符号
        # from reportlab.graphics.widgets.markers import makeMarker
        # self.chart.strands[1].symbol = makeMarker("Circle")
        # self.chart.strands[1].symbol.strokeWidth = 0.5
        # self.chart.strands[1].symbol.fillColor = colors.yellow

        # 线条标签的字体和字体大小
        self.chart.strandLabels.fontName = 'Helvetica'
        self.chart.strandLabels.fontSize = 5
        self.chart.strandLabels[0, 3]._text = 'special'
        self.chart.strandLabels[0, 1]._text = 'one'
        self.chart.strandLabels[0, 0]._text = 'zero'
        self.chart.strandLabels[1, 0]._text = 'Earth'
        self.chart.strandLabels[2, 2]._text = 'Mars'
        self.chart.strandLabels.format = 'values'
        # 微调标签与节点距离
        self.chart.strandLabels.dR = -5
        # 也可以单独调节某一个节点位置,比如'zero'被往上调了5
        self.chart.strandLabels[0, 0].dR = 5

        # 图表标题
        self._add(self,Label(),name='Title',validate=None,desc="The title at the top of the chart")
        # 标题字体和大小
        self.Title.fontName   = 'Helvetica-Bold'
        self.Title.fontSize   = 10
        # 标题坐标
        self.Title.x          = 100
        self.Title.y          = 130
        # 标题内容
        self.Title._text      = 'Radar'
        # 最大宽度
        self.Title.maxWidth   = 180
        # 高度
        self.Title.height     = 20
        # 居中对齐
        self.Title.textAnchor ='middle'

        self._add(self,Legend(),name='Legend',validate=None,desc="The legend or key for the chart")
        # 色块颜色与文字的一一对应
        self.Legend.colorNamePairs = [(color01, 'Widgets'), (color02, 'Sprockets')]
        # 图例文字字体和大小
        self.Legend.fontName       = 'Helvetica'
        self.Legend.fontSize       = 7
        # 默认锚点是nw,左上角,坐标是锚点的坐标
        self.Legend.x              = 153
        self.Legend.y              = 85
        # 文字与色块的间距
        self.Legend.dxTextSpace    = 5
        # 色块的宽高
        self.Legend.dy             = 5
        self.Legend.dx             = 5
        # 色块y方向的间距
        self.Legend.deltay         = 5
        # 文字在色块右
        self.Legend.alignment      ='right'

if __name__=="__main__":
    radarChart_radar().save(formats=['pdf'],outDir=None,fnRoot='radar')