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/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 QBarSeries *series = new QBarSeries();
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
67//![2]
68
69//![3]
70 QChart *chart = new QChart();
71 chart->addSeries(series);
72 chart->setTitle("Simple barchart example");
73 chart->setAnimationOptions(QChart::SeriesAnimations);
74//![3]
75
76//![4]
77 QStringList categories;
78 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
79 QBarCategoryAxis *axisX = new QBarCategoryAxis();
80 axisX->append(categories);
81 chart->addAxis(axis: axisX, alignment: Qt::AlignBottom);
82 series->attachAxis(axis: axisX);
83
84 QValueAxis *axisY = new QValueAxis();
85 axisY->setRange(min: 0,max: 15);
86 chart->addAxis(axis: axisY, alignment: Qt::AlignLeft);
87 series->attachAxis(axis: axisY);
88//![4]
89
90//![5]
91 chart->legend()->setVisible(true);
92 chart->legend()->setAlignment(Qt::AlignBottom);
93//![5]
94
95//![6]
96 QChartView *chartView = new QChartView(chart);
97 chartView->setRenderHint(hint: QPainter::Antialiasing);
98//![6]
99
100//![7]
101 QMainWindow window;
102 window.setCentralWidget(chartView);
103 window.resize(w: 420, h: 300);
104 window.show();
105//![7]
106
107 return a.exec();
108}
109

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