1

そのために pChart2 と php を学習しようとしているので、コーディングが少し荒いかもしれません。メインの index.php ページにテスト チャートを表示する際に問題が発生しています。すべてのコードを別のファイルに保存してそのファイルを表示すると、この例は正常に機能します。メインのphp Webページに追加すると、意味不明なことがたくさんあります。これが私の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 は、その出力をファイルにレンダリングするか、HTTPContent-Typeののバイナリ データのストリームとしてレンダリングできますimage/png

したがって、pChart を Web ページに組み込む最も簡単な方法は、グラフ作成/描画関数を別の PHP ファイルに配置し、イメージ タグを介して HTML 内からその別のスクリプトを呼び出すことです。<img src="yourPChartFile.php">これにより、スクリプトから生成された画像が表示されます。スクリプトimage/pngは、ブラウザがレンダリングできる を返しています。

HTML ページと同じ PHP ファイルでグラフを作成する必要がある場合は、pChart スクリプトの結果を、$myPicture->render("FILE_NAME_HERE.png")Web サーバーがアクセスできる場所にある画像ファイルに ( 経由で) 保存し、生成されたファイルに<img>タグでリンクする必要があります。

これはすべてpChart ドキュメントで説明されています

于 2013-09-11T13:34:13.133 に答える