Line and BarChart Example▲
In the example we combine a linechart with a barchart and use the category axis as a common axis for both.
Running the Example▲
To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.
Combining Charts▲
Here we create data for our bar series.
QBarSet *
set0 =
new
QBarSet("Jane"
);
QBarSet *
set1 =
new
QBarSet("John"
);
QBarSet *
set2 =
new
QBarSet("Axel"
);
QBarSet *
set3 =
new
QBarSet("Mary"
);
QBarSet *
set4 =
new
QBarSet("Sam"
);
*
set0 &
lt;&
lt; 1
&
lt;&
lt; 2
&
lt;&
lt; 3
&
lt;&
lt; 4
&
lt;&
lt; 5
&
lt;&
lt; 6
;
*
set1 &
lt;&
lt; 5
&
lt;&
lt; 0
&
lt;&
lt; 0
&
lt;&
lt; 4
&
lt;&
lt; 0
&
lt;&
lt; 7
;
*
set2 &
lt;&
lt; 3
&
lt;&
lt; 5
&
lt;&
lt; 8
&
lt;&
lt; 13
&
lt;&
lt; 8
&
lt;&
lt; 5
;
*
set3 &
lt;&
lt; 5
&
lt;&
lt; 6
&
lt;&
lt; 7
&
lt;&
lt; 3
&
lt;&
lt; 4
&
lt;&
lt; 5
;
*
set4 &
lt;&
lt; 9
&
lt;&
lt; 7
&
lt;&
lt; 5
&
lt;&
lt; 3
&
lt;&
lt; 1
&
lt;&
lt; 2
;
We create a bar series and append sets to it. The first values of each set are grouped together in the first category, the second values in the second category and so on.
QBarSeries *
barseries =
new
QBarSeries();
barseries-&
gt;append(set0);
barseries-&
gt;append(set1);
barseries-&
gt;append(set2);
barseries-&
gt;append(set3);
barseries-&
gt;append(set4);
Then we create a line series and add data to it. To make the data match with the barchart, we use the index as an x-value for our line series, so that first point is at (0,value) second at (1,value) and so on.
QLineSeries *
lineseries =
new
QLineSeries();
lineseries-&
gt;setName("trend"
);
lineseries-&
gt;append(QPoint(0
, 4
));
lineseries-&
gt;append(QPoint(1
, 15
));
lineseries-&
gt;append(QPoint(2
, 20
));
lineseries-&
gt;append(QPoint(3
, 4
));
lineseries-&
gt;append(QPoint(4
, 12
));
lineseries-&
gt;append(QPoint(5
, 17
));
Here we create the chart and add both series to it.
QChart *
chart =
new
QChart();
chart-&
gt;addSeries(barseries);
chart-&
gt;addSeries(lineseries);
chart-&
gt;setTitle("Line and barchart example"
);
To make the chart show the series properly, we have to create custom axes for the series. If we don't create custom axes, then each series will get scaled to use the maximum area of the chart (like in single series case) and the result will be incorrect. With custom axes we set the range of both series to follow the same axis. For the x-axis we use the QBarCategoryAxis and for the y-axis we use QValuesAxis.
QStringList categories;
categories &
lt;&
lt; "Jan"
&
lt;&
lt; "Feb"
&
lt;&
lt; "Mar"
&
lt;&
lt; "Apr"
&
lt;&
lt; "May"
&
lt;&
lt; "Jun"
;
QBarCategoryAxis *
axisX =
new
QBarCategoryAxis();
axisX-&
gt;append(categories);
chart-&
gt;addAxis(axisX, Qt::
AlignBottom);
lineseries-&
gt;attachAxis(axisX);
barseries-&
gt;attachAxis(axisX);
axisX-&
gt;setRange(QString("Jan"
), QString("Jun"
));
QValueAxis *
axisY =
new
QValueAxis();
chart-&
gt;addAxis(axisY, Qt::
AlignLeft);
lineseries-&
gt;attachAxis(axisY);
barseries-&
gt;attachAxis(axisY);
axisY-&
gt;setRange(0
, 20
);
And we also want to show the legend.
chart-&
gt;legend()-&
gt;setVisible(true
);
chart-&
gt;legend()-&
gt;setAlignment(Qt::
AlignBottom);
Finally we add the chart onto a view.
QChartView *
chartView =
new
QChartView(chart);
chartView-&
gt;setRenderHint(QPainter::
Antialiasing);
Now it is ready to be shown in a window.
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(440
, 300
);
window.show();