doutang7414 2014-06-18 16:35 采纳率: 0%
浏览 42
已采纳

使用pchart创建的图表可以缩短时间

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");
?>
  • 写回答

1条回答 默认 最新

  • duanfu7840 2014-09-29 05:04
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)