进一步讨论PHP Pear图表创建包Image_Graph

转载请注明出处:Just Do IT :: http://www.toplee.com/blog/?p=178
TrackBack URL: http://www.toplee.com/blog/wp-trackback.php?p=178

我已经喜欢上了Image_Graph, 它简单好用而功能强大,这里在上次入门介绍基础上进一步讨论一下这个包。
Image_Graph入门请参考这篇Blog:http://www.toplee.com/blog/?p=169

使用背景图片
有些时候,我们需要实现效果更加丰富的图表,比如为客户增加必要的背景图片,这里我们在上次讨论的image_graph4.php 基础上增加图表的背景图片.

2image_graph1.php

<?php
include 'Image/Graph.php';
$Graph =& Image_Graph::factory('graph', array(600, 300));
$Plotarea =& $Graph->addNew('plotarea');
$Dataset =& Image_Graph::factory('dataset');
$Dataset->addPoint('Jan', 5, 'J');
$Dataset->addPoint('Feb', 13, 'F');
$Dataset->addPoint('March', 10, 'M');
$Plot =& $Plotarea->addNew('bar', &$Dataset);
 
$Plot->setLineColor('green');
$Plot->setBackgroundColor('green@0.1');
 
$fill =& Image_Graph::factory('Image_Graph_Fill_Array');
$fill->addColor('red', 'J');
$fill->addColor('blue', 'F');
$fill->addColor('yellow', 'M');
$Plot->setFillStyle($fill);
 
//set Background
$bgfill =& Image_Graph::factory('Image_Graph_Fill_Image', 'images/helicopter.jpg');
$Plotarea->setFillStyle($bgfill);
 
$Font =& $Graph->addNew('ttf_font', 'Bitstream-Vera-Sans-Mono');
$Font->setSize(12);
$Graph->setFont($Font);
 
$YAxis =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y);
$YAxis->setTitle('Rainy Days', 'vertical');
$XAxis =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X);
$XAxis->setTitle('Month');
 
$Graph->done();
?>

phpimggraph1

不幸的是,此时我们看到的背景图片被前面的图表遮盖,我们需要让它看起来更好看一些,这里可以通过使图表透明的方法来实现。修改下面的几行代码:

$fill->addColor('red@0.8', 'J');
$fill->addColor('blue@0.5', 'F');
$fill->addColor('yellow@0.2', 'M');

phpimggraph2

现在我们能看到全部的直升飞机图片,大多数的图片可以在0.2的参数下达到最好的透明效果。

图表堆叠
下面我们将两组数据产生的图表进行堆叠,这里采用上下堆叠的方式。
2image_graph2.php

<?php
include 'Image/Graph.php';
 
// create a graph 600 pixels wide by 300 high
$Graph = & Image_Graph::factory('graph', array(600, 300));
 
$Font =& $Graph->addNew('ttf_font', 'Bitstream-Vera-Sans-Mono');
$Font->setSize(12);
 
$Graph->setFont($Font);
 
$Graph->add(
 
Image_Graph::vertical(
    
Image_Graph::factory('title', array('My way or the highway', 12)),
    
Image_Graph::vertical(
      
$Plotarea = Image_Graph::factory('plotarea'),
      
$Legend = Image_Graph::factory('legend'),
      
90
    
),
    
5
 
)
);
$Legend->setPlotarea($Plotarea);
 
 
// Create and populate 3 datasets
 
$Dataset1 =& Image_Graph::factory('dataset');
$Dataset1->addPoint('Jan', 1);
$Dataset1->addPoint('Feb', 2);
$Dataset1->addPoint('Mar', 2);
$Dataset1->addPoint('Apr', 4);
 
$Dataset2 =& Image_Graph::factory('dataset');
$Dataset2->addPoint('Jan', 3);
$Dataset2->addPoint('Feb', 4);
$Dataset2->addPoint('Mar', 5);
$Dataset2->addPoint('Apr', 2);
 
$Dataset3 =& Image_Graph::factory('dataset');
$Dataset3->addPoint('Jan', 10);
$Dataset3->addPoint('Feb', 9);
$Dataset3->addPoint('Mar', 12);
$Dataset3->addPoint('Apr', 34);
 
 
$Dataset1->setName('The highway');
$Dataset2->setName('Their way');
$Dataset3->setName('My way');
 
$Datasets = array($Dataset1,$Dataset2,$Dataset3);
 
$Plot =& $Plotarea->addNew('Image_Graph_Plot_Area', array($Datasets,'stacked'));
 
$Plot->setLineColor('gray');
 
$FillArray =& Image_Graph::factory('Image_Graph_Fill_Array');
$FillArray->addColor('slateblue@0.2');
$FillArray->addColor('fuchsia@0.2');
$FillArray->addColor('lime@0.2');
 
// set a standard fill style
$Plot->setFillStyle($FillArray);
 
$XAxis =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X);
$XAxis->setTitle('Month');
 
$YAxis =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y);
$YAxis->setTitle('The bigger the better', 'vertical');
 
