Python- Reportlabs-在2个不同的页面中保存2个不同的图形? [英] Python- Reportlabs - save 2 different graphs in 2 different pages?

查看:87
本文介绍了Python- Reportlabs-在2个不同的页面中保存2个不同的图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我在PDF内绘制了垂直条形图和折线图.

I've the following code where I'm drawing a vertical bar graph and a line graph as well inside a PDF.

如何将这2个图形保存在PDF的2个不同页面中.我看到可以使用-

How do I save these 2 graphs in 2 different pages of the PDF. I saw that it can be done using -

c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()

但是,不是使用Canvas,而是使用其中不存在showPage()方法的Drawing对象.

But, instead of using Canvas, I'm using Drawing object in which showPage() method does not exist.

如何将2个图形保存在PDF的2个不同页面中?第二个图形(折线图)与第一个图形(垂直条形图)重叠,从而阻碍了该条形图.

How do I save the 2 graphs in 2 different pages of the PDF? Right the second graph(line chart) overlaps the first graph (vertical bar chart), thereby hindering the bar chart.

这是我的代码.

from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.barcharts import VerticalBarChart

drawing = Drawing(400, 200)
data = [
(13, 5, 20, 22, 37, 45, 19, 4),
(14, 6, 21, 23, 38, 46, 20, 5)
]
bc = VerticalBarChart()
bc.x = 50
bc.y = 50
bc.height = 125
bc.width = 300
bc.data = data
#bc.strokeColor = colors.black
bc.valueAxis.valueMin = 0
bc.valueAxis.valueMax = 50
bc.valueAxis.valueStep = 10
bc.categoryAxis.labels.boxAnchor = 'ne'
bc.categoryAxis.labels.dx = 8
bc.categoryAxis.labels.dy = -2
bc.categoryAxis.labels.angle = 30
bc.categoryAxis.categoryNames = ['Jan-99','Feb-99','Mar-99',
'Apr-99','May-99','Jun-99','Jul-99','Aug-99']

drawing.add(bc)
drawing.save()

from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.widgets.markers import makeMarker

drawing = Drawing(400, 200)
data = [
((1,1), (2,2), (2.5,1), (3,3), (4,5)),
((1,2), (2,3), (2.5,2), (3.5,5), (4,6))
]
lp = LinePlot()
lp.x = 50
lp.y = 50
lp.height = 125
lp.width = 300
lp.data = data
lp.joinedLines = 1
lp.lines[0].symbol = makeMarker('FilledCircle')
lp.lines[1].symbol = makeMarker('Circle')
lp.lineLabelFormat = '%2.0f'
#lp.strokeColor = colors.black
lp.xValueAxis.valueMin = 0
lp.xValueAxis.valueMax = 5
lp.xValueAxis.valueSteps = [1, 2, 2.5, 3, 4, 5]
lp.xValueAxis.labelTextFormat = '%2.1f'
lp.yValueAxis.valueMin = 0
lp.yValueAxis.valueMax = 7
lp.yValueAxis.valueSteps = [1, 2, 3, 5, 6]
drawing.add(lp)
drawing.save()
drawing.save(formats=['pdf'],outDir='.',fnRoot=None) 

推荐答案

制作画布并在其上渲染图形:

Make a canvas and render your drawings on it:

from reportlab.pdfgen import canvas
from reportlab.graphics import renderPDF

c = canvas.Canvas('hi.pdf')

# your drawing
# drawing = Drawing(400, 200)

x, y = 0, 0 # coordinates (from left bottom)
renderPDF.draw(drawing, c, x, y, showBoundary=False)

c.showPage() # to end a page and start a new one

# extra pages content

c.save() # to save :D the whole document

希望它会有所帮助:)

这篇关于Python- Reportlabs-在2个不同的页面中保存2个不同的图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