【发布时间】:2018-12-26 07:20:40
【问题描述】:
我正在使用 reportlab 创建图表。所以我想生成一个 eps 格式的文档,我想在其中添加一个带有图表的背景图像。当我尝试将背景图像添加到文档而不是作为背景时,它会堆叠在图表下方。 当我运行 pdf 时使用相同的逻辑它可以工作。为了找出问题,我浏览了reportlab库中的renderPS.py和renderPDF.py,它们分别是用于创建EPS和PDF的文件。 在 renderPDF 中,我们使用 drawInlineImage() 在图表中生成内联图像,但对于 renderPS 则不存在。 路径是正确的,因为我可以对 PDF 执行相同的操作,并且图像大小也是正确的。我也无法将 drawInlineImage 函数添加到 renderPS.py。
import reportlab
from reportlab.graphics.charts.barcharts import VerticalBarChart
from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin,Image
from reportlab.lib.colors import blue, PCMYKColor
from reportlab.graphics.charts.textlabels import LabelOffset
from reportlab.graphics.charts.legends import Legend
class VBarChartWLineBarLabels01(_DrawingEditorMixin,Drawing):
def __init__(self,width=200,height=300,*args,**kw):
Drawing.__init__(self,width,height,*args,**kw)
self._add(self,VerticalBarChart(),name='chart',validate=None,desc=None)
self._add(self,Legend(),name='legend',validate=None,desc=None)
self.legend.columnMaximum = 2
self.legend.alignment='right'
self.legend.dx = 6
self.legend.dy = 6
self.legend.dxTextSpace = 5
self.legend.deltay = 10
self.legend.strokeWidth = 0
self.legend.strokeColor = None
self.legend.subCols[0].minWidth = 75
self.legend.subCols[0].align = 'left'
self.legend.subCols[1].minWidth = 25
self.legend.subCols[1].align = 'right'
self.legend.boxAnchor = 'c'
self.legend.y = 150
self.legend.colorNamePairs = [(PCMYKColor(0,54,100,10,alpha=100), ('1985')),(PCMYKColor(100,60,0,50,alpha=100), ('2035'))]
self.width = 400
self.legend.x = 440
self.chart.y = 30
self.chart.x = 35
self.chart.width=self.width-self.chart.x - 60
self.chart.height=self.height-2*self.chart.y
self.chart.valueAxis.drawGridLast = False
self.chart.categoryAxis.drawGridLast = False
self.chart.valueAxis.valueMax = 30
self.chart.valueAxis.valueMin = 0
self.chart.valueAxis.valueStep = 5
self.chart.valueAxis.gridStrokeDashArray = None
self.chart.valueAxis.gridEnd = self.width-60
self.chart.valueAxis.gridStart = 35
self.chart.valueAxis.visibleGrid =1
self.chart.valueAxis.labelTextFormat = '%s%%'
self.chart.valueAxis.maximumTicks = 20
self.chart.valueAxis.minimumTickSpacing = 10
self.chart.valueAxis.gridStrokeColor = PCMYKColor(0,0,0,25,alpha=85)
self.chart.valueAxis.labels.dx = -10
self.chart.barWidth = 3
self.chart.groupSpacing = 5
self.chart.data = [(15,17,14,12,15),(23,26,25,23,23)]
self.chart.bars[0].fillColor = PCMYKColor(0,54,100,10,alpha=100)
self.chart.bars[1].fillColor = PCMYKColor(100,90,0,50,alpha=100)
self.chart.categoryAxis.categoryNames = ["England","Wales","Scotland","Northern\nIreland","UK"]
self.chart.categoryAxis.strokeColor = PCMYKColor(0,0,0,25,alpha=85)
self.chart.categoryAxis.labels.textAnchor='middle'
self.chart.valueAxis.strokeColor = PCMYKColor(0,0,0,25,alpha=85)
self.chart.bars.strokeColor = None
self.chart.bars.strokeWidth = 0
inPath = 'gepgraphic-allocation-piechart-map.jpg'
img = Image(0, 0, self.chart.width, self.chart.height, inPath)
self.chart.background = img
if __name__=="__main__": #NORUNTESTS
VBarChartWLineBarLabels01().save(formats=['eps'],outDir='.',fnRoot=None)
这是上述问题的代码,它也适用于 pdf,就在格式=['eps'] 的最后一行代码中,我们需要将 eps 替换为 pdf 并尝试运行我想要生成的文件的代码它以 eps 格式生成相同的内容。
【问题讨论】: