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/QBarSeries> |
34 | #include <QtCharts/QBarSet> |
35 | #include <QtCharts/QLegend> |
36 | #include <QtCharts/QBarCategoryAxis> |
37 | #include <QtCharts/QHorizontalStackedBarSeries> |
38 | #include <QtCharts/QValueAxis> |
39 | |
40 | QT_CHARTS_USE_NAMESPACE |
41 | |
42 | int main(int argc, char *argv[]) |
43 | { |
44 | QApplication a(argc, argv); |
45 | |
46 | //![1] |
47 | QBarSet *set0 = new QBarSet("Jane"); |
48 | QBarSet *set1 = new QBarSet("John"); |
49 | QBarSet *set2 = new QBarSet("Axel"); |
50 | QBarSet *set3 = new QBarSet("Mary"); |
51 | QBarSet *set4 = new QBarSet("Samantha"); |
52 | |
53 | *set0 << 1 << 2 << 3 << 4 << 5 << 6; |
54 | *set1 << 5 << 0 << 0 << 4 << 0 << 7; |
55 | *set2 << 3 << 5 << 8 << 13 << 8 << 5; |
56 | *set3 << 5 << 6 << 7 << 3 << 4 << 5; |
57 | *set4 << 9 << 7 << 5 << 3 << 1 << 2; |
58 | //![1] |
59 | |
60 | //![2] |
61 | QHorizontalStackedBarSeries *series = new QHorizontalStackedBarSeries(); |
62 | series->append(set: set0); |
63 | series->append(set: set1); |
64 | series->append(set: set2); |
65 | series->append(set: set3); |
66 | series->append(set: set4); |
67 | |
68 | //![2] |
69 | |
70 | //![3] |
71 | QChart *chart = new QChart(); |
72 | chart->addSeries(series); |
73 | chart->setTitle("Simple horizontal stacked barchart example"); |
74 | chart->setAnimationOptions(QChart::SeriesAnimations); |
75 | //![3] |
76 | |
77 | //![4] |
78 | QStringList categories; |
79 | categories << "Jan"<< "Feb"<< "Mar"<< "Apr"<< "May"<< "Jun"; |
80 | QBarCategoryAxis *axisY = new QBarCategoryAxis(); |
81 | axisY->append(categories); |
82 | chart->addAxis(axis: axisY, alignment: Qt::AlignLeft); |
83 | series->attachAxis(axis: axisY); |
84 | QValueAxis *axisX = new QValueAxis(); |
85 | chart->addAxis(axis: axisX, alignment: Qt::AlignBottom); |
86 | series->attachAxis(axis: axisX); |
87 | //![4] |
88 | |
89 | //![5] |
90 | chart->legend()->setVisible(true); |
91 | chart->legend()->setAlignment(Qt::AlignBottom); |
92 | //![5] |
93 | |
94 | //![6] |
95 | QChartView *chartView = new QChartView(chart); |
96 | chartView->setRenderHint(hint: QPainter::Antialiasing); |
97 | //![6] |
98 | |
99 | //![7] |
100 | QMainWindow window; |
101 | window.setCentralWidget(chartView); |
102 | window.resize(w: 420, h: 300); |
103 | window.show(); |
104 | //![7] |
105 | |
106 | return a.exec(); |
107 | } |
108 |