概述
我使用Django和Reportlabs生成PDF格式的报告。我指的是this tutorial。在
我读了this thread和this thread,也就是说使用canv.showpage(),然后我就可以在1个pdf中组合2个图表,但是我仍然只得到代码中第二个图表,在我的例子中只有线图。在
如何将2个图表保存为1个pdf?在
这是我的密码。在import barchart
import linechart
from django.http import HttpResponse
from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.barcharts import VerticalBarChart
from reportlab.graphics import renderPDF
from reportlab.pdfgen import canvas
def generate_report(request):
#instantiate a drawing object
canv = canvas.Canvas('output.pdf')#,pagesize=LETTER)
canv.setPageCompression(0)
d = barchart.MyBarChartDrawing()
#extract the request params of interest.
#I suggest having a default for everything.
if 'height' in request:
d.height = int(request['height'])
if 'width' in request:
d.width = int(request['width'])
if 'numbers' in request:
strNumbers = request['numbers']
numbers = map(int, strNumbers.split(','))
d.chart.data = [numbers] #bar charts take a list-of-lists for data
if 'title' in request:
d.title.text = request['title']
#get a GIF (or PNG, JPG, or whatever)
binaryStuff = d.asString('pdf')
#binaryStuff.save()
#return HttpResponse(binaryStuff, 'image/pdf')
#instantiate a drawing object
canv.showPage()
a = linechart.MyLineChartDrawing()
#extract the request params of interest.
#I suggest having a default for everything.
a.height = 300
a.chart.height = 300
a.width = 300
a.chart.width = 300
a.title._text = request.session.get('Some custom title')
a.XLabel._text = request.session.get('X Axis Labell')
a.YLabel._text = request.session.get('Y Axis Label')
a.chart.data = [((1,1), (2,2), (2.5,1), (3,3), (4,5)),((1,2), (2,3), (2.5,2), (3.5,5), (4,6))]
labels = ["Label One","Label Two"]
if labels:
# set colors in the legend
a.Legend.colorNamePairs = []
for cnt,label in enumerate(labels):
a.Legend.colorNamePairs.append((a.chart.lines[cnt].strokeColor,label))
#get a GIF (or PNG, JPG, or whatever)
binaryStuff1 = a.asString('pdf')
canv.showPage()
return HttpResponse(binaryStuff, 'pdf')
条形图和折线图代码与this site中的相同。在
如何保存在一个pdf文件中?在
最后
以上就是失眠鸵鸟为你收集整理的python怎么合并图表_使用ReportLab将两个图表合并到一个PDF中?的全部内容,希望文章能够帮你解决python怎么合并图表_使用ReportLab将两个图表合并到一个PDF中?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复