0

我有这个C:\\xampp\htdocs\exact\sample_pie.php包含图表的文件。这是代码:

<?php
include("phpgraphlib.php");
include("phpgraphlib_pie.php");
include("connection.php");
$graph=new PHPGraphLibPie(400,200); 
$link = mysql_connect('localhost', 'root', '');
mysql_select_db('exact');

$querypa = "SELECT COUNT(nature) FROM approved WHERE nature='Parade'";
$resultpa = mysql_query($querypa);
$printpa = mysql_result($resultpa,0);
$querycs = "SELECT COUNT(nature) FROM approved WHERE nature='Community Service'";
$resultcs = mysql_query($querycs);
$printcs = mysql_result($resultcs,0);
$queryga = "SELECT COUNT(nature) FROM approved WHERE nature='General Assembly'";
$resultga = mysql_query($queryga);
$printga = mysql_result($resultga,0);
$querypl = "SELECT COUNT(nature) FROM approved WHERE nature='Play/Showcase/Socio-Cultural Show/Film Showing'";
$resultpl = mysql_query($querypl);
$printpl = mysql_result($resultpl,0);
$queryco = "SELECT COUNT(nature) FROM approved WHERE nature='Competition/Sportsfest'";
$resultco = mysql_query($queryco);
$printco = mysql_result($resultco,0);
$queryfr = "SELECT COUNT(nature) FROM approved WHERE nature='Fund Raising'";
$resultfr = mysql_query($queryfr);
$printfr = mysql_result($resultfr,0);
$queryse = "SELECT COUNT(nature) FROM approved WHERE nature='Seminar/Convention/Conference/Training'";
$resultse = mysql_query($queryse);
$printse = mysql_result($resultse,0);


$totalAct=$printpa+$printga+$printpl+$printcs+$printco+$printfr+$printse;
$avepa=($printpa/$totalAct)*100;
$avecs=($printcs/$totalAct)*100;
$avega=($printga/$totalAct)*100;
$avepl=($printpl/$totalAct)*100;
$aveco=($printco/$totalAct)*100;
$avefr=($printfr/$totalAct)*100;
$avese=($printse/$totalAct)*100;
$pa=number_format($avepa,2);
$cs=number_format($avecs,2);
$ga=number_format($avega,2);
$pl=number_format($avepl,2);
$co=number_format($aveco,2);
$fr=number_format($avefr,2);
$se=number_format($avese,2);

$data=array("Parade"=>$pa, "General Assembly"=>$ga, "Play/Showcase/Socio-Cultural Show/Film Showing"=>$pl, "Community Service"=>$cs, "Competition/Sportsfest"=>$co, "Fund Raising"=>$fr, "Seminar/Convention/Conference/Training"=>$se);
$graph->addData($data);
$graph->setTitle("Total Activities per Nature of Activity");
$graph->setPrecision(2);
$graph->setLabelTextColor("0,0,0");
$graph->setLegendTextColor("0,0,0");
$graph->setGradient("210,245,255","pastel_purple");

$graph->createGraph();

?>

如何通过 php 脚本将图形作为 .jpg 获取?我试过这个file_get_contents("C:\\xampp\htdocs\exact\sample_pie.php"); ,但它只输出这个:

$pa, "General Assembly"=>$ga, "Play/Showcase/Socio-Cultural Show/Film Showing"=>$pl, "Community Service"=>$cs, "Competition/Sportsfest"=>$co, "Fund Raising"=>$fr, "Seminar/Convention/Conference/Training"=>$se); $graph->addData($data); $graph->setTitle("Total Activities per Nature of Activity"); $graph->setPrecision(2); $graph->setLabelTextColor("0,0,0"); $graph->setLegendTextColor("0,0,0"); $graph->setGradient("210,245,255","pastel_purple"); $graph->createGraph(); ?>

请帮我。谢谢。

4

3 回答 3

1

像这样使用file_get_contents()只会检索脚本本身。该文本是文件包含的内容,就是这样file_get_contents()做的;正是它在锡上说的。

尝试查看 PHPGraphLib 的文档或失败的源代码。应该有某种可以调用的保存方法,而不是createGraph()或者您可以只将文件名参数传递给createGraph().

于 2009-10-16T01:25:03.503 回答
1

在旁注中,我应该提到您的脚本比它应该的要复杂得多。mysql_select_db()和之间的一切都$graph->addData()可以改写成这样:

$data = array(
    'Parade' => 0,
    'Community Service' => 0,
    'General Assembly' => 0,
    'Play/Showcase/Socio-Cultural Show/Film Showing' => 0,
    'Competition/Sportsfest' => 0,
    'Fund Raising' => 0,
    'Seminar/Convention/Conference/Training' => 0
);

$sql = "SELECT nature, COUNT(*) AS cnt
          FROM approved
         WHERE nature IN ('" . implode("','", array_keys($data)) . "')
      GROUP BY nature";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result))
{
    $data[$row['nature']] = $row['cnt'];
}
$totalAct = array_sum($data);

foreach ($data as $nature => &$value)
{
    $value = number_format(100 * $value / $totalAct, 2);
}

其余的,请按照 Ollie Saunders 的建议查阅图形库的文档。

于 2009-10-16T01:48:48.790 回答
0
<img src="/exact/sample_pie.php" />

您可能希望使用header()在 sample_pie.php 中设置适当的 Content-type。

于 2009-10-16T02:04:16.607 回答