1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Charts module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29
30#include <QtWidgets/QApplication>
31#include <QtWidgets/QMainWindow>
32#include <QtCharts/QChartView>
33#include <QtCharts/QStackedBarSeries>
34#include <QtCharts/QBarSet>
35#include <QtCharts/QLegend>
36#include <QtCharts/QBarCategoryAxis>
37#include <QtCharts/QValueAxis>
38
39QT_CHARTS_USE_NAMESPACE
40
41int main(int argc, char *argv[])
42{
43 QApplication a(argc, argv);
44
45//![1]
46 QBarSet *set0 = new QBarSet("Jane");
47 QBarSet *set1 = new QBarSet("John");
48 QBarSet *set2 = new QBarSet("Axel");
49 QBarSet *set3 = new QBarSet("Mary");
50 QBarSet *set4 = new QBarSet("Samantha");
51
52 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
53 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
54 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
55 *set3 << 5 << 6 << 7 << 3 << 4 << 5;
56 *set4 << 9 << 7 << 5 << 3 << 1 << 2;
57//![1]
58
59//![2]
60 QStackedBarSeries *series = new QStackedBarSeries();
61 series->append(set: set0);
62 series->append(set: set1);
63 series->append(set: set2);
64 series->append(set: set3);
65 series->append(set: set4);
66//![2]
67
68//![3]
69 QChart *chart = new QChart();
70 chart->addSeries(series);
71 chart->setTitle("Simple stackedbarchart example");
72 chart->setAnimationOptions(QChart::SeriesAnimations);
73//![3]
74
75//![4]
76 QStringList categories;
77 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
78 QBarCategoryAxis *axisX = new QBarCategoryAxis();
79 axisX->append(categories);
80 chart->addAxis(axis: axisX, alignment: Qt::AlignBottom);
81 series->attachAxis(axis: axisX);
82 QValueAxis *axisY = new QValueAxis();
83 chart->addAxis(axis: axisY, alignment: Qt::AlignLeft);
84 series->attachAxis(axis: axisY);
85//![4]
86
87//![5]
88 chart->legend()->setVisible(true);
89 chart->legend()->setAlignment(Qt::AlignBottom);
90//![5]
91
92//![6]
93 QChartView *chartView = new QChartView(chart);
94 chartView->setRenderHint(hint: QPainter::Antialiasing);
95//![6]
96
97//![7]
98 QMainWindow window;
99 window.setCentralWidget(chartView);
100 window.resize(w: 420, h: 300);
101 window.show();
102//![7]
103
104 return a.exec();
105}
106
107

source code of qtcharts/examples/charts/stackedbarchart/main.cpp