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.

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

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向