| 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/QLineSeries> |
| 36 | #include <QtCharts/QLegend> |
| 37 | #include <QtCharts/QBarCategoryAxis> |
| 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("Sam" ); |
| 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 | QBarSeries *barseries = new QBarSeries(); |
| 62 | barseries->append(set: set0); |
| 63 | barseries->append(set: set1); |
| 64 | barseries->append(set: set2); |
| 65 | barseries->append(set: set3); |
| 66 | barseries->append(set: set4); |
| 67 | //![2] |
| 68 | |
| 69 | //![8] |
| 70 | QLineSeries *lineseries = new QLineSeries(); |
| 71 | lineseries->setName("trend" ); |
| 72 | lineseries->append(point: QPoint(0, 4)); |
| 73 | lineseries->append(point: QPoint(1, 15)); |
| 74 | lineseries->append(point: QPoint(2, 20)); |
| 75 | lineseries->append(point: QPoint(3, 4)); |
| 76 | lineseries->append(point: QPoint(4, 12)); |
| 77 | lineseries->append(point: QPoint(5, 17)); |
| 78 | //![8] |
| 79 | |
| 80 | //![3] |
| 81 | QChart *chart = new QChart(); |
| 82 | chart->addSeries(series: barseries); |
| 83 | chart->addSeries(series: lineseries); |
| 84 | chart->setTitle("Line and barchart example" ); |
| 85 | //![3] |
| 86 | |
| 87 | //![4] |
| 88 | QStringList categories; |
| 89 | categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" ; |
| 90 | QBarCategoryAxis *axisX = new QBarCategoryAxis(); |
| 91 | axisX->append(categories); |
| 92 | chart->addAxis(axis: axisX, alignment: Qt::AlignBottom); |
| 93 | lineseries->attachAxis(axis: axisX); |
| 94 | barseries->attachAxis(axis: axisX); |
| 95 | axisX->setRange(minCategory: QString("Jan" ), maxCategory: QString("Jun" )); |
| 96 | |
| 97 | QValueAxis *axisY = new QValueAxis(); |
| 98 | chart->addAxis(axis: axisY, alignment: Qt::AlignLeft); |
| 99 | lineseries->attachAxis(axis: axisY); |
| 100 | barseries->attachAxis(axis: axisY); |
| 101 | axisY->setRange(min: 0, max: 20); |
| 102 | //![4] |
| 103 | |
| 104 | //![5] |
| 105 | chart->legend()->setVisible(true); |
| 106 | chart->legend()->setAlignment(Qt::AlignBottom); |
| 107 | //![5] |
| 108 | |
| 109 | //![6] |
| 110 | QChartView *chartView = new QChartView(chart); |
| 111 | chartView->setRenderHint(hint: QPainter::Antialiasing); |
| 112 | //![6] |
| 113 | |
| 114 | //![7] |
| 115 | QMainWindow window; |
| 116 | window.setCentralWidget(chartView); |
| 117 | window.resize(w: 440, h: 300); |
| 118 | window.show(); |
| 119 | //![7] |
| 120 | |
| 121 | return a.exec(); |
| 122 | } |
| 123 | |