📜 ⬆️ ⬇️

pChart - we build graphs and diagrams in PHP. Practice

Hello!
On a tip from the article “ pChart - we build graphs and diagrams for PHP ” implemented the graphing in oneself. This is what it looks like:

Examples of this, of course, well, but practical implementation can help in understanding. My application under the cut.

There are many frameworks for plotting graphs. There are paid good, there are free and good. PChart seemed to me quite comfortable and functional. So to the implementation.
pChart works as follows. Logs data into its array, draws a picture with a graph, saves it to disk. Then you need to display this image in the document.
<head> 
 <?php /* Include all the classes pChart*/ include("class/pDraw.class.php"); include("class/pImage.class.php"); include("class/pData.class.php"); ?> 
 </head> 
 <?php $myData = new pData(); //     ,  . while (list($dt,$sum) = mysql_fetch_row($result)) { /*     */ $myData->addPoints($sum,"Total"); $myData->addPoints($dt,"Labels"); }; $unique = date("Ymd_H.i"); $gsFilename_Traffic = "traffic_".$unique.".png"; $myData->setSerieDescription("Labels","Days"); $myData->setAbscissa("Labels"); $myData->setAxisUnit(0," KB"); $serieSettings = array("R"=>229,"G"=>11,"B"=>11,"Alpha"=>100); $myData->setPalette("Total",$serieSettings); $myPicture = new pImage(1250,400,$myData); // <--   $myPicture->setFontProperties(array("FontName"=>"fonts/tahoma.ttf","FontSize"=>8)); $myPicture->setGraphArea(50,20,1230,380); // <--     $myPicture->drawScale(); $myPicture->drawBestFit(array("Alpha"=>40)); // <--   $myPicture->drawLineChart(); $myPicture->drawPlotChart(array("DisplayValues"=>FALSE,"PlotBorder"=>TRUE,"BorderSize"=>0,"Surrounding"=>-60,"BorderAlpha"=>50)); // <--    $myPicture->drawLegend(700,10,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));// <--   $myPicture->Render("pChartPic\\".$gsFilename_Traffic); ?> 
 <br /><h3></h3> <br /><IMG SRC="pChartPic/ 
 <?php echo $gsFilename_Traffic; ?> 
 " /> 

I hope my example will help beginners (and maybe not only). An example is simple, nothing superfluous. Often, more is needed. Then, if you wish, you can curb the beauty.

')

Source: https://habr.com/ru/post/205532/


All Articles