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 "drilldownchart.h" |
31 | #include "drilldownslice.h" |
32 | #include <QtWidgets/QApplication> |
33 | #include <QtWidgets/QMainWindow> |
34 | #include <QtCore/QRandomGenerator> |
35 | #include <QtCharts/QChartView> |
36 | #include <QtCharts/QLegend> |
37 | #include <QtCharts/QPieSeries> |
38 | |
39 | QT_CHARTS_USE_NAMESPACE |
40 | |
41 | int main(int argc, char *argv[]) |
42 | { |
43 | QApplication a(argc, argv); |
44 | |
45 | QMainWindow window; |
46 | |
47 | DrilldownChart *chart = new DrilldownChart(); |
48 | chart->setTheme(QChart::ChartThemeLight); |
49 | chart->setAnimationOptions(QChart::AllAnimations); |
50 | chart->legend()->setVisible(true); |
51 | chart->legend()->setAlignment(Qt::AlignRight); |
52 | |
53 | QPieSeries *yearSeries = new QPieSeries(&window); |
54 | yearSeries->setName("Sales by year - All" ); |
55 | |
56 | const QStringList months = { |
57 | "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" |
58 | }; |
59 | const QStringList names = { |
60 | "Jane" , "John" , "Axel" , "Mary" , "Susan" , "Bob" |
61 | }; |
62 | |
63 | for (const QString &name : names) { |
64 | QPieSeries *series = new QPieSeries(&window); |
65 | series->setName("Sales by month - " + name); |
66 | |
67 | for (const QString &month : months) |
68 | *series << new DrilldownSlice(QRandomGenerator::global()->bounded(highest: 1000), month, yearSeries); |
69 | |
70 | QObject::connect(sender: series, signal: &QPieSeries::clicked, receiver: chart, slot: &DrilldownChart::handleSliceClicked); |
71 | |
72 | *yearSeries << new DrilldownSlice(series->sum(), name, series); |
73 | } |
74 | |
75 | QObject::connect(sender: yearSeries, signal: &QPieSeries::clicked, receiver: chart, slot: &DrilldownChart::handleSliceClicked); |
76 | |
77 | chart->changeSeries(series: yearSeries); |
78 | |
79 | QChartView *chartView = new QChartView(chart); |
80 | chartView->setRenderHint(hint: QPainter::Antialiasing); |
81 | window.setCentralWidget(chartView); |
82 | window.resize(w: 800, h: 500); |
83 | window.show(); |
84 | |
85 | return a.exec(); |
86 | } |
87 | |