doudan5136 2013-09-24 10:25
浏览 49
已采纳

可以在函数中嵌入一个pchart吗?

I want to populate a chart with dynamic data, but in order to do so, I want to embed the pchart php file in a function, and then call it with

echo "<img src='grafice();' /> ";

Is this possible? Here is what i`ve done:

function grafice()
{     
/* pChart library inclusions */
include("../class/pData.class.php");
include("../class/pDraw.class.php");
include("../class/pImage.class.php");


/* Create and populate the pData object */

$MyData = new pData();  

$MyData->addPoints(array(10,5,7,8), "Probe 1");


$MyData->setSerieWeight("Probe 1",2);

$MyData->setAxisName(0,"Media Generala");

$MyData->addPoints(array("Ian","Feb","Mar","Apr","Mai","Iun"),"Labels");

$MyData->setSerieDescription("Labels","Luni");

$MyData->setAbscissa("Labels");
 $serieSettings = array("R"=>255,"G"=>185,"B"=>11);
 $MyData->setPalette("Probe 1",$serieSettings);


/* Create the pChart object */

$myPicture = new pImage(500,230,$MyData);



/* Draw the background */

$Settings = array("R"=>248, "G"=>226, "B"=>174, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107);

$myPicture->drawFilledRectangle(0,0,700,230,$Settings);



/* Overlay with a gradient */

$Settings = array("StartR"=>238, "StartG"=>216, "StartB"=>174, "EndR"=>238, "EndG"=>216, "EndB"=>174, "Alpha"=>50);

$myPicture->drawGradientArea(0,0,700,230,DIRECTION_VERTICAL,$Settings);

$myPicture->drawGradientArea(0,0,700,20,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>80));



/* Add a border to the picture */

$myPicture->drawRectangle(0,0,499,229,array("R"=>0,"G"=>0,"B"=>0));



/* Write the picture title */ 

$myPicture->setFontProperties(array("FontName"=>"../fonts/Silkscreen.ttf","FontSize"=>6));

$myPicture->drawText(10,13,"Evolutia Mediei Generale",array("R"=>255,"G"=>255,"B"=>255));



/* Write the chart title */ 

$myPicture->setFontProperties(array("FontName"=>"../fonts/Forgotte.ttf","FontSize"=>11));

$myPicture->drawText(250,55,"Media Generala",array("FontSize"=>20,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE));



/* Draw the scale and the 1st chart */

$AxisBoundaries = array(0=>array("Min"=>0,"Max"=>10));
$scaleSettings  = array("DrawSubTicks"=>TRUE,"Mode"=>SCALE_MODE_MANUAL, "ManualScale"=>$AxisBoundaries);

$myPicture->setGraphArea(60,60,450,190);

$myPicture->drawFilledRectangle(60,60,450,190,array("R"=>0,"G"=>0,"B"=>0,"Surrounding"=>-200,"Alpha"=>10));

$myPicture->drawScale($scaleSettings);

$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));

$myPicture->setFontProperties(array("FontName"=>"../fonts/pf_arma_five.ttf","FontSize"=>10,"R"=>0,"G"=>0,"B"=>0));

$myPicture->drawLineChart(array("DisplayValues"=>TRUE,"DisplayR"=>0, "DisplayG"=>0, "DisplayB"=>0, "DisplayOffset"=>10));

$myPicture->setShadow(FALSE);




/* Render the picture (choose the best way) */

$myPicture->autoOutput("pictures/example.drawLineChart.png");
}

Is this even possible? If not, is there another way of doing this? P.S. I just want the function to work with predefined points. I think I'll manage to add dynamic data to it.

  • 写回答

1条回答 默认 最新

  • dongyan9838 2013-09-24 12:18
    关注

    I managed to do it, and posting here to help others!

    so, I made the function, but I edited the BOLD part

    function grafice()
    {     
    /* pChart library inclusions */
    include("../class/pData.class.php");
    include("../class/pDraw.class.php");
    include("../class/pImage.class.php");
    
    
    /* Create and populate the pData object */
    
    $MyData = new pData();  
    
    $MyData->addPoints(array(10,5,7,8), "Probe 1");
    
    
    $MyData->setSerieWeight("Probe 1",2);
    
    $MyData->setAxisName(0,"Media Generala");
    
    $MyData->addPoints(array("Ian","Feb","Mar","Apr","Mai","Iun"),"Labels");
    
    $MyData->setSerieDescription("Labels","Luni");
    
    $MyData->setAbscissa("Labels");
     $serieSettings = array("R"=>255,"G"=>185,"B"=>11);
     $MyData->setPalette("Probe 1",$serieSettings);
    
    
    /* Create the pChart object */
    
    $myPicture = new pImage(500,230,$MyData);
    
    
    
    /* Draw the background */
    
    $Settings = array("R"=>248, "G"=>226, "B"=>174, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107);
    
    $myPicture->drawFilledRectangle(0,0,700,230,$Settings);
    
    
    
    /* Overlay with a gradient */
    
    $Settings = array("StartR"=>238, "StartG"=>216, "StartB"=>174, "EndR"=>238, "EndG"=>216, "EndB"=>174, "Alpha"=>50);
    
    $myPicture->drawGradientArea(0,0,700,230,DIRECTION_VERTICAL,$Settings);
    
    $myPicture->drawGradientArea(0,0,700,20,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>80));
    
    
    
    /* Add a border to the picture */
    
    $myPicture->drawRectangle(0,0,499,229,array("R"=>0,"G"=>0,"B"=>0));
    
    
    
    /* Write the picture title */ 
    
    $myPicture->setFontProperties(array("FontName"=>"../fonts/Silkscreen.ttf","FontSize"=>6));
    
    $myPicture->drawText(10,13,"Evolutia Mediei Generale",array("R"=>255,"G"=>255,"B"=>255));
    
    
    
    /* Write the chart title */ 
    
    $myPicture->setFontProperties(array("FontName"=>"../fonts/Forgotte.ttf","FontSize"=>11));
    
    $myPicture->drawText(250,55,"Media Generala",array("FontSize"=>20,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE));
    
    
    
    /* Draw the scale and the 1st chart */
    
    $AxisBoundaries = array(0=>array("Min"=>0,"Max"=>10));
    $scaleSettings  = array("DrawSubTicks"=>TRUE,"Mode"=>SCALE_MODE_MANUAL, "ManualScale"=>$AxisBoundaries);
    
    $myPicture->setGraphArea(60,60,450,190);
    
    $myPicture->drawFilledRectangle(60,60,450,190,array("R"=>0,"G"=>0,"B"=>0,"Surrounding"=>-200,"Alpha"=>10));
    
    $myPicture->drawScale($scaleSettings);
    
    $myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
    
    $myPicture->setFontProperties(array("FontName"=>"../fonts/pf_arma_five.ttf","FontSize"=>10,"R"=>0,"G"=>0,"B"=>0));
    
    $myPicture->drawLineChart(array("DisplayValues"=>TRUE,"DisplayR"=>0, "DisplayG"=>0, "DisplayB"=>0, "DisplayOffset"=>10));
    
    $myPicture->setShadow(FALSE);
    
    
    
    
    /* Render the picture (choose the best way) */
    
    /* $myPicture->**( I DELETED ThIS)--> autoOutput <---** ("pictures/example.drawLineChart.png"); and added */
    
     $myPicture->render("pictures/example.drawLineChart.png");
    
    
    }
    

    Then, when I want to call it, like so :

    <?php grafice(); ?>
    

    And then I can do:

    <img src="pictures/example.drawLineChart.png" />
    

    Now I just change the picture name with the session id, and so I will have a different graph for everyone ;) Hopefully this will help someone!

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

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)