This example demonstrates joining data points with steps. It also demonstrates irregularly spaced data points on an auto-scaled true date/time axis.
The step line layer is created using
XYChart.addStepLineLayer.
Most of the sample charts in this document use enumerated x-axes. In this axis type, the data points are assumed to be evenly spread on the x-direction, so it is not necessary to supply the x values of the data points. The x-axis is treated as a series of text labels using
Axis.setLabels or
Axis.setLabels2. The labels can be dates/times, numbers, names, or any arbitrary text in any format. Enumerated x-axis is an easy to use yet extremely flexible axis type that is suitable for most charts.
However, for charts with data points not regularly spaced, it is necessary to supply the x values of the data points using
Layer.setXData. In this case, ChartDirector can automatically determine the type and scale on the x-axis (auto-scaling).
For date/time axis, in many cases one may need to control the date/time label format. This can be done using
Axis.setDateScale3.
The following is the command line version of the code in "cppdemo/stepline". The MFC version of the code is in "mfcdemo/mfcdemo". The Qt Widgets version of the code is in "qtdemo/qtdemo". The QML/Qt Quick version of the code is in "qmldemo/qmldemo".
#include "chartdir.h"
int main(int argc, char *argv[])
{
// The data for the chart
double dataY0[] = {4, 4.5, 5, 5.25, 5.75, 5.25, 5, 4.5, 4, 3, 2.5, 2.5};
const int dataY0_size = (int)(sizeof(dataY0)/sizeof(*dataY0));
double dataX0[] = {Chart::chartTime(1997, 1, 1), Chart::chartTime(1998, 6, 25),
Chart::chartTime(1999, 9, 6), Chart::chartTime(2000, 2, 6), Chart::chartTime(2000, 9, 21),
Chart::chartTime(2001, 3, 4), Chart::chartTime(2001, 6, 8), Chart::chartTime(2002, 2, 4),
Chart::chartTime(2002, 5, 19), Chart::chartTime(2002, 8, 16), Chart::chartTime(2002, 12, 1),
Chart::chartTime(2003, 1, 1)};
const int dataX0_size = (int)(sizeof(dataX0)/sizeof(*dataX0));
double dataY1[] = {7, 6.5, 6, 5, 6.5, 7, 6, 5.5, 5, 4, 3.5, 3.5};
const int dataY1_size = (int)(sizeof(dataY1)/sizeof(*dataY1));
double dataX1[] = {Chart::chartTime(1997, 1, 1), Chart::chartTime(1997, 7, 1), Chart::chartTime(
1997, 12, 1), Chart::chartTime(1999, 1, 15), Chart::chartTime(1999, 6, 9), Chart::chartTime(
2000, 3, 3), Chart::chartTime(2000, 8, 13), Chart::chartTime(2001, 5, 5), Chart::chartTime(
2001, 9, 16), Chart::chartTime(2002, 3, 16), Chart::chartTime(2002, 6, 1), Chart::chartTime(
2003, 1, 1)};
const int dataX1_size = (int)(sizeof(dataX1)/sizeof(*dataX1));
// Create a XYChart object of size 500 x 270 pixels, with a pale blue (e0e0ff) background, black
// border, 1 pixel 3D border effect and rounded corners
XYChart* c = new XYChart(600, 300, 0xe0e0ff, 0x000000, 1);
c->setRoundedFrame();
// Set the plotarea at (55, 60) and of size 520 x 200 pixels, with white (ffffff) background.
// Set horizontal and vertical grid lines to grey (cccccc).
c->setPlotArea(50, 60, 525, 200, 0xffffff, -1, -1, 0xcccccc, 0xcccccc);
// Add a legend box at (55, 32) (top of the chart) with horizontal layout. Use 9pt Arial Bold
// font. Set the background and border color to Transparent.
c->addLegend(55, 32, false, "Arial Bold", 9)->setBackground(Chart::Transparent);
// Add a title box to the chart using 15pt Times Bold Italic font. The text is white (ffffff) on
// a deep blue (000088) background, with soft lighting effect from the right side.
c->addTitle("Long Term Interest Rates", "Times New Roman Bold Italic", 15, 0xffffff
)->setBackground(0x000088, -1, Chart::softLighting(Chart::Right));
// Set the y axis label format to display a percentage sign
c->yAxis()->setLabelFormat("{value}%");
// Add a red (ff0000) step line layer to the chart and set the line width to 2 pixels
StepLineLayer* layer0 = c->addStepLineLayer(DoubleArray(dataY0, dataY0_size), 0xff0000,
"Country AAA");
layer0->setXData(DoubleArray(dataX0, dataX0_size));
layer0->setLineWidth(2);
// Add a blue (0000ff) step line layer to the chart and set the line width to 2 pixels
StepLineLayer* layer1 = c->addStepLineLayer(DoubleArray(dataY1, dataY1_size), 0x0000ff,
"Country BBB");
layer1->setXData(DoubleArray(dataX1, dataX1_size));
layer1->setLineWidth(2);
// Output the chart
c->makeChart("stepline.png");
//free up resources
delete c;
return 0;
}
© 2023 Advanced Software Engineering Limited. All rights reserved.