1

我正在尝试学习 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>
4

2 回答 2

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";
于 2014-07-11T02:06:39.883 回答
1

pChart 可以将其输出呈现到文件中,也可以呈现为带有 HTTPContent-Type的二进制数据流image/png

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

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

所有这些都在pChart 文档中进行了描述

于 2013-09-11T13:34:13.133 回答