1

I am trying to learn pChart2 and php for that matter so my coding may be a little rough. I am having a problem displaying a test chart on my main index.php page. The example works fine when I have all the code in a separate file and view that file. I get a bunch of gibberish when I add it back to my main php web page. Here is my php code.

<!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>
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2646288
  • 31
  • 1
  • 5

2 Answers2

1

pChart can either render its output to a file, or as a stream of binary data with an HTTP Content-Type of image/png.

So the easiest way to incorporate pChart with a webpage is to place the charting/drawing functions in a separate PHP file, then call that separate script from within your HTML via an image tag: <img src="yourPChartFile.php"> which will display the image generated from the script because the separate script is returning an image/png that your browser can render.

If you insist on creating the chart in the same PHP file as your HTML page, you have to save the pChart script results to an image file (via $myPicture->render("FILE_NAME_HERE.png")) in a location your webserver can access, then link to that generated file in your <img> tag.

All this is described in the pChart documentation

Eric L.
  • 3,232
  • 2
  • 22
  • 20
1

In your script create your chart just like pChart library says to.

This code Creates the pChart like you expect it to

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));

Then just use this code to output the 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";
Trapa
  • 51
  • 1
  • 5