// output the Graph
$Graph->done();
?>

phpimggraph3

在这里我们使用了相同的month作为横轴标记,如果我们改变任何一个点的标记,图表会跟着改变,比如我们在第一个数据集中用Dec替换Jan,修改如下:

$Dataset1->addPoint('Dec', 1);
$Dataset1->addPoint('Feb', 2);
$Dataset1->addPoint('Mar', 2);
$Dataset1->addPoint('Apr', 4);
在数据集
2中添加一个新的标记
$Dataset1 =& Image_Graph::factory('dataset');
$Dataset1->addPoint('Dec', 1);
$Dataset1->addPoint('Feb', 2);
$Dataset1->addPoint('Mar', 2);
$Dataset1->addPoint('Apr', 4);
 
$Dataset2 =& Image_Graph::factory('dataset');
$Dataset2->addPoint('Jan', 3);
$Dataset2->addPoint('Feb', 4);
$Dataset2->addPoint('Apr', 5);
$Dataset2->addPoint('May', 2);

phpimggraph4


两个图表使用不同的轴
在一个象限中我们可能希望放置两个图表,但是希望使用不同的尺度来定义轴。下面我在image_graph4.php的基础上进行修改。
2image_graph5.php

<?php
include 'Image/Graph.php';
$Graph =& Image_Graph::factory('graph', array(600, 300));
$Plotarea =& $Graph->addNew('plotarea');
$Dataset =& Image_Graph::factory('dataset');
$Dataset->addPoint('Jan', 5, 'J');
$Dataset->addPoint('Feb', 13, 'F');
$Dataset->addPoint('March', 10, 'M');
$Plot =& $Plotarea->addNew('bar', &$Dataset);
 
$Plot->setLineColor('green');
$Plot->setBackgroundColor('green@0.1');
 
$Dataset2 =& Image_Graph::factory('dataset');
$Dataset2->addPoint('Jan', 9);
$Dataset2->addPoint('Feb', 4);
$Dataset2->addPoint('March', 12);
$Plot2 =& $Plotarea->addNew('line', &$Dataset2);
 
$Plot2->setLineColor('firebrick'); // yes, that's a colour
$Plot2->setBackgroundColor('green@0.1');
 
$fill =& Image_Graph::factory('Image_Graph_Fill_Array');
$fill->addColor('red@0.2', 'J');
$fill->addColor('blue@0.2', 'F');
$fill->addColor('yellow@0.2', 'M');
$Plot->setFillStyle($fill);
 
$Font =& $Graph->addNew('ttf_font', 'Bitstream-Vera-Sans-Mono');
$Font->setSize(12);
$Graph->setFont($Font);
 
$YAxis =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y);
$YAxis->setTitle('Rainy Days', 'vertical');
$XAxis =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X);
$XAxis->setTitle('Month');
 
$Graph->done();
?>

phpimggraph5

我们可以添加更多数据集,同时使用不同的图表类型。看下面的例子:

<?php
include 'Image/Graph.php';
$Graph =& Image_Graph::factory('graph', array(600, 300));
$Plotarea =& $Graph->addNew('plotarea');
$Dataset =& Image_Graph::factory('dataset');
$Dataset->addPoint('Jan', 5, 'J');
$Dataset->addPoint('Feb', 13, 'F');
$Dataset->addPoint('March', 10, 'M');
$Plot =& $Plotarea->addNew('bar', &$Dataset);
 
$Plot->setLineColor('green');
$Plot->setBackgroundColor('green@0.1');
 
$Dataset2 =& Image_Graph::factory('dataset');
$Dataset2->addPoint('Jan', 90);
$Dataset2->addPoint('Feb', 40);
$Dataset2->addPoint('March', 120);
$Plot2 =& $Plotarea->addNew('line', &$Dataset2, IMAGE_GRAPH_AXIS_Y_SECONDARY);
 
$Plot2->setLineColor('firebrick'); // yes, that's a colour
$Plot2->setBackgroundColor('green@0.1');
 
$fill =& Image_Graph::factory('Image_Graph_Fill_Array');
$fill->addColor('red@0.2', 'J');
$fill->addColor('blue@0.2', 'F');
$fill->addColor('yellow@0.2', 'M');
$Plot->setFillStyle($fill);
 
$Font =& $Graph->addNew('ttf_font', 'Bitstream-Vera-Sans-Mono');
$Font->setSize(12);
$Graph->setFont($Font);
 
$YAxis =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y);
$YAxis->setTitle('Rainy Days', 'vertical');
$XAxis =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X);
$XAxis->setTitle('Month');
 
$YAxisSecondary =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y_SECONDARY);
$YAxisSecondary->setTitle('Butterfly sightings','vertical');
 
$Graph->done();
?>

phpimggraph6

不同轴的方向定义
2image_graph6.php

<?php
include 'Image/Graph.php';
$Graph =& Image_Graph::factory('graph', array(600, 300));
 
