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 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?