Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional

16.01.2014 Views

CHAPTER 21 ■ PROJECT 2: PAINTING A PRETTY PICTURE 415 Constructing Some PolyLines To create a line diagram (a graph) of the sunspot data, you have to create some lines. In fact, you have to create several lines that are linked. ReportLab has a special class for this: PolyLine. A PolyLine is created with a list of coordinates as its first argument. This list is of the form [(x0, y0), (x1, y1), ...], with each pair of x and y coordinates making one point on the PolyLine. See Figure 21-2 for a simple PolyLine. Figure 21-2. PolyLine([(0, 0), (10, 0), (10, 10), (0, 10)]) To make a line diagram, one polyline must be created for each column in the data set. Each point in these polylines will consist of a time (constructed from the year and month) and a value (which is the number of sunspots taken from the relevant column). To get one of the columns (the values), list comprehensions can be useful: pred = [row[2] for row in data] Here pred (for “predicted”) will be a list of all the values in the third column of the data. You can use a similar strategy for the other columns. (The time for each row would have to be calculated from both the year and month: for example, year + month/12.) Once you have the values and the time stamps, you can add your polylines to the drawing like this: drawing.add(PolyLine(zip(times, pred), strokeColor=colors.blue)) It isn’t necessary to set the stroke color, of course, but it makes it easier to tell the lines apart. (Note how zip is used to combine the times and values into a list of tuples.)

416 CHAPTER 21 ■ PROJECT 2: PAINTING A PRETTY PICTURE The Prototype You now have what you need to write your first version of the program. The source code is shown in Listing 21-2. Listing 21-2. The First Prototype for the Sunspot Graph Program from reportlab.lib import colors from reportlab.graphics.shapes import * from reportlab.graphics import renderPDF data = [ # Year Month Predicted High Low (2005, 8, 113.2, 114.2, 112.2), (2005, 9, 112.8, 115.8, 109.8), (2005, 10, 111.0, 116.0, 106.0), (2005, 11, 109.8, 116.8, 102.8), (2005, 12, 107.3, 115.3, 99.3), (2006, 1, 105.2, 114.2, 96.2), (2006, 2, 104.1, 114.1, 94.1), (2006, 3, 99.9, 110.9, 88.9), (2006, 4, 94.8, 106.8, 82.8), (2006, 5, 91.2, 104.2, 78.2), ] drawing = Drawing(200, 150) pred = [row[2]-40 for row in data] high = [row[3]-40 for row in data] low = [row[4]-40 for row in data] times = [200*((row[0] + row[1]/12.0) - 2005)-110 for row in data] drawing.add(PolyLine(zip(times, pred), strokeColor=colors.blue)) drawing.add(PolyLine(zip(times, high), strokeColor=colors.red)) drawing.add(PolyLine(zip(times, low), strokeColor=colors.green)) drawing.add(String(65, 115, 'Sunspots', fontSize=18, fillColor=colors.red)) renderPDF.drawToFile(drawing, 'report1.pdf', 'Sunspots') As you can see, I have adjusted the values and time stamps to get the positioning right. The resulting drawing is shown in Figure 21-3.

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

The Pro<strong>to</strong>type<br />

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

shown in Listing 21-2.<br />

Listing 21-2. The First Pro<strong>to</strong>type 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.pdf', 'Sunspots')<br />

As you can see, I have adjusted the values and time stamps <strong>to</strong> 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!