利用PHP的jpgraph将python脚本的数据绘制成折线图

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Horizonhui/article/details/81331451

首先是PHP绘制折线图的代码,利用jpgraph中的两个库文件,通过shell_exec调用python脚本,并将python脚本输出的数据保存到数组当中,其中要做字符串的拆分。这里假设是一条温度曲线 

<?php 
	/**
	* Created by sublime.
	* User: cui
	
	*/

	//include the library of jpgraph
	require_once ('jpgraph/jpgraph.php');
	require_once ('jpgraph/jpgraph_line.php');
	$output = shell_exec('C:\Users\99113\AppData\Local\Programs\Python\Python36-32\python port.py 2>error.txt 2>&1');
	$array = explode(',', $output);
	$data = array(10,20,30);
	$x = array(1,2,3,4,5,6,7,8,9,10);
	$y = array(0,5,10,15,20,25,30,35,40,45,50);
	$i = 0;
	
	//创建统计数据,保存到数组中,数据来自python脚本
	for($i = 0; $i < count($array); $i++){
		$data[$i] = intval($array[$i]);
	}
	//创建统计图样式,设置刻度样式
	$graph = new Graph(600,400);
	$graph->cleartheme();
	$graph->SetScale("textlin");
	$graph->SetShadow();
	$graph->img->SetMargin(40, 50, 20, 40);
	$graph->title->Set(iconv('utf-8', 'GB2312//IGNORE', '温度变化曲线图'));
	//$graph->title->Set("the change of temperature");
	//创建LinePlot对象,将创建的对象加入到创建的统计图对象中
	$lineplot = new LinePlot($data);

	$graph->Add($lineplot);
	//设置标题等样式,输出统计图
	$graph->xaxis->title->Set(iconv('utf-8', 'GB2312//IGNORE', "time/day"));//设置标题
	$graph->yaxis->title->Set(iconv('utf-8', 'GB2312//IGNORE', "temperature/℃"));
	//$graph->xaxis->SetTickLabels($x);
	//$graph->yaxis->SetTickLabels(range(0,42));
	$graph->title->SetFont(FF_SIMSUN, FS_BOLD);
	$graph->yaxis->title->SetFont(FF_SIMSUN, FS_BOLD);
	$lineplot->SetColor('red');//设置颜色
	//$lineplot->SetLegend(iconv('utf-8', 'GB2312//IGNORE', "温度曲线"));//绑定
	$graph->legend->SetLayout(LEGEND_HOR);
	$graph->legend->Pos(0.4, 0.95, 'center', 'bottom');
	//图例文字框的位置 0.4,0.95右上角为基准的,0.4是距左右距离,0.95是上下距离。
	$graph->Stroke();//输出
?>

python脚本的代码


import sys

print("20,")
print("30,")
print("25,")
print("26,")
print("27,")
print("28,")
print("29,")
print("28,")
print("26,")
print("30")

输出效果图

猜你喜欢

转载自blog.csdn.net/Horizonhui/article/details/81331451