A trend line is a straight line that fits a number of data points computed using linear regression (the least square method).
In ChartDirector, a trend line is created by adding a trend line layer using
XYChart.addTrendLayer or
XYChart.addTrendLayer2 method.
Typically, a trend line layer is used with other layers for the data points. For example, in this example, two layers are used. A line chart layer draws the line that joins the data points. The trend line layer draws a straight line that best fit the same data points.
Note that the x-axis labels on the above chart are rotated by 90 degrees to save space. This is achieved by using
TextBox.setFontAngle of the
TextBox object that represents the x-axis label prototype.
[File: phpdemo/trendline.php".]
setPlotArea(55, 45, 420, 210, 0xffffff, -1, -1, 0xc0c0c0, -1);
#Add a legend box at (55, 25) (top of the chart) with horizontal layout. Use 8
#pts Arial font. Set the background and border color to Transparent.
$legendObj = $c->addLegend(55, 25, false, "", 8);
$legendObj->setBackground(Transparent);
#Add a title box to the chart using 13 pts Times Bold Italic font. The text is
#white (0xffffff) on a purple (0x800080) background, with a 1 pixel 3D border.
$titleObj = $c->addTitle("Long Term Server Load", "timesbi.ttf", 13, 0xffffff);
$titleObj->setBackground(0x800080, -1, 1);
#Add a title to the y axis
$c->yAxis->setTitle("MBytes");
#Set the labels on the x axis. Rotate the font by 90 degrees.
$labelsObj = $c->xAxis->setLabels($labels);
$labelsObj->setFontAngle(90);
#Add a line layer to the chart
$lineLayer = $c->addLineLayer();
#Add the data to the line layer using light brown color (0xcc9966) with a 7
#pixel square symbol
$dataSetObj = $lineLayer->addDataSet($data, 0xcc9966, "Server Utilization");
$dataSetObj->setDataSymbol(SquareSymbol, 7);
#Set the line width to 2 pixels
$lineLayer->setLineWidth(2);
#Add a trend line layer using the same data with a dark green (0x008000) color.
#Set the line width to 2 pixels
$trendLayerObj = $c->addTrendLayer($data, 0x8000, "Trend Line");
$trendLayerObj->setLineWidth(2);
#output the chart
header("Content-type: image/png");
print($c->makeChart2(PNG));
?> |
© 2004 Advanced Software Engineering Limited. All rights reserved.