我正在尝试学习 pChart2 和 php,所以我的编码可能有点粗糙。我在 index.php 主页面上显示测试图表时遇到问题。当我将所有代码放在一个单独的文件中并查看该文件时,该示例工作正常。当我将它添加回我的主 php 网页时,我得到了一堆乱码。这是我的php代码。
<!DOCTYPE html>
<?php
session_start();
include "/var/www/pchart/class/pDraw.class.php";
include "../pchart/class/pImage.class.php";
include "../pchart/class/pData.class.php";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<table border="1">
<tr><th>Source Country</th><th>Destination Country</th><th>Destination IP</th></tr>
<td>
<?php
#header("Content-Type: image/png");
$db_host = "localhost";
$db_name = "silk";
$db_login = "";
$db_pass = "";
$con = mysql_connect($db_host, $db_login, $db_pass, $db_name);
$sql = mysql_query("SELECT srcCC,COUNT(*) AS Hits FROM silk.pipeline GROUP BY srcCC ORDER BY COUNT(*) DESC LIMIT 10");
$scountryinfo = array();
echo "srcCC        Hits<br />";
while ($row_country = mysql_fetch_assoc($sql)) {
$scountryinfo[] = $row_country;
}
foreach ($scountryinfo as $countryinfo){
echo $countryinfo['srcCC'];
echo "            ";
echo $countryinfo['Hits'];
echo "<br />";
//echo "Hits: ".$usrinfo['COUNT']."<br />";
}
echo "</td>";
echo "<td>";
$dstcc = mysql_query("SELECT dstCC,COUNT(*) AS Hits FROM silk.pipeline GROUP BY dstCC ORDER BY COUNT(*) DESC LIMIT 10");
$dcountryinfo = array();
echo "dstCC        Hits<br />";
while ($row_dcountry = mysql_fetch_assoc($dstcc)) {
$dcountryinfo[] = $row_dcountry;
}
foreach ($dcountryinfo as $countryinfo) {
echo "    ";
echo $countryinfo['dstCC'];
echo "            ";
echo $countryinfo['Hits'];
echo "<br />";
}
echo "</td>";
echo "<td>";
$dcountryIP = mysql_query("SELECT dIP,COUNT(*) AS Hits FROM silk.pipeline GROUP BY dIP ORDER BY COUNT(*) DESC LIMIT 10");
$dcountryipinfo = array();
echo "    dIP                     Hits<br />";
while ($row_dip = mysql_fetch_assoc($dcountryIP)) {
$dcountryipinfo[] = $row_dip;
}
foreach ($dcountryipinfo as $dip) {
echo "    ";
echo $dip['dIP'];
echo "            ";
echo $dip['Hits'];
echo "<br />";
}
echo "</td></tr>";
echo "<tr><th>Source IP</th><th>Destination Port</th></tr><tr><td>";
$scountryIP = mysql_query("SELECT sIP,COUNT(*) AS Hits FROM silk.pipeline GROUP BY sIP ORDER BY COUNT(*) DESC LIMIT 10");
$scountryipinfo = array();
echo "    sIP                     Hits<br />";
while ($row_sip = mysql_fetch_assoc($scountryIP)) {
$scountryipinfo[] = $row_sip;
}
foreach ($scountryipinfo as $sip) {
echo "    ";
echo $sip['sIP'];
echo "            ";
echo $sip['Hits'];
echo "<br />";
}
echo "</td>";
echo "<td>";
$dport = mysql_query("SELECT dPort,COUNT(*) AS Hits FROM silk.pipeline GROUP BY dPort ORDER BY COUNT(*) DESC LIMIT 10");
$dportinfo = array();
echo "    Port                     Hits<br />";
while ($row_dport = mysql_fetch_assoc($dport)) {
$dportinfo[] = $row_dport;
}
foreach ($dportinfo as $dp) {
echo "      ";
echo $dp['dPort'];
echo "            ";
echo $dp['Hits'];
echo "<br />";
}
echo "<td>";
$MyData = new pData();
$MyData->addPoints(array(60,30,10),"Answers");
$MyData->setAxisName(0,"Answers (%)");
$MyData->addPoints(array("I do agree ","I disagree ","No opinion "),"Options");
$MyData->setAbscissa("Options");
/* Create the pChart object */
$myPicture = new pImage(500,220,$MyData);
/* Write the chart title */
$myPicture->setFontProperties(array("FontName"=>"../pchart/fonts/Forgotte.ttf","FontSize"=>15));
$myPicture->drawText(20,34,"Q: Flexibility is a key point of this library",array("FontSize"=>20));
/* Define the default font */
$myPicture->setFontProperties(array("FontName"=>"../pchart/fonts/pf_arma_five.ttf","FontSize"=>6));
/* Set the graph area */
$myPicture->setGraphArea(70,60,480,200);
$myPicture->drawGradientArea(70,60,480,200,DIRECTION_HORIZONTAL,array("StartR"=>200,"StartG"=>200,"StartB"=>200,"EndR"=>240,"EndG"=>240,"EndB"=>240,"Alpha"=>30));
/* Draw the chart scale */
$scaleSettings = array("DrawXLines"=>FALSE,"Mode"=>SCALE_MODE_START0,"GridR"=>0,"GridG"=>0,"GridB"=>0,"GridAlpha"=>10,"Pos"=>SCALE_POS_TOPBOTTOM);
$myPicture->drawScale($scaleSettings);
/* Turn on shadow computing */
$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
/* Draw the chart */
$myPicture->drawBarChart(array("Rounded"=>TRUE,"Surrounding"=>30));
/* Render the picture (choose the best way) */
$myPicture->autoOutput("pictures/example.drawBarChart.poll.png");
echo "</td>";
?>
</td>
</tr></table>
</body>
</html>