KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones

PHP create real-time dynamic chart

Many web sites need to create real-time or dynamic charts and graphs(like online stocks, web traffic statistics) from live data sets. PHP and GD library provides a great way for this dynamic creation of images.

PHP and GD library creates image on server side. The advantage of using the server to create an image (server side scripting) is that one does not have to worry about browser compatibility or client operating system compatibility issues.The PHP image generating library uses the GD library to create elementary shapes (elipse, line, rectangle, ...).

PHPlot is a graphics library which provides a means by which you can have your (PHP enabled) web server create and manipulate graphs as objects and display the completed graph as an image. Data sets passed to PHPlot use a very convenient way for database driven sites, in rows with y coordinate data.

First, lets discuss how PHPlot works in general with some terminology. A PHPlot image can consist of several graphs , each graph consisting of several elements. You define an object (e.g. a variable like $graph), select the properties of the element that compose the graph and "Draw" what you want into the object. Tipically by selecting the plot type with SetPlotType and at the end calling DrawGraph. You can also directly invoke PrintImage, which either inserts the image into the data streaming to the client or writes it to disk.

In PHPlot there are World coordinates, which are the XY coordinates relative to the axis origin, in the units of the data set; and device (pixel) coordinates which in GD are relative to the origin at the upper left side of the image.

You can think of the "Draw" functions as shaping the image object and the "Print" function as the method of finally creating the digital image. PHPlot is smart enough that if you only have one graph on an image, then the "Print" is done for you automatically. If you do have multiple graphs per image then you'll need to use both the "Draw" and "Print" functions. We'll talk about that a bit later.

Since PHP is a server scripted language you have several options for how you can "Print" the image. You can:

  1. Write the image as a file on the server. (You specify a file name and can specify caching as well)
  2. Have the raw data stream out within an HTML file, as <IMG SRC="my_PHPlot_code.php">
  3. Precede the raw data with html image headers and call the script directly (showing up as an image) e.g. http://somewhere/my_PHPlot_code2.php .

Download PHPlot

PHPlot 5.0RC2 is available here .

Unzip file

UNIX:
tar zxvf phplot-5.0rc2.tar.gz

Windows: using your favorite unzip software.Move to your web direstory.

System Requirement

PHP 4.2.0 (or later) and GD Lib 2 are necessary. Works good for Windows and Linux(at least).

You may test your system ready or not by using this example.

Real-time dynamic chart examples

A simple graph

<?php
//Include the code
include("phplot/phplot.php");

//Define the object
$graph =& new PHPlot(200,150);

//Define some data
$example_data = array(
array('a',3),
array('b',5),
array('c',7),
array('d',8),
array('e',2),
array('f',6),
array('g',7)
);
$graph->SetDataValues($example_data);
$graph->SetXTickPos('none');
$graph->SetXTickLabelPos('none');

//Draw it
$graph->DrawGraph();
?>

Multiple line graph

Assume we want to plot several y values for each x position. With PHPlot it is easy to specify the multiple data lines by just passing in all the Y values for a given X value at once. So instead of array('label', y) we specify array('label', y1, y2, y3, ...) This is very convenient when working with rows of data from databases.
Now our data will have three Y values for each position on the X axis

<?php
//Include the code
include("phplot/phplot.php");

//Define the object
$graph =& new PHPlot(300,250);

//Set titles
$graph->SetTitle("Title\n\rSubtitle");
$graph->SetXTitle('X data');
$graph->SetYTitle('Y data');

//Define some data
$example_data = array(
array('a',3,4,2),
// here we have a missing data point, that's ok
array('b',5,'',1),
array('c',7,2,6),
array('d',8,1,4),
array('e',2,4,6),
array('f',6,4,5),
array('g',7,2,3)
);
$graph->SetDataValues($example_data);
$graph->SetXTickPos('none');
$graph->SetXTickLabelPos('none');

//Draw it
$graph->DrawGraph();
?>

 

See the above data, PHPlot could allow missing data.

$graph = &new PHPlot(); create an PHPlot instance. The image default size is 600 X 400 png. You may set to GIF JPG (depend on your system supprort) by changing

var $file_format = "<filetype>";

in phplot.php file. such as var $file_format = "GIF";

Pie chart

<?php
//Include the code
include('phplot/phplot.php');

//Define the object
$graph =& new PHPlot(200,150);
$graph->SetPlotType("pie");
$legend = array();
$legend[] = "stock 1";
$legend[] = "stock 2";
$legend[] = "stock 3";

//Define some data
$example_data = array(
array('a',3,4,5)
);

$graph->SetDataValues($example_data);
$graph->SetLegendPixels(1,5,false);
$graph->SetLegend($legend);
//Draw it
$graph->DrawGraph();
?>

 

 

 

For pie chart, you must define plot type before drawing by SetPlotType("pie");.

Also note that, if you use the following data format:

$example_data = array(
array('a',3),
array('b',5),
array('c',7),
array('d',8),
array('e',2),
array('f',6)
);

shown in above code for pie chart, you need define $graph->SetDataType("text-data-single"); . Or if you use data format like $example_data=array(array('piechart',5,6,7,8) you may omit $graph->SetDataType("text-data-single");

 

 
PHP Calendar
Comprehensive PHP Web Calendar
Configurable PHP Calendar
PHP Calendar
PHP Image Processing
PHP grahic softwares
PHP create real-time dynamic chart
create antispam image(image verification)
Add water mark to image
PHP Image processing introduction
Add text on Image
Create thumbnail for image
Create image from text on fly
PHP code sample
PHP create dynamic PDF with FPDF
PHP create dynamic PDF with PDFLib
PHP create dynamic PDF with ClibPDF
Improved PHP configurable hit counter
Install phpMyAdmin,MySql, PHP
PhpMyAdmin export/import data
A simple PHP RSS feed reader
PHP include (SSI)
Working with Forms
PHP formmail Generator
Using phpMyAdmin Create Database
Insert Form Data into Database
Retrieve data from Database
Retrieve multiple records
Batch insert text using phpMyAdmin
Using phpMyAdmin insert binary file
Insert binary file using PHP code
Using form insert binary file into MySQL
Retrieve binary file from MySQL
Image hosting business in minutes
Send HTML mail in PHP
Send bulk emails in PHP
Polling/Rating System in PHP
Polling/Rating System in PHP(radio button)
Check visitor's IP address
Check reciprocal link
PHP tutorial
What is PHP?
Install PHP on Windows XP (part1)
Install PHP on Windows XP (part2)
PHP open and closing tags
PHP types
Variables
Constants
Operators(part1)
Operators(part2)
Logic Structures
Include files
Loops
Set cookie and delete cookie
Most often used MySQL functions

 

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer