6月
15
最近接到了这个项目,里面需要生成数据的趋势图,搜了下,决定用PEAR的Image Graph类去做。
首先,需要去http://www.pearchina.com/下载所需要的类文件。
需要的文件列表如下:
PEAR Package: PEAR Installer 1.3.1 or newer
PEAR Package: Image_Canvas 0.3.0 or newer
PEAR Package: Image_Color 1.0.0 or newer
当然,您的php配置必须支持GD库才可以哟~!
您可以去http://pear.veggerby.dk/samples/这里察看更多的例子,
相信通过您的摸索,可以做出更棒的功能!
下面是一份代码,可以生成2线图表,支持坐标格。

<?php
// include libraries
require_once 'Image/Graph.php';
require_once 'Image/Canvas.php';
$Canvas =& Image_Canvas::factory('png', array('width' => 800, 'height' => 600));
// create the graph
$Graph =& Image_Graph::factory('graph', $Canvas);
// add a TrueType font
$Font =& $Graph->addNew('font', 'Verdana');
// set the font size to 8 pixels
$Font->setSize(8);
// set the font
$Graph->setFont($Font);
// create the layout
$Graph->add(
Image_Graph::vertical(
Image_Graph::factory('title', array("Trend Chart For Ganji.com", 12)),
Image_Graph::vertical(
$Plotarea1 = Image_Graph::factory('plotarea'),
$Legend = Image_Graph::factory('legend'),
80
),
5
)
);
// add grids
$Grid =& $Plotarea1->addNew('line_grid', IMAGE_GRAPH_AXIS_Y);
$Grid->setLineColor('silver');
// setup legend
$Legend->setPlotarea($Plotarea1);
// create the dataset
$Dataset1 =& Image_Graph::factory('dataset'); //有效帖子
$Dataset1->addPoint('a', 400); //设置点值
$Dataset1->addPoint('b', 375);
$Dataset1->addPoint('c', 500);
$Dataset1->addPoint('d', 550);
$Dataset1->addPoint('e', 620);
$Dataset1->addPoint('f', 681);
$Dataset1->addPoint('g', 730);
$Dataset2 =& Image_Graph::factory('dataset'); //删除帖子数
$Dataset2->addPoint('a', 10);
$Dataset2->addPoint('b', 25);
$Dataset2->addPoint('c', 55);
$Dataset2->addPoint('d', 75);
$Dataset2->addPoint('e', 80);
$Dataset2->addPoint('f', 55);
$Dataset2->addPoint('g', 27);
$Datasets =array($Dataset1,$Dataset2);
// create the plot as stacked area chart using the datasets
$Plot =& $Plotarea1->addNew('Image_Graph_Plot_Area', array($Datasets, 'stacked'));
// set names for datasets (for legend)
$Datasets[0]->setName('Avalueable');
$Datasets[1]->setName('Deleted');
// set line color for plot
$Plot->setLineColor('gray');
// create and populate the fillarray
$FillArray =& Image_Graph::factory('Image_Graph_Fill_Array');
$FillArray->addColor('green@0.2');
$FillArray->addColor('yellow@0.2');
// set a fill style
$Plot->setFillStyle($FillArray);
// set color
$Plotarea1->setFillColor('silver@0.3');
// output the Graph
$Graph->done();
?>
如果需要做成那种A页面上一个按钮,点击A页面上的按钮,弹出一个无头的独立页面B,显示生成的图片,
可以在A页面上添加下面的js代码:
在A页面的按钮处,添加语句如:
这样,在A处点击,可以弹出走势图。
还要注意一点,如果你使用的是linux系统,恐怕还会报告说找不到字体库的错误,,
所以你最好把你用到的字体文件拷贝到Image\Canvas\Fonts下面,,
必要情况下,还要在fontmap.txt中添加字体名称定义及字体文件名。
首先,需要去http://www.pearchina.com/下载所需要的类文件。
需要的文件列表如下:
PEAR Package: PEAR Installer 1.3.1 or newer
PEAR Package: Image_Canvas 0.3.0 or newer
PEAR Package: Image_Color 1.0.0 or newer
当然,您的php配置必须支持GD库才可以哟~!
您可以去http://pear.veggerby.dk/samples/这里察看更多的例子,
相信通过您的摸索,可以做出更棒的功能!
下面是一份代码,可以生成2线图表,支持坐标格。

