04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

416 CHAPTER 21 ■ PROJECT 2: PAINTING A PRETTY PICTURE<br />

The Prototype<br />

You now have what you need to write your first version of the program. The source code is<br />

shown in Listing 21-2.<br />

Listing 21-2. The First Prototype for the Sunspot Graph Program<br />

from reportlab.lib import colors<br />

from reportlab.graphics.shapes import *<br />

from reportlab.graphics import renderPDF<br />

data = [<br />

# Year Month Predicted High Low<br />

(2005, 8, 113.2, 114.2, 112.2),<br />

(2005, 9, 112.8, 115.8, 109.8),<br />

(2005, 10, 111.0, 116.0, 106.0),<br />

(2005, 11, 109.8, 116.8, 102.8),<br />

(2005, 12, 107.3, 115.3, 99.3),<br />

(2006, 1, 105.2, 114.2, 96.2),<br />

(2006, 2, 104.1, 114.1, 94.1),<br />

(2006, 3, 99.9, 110.9, 88.9),<br />

(2006, 4, 94.8, 106.8, 82.8),<br />

(2006, 5, 91.2, 104.2, 78.2),<br />

]<br />

drawing = Drawing(200, 150)<br />

pred = [row[2]-40 for row in data]<br />

high = [row[3]-40 for row in data]<br />

low = [row[4]-40 for row in data]<br />

times = [200*((row[0] + row[1]/12.0) - 2005)-110 for row in data]<br />

drawing.add(PolyLine(zip(times, pred), strokeColor=colors.blue))<br />

drawing.add(PolyLine(zip(times, high), strokeColor=colors.red))<br />

drawing.add(PolyLine(zip(times, low), strokeColor=colors.green))<br />

drawing.add(String(65, 115, 'Sunspots', fontSize=18, fillColor=colors.red))<br />

renderPDF.drawToFile(drawing, 'report1.<strong>pdf</strong>', 'Sunspots')<br />

As you can see, I have adjusted the values and time stamps to get the positioning right. The<br />

resulting drawing is shown in Figure 21-3.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!