This example demonstates using x zone colors - colors that change at certain x value thresholds.
In this example, x zone colors are used as the color of the line, and as the fill color of the band the surrounds the line.
The x zone color for the line is configured to change from blue to a red dash line color upon reaching a certain x value threshold. The x zone color for the band is configured to change from semi-transparent blue to semi-transparent red upon reaching the same threshold.
X zone colors are created using
Layer.xZoneColor. You can use a x zone color in any ChartDirector API that expects a color. For instance, you can use a x zone color as background color, fill color of data sets, line color, etc.
A single x zone color can support one threshold value and change between two colors. The two colors can be any colors, including another x zone colors. You may create x zone colors with multiple thresholds by cascading multiple x zone colors this way.
[File: phpdemo/xzonecolor.php".]
setPlotArea(50, 5, 480, 180);
$plotAreaObj->setGridColor(0xc0c0c0, 0xc0c0c0);
#Add a legend box (50, 5) (top of plot area) using horizontal layout. Use 8 pts
#Arial font. Disable bounding box (set border to transparent).
$legendBox = $c->addLegend(50, 5, false, "", 8);
$legendBox->setBackground(Transparent);
#Add keys to the legend box to explain the color zones
$legendBox->addKey("Historical", 0x9999ff);
$legendBox->addKey("Forecast", 0xff9966);
#Add a title to the y axis.
$c->yAxis->setTitle("Energy Consumption");
#Set the labels on the x axis
$c->xAxis->setLabels($labels);
#Add a line layer to the chart
$layer = $c->addLineLayer2();
#Create the color to draw the data line. The line is blue (0x333399) to the left
#of x = 18, and become a red (0xd04040) dash line to the right of x = 18.
$lineColor = $layer->xZoneColor(18, 0x333399, $c->dashLineColor(0xd04040,
DashLine));
#Add the data line
$layer->addDataSet($data, $lineColor);
#Create the color to draw the err zone. The color is semi-transparent blue
#(0x809999ff) to the left of x = 18, and become semi-transparent red
#(0x80ff9966) to the right of x = 18.
$errColor = $layer->xZoneColor(18, 0x809999ff, 0x80ff9966);
#Add the upper border of the err zone
$upperBorder = new ArrayMath($data);
$upperBorder->add($errData);
$layer->addDataSet($upperBorder->result(), $errColor);
#Add the lower border of the err zone
$lowerBorder = new ArrayMath($data);
$lowerBorder->sub($errData);
$layer->addDataSet($lowerBorder->result(), $errColor);
#Set the default line width to 2 pixels
$layer->setLineWidth(2);
#Color the region between the err zone lines
$c->addInterLineLayer($layer->getLine(1), $layer->getLine(2), $errColor);
#output the chart
header("Content-type: image/png");
print($c->makeChart2(PNG));
?> |
© 2004 Advanced Software Engineering Limited. All rights reserved.