<?php
// include libraries
require_once 'Image/Graph.php';
require_once 'Image/Canvas.php';
$Canvas =& Image_Canvas::factory('png', array('width' => 800, 'height' => 600));
// create the graph
$Graph =& Image_Graph::factory('graph', $Canvas);
// add a TrueType font
$Font =& $Graph->addNew('font', 'Verdana');
// set the font size to 8 pixels
$Font->setSize(8);
// set the font
$Graph->setFont($Font);
// create the layout
$Graph->add(
Image_Graph::vertical(
Image_Graph::factory('title', array("Trend Chart For Ganji.com", 12)),
Image_Graph::vertical(
$Plotarea1 = Image_Graph::factory('plotarea'),
$Legend = Image_Graph::factory('legend'),
80
),
5
)
);
// add grids
$Grid =& $Plotarea1->addNew('line_grid', IMAGE_GRAPH_AXIS_Y);
$Grid->setLineColor('silver');
// setup legend
$Legend->setPlotarea($Plotarea1);
// create the dataset
$Dataset1 =& Image_Graph::factory('dataset'); //有效帖子
$Dataset1->addPoint('a', 400); //设置点值
$Dataset1->addPoint('b', 375);
$Dataset1->addPoint('c', 500);
$Dataset1->addPoint('d', 550);
$Dataset1->addPoint('e', 620);
$Dataset1->addPoint('f', 681);
$Dataset1->addPoint('g', 730);
$Dataset2 =& Image_Graph::factory('dataset'); //删除帖子数
$Dataset2->addPoint('a', 10);
$Dataset2->addPoint('b', 25);
$Dataset2->addPoint('c', 55);
$Dataset2->addPoint('d', 75);
$Dataset2->addPoint('e', 80);
$Dataset2->addPoint('f', 55);
$Dataset2->addPoint('g', 27);
$Datasets =array($Dataset1,$Dataset2);
// create the plot as stacked area chart using the datasets
$Plot =& $Plotarea1->addNew('Image_Graph_Plot_Area', array($Datasets, 'stacked'));
// set names for datasets (for legend)
$Datasets[0]->setName('Avalueable');
$Datasets[1]->setName('Deleted');
// set line color for plot
$Plot->setLineColor('gray');
// create and populate the fillarray
$FillArray =& Image_Graph::factory('Image_Graph_Fill_Array');
$FillArray->addColor('green@0.2');
$FillArray->addColor('yellow@0.2');
// set a fill style
$Plot->setFillStyle($FillArray);
// set color
$Plotarea1->setFillColor('silver@0.3');
// output the Graph
$Graph->done();
?>
如果需要做成那种A页面上一个按钮,点击A页面上的按钮,弹出一个无头的独立页面B,显示生成的图片,
可以在A页面上添加下面的js代码:
<script LANGUAGE="JavaScript">
<!--
function openwin()
{ window.open ("B.php","newwindow","height=600,width=850,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no");
//写成一行
}
//-->
</script>
<!--
function openwin()
{ window.open ("B.php","newwindow","height=600,width=850,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no");
//写成一行
}
//-->
</script>
在A页面的按钮处,添加语句如:
<a href="#" target="_blank" onclick="openwin();">生成走势图</a>
这样,在A处点击,可以弹出走势图。
还要注意一点,如果你使用的是linux系统,恐怕还会报告说找不到字体库的错误,,
Warning: imagettfbbox() [function.imagettfbbox]: Could not find/open font in C:\Program Files\vtigercrm5\apache\htdocs\vtigerCRM\Image\Canvas\GD.php on line 1245
所以你最好把你用到的字体文件拷贝到Image\Canvas\Fonts下面,,
必要情况下,还要在fontmap.txt中添加字体名称定义及字体文件名。
随想
php下的日期比较与日期计算
