User this class the PHP programer can plot data to image as solid bar or line or pie .
Bar chart Preview:
This chart show three bars with red, blue and green .
Line chart Preview:
This chart show three lines with red, blue and green .
Pie chart Preview:
This chart show eight arrays with different colors .
Require :
PHP 3 or PHP 4 and GD Libaray
1. Define Public variables, including:
2. Functions
Graphite_line Class is just extend plot() function in Graphite_bar class
1. Define Public variables, including:
2. Functions
2 / 6 - 3 / 6
5 / 6 - 8 / 6
class graph {
/* filename image is saved as */
var $filename = "graph.gif";
/* Defaule Size of the gif */
var $width = 600;
var $height = 300;
/*Top margin for write title and the legend*/
var $top_margin=50;
/* Text labels describing the x and y axis */
var $x_axis_label = "x_axis";
var $y_axis_label = "y_axis";
/* Image title description */
var $title_label="Macro Graph Analysis";
/* The array of data to be plotted $data_x
is only for draw line chart*/
var $data = array();
var $data_x = array();
/* Text labels on the x axis corresponding to each element in $data */
var $x_labels= array();
/* Text labels on the y axis describing the range of the data */
var $y_labels = array();
/* legend
array for the graph */
var $legend = array();
/* Default font to use when ploted*/
var $font_title = 5;
var $font = 3;
/*Allocate Default colors */
var $title_color = 0;
var $background_color = 0;
var $border_color = 0;
var $graph_color = array();
/* Default not show dashed line*/
var $x_dashed_line = 0;
var $y_dashed_line = 0;
/* Default not show the detail for each element in the graph*/
var $show_x_detail = 0;
var $show_y_detail = 0;
/* The diameter of a circle */
var $diameter=150;
/* The (x,y) coordinate for the centre of a circle */
var $x_centre=120;
var $y_centre=160
/* image handle */
var $image;
/* The virtual range within that the data is displayed on the graph */
var $y_range_pos;
var $y_range_neg;
/* cooridnate the x axis is graphed */
var $x_axis;
/* Width and Height in pixels of the plotting area */
var $plotarea_width;
var $plotarea_height;
/*Average space from one grid to another on the x axis*/
var $average_space;
/* Array of Plotted data in (x,y) format */
var $plotted_data = array();
/* Text labels on the x axis corresponding to each element in $data */
var $x_labels_default= array();
/*
* Structure function
*/
function graph($width=600, $height=300)
{
/* Create image handle */
/* Allocate Standard Colors */
}
/*
* Change the default allocate color function
*/
function change_color($num,$red,$green,$blue)
{
$this->graph_color[$num] =
ImageColorAllocate($this->image,$red,$green,$blue);
.........
}
/*
* Defacto Data class constructor
*/
function init($y_grid=8)
{
/* Draw Background */
/* Draw y axis label */
/* Draw title label */
/* Draw x axis label */
/* Draw plot borders */
/* Draw x axis*/
/* Draw x labels, y
labels,if present */
}
/*
* Plot data to plotted_data
*/
function prep()
{
/* If x_labels spresent get spacing from x_labels */
/* If data present get spacing from data */
/* if data_entry off the top of the chart, clip it */
/* if data_entry off the bottom of the chart, clip it */
/* Write plotting data to plotted_data variable */
}
/*
* Plot data to image as solid bars
*/
function plot($bar_width = 10)
{
/* Plot each bar
with different colors */
/* write the description for each bar */
}
/*
* Save file and free memory
*/
function close()
{
imagegif($this->image, $this->filename);
imagedestroy($this->image);
}
/*
* Put functions together
* $y_grid is the grids you want to separate the y axis
* $bar_width is the width for each edrectangle
*/
function draw($y_grid=8,$bar_width =10){
$this->init($y_grid);
$this->prep();
$this->plot($bar_width);
$this->close();
}
} //----close the graph
class
{
function prep()
{
/* If $data_x is NULL ,Default allocate the increase data to it*/
/* Check the $data_x*/
/* Get each value of $data_x array*/
/* Write plotting data to plotted_data variable */
}
function plot($bar_width = 10)
{
/* Plot each line with different colors */
/* write the description for each line*/
/* Whether to show detail of each dot on the line chart*/
}
} //----close the graph_line class
class graph_dot extends graph
{
function init()
{
/* draw the dot
border*/
/* count
the data array */
/*separate the dot by lines */
/* fill in the color and write the description for each partition*/
/* If needed,show the detail for each partition */
}
function draw(){
$this->init();
$this->close();
}
} //----close
the graph_line class
/*
* Returns an array of integers given a range
of Y axis
*/
function xrange()
{
}
<?
include ("graph.inc");
$test = new graph();
$fileName="0.gif";
$test->filename = "$fileName";
$test->font=3;
$test->x_dashed_line=1;
$test->show_y_detail=1;
//$test->show_x_detail=1;
$test->y_axis_label = "Macro Graph Test";
$test->x_axis_label = "OverTime of the Week";
$test->data = array(
array(1, 2, 3, 4, 35, 6,7,8,9,10),
array(2, 4, 6, 8, 10, 12, 14,16,18),
array(16, 14, 13, 12, 9,6,3)
);
$test->legend=array("Macro", "Jackie", "Robert");
$test->change_color(bd,200,0,0);
$test->change_color(1,0,0,0);
$test->change_color(2,0,0,255);
$test->draw();
echo "<img src=$fileName><br>";
?>

<?
include ("graph.inc");
$test = new graph_line(600,300);
$fileName="1.gif";
$test->filename = "$fileName";
$test->font=3;
$test->show_y_detail=0;
$test->show_x_detail=0;
$test->x_dashed_line=1;
$test->y_dashed_line=1;
$test->y_axis_label = "Macro Graph Test";
$test->x_axis_label = "OverTime of the Week";
$test->data = array(
array(1, 2, 3, 4, 35, 6,7,8,9,10),
array(2, 4, 6, 8, 10, 12, 14,16,18),
array(16, 14, 13, 12, 9,6,3)
);
$test->data_x = array(
array(2, 12, 23, 42, 46,57,58,65,67,78),
array(4, 9, 12, 19, 24, 32, 38,46,58),
array(3, 12, 23, 32, 49,56,63)
);
$test->x_labels=array(Jan,Feb,Mar,May,Apr,Jun,Jul,Aug,Sep,Oct);
$test->change_color(bg,230,230,230);
$test->change_color(1,0,0,0);
$test->change_color(2,0,0,255);
$test->draw();
echo "<img src=$fileName><br>";
?>

<?
include ("graph.inc");
$fileName="macro001.gif";
$array=array(3,2,8,3,6,5,11,2);
$array_label=array(Jan,Feb,Mar,May,Apr,Jun,Jul,Aug);
$title="Timesheet Graph Analysis";
$gd = new graph_dot(400,300);
$gd->filename = "$fileName";
$gd->data=$array;
$gd->x_label=$array_label;
$gd->show_y_detail=1;
$gd->title_label="$title";
$gd->change_color(bd,255,255,255);
$gd->change_color(tt,0,0,0);
$gd->change_color(bg,240,240,250);
$gd->draw();
echo "<img src=$fileName><br>";
?>

http://10.10.80.80/~macro/graph/graph_class.php
Written by Macro Zeng
2000-06-08