$Graph->add(
 
Image_Graph::vertical(
      
Image_Graph::factory('title', array('Top to bottom axis', 12)),
      
Image_Graph::horizontal(
        
$Plotarea1 = Image_Graph::factory('plotarea'),
        
$Plotarea2 = Image_Graph::factory('plotarea'),
      
50
      
),
      
5
  
)
);
 
$Dataset1 =& Image_Graph::factory('dataset');
$Dataset1->addPoint('Jan', 5, 'J');
$Dataset1->addPoint('Feb', 13, 'F');
$Dataset1->addPoint('March', 10, 'M');
 
$Dataset2 =& Image_Graph::factory('dataset');
$Dataset2->addPoint('Jan', -6, 'J');
$Dataset2->addPoint('Feb', -13, 'F');
$Dataset2->addPoint('March', -10, 'M');
 
$Plot1 =& $Plotarea1->addNew('bar', &$Dataset1);
$Plot2 =& $Plotarea2->addNew('bar', &$Dataset2);
 
$Plot1->setLineColor('green');
$Plot1->setBackgroundColor('green@0.1');
 
$fill =& Image_Graph::factory('Image_Graph_Fill_Array');
$fill->addColor('red@0.2', 'J');
$fill->addColor('blue@0.2', 'F');
$fill->addColor('yellow@0.2', 'M');
$Plot1->setFillStyle($fill);
$Plot2->setFillStyle($fill);
 
$Font =& $Graph->addNew('ttf_font', 'Bitstream-Vera-Sans-Mono');
$Font->setSize(12);
$Graph->setFont($Font);
 
$YAxis1 =& $Plotarea1->getAxis(IMAGE_GRAPH_AXIS_Y);
$YAxis1->setTitle('Rainy Days', 'vertical');
$XAxis1 =& $Plotarea1->getAxis(IMAGE_GRAPH_AXIS_X);
$XAxis1->setTitle('Month');
 
$YAxis2 =& $Plotarea2->getAxis(IMAGE_GRAPH_AXIS_Y);
$YAxis2->setTitle('Minimum Temperature', 'vertical');
$XAxis2 =& $Plotarea2->getAxis(IMAGE_GRAPH_AXIS_X);
 
$Graph->done();
?>

phpimggraph7

2image_graph7.php

<?php
include 'Image/Graph.php';
$Graph =& Image_Graph::factory('graph', array(600, 300));
 
$Graph->add(
 
Image_Graph::vertical(
      
Image_Graph::factory('title', array('Top to bottom axis', 12)),
      
Image_Graph::horizontal(
        
$Plotarea1 = Image_Graph::factory('plotarea'),
        
$Plotarea2 = Image_Graph::factory('plotarea'),
      
50
      
),
      
5
  
)
);
 
$Dataset1 =& Image_Graph::factory('dataset');
$Dataset1->addPoint('Jan', 5, 'J');
$Dataset1->addPoint('Feb', 13, 'F');
$Dataset1->addPoint('March', 10, 'M');
 
$Dataset2 =& Image_Graph::factory('dataset');
$Dataset2->addPoint('Jan', 6, 'J');
$Dataset2->addPoint('Feb', 13, 'F');
$Dataset2->addPoint('March', 10, 'M');
 
$Plot1 =& $Plotarea1->addNew('bar', &$Dataset1);
$Plot2 =& $Plotarea2->addNew('bar', &$Dataset2);
 
$Plot1->setLineColor('green');
$Plot1->setBackgroundColor('green@0.1');
 
$fill =& Image_Graph::factory('Image_Graph_Fill_Array');
$fill->addColor('red@0.2', 'J');
$fill->addColor('blue@0.2', 'F');
$fill->addColor('yellow@0.2', 'M');
$Plot1->setFillStyle($fill);
$Plot2->setFillStyle($fill);
 
$Font =& $Graph->addNew('ttf_font', 'Bitstream-Vera-Sans-Mono');
$Font->setSize(12);
$Graph->setFont($Font);
 
$YAxis1 =& $Plotarea1->getAxis(IMAGE_GRAPH_AXIS_Y);
$YAxis1->setTitle('Rainy Days', 'vertical');
$XAxis1 =& $Plotarea1->getAxis(IMAGE_GRAPH_AXIS_X);
$XAxis1->setTitle('Month');
 
$YAxis2 =& $Plotarea2->getAxis(IMAGE_GRAPH_AXIS_Y);
$YAxis2->setTitle('Minimum Temperature', 'vertical');
$XAxis2 =& $Plotarea2->getAxis(IMAGE_GRAPH_AXIS_X);
$YAxis2->setInverted(true);
$XAxis2->setInverted(true);
 
$Graph->done();
?>

phpimggraph8

点击后面的连接查看非常丰富的通过Image_Graph实现的图表例子:
http://pear.veggerby.dk/samples/
每个图表例子都包含PHP的源码可以参考。
下面是一些精彩的范例图片:
example1example2example3example4example5example6example7

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
One Response
  1. Pingback: 化妆镜

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image