我正在连接mySQL并运行一个简单的查询并将查询返回到数组中,然后使用jpgraph对数组进行内爆并尝试使用jpgraph进行图形化,但我没有得到任何数据点。
<?php
// content="text/plain; charset=utf-8"
include($_SERVER["DOCUMENT_ROOT"] . "/jpgraph/jpgraph.php");
include($_SERVER["DOCUMENT_ROOT"] . "/jpgraph/jpgraph_line.php");
$monday1 = strtotime("this monday");
$monday2 = date("Y-m-d",$monday1);
//connect to Database and return graph values
$con=mysql_connect("localhost:3306","root","","mysql");
$db_selected = mysql_select_db('mysql', $con);
if (!$db_selected)
{
die ('Can\'t use mysql : ' . mysql_error());
}
$mondayquery = "SELECT invoicetotal
FROM thermdata
WHERE CAST( datestamp AS DATE ) = '$monday2'
ORDER BY datehour";
$mondayresult = mysql_query($mondayquery,$con);
while($mondayinfo = mysql_fetch_array( $mondayresult ))
{
$monimp[] = $mondayinfo['invoicetotal'];
}
// Convert the array
$mondayfinal = implode(",", $monimp);
//set variables for graph values as array
$datay1 = array($mondayfinal);
// Setup the graph
$graph = new Graph(1200,600);
$graph->SetScale("intint");
$theme_class=new UniversalTheme;
$graph->SetTheme($theme_class);
$graph->img->SetAntiAliasing(false);
$graph->SetMargin(10,10,-500,10);
$graph->SetBox(false);
$graph->legend->SetPos(.01,0,'left','top');
$graph->legend->SetColumns(7);
$graph->legend->SetFont(FF_FONT2,FS_NORMAL, 72);
$graph->legend->SetHColMargin(10);
$graph->legend->SetLineWeight(5);
$graph->img->SetAntiAliasing();
$graph->yaxis->HideZeroLabel();
$graph->yaxis->HideLine(true);
$graph->yaxis->HideTicks(true,true);
$graph->yaxis->SetColor('white','white');
$graph->xgrid->Show();
$graph->xgrid->SetLineStyle("solid");
$graph->xaxis->SetTickLabels(array('00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24'));
$graph->xgrid->SetColor('#E3E3E3');
// Create the first line
$p1 = new LinePlot($datay1);
$graph->Add($p1);
$p1->SetColor("#ff0000");
$p1->SetLegend('Sunday');
// Output line
$graph->Stroke();
mysql_close($con);
?>
基本上,数据库每小时都包含一个新的invoicetotal,我想在当前一周的每天24小时内绘制它们。
变量$ mondayfinal解析为“0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,078,627, 1235,1919,2015“,但我不知道我做错了什么阻止jpgraph绘制它们。
任何帮助将不胜感激!
答案 0 :(得分:1)
您需要确保$ datay1数组在每个元素上都有单独的点,即:
Array ( [0] => a [1] => b [2] => c )
目前,代码将创建一个元素数组,其中所有值都在元素0处:
Array ( [0] => a,b,c )
这在jpgraph中不起作用。
相反,在csv表单中的$ mondayfinal上尝试 explode()来创建正确的数组或只需使用$ monimp,因为它看起来是正确的格式。