我的问题找不到答案。我使用PhPmyGraph (http://phpmygraph.abisvmm.nl/)从我的数据库显示一些数据的图形。问题是我必须在文件本身中创建数组,如果我想在页面上创建2个图形,我需要创建2个不同的文件。显然,该文件更容易使用CMS,但我没有使用一个。
这个文件是:
<?php
//Set content-type header for the graphs
header("Content-type: image/png");
//Include phpMyGraph5.0.php
include_once('../phpMyGraph5.0.php');
//Set config directives
$cfg['title'] = 'Example graph';
$cfg['width'] = 500;
$cfg['height'] = 250;
//Set data
$data = array(
'Jan' => 12,
'Nov' => 78,
'Dec' => 23
);
//Create phpMyGraph instance
$graph = new phpMyGraph();
//Parse
$graph->parseVerticalPolygonGraph($data, $cfg);
?>
在我的page index。php中调用:
echo " < img src='"graph.php'"> ";
还有别的方法吗?将数据从index。php发送到graph。php?或者将代码graph。php移到index。php ?问题是对于图像对象,我真的不知道怎么做!
更新:我几乎找到了一个解决方案,我的代码现在是:
在graph.php://Parse
$graph->parseVerticalPolygonGraph(unserialize($_GET['data']), $cfg);
index . php:
$select_daily = mysql_query("SELECT * FROM table");
while ($row_daily = mysql_fetch_assoc($select_daily) ){
$y = substr($row_daily['ymd'], 0, -4); // Year
$m = substr($row_daily['ymd'], 4, -2); // Month
$d = substr($row_daily['ymd'], -2); // Day
$key = $d."/".$m."/".$y;
$data_daily [$key] = $row_daily['members'];
}
foreach($data_daily as $key => $value) {
echo $key ,' : ', $value ,'<br/>';
}
echo "< img src='"graph.php?data=".serialize($data_daily)."'">";
但是我得到错误"提供的数据不是一个数组"
我看不出有什么问题?如果我做var_dump($data_daily)我得到:
array(8) {["14/12/2011"]=> string(1)"0" ["13/12/2011"]=> string(2)"11" ["12/12/2011"]=> string(1)"0" ["11/12/2011"]=> string(1)"2"[" tv "] =>字符串(1)"9" ["09/12/2011"]=> string(1)"3"[" 08/12/2011 "] =>字符串(1)"6" ["07/12/2011"]=> string(1)"6"}
更新2:
var_dump ($ data1);给:
array(12) { ["Jan"]=> int(12) ["Feb"]=>
int(25) ["Mar"]=> int(0) ["Apr"]=> int(7) ["May"]=> int(80) ["Jun"]=>
int(67) ["Jul"]=> int(45) ["Aug"]=> int(66) ["Sep"]=> int(23)
["Oct"]=> int(23) ["Nov"]=> int(78) ["Dec"]=> int(23) }
and var_dump($s_data1 = serialize($data1))给出:
a:12:s:3:"Jan";i:12;s:3:"Feb";i:25;s:3:"Mar";i:0;s:3:"Apr";i:7;s:3:"May";i:80;s:3:"Jun";i:67;s:3:"Jul";i:45;s:3:"Aug";i:66;s:3:"Sep";i:23;s:3:"Oct";i:23;s:3:"Nov";i:78;s:3:"Dec";i:23;}
然后unserialize (s_data1美元);与$data1
相同所以解析的参数1应该是正确的…我看不出有什么问题
我最终放弃了,并将数组加载到graph。php中:
if ($_GET['data'] == 'daily'){
$cfg['title'] = 'daily';
$graph->parseVerticalPolygonGraph($data_daily, $cfg);
}
我这样命名这个文件:
echo "<img src='"graph.php?data=daily'">";
谢谢你的帮助
我以前需要一个页面来显示多个图形使用phpMyGraph和我采取的方法是使用数据URI的和php的ob_start()和ob_get_clean()
只需对每个图使用这个:
ob_start();
$graph->parseVerticalPolygonGraph($data, $cfg);
$img = ob_get_clean();
echo "<img src='data:image/gif;base64," . base64_encode($img) . "/>";
我建议使用gif格式,因为这样你的页面大小就不会太大,你可以通过设置$cfg["type"]为"gif"(见http://phpmygraph.abisvmm.nl/#ConfigDirectives)
这也将减少多个请求的开销,并防止到图像的热链接。
你可以在这里阅读更多关于数据URI的信息http://en.wikipedia.org/wiki/Data_URI_scheme
你可以试试
echo "< img src='"graph.php?data=".urlencode(serialize($data_daily))."'">"
我可能误解了哪个脚本抛出错误,然而(我假设它是graph.php,给你提供的数据不是一个数组)。
尝试使用json代替序列化
echo "< img src='"graph.php?data=".urlencode(json_encode($data_daily))."'">"
$graph->parseVerticalPolygonGraph(json_decode($_GET['data'],true), $cfg);