pChart2在单独的php页面中呈现图形,但在主页面中显示乱码

时间:2013-08-02 14:52:41

标签: php pchart

我正在尝试学习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&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspHits<br />";
        while ($row_country = mysql_fetch_assoc($sql)) {
              $scountryinfo[] = $row_country;
        }

        foreach ($scountryinfo as $countryinfo){
           echo $countryinfo['srcCC'];
           echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
           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&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspHits<br />";
        while ($row_dcountry = mysql_fetch_assoc($dstcc)) {
            $dcountryinfo[] = $row_dcountry;
        }

        foreach ($dcountryinfo as $countryinfo) {
            echo "&nbsp&nbsp&nbsp&nbsp";
            echo $countryinfo['dstCC'];
            echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
            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 "&nbsp&nbsp&nbsp&nbspdIP&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspHits<br />";
        while ($row_dip = mysql_fetch_assoc($dcountryIP)) {
            $dcountryipinfo[] = $row_dip;
        }
        foreach ($dcountryipinfo as $dip) {
            echo "&nbsp&nbsp&nbsp&nbsp";
            echo $dip['dIP'];
            echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
            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 "&nbsp&nbsp&nbsp&nbspsIP&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspHits<br />";
        while ($row_sip = mysql_fetch_assoc($scountryIP)) {
            $scountryipinfo[] = $row_sip;
        }
        foreach ($scountryipinfo as $sip) {
            echo "&nbsp&nbsp&nbsp&nbsp";
            echo $sip['sIP'];
            echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
            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 "&nbsp&nbsp&nbsp&nbspPort&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspHits<br />";
        while ($row_dport = mysql_fetch_assoc($dport)) {
            $dportinfo[] = $row_dport;
        }
        foreach ($dportinfo as $dp) {
            echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
            echo $dp['dPort'];
            echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
            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>

2 个答案:

答案 0 :(得分:1)

pChart可以将其输出呈现为文件,也可以呈现为Content-Type image/png的二进制数据流。

因此,将pChart与网页合并的最简单方法是将图表/绘图函数放在单独的PHP文件中,然后在HTML中调用单独的脚本通过图片代码<img src="yourPChartFile.php">显示从脚本生成的图片,因为单独脚本返回了您的浏览器可以呈现的image/png

如果您坚持在与HTML页面相同的PHP文件中创建图表,则必须将pChart脚本结果保存到Web服务器可以访问的位置的图像文件(通过$myPicture->render("FILE_NAME_HERE.png")),然后链接到<img>代码中生成的文件。

所有这些都在pChart documentation

中描述

答案 1 :(得分:1)

在您的脚本中创建图表,就像pChart库所说的那样。

此代码按照您希望的那样创建pChart

include("../libraries/pChart/class/pDraw.class.php");
include("../libraries/pChart/class/pImage.class.php");

/* Create the pChart object */
$myPicture = new pImage(700,230);

/* Draw the background */
$Settings = array("R"=>170, "G"=>183, "B"=>87, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107);
$myPicture->drawFilledRectangle(0,0,700,230,$Settings);

/* Overlay with a gradient */
$Settings = array("StartR"=>219, "StartG"=>231, "StartB"=>139, "EndR"=>1, "EndG"=>138, "EndB"=>68, "Alpha"=>50);
$myPicture->drawGradientArea(0,0,700,230,DIRECTION_VERTICAL,$Settings);
$myPicture->drawGradientArea(0,0,700,20,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>80));

/* Draw the picture border */
$myPicture->drawRectangle(0,0,699,229,array("R"=>0,"G"=>0,"B"=>0));

/* Write the picture title */
$myPicture->setFontProperties(array("FontName"=>"../fonts/Silkscreen.ttf","FontSize"=>6));
$myPicture->drawText(10,13,"drawLine() - Basis",array("R"=>255,"G"=>255,"B"=>255));

/* Turn on shadow computing */
$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>20));

/* Draw some lines */
for($i=1;$i<=100;$i=$i+4)
  $myPicture->drawLine($i+5,215,$i*7+5,30,array("R"=>rand(0,255),"G"=>rand(0,255),"B"=>rand(0,255),"Ticks"=>rand(0,4)));

/* Draw an horizontal dashed line with extra weight */
$myPicture->drawLine(370,160,650,160,array("R"=>0,"G"=>0,"B"=>0,"Ticks"=>4,"Weight"=>3));

/* Another example of extra weight */
$myPicture->drawLine(370,180,650,200,array("R"=>255,"G"=>255,"B"=>255,"Ticks"=>15,"Weight"=>1));

然后只需使用此代码输出pChart

/* Render the picture (choose the best way) */
ob_start();
  imagepng($myPicture->Picture);
  $contents = ob_get_contents();
ob_end_clean();
print "<img src='data:image/png;base64,".base64_encode($contents)."' />\n";