Tài liệu PHP: The Good Parts: Delivering the Best of PHP- P7 - Pdf 87

$this->SetFont('','B');
//Header
// make an array for the column widths
$w=array(85,40,15);
// send the headers to the PDF document
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C',1);
$this->Ln();
//Color and font restoration
$this->SetFillColor(175);
$this->SetTextColor(0);
$this->SetFont('');
//now spool out the data from the $data array
$fill=true; // used to alternate row color backgrounds
foreach($data as $row)
{
$this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
// set colors to show a URL style link
$this->SetTextColor(0,0,255);
$this->SetFont('', 'U');
$this->Cell($w[1],6,$row[1],'LR',0,'L',$fill, '');
// restore normal color settings
$this->SetTextColor(0);
$this->SetFont('');
$this->Cell($w[2],6,$row[2],'LR',0,'C',$fill);
$this->Ln();
// flips from true to false and vise versa
$fill =! $fill;
}
$this->Cell(array_sum($w),0,'','T');
}

After the table header is sent out, we use the $data array that contains the database
information and walk through that array with a foreach loop. Notice here that the
cell() method uses LR for its border parameter. This refers to borders on the left and
right of the cell in question, thus effectively adding the sides to the table rows. We also
add a URL link to the second column just to show that it can be done in connection
with the table row construction. Finally, we use a $fill variable to flip back and forth
so that the background color will alternate as the table is constructed row by row.
The final call to the cell() method in this BuildTable() method draws the bottom of
the table and closes off the columns.
The result of executing this code in a browser is shown in Figure 8-11.
Figure 8-11. Table data taken from MySQL placed in a dynamic PDF
104 | Chapter 8: PHP and Friends
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Graphical Reports Generation
JPGraph is used to make graphical statistical reports like bar charts and pie charts. It
is an object-oriented code library, so by now it should be fairly straightforward for you
to use. As before, you access this library with a require statement.
Typically, a graphical report will ask the user for input in order to build the report. This
is information like the date range that the report will cover, the sorting order of the
data, and so on. In our samples, however, we will simply provide arrays with preset
values to make them a little easier to review.
Pie Charts
The first sample graph that we will look at is a pie chart. In the following listing you
will see an array of data to be plotted on the chart assigned to a variable called $data;
this is the data that would normally be provided by a data entry page, a select statement
from a database, or a combination of both. We can do this after we bring in the ap-
propriate libraries for the chart that we are about to build.
JPGraph is a little different than other libraries in the sense that there is a basic library
required by all graphs being generated, as well as individual specialized libraries, or
sublibraries, that are more suited to each graph type. In this case, we use the

$pPlot->value->SetFont(FF_ARIAL,FS_NORMAL,9);
$pPlot->value->SetFormat('%2.1f%%');
// Add and stroke
$piechart->Add($pPlot);
$piechart->Stroke();
The time function is added here to trigger a difference in the browser’s
cache
registration so that the same file can be used by many concurrent
users of the same web page.
This code will send the graph shown in Figure 8-12 to the browser. Remember that you
can augment this display with other HTML markup if desired.
Figure 8-12. Pie chart generated by JPGraph and PHP
106 | Chapter 8: PHP and Friends
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
If you decide to add additional formatting to the output of the graph,
you will have to save the generated graphic as a file and pivot it to the
server’s hard drive for later display with the accompanying HTML. The
stroke method used to generate the graph has an option to name the
file and save it for you. The code to save the graphic is:
$graph->Stroke("graph.jpg");
And the code to bring the graphic back to combine with HTML code is:
echo ('<img src="graph.jpg?' .time(). '">');
Bar Charts
Another type of chart that we can create is a bar chart. Again, here we will provide the
benchmark values to the graph directly for easier code review. The code for this sample
follows, and you will see that it uses the specific sublibrary for bar charts to work
properly. Other than the proper selection of the sublibrary, there is really not too much
difference in the approach—there are specific methods used, but the concept is basi-
cally the same. Here is the code:
include ("../../jpgraph/jpgraph.php");

$graph->yaxis->SetFont(FF_VERDANA,FS_NORMAL,10);
$graph->yaxis->title->Set("Value Range");
$graph->yaxis->title->SetColor("white");
$graph->yaxis->title->SetFont(FF_VERDANA,FS_NORMAL,10);
// Set up X-axis title (color & font)
$graph->xaxis->title->Set("item Count");
$graph->xaxis->title->SetColor("white");
$graph->xaxis->title->SetFont(FF_VERDANA,FS_NORMAL,10);
// Finally send the graph to the browser
$graph->Stroke();
Figure 8-13 shows the chart that this code produces.
Figure 8-13. Bar chart generated by JPGraph and PHP
108 | Chapter 8: PHP and Friends
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Captchas
One last quick sample is in order. If you have ever ordered concert tickets online, you
will be familiar with the kind of antispam image shown in Figure 8-14.
Figure 8-14. Antispam graphic generated by JPGraph and PHP
JPGraph
can generate this kind of captcha in just a few lines of code, and the supplied
characters can be either provided manually (by you) or generated randomly:
require_once "../../jpgraph/jpgraph_antispam.php";
$spam = new AntiSpam();
// saved to $chars for later verification of correct entry
$chars = $spam->Rand(8);
$spam->Stroke() ;
Be sure to visit the website for this library and review all the other options that it pro-
vides. You can add background images to the graphs, adjust the grid lines behind the
bars, and so much more. Many other types of charts and graphs are also available, like
stock, radar, scatter, polar, and Gantt charts.


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status