Question

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

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()

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

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.

Here is my code.

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) 

EDIT

Pie Chart

I'm referring this link for generation of Pie Chart. In this the colors are pre-defined for each data, ie 10 colors for 10 data points. What if I have 11 data points and then there will be no color for that 11th data point. I want something dynamic which should automatically pick a color by itself and plot it on the pie.

I found this link and this link, but the colors over here are not catchy and not at all good.

Can you please brief me about how to get catchy colors in pie graph?

Thanks

Was it helpful?

Solution

You could use renderPDF to do this like so:

from reportlab.pdfgen import canvas
from reportlab.graphics import renderPDF
from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.charts.barcharts import VerticalBarChart
from reportlab.graphics.widgets.markers import makeMarker

def bar_chart():
    drawing = Drawing(400, 200)
    # ...
    drawing.add(bc)
    return drawing


def line_plot():
    drawing = Drawing(400, 200)
    # ...
    drawing.add(lp)
    return drawing


the_canvas = canvas.Canvas("output.pdf")
renderPDF.draw(bar_chart(), the_canvas, 0, 0)
the_canvas.showPage()
renderPDF.draw(line_plot(), the_canvas, 0, 0)
the_canvas.showPage()
the_canvas.save()

The arguments to renderPDF.draw() are (in order) the Drawing object you already know how to create, the canvas onto which you want to paste the drawing and the horizontal and vertical position of the drawing on the page (as measured from the bottom left).

Cutting the PDF to size I leave as an exercise to you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top