使用不同的字体和文字
image_graph4.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('[email protected]');
 
$fill =& Image_Graph::factory('Image_Graph_Fill_Array');
$fill->addColor('red', 'J');
$fill->addColor('blue', 'F');
$fill->addColor('yellow', '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();
?>

这里,我们使用了ttf 字体Bitstream-Vera-Sans-Mono, 字号12. 通常会有这样的错误提示信息:
Warning: imagettfbbox(): Could not find/open font in /usr/share/php/Image/Canvas/GD.php on line 1245
这主要是因为你没有把字体文件放到字体目录php/Image/Canvas/Fonts/. 上面的例子中,我们需要把Bitstream-Vera-Sans-Mono.ttf文件放到该目录中。

一个图中多个象限(图表)
如前面提到,在一个图中我们可以生成多个图表,也可以说多个象限,参考下面的范例:
image_graph5.php

<?php
include 'Image/Graph.php';
$Graph =& Image_Graph::factory('graph', array(600, 300));
 
$Graph->add(
    
Image_Graph::horizontal(
        
Image_Graph::vertical(
            
$PlotareaTopLeft = Image_Graph::factory('plotarea'),
            
$PlotareaBottomLeft = Image_Graph::factory('plotarea'),
            
60
        
),
            
$PlotareaRight = Image_Graph::factory('plotarea'),
        
30
    
)
);
 
$Dataset =& Image_Graph::factory('dataset');
$Dataset->addPoint('Jan', 5, 'J');
$Dataset->addPoint('Feb', 13, 'F');
$Dataset->addPoint('March', 10, 'M');
$Plot =& $PlotareaRight->addNew('bar', &$Dataset);
 
$Plot->setLineColor('green');
$Plot->setBackgroundColor('[email protected]');
 
$fill =& Image_Graph::factory('Image_Graph_Fill_Array');
$fill->addColor('red', 'J');
$fill->addColor('blue', 'F');
$fill->addColor('yellow', 'M');
$Plot->setFillStyle($fill);
 
$Font =& $Graph->addNew('ttf_font', 'Bitstream-Vera-Sans-Mono');
$Font->setSize(12);
$Graph->setFont($Font);
 
$YAxis =& $PlotareaRight->getAxis(IMAGE_GRAPH_AXIS_Y);
$YAxis->setTitle('Rainy Days', 'vertical');
$XAxis =& $PlotareaRight->getAxis(IMAGE_GRAPH_AXIS_X);
$XAxis->setTitle('Month');
 
//2nd Plotarea
$Plot2 =& $PlotareaTopLeft->addNew('pie', &$Dataset);
$Plot2->setLineColor('green');
$Plot2->setBackgroundColor('[email protected]');
 
$fill =& Image_Graph::factory('Image_Graph_Fill_Array');
$fill->addColor('red', 'J');
$fill->addColor('blue', 'F');
$fill->addColor('yellow', 'M');
$Plot2->setFillStyle($fill);
 
//3rd Plotarea
$Plot3 =& $PlotareaBottomLeft->addNew('line', &$Dataset);
$Plot3->setLineColor('green');
$Plot3->setBackgroundColor('[email protected]');
 
$Graph->done();
?>

首先我们纵向分成两个部分,一个占据30%,一个占据60%,利用同样的数据源,我们生成不同表现方式的图表。 这里我们使用了饼图和曲线图,Image_Graph为我们提供了非常多的表现方式,大致有下面这些:
line (Image_Graph_Plot_Line)
area (Image_Graph_Plot_Area)
bar (Image_Graph_Plot_Bar)
smooth_line (Image_Graph_Plot_Smoothed_Line)
smooth_area (Image_Graph_Plot_Smoothed_Area)
pie (Image_Graph_Plot_Pie)
step (Image_Graph_Plot_Step)
impulse (Image_Graph_Plot_Impulse)
dot (Image_Graph_Plot_Dot)
scatter, a synonym for dot
radar (Image_Graph_Plot_Radar)
Image_Graph_Plot_CandleStick
Image_Graph_Plot_Band

标题
我们为图表增加一个标题:

<?php
include 'Image/Graph.php';
$Graph =& Image_Graph::factory('graph', array(600, 300));
 
$Graph->add(
 
Image_Graph::vertical(
    
Image_Graph::factory('title',array('3 graph rainy day',12)),
    
Image_Graph::horizontal(
        
Image_Graph::vertical(
            
$PlotareaTopLeft = Image_Graph::factory('plotarea'),
            
$PlotareaBottomLeft = Image_Graph::factory('plotarea'),
            
60
        
),
            
$PlotareaRight = Image_Graph::factory('plotarea'),
        
30
    
),
    
5
 
)
);
 
$Dataset =& Image_Graph::factory('dataset');
$Dataset->addPoint('Jan', 5, 'J');
$Dataset->addPoint('Feb', 13, 'F');
$Dataset->addPoint('March', 10, 'M');
$Plot =& $PlotareaRight->addNew('bar', &$Dataset);
 
$Plot->setLineColor('green');
$Plot->setBackgroundColor('[email protected]');
 
$fill =& Image_Graph::factory('Image_Graph_Fill_Array');
$fill->addColor('red', 'J');
$fill->addColor('blue', 'F');
$fill->addColor('yellow', 'M');
$Plot->setFillStyle($fill);
 
$Font =& $Graph->addNew('ttf_font', 'Bitstream-Vera-Sans-Mono');
$Font->setSize(12);
$Graph->setFont($Font);
 
$YAxis =& $PlotareaRight->getAxis(IMAGE_GRAPH_AXIS_Y);
$YAxis->setTitle('Rainy Days', 'vertical');
$XAxis =& $PlotareaRight->getAxis(IMAGE_GRAPH_AXIS_X);
$XAxis->setTitle('Month');
 
//2nd Plotarea
$Plot2 =& $PlotareaTopLeft->addNew('pie', &$Dataset);
$Plot2->setLineColor('green');
$Plot2->setBackgroundColor('[email protected]');
 
$fill =& Image_Graph::factory('Image_Graph_Fill_Array');
$fill->addColor('red', 'J');
$fill->addColor('blue', 'F');
$fill->addColor('yellow', 'M');
$Plot2->setFillStyle($fill);
 
//3rd Plotarea
$Plot3 =& $PlotareaBottomLeft->addNew('line', &$Dataset);
$Plot3->setLineColor('green');
$Plot3->setBackgroundColor('[email protected]');
 
$Graph->done();
?>
有关Image_Graph的入门介绍就到这里