📜 ⬆️ ⬇️

Creating graphs with pChart. Data from mysql

Hello In this post I wanted to tell you about charting with pChart. There is nothing complicated in it, but it’s standard (later, on the developer’s website, I found MySQL info), it takes data from a file. It didn’t fit me for my needs, and I started poking around in the script. Result below.

So, what we need:
1. php-module GD (for Apache, of course).
2. Download pChart 1.27 library .

In order for a long time not to describe this whole thing, I will immediately give a ready-made script for plotting graphs, the data is taken from the database.

I used it to display the customer's ID and the amount of his order. See the script below.
')
<? /* Include the pData class */ include 'pChart/pData.class'; include 'pChart/pCache.class'; include 'pChart/pChart.class'; //   $myData = new pData(); /*   MySQL   */ $db = mysql_connect("localhost", "root", "Ld7n\|/fNf"); if ( $db == "" ) { echo " DB Connection error...\r\n"; exit(); } mysql_select_db("dle",$db); $Requete = "SELECT `summa`,`id` FROM `maz_shop_orders`"; $result = mysql_query($Requete,$db); while($row = mysql_fetch_array($result)) { $myData->AddPoint($row["id"],"id"); $myData->AddPoint($row["summa"],"summa"); } //    //   $myData->SetAbsciseLabelSerie("id"); //     // $myData->AddSerie("summa"); //  $myData->SetSerieName( mb_convert_encoding("",'utf-8','windows-1251'), "summa"); //    1000    500 px $graph = new pChart(1000,500); //     $graph->setFontProperties("Fonts/tahoma.ttf",10); //       //  $graph->setGraphArea(85,30,950,400); //   $graph->drawFilledRoundedRectangle(7,7,993,493,5,240, 240,240); //     $graph->drawRoundedRectangle(5,5,995,495,5,230, 230,230); //   $graph->drawGraphArea(255,255,255,TRUE); //    $graph->drawScale($myData->GetData(), $myData->GetDataDescription(), SCALE_NORMAL,150,150,150,true,0,2); //    $graph->drawGrid(4,TRUE,230,230,230,50); //   $graph->drawLineGraph($myData->GetData(), $myData->GetDataDescription()); //     $graph->drawPlotGraph($myData->GetData(), $myData->GetDataDescription(),3,2,255,255,255); //      $graph->setFontProperties("Fonts/tahoma.ttf",10); $graph->drawTextBox(870,450,990,460,"Powered By pChart", 0,250,250,250,ALIGN_CENTER,TRUE,-1,-1,-1,30); $graph->drawTextBox(805,470,990,480,"http://pchart.sourceforge.net", 0,250,250,250,ALIGN_CENTER,TRUE,-1,-1,-1,30); $graph->drawTextBox(15,450,140,460,"Developed By kv4nt", 0,250,250,250,ALIGN_CENTER,TRUE,-1,-1,-1,30); $graph->drawTextBox(10,470,140,480,"http://www.piarcom.com", 0,250,250,250,ALIGN_CENTER,TRUE,-1,-1,-1,30); //  $graph->drawLegend(90,35,$myData->GetDataDescription(),255,255,255); //  $graph->setFontProperties("Fonts/tahoma.ttf",10); $graph->drawTitle(480,22, mb_convert_encoding("", 'utf-8','windows-1251'), 50,50,50,-1,-1,true); //   $graph->Stroke(); /** * @return array */ ?> 


What was used in the script:
localhost - database host
root - DB user
Ld7n \ | / fNf - DB password
maz_shop_orders - table
id - string
summa - string
You don't need anything else.

Additional Information:
1. To output in the browser in your CMS or just on other pages, use
2. On the manufacturer's website pChart there are examples for displaying graphs of another type. There you will find for yourself the right one.

Thank you all for your attention! Good luck with charting!

Result:
image

UPD : fixed the info

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


All Articles