In this example, we will create a bar chart that displays the annual revenue of
a company for the last 10 years. When a bar is clicked, the browser will load a
line chart showing the monthly revenue for the selected year. When the line
chart is clicked, the browser will load a pie chart showing the breakdown of the
revenue for the selected month. When the pie chart is clicked, it will show the
sector details in a web page.
The capability is often called "drill-down", because the user can "zoom-in" to
get more details by clicking on the chart.
[File: phpdemo/clickbar.php".]
addTitle("Annual Revenue for Star Tech", "timesbi.ttf");
#Set the plotarea at (60, 25) and of size 350 x 150 pixels
$c->setPlotArea(60, 25, 350, 150);
#Add a blue (0x3333cc) bar chart layer using the given data. Set the bar border
#to 1 pixel 3D style.
$barLayerObj = $c->addBarLayer($revenue, 0x3333cc, "Revenue");
$barLayerObj->setBorderColor(-1, 1);
#Set x axis labels using the given labels
$c->xAxis->setLabels($labels);
#Add a title to the y axis
$c->yAxis->setTitle("USD (K)");
#Create the image and save it in a temporary location
$chart1URL = $c->makeSession("chart1");
#Create an image map for the chart
$imageMap = $c->getHTMLImageMap("clickline.php", "",
"title='{xLabel}: USD {value|0}K'");
?>
Simple Clickable Bar Chart
">
View Source Code
|
In the above code, the chart is created and saved in a session variable using
BaseChart.makeSession. An <IMG> tag is used to retrieve the chart with
"myimage.php?<?php echo $chart1URL?>" as the URL.
"myimage.php" is a simple utility that comes with ChartDirector for retrieving images from
session variables.
The image map for the chart is created using the following code:
$imageMap = $c->getHTMLImageMap("clickline.php", "",
"title='{xLabel}: USD {value|0}K'");
|
As seen above, only one line of code is needed.
BaseChart.getHTMLImageMap will
generate the image map for the entire chart. The image map will use "clickline.php"
as the handler when a bar is clicked.
If you right click on the browser and choose "View Source" to look at the HTML
of the web page as received by the browser, you can see that the image map
generated above will be something like:
...... (one tag for each bar) ...... |
The image map generated by ChartDirector contains one <AREA> tag per bar.
This defines the bars as hot spots. The "href" attributes of the <AREA>
tags use "clickline.php" as the URL, with query parameters appended to
describe the bars. In this way, "clickline.php" can distinguish which bar the
user has clicked.
The <AREA> tags also include "title" attributes, which act as tool tips
when the mouse pointer moves over the bars. In this example, the tool tips are
in the format:
"title='{xLabel}: USD {value|0}K'"
which is specified as the third argument of
BaseChart.getHTMLImageMap.
As mentioned above, the "myimage.php" is a simply utility used to retrieve
the image from a session variable and
returns it to the browser.
[File: phpdemo/myimage.php".]
In the previous Clickable Bar Chart sample code, when a bar is clicked, the
handler "clickline.php" will be invoked.
The code for the image map handler "clickline.php" is listed below. It
determines which year the user has clicked from query parameters. It
then draws a line chart based on the selected year. To produce a
clickable line chart, it generates and saves the chart in a
session variable using
BaseChart.makeSession
, and generates the image map using
BaseChart.getHTMLImageMap, with "clickpie.php" as the handler.
As "clickline.php" is very similar to "clickbar.php", it will not be explained
further here.
[File: phpdemo/clickline.php".]
addTitle("Month Revenue for Star Tech for $selectedYear", "timesbi.ttf");
#Set the plotarea at (60, 5) and of size 350 x 150 pixels. Enable both
#horizontal and vertical grids by setting their colors to grey (0xc0c0c0)
$plotAreaObj = $c->setPlotArea(60, 25, 350, 150);
$plotAreaObj->setGridColor(0xc0c0c0, 0xc0c0c0);
#Add a line chart layer using the data
$lineLayerObj = $c->addLineLayer();
$dataSet = $lineLayerObj->addDataSet($data, 0x993399);
#Set the line width to 3 pixels
$dataSet->setLineWidth(3);
#Use a 11 point triangle symbol to plot the data points
$dataSet->setDataSymbol(TriangleSymbol, 11);
#Set the x axis labels. In this example, the labels must be Jan - Dec.
$labels = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept",
"Oct", "Nov", "Dec");
$c->xAxis->setLabels($labels);
#Add a title to the x axis to reflect the selected year
$c->xAxis->setTitle("Year $selectedYear");
#Add a title to the y axis
$c->yAxis->setTitle("USD (K)");
#Reserve 10% margin at the top of the plot area just to make sure the line does
#not go too near the top of the plot area
$c->yAxis->setAutoScale(0.1);
#Create the image and save it in a temporary location
$chart1URL = $c->makeSession("chart1");
#Create an image map for the chart
$imageMap = $c->getHTMLImageMap("clickpie.php?year=$selectedYear", "",
"title='{xLabel}: USD {value|0}K'");
?>
Simple Clickable Line Chart
">
View Source Code
|
In the previous Clickable Line Chart sample code, when a line is clicked, the
handler "clickpie.php" will be invoked.
The code for "clickpie.php" is listed below. It determines which year and month
the user has clicked from query parameters. It then draws a pie chart based
on those parameters. To produce a clickable pie chart, it generates
and saves the chart in a session variable using
BaseChart.makeSession,
and generates the image map using
BaseChart.getHTMLImageMap, with
"piestub.php" as the handler.
As "clickpie.php" is very similar to "clickbar.php" and "clickline.php", it will
not be explained further here.
[File: phpdemo/clickpie.php".]
setPieSize(180, 130, 100);
#Add a title to the pie chart using 13 pts Times Bold Italic font
$c->addTitle("Revenue Breakdown for $selectedMonth/$selectedYear",
"timesbi.ttf", 13);
#Draw the pie in 3D
$c->set3D();
#Set the pie data and the pie labels
$c->setData($data, $labels);
#Create the image and save it in a temporary location
$chart1URL = $c->makeSession("chart1");
#Create an image map for the chart
$imageMap = $c->getHTMLImageMap("piestub.php", "",
"title='{label}:USD {value|0}K'");
?>
Simple Clickable Pie Chart
">
View Source Code
|
In the previous Clickable Pie Chart sample code, when a sector is clicked, the
handler "piestub.php" will be invoked. Its code is listed below. In this
example, "piestub.php" simply displays some data about the sector clicked.
[File: phpdemo/piestub.php".]
Simple Clickable Pie Chart Handler
">
View Source Code
You have clicked on the following sector :
- Sector Number :
- Sector Name :
- Sector Value :
- Sector Percentage : %
|
© 2004 Advanced Software Engineering Limited. All rights reserved.