<?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(600,450,"auto"); //创建新的Graph对象
$graph->SetScale("textlin");
$graph->SetShadow();
//设置图像的阴影样式
$graph->SetMarginColor('darkgreen@0.8');
$graph->SetColor('red'); //设置背景
//$graph->Set3DPerspective(SKEW3D_LEFT,700,700,true); 3D倾斜
//$graph->SetBackgroundImage("图片地址",1); //设置背景
//$graph->img->SetMargin(60,30,30,95); // 空余四角边距(左右上下)
$graph->img->SetMargin(60,30,30,95); //设置图像边距(相对图片顶部)
$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轴的各坐标点中心位置绘制折线
$lineplot1->value->SetFormat('%0.2f'); //值的格式化
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);
$lineplot1->value->SetColor("red");//曲线上面显示的数值的文字颜色
$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("10-10 1:00","10-10 2:00","10-10 3:00","10-10 4:00","10-10 5:00","10-10 6:00","10-10 7:00","10-10 8:00","10-10 9:00","10-10 11:00","10-10 12:00","10-10 13:00","10-10 14:00");
graph-\>xaxis-\>SetTickLabels(mouth_arr);
$graph->xaxis->SetFont(FF_SIMSUN); //设置X坐标轴的字体
// 设置 x 轴标注文字为斜体,粗体,6号小字
$graph->xaxis->SetFont(FF_SIMSUN,FS_BOLD,8);
// 设置 x 轴标注文字 45 度倾斜。注:前面 SetFont 必须为 FF_ARIAL
$graph->xaxis->SetLabelAngle(45);
$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->yaxis->SetWeight(2); //y轴线宽
$graph->xaxis->SetWeight(2);
//隐藏x轴上的刻度线
$graph->xaxis->HideTicks(false,false);//显示刻度线
$graph->xgrid->Show();//设置垂直于X轴的网格线显示,默认不显示
$graph->Stroke(); //输出图像
//折线图
?>