Search code examples
phpmysqlpchart

Charts created with pchart gives time out


Im new to pchart and I'm getting problems (probably something I missed) on creating a chart from a mysql table. What I want is just chart one row depending the item I will select. This it worked only one time, then every time I try to run the script again, apache (I'm using apache2) gives a timeout and checking the server using "top", the process apache uses 100% cpu usage. Some times the chart is created some times it just stay using 100% cpu usage and does nothing. If I use "autoOutput" method to create the .png images ath the end an error says that the image can not be displayed because it contains errors but using "Render" the chart is created but just sometimes as I'm explaining,. The following is my code, hope somebody can give some light .

<?php
    /* pChart library inclusions */
     include("../../Library/class/pData.class.php");
     include("../../Library/class/pDraw.class.php");
     include("../../Library/class/pImage.class.php");
     include "../../connection.php";
    // Values from List.php
    $Selected=$_POST['Selection'];
    $MyData = new pData();  
    /* QUERY FOR THE CHART */
    $Requete = "SELECT * FROM table WHERE Sector = '$Selected' LIMIT 0 , 30";
    $Result  = mysql_query($Requete,$db)or die(mysql_error());
    while($row = mysql_fetch_array($Result))
     {
      $Sector = $row["Sector"];
      $Current_reading = $row["Current_reading"];
      $Old_Reading = $row['Old_reading'];
      $Month_Consumption = $row['Month_Consumption'];
      $Current_reading = $row['Current_reading'];
      $DM = $row['DM'];
      $FP = $row['FP'];
      $Cost = $row['Cost_KWh_$90.0000'];  


      $MyData->addPoints($Current_reading,'Current_reading');
      $MyData->addPoints($Old_Reading,'Old_reading');
      $MyData->addPoints($Month_Consumption,'Month_Consumption');
      $MyData->addPoints($DM,'DM');
      $MyData->addPoints($FP,'FP');
      $MyData->addPoints($Cost,'Cost_KWh_$90.0000');

     }
       /* Save the data in the pData array */
      $MyData->setAbscissa("Consumos_de");
      $MyData->setAbscissaName('February 2014');
      $MyData->setSerieDescription($Sector,"Sectores");
      $MyData->setAxisName(0,"Kw");  

     /* Create the pChart object */
     $myPicture = new pImage(800,230,$MyData);
     $myPicture->drawGradientArea(0,0,800,230,DIRECTION_VERTICAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>100));
     $myPicture->drawGradientArea(0,0,800,230,DIRECTION_HORIZONTAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>20));
     $myPicture->setFontProperties(array("FontName"=>"../../Library/fonts/verdana.ttf","FontSize"=>8));

     /* Draw the scale  */
     $myPicture->setGraphArea(50,30,780,200);
     $myPicture->drawScale(array("CycleBackground"=>TRUE,"DrawSubTicks"=>TRUE,"GridR"=>0,"GridG"=>0,"GridB"=>0,"GridAlpha"=>10));

     /* Turn on shadow computing */ 
     $myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));

     /* Draw the chart */
     $settings = array("Gradient"=>TRUE,"DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"DisplayR"=>255,"DisplayG"=>255,"DisplayB"=>255,"DisplayShadow"=>TRUE,"Surrounding"=>10);
     $myPicture->drawBarChart($settings);

     /* Write the chart legend */
     $myPicture->drawLegend(680,12,array("Style"=>LEGEND_BORDER,"Mode"=>LEGEND_VERTICAL));

     /* Render the picture (choose the best way) */
     $myPicture->Render("/var/www/images/YourGraph.png");
?>

Solution

  • Well, it worked by doing this :

    I moved out of the while loop all the following :

    $MyData->addPoints($Current_reading,'Current_reading');
    $MyData->addPoints($Old_Reading,'Old_reading');
    $MyData->addPoints($Month_Consumption,'Month_Consumption');
    $MyData->addPoints($DM,'DM');
    $MyData->addPoints($FP,'FP');
    $MyData->addPoints($Cost,'Cost_KWh_$90.0000');
    

    And at the end I changed

    $myPicture->Render("/var/www/images/YourGraph.png");  
    

    and added this instead :

    $myPicture->autoOutput("YourChart.png");
    

    I had to sort a little bit more the information I was trying to chart but with that the problem causing the 100% CPU usage was solved and the chart appeared. This are really my first steps in to PHP and I'm sorry all the mess here. Hope this be helpful for someone.