doutang7414 2014-06-18 08: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-28 21: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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部