ChartDirector Ver 3.0 (C++ Edition)

Multi-Line Chart




This example demonstrates how to plot multiple data sets as multiple lines. ChartDirector allows each line to have different color, line width and dash styles.

This example also demonstrates how to set background colors and 3D borders for a chart, enable both vertical and horizontal grid lines, and add a legend box at the top of the chart.

Source Code Listing

[The following project is available in "cppdemo/multiline".]
#include "chartdir.h" int main(int argc, char *argv[]) { //The data for the line chart double data0[] = {42, 49, 33, 38, 51, 46, 29, 41, 44, 57, 59, 52, 37, 34, 51, 56, 56, 60, 70, 76, 63, 67, 75, 64, 51}; double data1[] = {50, 55, 47, 34, 42, 49, 63, 62, 73, 59, 56, 50, 64, 60, 67, 67, 58, 59, 73, 77, 84, 82, 80, 84, 98}; double data2[] = {36, 28, 25, 33, 38, 20, 22, 30, 25, 33, 30, 24, 28, 15, 21, 26, 46, 42, 48, 45, 43, 52, 64, 60, 70}; //The labels for the line chart const char *labels[] = {"0", "", "", "3", "", "", "6", "", "", "9", "", "", "12", "", "", "15", "", "", "18", "", "", "21", "", "", "24"}; //Create a XYChart object of size 500 x 300 pixels, with a pale yellow //(0xffff80) background, a black border, and 1 pixel 3D border effect XYChart *c = new XYChart(500, 300, 0xffff80, 0x0, 1); //Set the plotarea at (55, 45) and of size 420 x 210 pixels, with white //background. Turn on both horizontal and vertical grid lines with light //grey color (0xc0c0c0) c->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. c->addLegend(55, 25, false, "", 8)->setBackground(Transparent); //Add a title box to the chart using 11 pts Arial Bold Italic font. The text //is white (0xffffff) on a dark red (0x800000) background, with a 1 pixel 3D //border. c->addTitle("Daily Server Load", "arialbi.ttf", 11, 0xffffff )->setBackground(0x800000, -1, 1); //Add a title to the y axis c->yAxis()->setTitle("MBytes"); //Set the labels on the x axis c->xAxis()->setLabels(StringArray(labels, sizeof(labels)/sizeof(*labels))); //Add a title to the x axis c->xAxis()->setTitle("Jun 12, 2001"); //Add a line layer to the chart LineLayer *layer = c->addLineLayer(); //Set the default line width to 2 pixels layer->setLineWidth(2); //Add the three data sets to the line layer. For demo purpose, we use a dash //line color for the last line layer->addDataSet(DoubleArray(data0, sizeof(data0)/sizeof(*data0)), -1, "Server # 1"); layer->addDataSet(DoubleArray(data1, sizeof(data1)/sizeof(*data1)), -1, "Server # 2"); layer->addDataSet(DoubleArray(data2, sizeof(data2)/sizeof(*data2)), c->dashLineColor(0x3333ff, DashLine), "Server # 3"); //output the chart c->makeChart("multiline.png"); //free up resources delete c; return 0; }