<?php
include ("jpgraph/jpgraph.php");
include ("jpgraph/jpgraph_line.php");
//
// 假定数据库用户名:root,密码:123456,数据库:RUNOOB
$con=mysqli_connect("localhost","root","123456","login");
$data3=array(10,1,2,3,4,5);
$i=0;
if (mysqli_connect_errno($con))
{
echo "连接 MySQL 失败: " . mysqli_connect_error();
}
$sql="SELECT * FROM savedata WHERE id<73 order by id";
$result=mysqli_query($con,$sql);
// 数字数组
while($row=mysqli_fetch_array($result)){
// echo $row["dataval0"];
$data3[$i]=$row["dataval0"];
$i=$i+1;
/*显示数据结果*/
}
// 释放结果集
mysqli_free_result($result);
mysqli_close($con);
///
$data1 = array(523,634,371,278,685,587,490,256,398,545,367,1577); //第一条曲线的数组
$data2 = array(19,23,34,38,45,67,71,78,85,87,90,96,109); //第二条曲线的数组
// print_r($data3);
$graph = new Graph(800,400,"auto"); //创建新的Graph对象
$graph->SetScale("textlin");
$graph->SetShadow(); //设置图像的阴影样式
$graph->img->SetMargin(60,30,30,70); //设置图像边距(相对图片顶部)
$graph->title->Set("用电量图"); //设置图像标题
$graph->title->SetMargin(15); //设置标题的边距
//$lineplot1=new LinePlot($data1);
$lineplot1=new LinePlot($data3); //创建设置两条曲线对象
$lineplot2=new LinePlot($data2);
$lineplot1->value->Show();
$lineplot1->value->SetColor("black");
$lineplot1->mark->SetType(MARK_FILLEDCIRCLE); //设置数据坐标点为圆形标记
$lineplot1->mark->SetFillColor("red"); //设置填充的颜色
$lineplot1->mark->SetWidth(2); //设置圆形标记的直径为4像素
$lineplot1->SetColor("blue"); //设置折形颜色为蓝色
$lineplot1->SetCenter(); //在X轴的各坐标点中心位置绘制折线
$graph->Add($lineplot1); //将曲线放置到图像上
$lineplot1->value->Show();
//
$lineplot2->mark->SetType(MARK_FILLEDCIRCLE); //设置数据坐标点为圆形标记
$lineplot2->mark->SetFillColor("green"); //设置填充的颜色
$lineplot2->mark->SetWidth(2); //设置圆形标记的直径为4像素
$lineplot2->SetColor("blue"); //设置折形颜色为蓝色
//
$graph->Add($lineplot2);
$lineplot2->value->Show();
$graph->xaxis->title->Set("小时"); //设置坐标轴名称
$graph->yaxis->title->Set("用电量(kwh)");
$graph->xaxis->title->SetMargin(10);
$graph->yaxis->title->SetMargin(10);
$graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置字体
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
//设置x轴各值,传入一维数组
//参数:$gDateLocale->GetShortMonth() 底部为 Jan Feb...
$mouth_arr=array("1:00","2:00","3:00","4:00","5:00","6:00","7:00","8:00","9:00","11:00","12:00","13:00","14:00");
$graph->xaxis->SetTickLabels($mouth_arr);
$graph->xaxis->SetFont(FF_SIMSUN); //设置X坐标轴的字体
$lineplot1->SetColor("red"); //设置颜色
$lineplot2->SetColor("blue");
$lineplot1->SetLegend("最大值"); //设置图例名称
$lineplot2->SetLegend("最小值");
$graph->legend->SetFont(FF_SIMSUN,FS_BOLD);
$graph->legend->SetLayout(LEGEND_HOR); //设置图例样式和位置
$graph->legend->Pos(0.5,0.96,"center","bottom"); //设置下部lengend位置
$graph->Stroke(); //输出图像
//折线图
?>