0

I try to create a chart with pchart, I have done like in this link (in official website):

https://wiki.pchart.net/doc.mysql.integration.html

but everytime I get this error !!

Fatal error: Call to a member function addPoints() on a non-object in C:\xampp\htdocs\pChart2.1.0\examples\test2.php on line 39

Help !!

Here is my full code :

     /* CAT:Line chart */

 /* pChart library inclusions */
 include("../class/pData.class.php");
 include("../class/pDraw.class.php");
 include("../class/pImage.class.php");

 /* Create and populate the pData object */
 $MyData = new pData();  

 /* Connect to the MySQL database */
$db = mysql_connect("localhost", "root", "");
mysql_select_db("pchart",$db);


/* Build the query that will returns the data to graph */
$Requete = "SELECT * FROM `measures`";
$Result = mysql_query($Requete,$db);
while($row = mysql_fetch_array($Result))
{
/* Push the results of the query in an array */
$timestamp[] = $row["timestamp"];
$temperature[] = $row["temperature"];
$humidity[] = $row["humidity"];



}
/* Save the data in the pData array */
$myData->addPoints($timestamp,"Timestamp");
$myData->addPoints($temperature,"Temperature");
$myData->addPoints($humidity,"Humidity");

 /* Create the pChart object */
 $myPicture = new pImage(700,230,$MyData);

 /* Turn of Antialiasing */
 $myPicture->Antialias = FALSE;

 /* Add a border to the picture */
 $myPicture->drawRectangle(0,0,699,229,array("R"=>0,"G"=>0,"B"=>0));

 /* Write the chart title */ 
 $myPicture->setFontProperties(array("FontName"=>"../fonts/Forgotte.ttf","FontSize"=>11));
 $myPicture->drawText(150,35,"Average temperature",array("FontSize"=>20,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE));

 /* Set the default font */
 $myPicture->setFontProperties(array("FontName"=>"../fonts/pf_arma_five.ttf","FontSize"=>6));

 /* Define the chart area */
 $myPicture->setGraphArea(60,40,650,200);

 /* Draw the scale */
 $scaleSettings = array("XMargin"=>10,"YMargin"=>10,"Floating"=>TRUE,"GridR"=>200,"GridG"=>200,"GridB"=>200,"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE);
 $myPicture->drawScale($scaleSettings);

 /* Turn on Antialiasing */
 $myPicture->Antialias = TRUE;

 /* Draw the line chart */
 $myPicture->drawLineChart();

 /* Write the chart legend */
 $myPicture->drawLegend(540,20,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));

 /* Render the picture (choose the best way) */
 $myPicture->autoOutput("pictures/example.drawLineChart.simple.png");

Thanks a lot.

Michael Robinson
  • 29,278
  • 12
  • 104
  • 130
Bizboss
  • 7,792
  • 27
  • 109
  • 174
  • Check out HighChart. Far and above the best out there. Many others (including FusionCharts, which is not free, use HighCharts code to generate their own charts) – bpeterson76 Feb 25 '11 at 17:27

3 Answers3

3
 include("../class/pDraw.class.php");
 include("../class/pImage.class.php");
 include("../class/pData.class.php");
$db = mysql_connect("localhost", "root", "");
mysql_select_db("test",$db);
$query = "SELECT `temperature` FROM `measures`";
$result  = mysql_query($query,$db);
$cpu="";

while($row = mysql_fetch_array($result)) 
{
   $timestamp[] = $row["timestamp"];
   $temperature[] = $row["temperature"];
   $humidity[] = $row["humidity"];
}
$myData = new pData();
$myData->addPoints($timestamp,"Timestamp");
$myData->addPoints($temperature,"Temperature");
$myData->addPoints($humidity,"Humidity");

 $myData->setAbscissa("Timestamp");
 $myData->setSerieOnAxis("Humidity", 1);
 $myData->setXAxisName("Time");
  //$myData->setXAxisDisplay(AXIS_FORMAT_TIME,"H:i");
  $myData->setAxisName(0,"Temperature");
 $myData->setAxisUnit(0,"°C");
 /* Second Y axis will be dedicated to humidity */
 $myData->setAxisName(1,"Humidity");
 $myData->setAxisUnit(0,"%");
$myPicture = new pImage(700,230,$myData); 
$myPicture->setGraphArea(60,40,670,190);
$myPicture->setFontProperties(array("FontName"=>"../fonts/Forgotte.ttf","FontSize"=>15));
$myPicture->drawScale();
$myPicture->drawPlotChart();
$myPicture->Stroke();
  • Your code works fine. I get an error when I put a `require` in order to retrieve user and password of the db. – Zeke Aug 09 '16 at 13:22
3

you define MyData as newData with uppercase M

/* Create and populate the pData object */
$MyData = new pData();

and use it without the uppercase M

$myData->addPoints($timestamp,"Timestamp");
$myData->addPoints($temperature,"Temperature");
$myData->addPoints($humidity,"Humidity");
stephk
  • 31
  • 2
1

Check that the page is properly linked to pData.class.php. When it's trying to call to a member function on a non-object, that often points to the class not being available to the page doing the work. Without seeing specific code, it's going to be tough to help more.

bpeterson76
  • 12,918
  • 5
  • 49
  • 82
  • Hello, my page is properly linked to pData.class.php; I have also : /* pChart library inclusions */ include("../class/pData.class.php"); include("../class/pDraw.class.php"); include("../class/pImage.class.php"); Thanks. – Bizboss Feb 28 '11 at 08:23