我看书写了一段jpgraph折线图的代码!我想把它写成一个类,然后另外一个页面调用。但是总提示我错误!请各位前辈帮我指正和改写一下这个类!createGraphPic.php<?php
include ("../jpgraph/jpgraph.php");
include ("../jpgraph/jpgraph_line.php"); //引用折线图LinePlot类文件class GraphPolylines
{
var $graph;
var $p1; function GraphPolylines()
{
global $graph;
$graph = new Graph(600,300,"auto"); //创建画布
}
function setPicTiele($title) //设置标题
{
$graph->title->Set($title);
}
function setTickLabels($xTitle)//设置X轴显示内容
{
$graph->xaxis->SetTickLabels($a); //设置X轴
}
function setLinePlot($data) //创建折线对象
{
global $p1;
$p1 = new LinePlot($data); //创建折线图对象
$p1->->SetType(MARK_FILLEDCIRCLE); //设置数据坐标点为圆形标记
$p1->->SetFillColor("red"); //设置填充的颜色
$p1->->SetWidth(4); //设置圆形标记的直径为4像素
$p1->SetColor("blue"); //设置折线颜色为蓝色
}
function createPic()//真正画图
{ //设置统计图所在画布的位置,左边距50、右边距40、上边距30、下边距40,单位为像素
$graph->img->SetMargin(50,40,30,40);
$graph->img->SetAntiAliasing();
//设置折线的平滑状态
$graph->SetScale("textlin");
//设置刻度样式
$graph->SetShadow(); //创建画布阴影 $graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置标题字体
$graph->SetMarginColor("lightblue"); //设置画布的背景颜色为淡蓝色
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); //设置Y轴标题的字体
$graph->xaxis->SetPos("min");
$graph->yaxis->HideZeroLabel();
$graph->ygrid->SetFill(true,'#[email protected]','#[email protected]'); $graph->xaxis->SetFont(FF_SIMSUN); //设置X坐标轴的字体
$graph->yscale->SetGrace(20);
$graph->Add($p1); //在统计图上绘制折线
$graph->Stroke("consture.png"); //输出图像
}}
/* 原代码我就是仿这个程序写成类$datay = array(8320,9360,14956,17028,13060,15376,25428,16216,28548,18632,22724,28460);
//定义数组
$graph = new Graph(600,300,"auto"); //创建画布
//设置统计图所在画布的位置,左边距50、右边距40、上边距30、下边距40,单位为像素
$graph->img->SetMargin(50,40,30,40);
$graph->img->SetAntiAliasing();
//设置折线的平滑状态
$graph->SetScale("textlin");
//设置刻度样式
$graph->SetShadow(); //创建画布阴影
$graph->title->Set("2007年《PHP5从入门到精通》图书月销售额折线图"); //设置标题
$graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置标题字体
$graph->SetMarginColor("lightblue"); //设置画布的背景颜色为淡蓝色
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); //设置Y轴标题的字体
$graph->xaxis->SetPos("min");
$graph->yaxis->HideZeroLabel();
$graph->ygrid->SetFill(true,'#[email protected]','#[email protected]');
$a=array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");//X轴
$graph->xaxis->SetTickLabels($a); //设置X轴
$graph->xaxis->SetFont(FF_SIMSUN); //设置X坐标轴的字体
$graph->yscale->SetGrace(20);
$p1 = new LinePlot($datay); //创建折线图对象
$p1->->SetType(MARK_FILLEDCIRCLE); //设置数据坐标点为圆形标记
$p1->->SetFillColor("red"); //设置填充的颜色
$p1->->SetWidth(4); //设置圆形标记的直径为4像素
$p1->SetColor("blue"); //设置折线颜色为蓝色
$p1->SetCenter(); //在X轴的各坐标点中心位置绘制折线
$graph->Add($p1); //在统计图上绘制折线
$graph->Stroke("consture.png"); //输出图像*/
?>
另外一个页面调用
jpgraphzhe.php<?php
include ("createGraphPic.php");
$graphPic=new GraphPolylines();
$title="2007年《PHP5从入门到精通》图书月销售额折线图";
$graphPic->setPicTiele($title);
$a=array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");//X轴
$graphPic->setTickLabels($a);
$datay = array(8320,9360,14956,17028,13060,15376,25428,16216,28548,18632,22724,28460);
$graphPic->setLinePlot($datay);
$graphPic->createPic();
?>
<html><head>
<title>jpgraph折线图</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="../images/style.css" type="text/css">
</head>
<body bgcolor="#FFFFFF">
<br>
<br>
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr align="center">
<td><img src="consture.png" border=0 alt="<?=$title?>">
</td>
</tr>
</table>
</body>
</html>
include ("../jpgraph/jpgraph.php");
include ("../jpgraph/jpgraph_line.php"); //引用折线图LinePlot类文件class GraphPolylines
{
var $graph;
var $p1; function GraphPolylines()
{
global $graph;
$graph = new Graph(600,300,"auto"); //创建画布
}
function setPicTiele($title) //设置标题
{
$graph->title->Set($title);
}
function setTickLabels($xTitle)//设置X轴显示内容
{
$graph->xaxis->SetTickLabels($a); //设置X轴
}
function setLinePlot($data) //创建折线对象
{
global $p1;
$p1 = new LinePlot($data); //创建折线图对象
$p1->->SetType(MARK_FILLEDCIRCLE); //设置数据坐标点为圆形标记
$p1->->SetFillColor("red"); //设置填充的颜色
$p1->->SetWidth(4); //设置圆形标记的直径为4像素
$p1->SetColor("blue"); //设置折线颜色为蓝色
}
function createPic()//真正画图
{ //设置统计图所在画布的位置,左边距50、右边距40、上边距30、下边距40,单位为像素
$graph->img->SetMargin(50,40,30,40);
$graph->img->SetAntiAliasing();
//设置折线的平滑状态
$graph->SetScale("textlin");
//设置刻度样式
$graph->SetShadow(); //创建画布阴影 $graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置标题字体
$graph->SetMarginColor("lightblue"); //设置画布的背景颜色为淡蓝色
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); //设置Y轴标题的字体
$graph->xaxis->SetPos("min");
$graph->yaxis->HideZeroLabel();
$graph->ygrid->SetFill(true,'#[email protected]','#[email protected]'); $graph->xaxis->SetFont(FF_SIMSUN); //设置X坐标轴的字体
$graph->yscale->SetGrace(20);
$graph->Add($p1); //在统计图上绘制折线
$graph->Stroke("consture.png"); //输出图像
}}
/* 原代码我就是仿这个程序写成类$datay = array(8320,9360,14956,17028,13060,15376,25428,16216,28548,18632,22724,28460);
//定义数组
$graph = new Graph(600,300,"auto"); //创建画布
//设置统计图所在画布的位置,左边距50、右边距40、上边距30、下边距40,单位为像素
$graph->img->SetMargin(50,40,30,40);
$graph->img->SetAntiAliasing();
//设置折线的平滑状态
$graph->SetScale("textlin");
//设置刻度样式
$graph->SetShadow(); //创建画布阴影
$graph->title->Set("2007年《PHP5从入门到精通》图书月销售额折线图"); //设置标题
$graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置标题字体
$graph->SetMarginColor("lightblue"); //设置画布的背景颜色为淡蓝色
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); //设置Y轴标题的字体
$graph->xaxis->SetPos("min");
$graph->yaxis->HideZeroLabel();
$graph->ygrid->SetFill(true,'#[email protected]','#[email protected]');
$a=array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");//X轴
$graph->xaxis->SetTickLabels($a); //设置X轴
$graph->xaxis->SetFont(FF_SIMSUN); //设置X坐标轴的字体
$graph->yscale->SetGrace(20);
$p1 = new LinePlot($datay); //创建折线图对象
$p1->->SetType(MARK_FILLEDCIRCLE); //设置数据坐标点为圆形标记
$p1->->SetFillColor("red"); //设置填充的颜色
$p1->->SetWidth(4); //设置圆形标记的直径为4像素
$p1->SetColor("blue"); //设置折线颜色为蓝色
$p1->SetCenter(); //在X轴的各坐标点中心位置绘制折线
$graph->Add($p1); //在统计图上绘制折线
$graph->Stroke("consture.png"); //输出图像*/
?>
另外一个页面调用
jpgraphzhe.php<?php
include ("createGraphPic.php");
$graphPic=new GraphPolylines();
$title="2007年《PHP5从入门到精通》图书月销售额折线图";
$graphPic->setPicTiele($title);
$a=array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");//X轴
$graphPic->setTickLabels($a);
$datay = array(8320,9360,14956,17028,13060,15376,25428,16216,28548,18632,22724,28460);
$graphPic->setLinePlot($datay);
$graphPic->createPic();
?>
<html><head>
<title>jpgraph折线图</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="../images/style.css" type="text/css">
</head>
<body bgcolor="#FFFFFF">
<br>
<br>
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr align="center">
<td><img src="consture.png" border=0 alt="<?=$title?>">
</td>
</tr>
</table>
</body>
</html>
特别是类内部如何访问实例化对象属性/方法,关键字$this
class a
{
//constructor
function a(){
$this->graph = 1;
}
function getGraph()
{
return $this->graph;
}
}$b = new a();
echo $b->getGraph();//1
//constructor
function a(){
$this->graph = 1;
}
function getGraph() {
return $this->graph;
}
}
$b = new a();
echo $b->getGraph();//1
{
global $graph;
$graph = new Graph(600,300,"auto");//这里我new了之后
}
function setPicTiele($title) //设置标题
{
$graph->title->Set($title);//这里调用的时候报错!
}
这里面的一个类
{
global $graph;
$GLOBALS['graph'] = new Graph(600,300,"auto");//这里我new了之后
$this->graph =$GLOBALS['graph'];//类里调用变量要使用$this
}
function setPicTiele($title) //设置标题
{
$this->graph->title->Set($title);//这里调用的时候报错!
}
---------------------------------
40多K PHP实现 ORM 数据库类http://topic.csdn.net/u/20100226/12/43bf3dc5-795f-4fa5-861a-2f7bf8fd0cd6.html
{
function GraphPolylines()
{
//global $graph;//你以为这里global $graph,类内各方法就能读到$graph?global不是这么用的.
$this->graph = new Graph(600,300,"auto");//这里我new了之后
}
function setPicTiele($title) //设置标题
{
$this->graph->title->Set($title);//这里调用的时候报错!
}
}
include ("../jpgraph/jpgraph.php");
include ("../jpgraph/jpgraph_line.php"); //引用折线图LinePlot类文件
class GraphPolylines
{
var $graph;
var $p1;
function GraphPolylines()
{
global $graph;
$GLOBALS['graph'] = new Graph(600,300,"auto");//创建画布
//$graph = new Graph(600,300,"auto");
$this->graph =$GLOBALS['graph'];//类里调用变量要使用$this
} function setPicTiele($title) //设置标题
{
$this->graph->title->Set($title); //这里不报错了!
}
function setTickLabels($xTitle)//设置X轴显示内容
{
$this->graph->xaxis->SetTickLabels($xTitle);//但是这里报错!
}
}
?>
错误描述:Fatal error: Call to a member function SetTickLabels() on a non-object in D:\wamp\www\admin\test\createGraphPic.php on line 26
<?php
include ("../jpgraph/jpgraph.php");
include ("../jpgraph/jpgraph_line.php"); //引用折线图LinePlot类文件
class GraphPolylines
{
var $graph;
var $p1;
function GraphPolylines()
{
//global $graph;
//$GLOBALS['graph'] = new Graph(600,300,"auto");//创建画布
$this->graph = new Graph(600,300,"auto");
//$this->graph =$GLOBALS['graph'];//类里调用变量要使用$this
} function setPicTiele($title) //设置标题
{
$this->graph->title->Set($title);
}
function setTickLabels($xTitle)//设置X轴显示内容
{
$this->graph->xaxis->SetPos("min");
$this->graph->xaxis->SetFont(FF_SIMSUN); //设置X坐标轴的字体
$this->graph->xaxis->SetTickLabels($xTitle);//设置X轴
}
}
?>错误描述:Fatal error: Call to a member function SetTickLabels() on a non-object in D:\wamp\www\admin\test\createGraphPic.php on line 26
<?php
include ("../jpgraph/jpgraph.php");
include ("../jpgraph/jpgraph_line.php"); //引用折线图LinePlot类文件class GraphPolylines
{
var $graph;
var $p1;
function GraphPolylines()
{
$this->graph = new Graph(600,300,"auto"); //创建画布
}
function setPicTiele($title) //设置标题
{
$this->graph->title->Set($title);
}
function setTickLabels($xTitle)//设置X轴显示内容
{
$this->graph->xaxis->SetTickLabels($xTitle); //设置X轴
}
function setLinePlot($data) //创建折线对象
{
$this->p1 = new LinePlot($data); //创建折线图对象
$this->p1->->SetType(MARK_FILLEDCIRCLE); //设置数据坐标点为圆形标记
$this->p1->->SetFillColor("red"); //设置填充的颜色
$this->p1->->SetWidth(4); //设置圆形标记的直径为4像素
$this->p1->SetColor("blue"); //设置折线颜色为蓝色
}
function createPic()//真正画图
{ //设置统计图所在画布的位置,左边距50、右边距40、上边距30、下边距40,单位为像素
//$this->graph->img->SetMargin(50,40,30,40);
$this->graph->img->SetAntiAliasing();
//设置折线的平滑状态
$this->graph->SetScale("textlin");
//设置刻度样式
$this->graph->SetShadow(); //创建画布阴影 $this->graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置标题字体
$this->graph->SetMarginColor("lightblue"); //设置画布的背景颜色为淡蓝色
$this->graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); //设置Y轴标题的字体
$this->graph->xaxis->SetPos("min");
$this->graph->yaxis->HideZeroLabel();
$this->graph->ygrid->SetFill(true,'#[email protected]','#[email protected]'); $this->graph->xaxis->SetFont(FF_SIMSUN); //设置X坐标轴的字体
$this->graph->yscale->SetGrace(20);
$this->graph->Add($this->p1); //在统计图上绘制折线
$this->graph->Stroke("consture.png"); //输出图像
}
}
if(!file_exists('consture.png'))
{
$graph = new GraphPolylines();
$graph->setPicTiele('2007年《PHP5从入门到精通》图书月销售额折线图');
$graph->setLinePlot(array(8320,9360,14956,17028,13060,15376,25428,16216,28548,18632,22724,28460));
$graph->createPic();
}
else
echo "<img src='consture.png'/>";?>
如果还有问题,可能是jpgragh版本问题.
$graph = new GraphPolylines();
$graph->setPicTiele('2007年《PHP5从入门到精通》图书月销售额折线图');
$graph->setTickLabels(array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"));
$graph->setLinePlot(array(8320,9360,14956,17028,13060,15376,25428,16216,28548,18632,22724,28460));
$graph->createPic();
echo "<img src='consture.png'/>";
12楼前辈您把这句话加上您在帮我看看!
$graph->setTickLabels(array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"));
就差这一行了!就这一行报错!
把所有$graph换成 $this->graph
或者你可以这么改
class GraphPolylines
{
var $graph;
var $p1;
function GraphPolylines()
{
$this->graph = new Graph(600,300,"auto"); //创建画布
}
function setPicTiele($title) //设置标题
{
$this->graph->title->Set($title);
}
function setTickLabels($xTitle)//设置X轴显示内容
{
$this->xTitle = $xTitle;
}
function setLinePlot($data) //创建折线对象
{
$this->p1 = new LinePlot($data); //创建折线图对象
$this->p1->->SetType(MARK_FILLEDCIRCLE); //设置数据坐标点为圆形标记
$this->p1->->SetFillColor("red"); //设置填充的颜色
$this->p1->->SetWidth(4); //设置圆形标记的直径为4像素
$this->p1->SetColor("blue"); //设置折线颜色为蓝色
}
function createPic()//真正画图
{ //设置统计图所在画布的位置,左边距50、右边距40、上边距30、下边距40,单位为像素
//$this->graph->img->SetMargin(50,40,30,40);
$this->graph->img->SetAntiAliasing();
//设置折线的平滑状态
$this->graph->SetScale("textlin");
//设置刻度样式
$this->graph->SetShadow(); //创建画布阴影 $this->graph->title->SetFont(FF_SIMSUN,FS_BOLD); //设置标题字体
$this->graph->SetMarginColor("lightblue"); //设置画布的背景颜色为淡蓝色
$this->graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); //设置Y轴标题的字体
$this->graph->xaxis->SetPos("min");
$this->graph->yaxis->HideZeroLabel();
$this->graph->ygrid->SetFill(true,'#[email protected]','#[email protected]'); $this->graph->xaxis->SetFont(FF_SIMSUN); //设置X坐标轴的字体
$this->graph->yscale->SetGrace(20);
if(!!$this->xTitle)
{
$this->graph->xaxis->SetTickLabels($this->xTitle);
}
$this->graph->Add($this->p1); //在统计图上绘制折线
$this->graph->Stroke("consture.png"); //输出图像
}
}
if(!file_exists('consture.png'))
{
$graph = new GraphPolylines();
$graph->setPicTiele('2007年《PHP5从入门到精通》图书月销售额折线图');
$graph->setLinePlot(array(8320,9360,14956,17028,13060,15376,25428,16216,28548,18632,22724,28460));
$graph->setTickLabels(array("a月","b月","c月","4月","5月","6月","7月","8月","9月","10月","11月","12月"));
$graph->createPic();
}
echo "<img src='consture.png'/>";
$graph->setPicTiele('2007年《PHP5从入门到精通》图书月销售额折线图');
$graph->setTickLabels(array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"));//$graph是new出来的对象。setTickLabels是一个方法。
//它报错主要是指这个setTickLabels方法里
//$this->graph->xaxis->SetTickLabels($xTitle); 的这行报错! $graph->setLinePlot(array(8320,9360,14956,17028,13060,15376,25428,16216,28548,18632,22724,28460));
$graph->createPic();
echo "<img src='consture.png'/>";
if(!!$this->xTitle)这里两个叹号在PHP里是什么意思!
$a = array();
echo !$a ;//1,true
echo !!$a ;//0,false
$a = array(1);
echo !$a ;//0,false
echo !!$a ;//1,true