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 <QtCore/QRandomGenerator>
33#include <QtCharts/QChartView>
34#include <QtCharts/QBarSet>
35#include <QtCharts/QLegend>
36#include "drilldownseries.h"
37#include "drilldownchart.h"
38
39QT_CHARTS_USE_NAMESPACE
40
41int main(int argc, char *argv[])
42{
43 QApplication a(argc, argv);
44 QMainWindow window;
45
46//! [1]
47 DrilldownChart *drilldownChart = new DrilldownChart();
48 drilldownChart->setAnimationOptions(QChart::SeriesAnimations);
49//! [1]
50
51//! [2]
52 // Define categories
53 const QStringList months = {
54 "May", "Jun", "Jul", "Aug", "Sep"
55 };
56 const QStringList weeks = {
57 "week 1", "week 2", "week 3", "week 4"
58 };
59 const QStringList plants = {
60 "Habanero", "Lemon Drop", "Starfish", "Aji Amarillo"
61 };
62//! [2]
63
64//! [3]
65 // Create drilldown structure
66 DrilldownBarSeries *seasonSeries = new DrilldownBarSeries(months, 320, drilldownChart);
67 seasonSeries->setName("Crop by month - Season");
68
69 // Each month in season series has drilldown series for weekly data
70 for (int month = 0; month < months.count(); month++) {
71
72 // Create drilldown series for every week
73 DrilldownBarSeries *weeklySeries = new DrilldownBarSeries(weeks, 80, drilldownChart);
74 seasonSeries->mapDrilldownSeries(index: month, drilldownSeries: weeklySeries);
75
76 // Drilling down from weekly data brings us back to season data.
77 for (int week = 0; week < weeks.count(); week++) {
78 weeklySeries->mapDrilldownSeries(index: week, drilldownSeries: seasonSeries);
79 weeklySeries->setName(QString("Crop by week - " + months.at(i: month)));
80 }
81
82 // Use clicked signal to implement drilldown
83 QObject::connect(sender: weeklySeries, signal: &DrilldownBarSeries::clicked,
84 receiver: drilldownChart, slot: &DrilldownChart::handleClicked);
85 }
86
87 // Enable drilldown from season series using clicked signal
88 QObject::connect(sender: seasonSeries, signal: &DrilldownBarSeries::clicked,
89 receiver: drilldownChart, slot: &DrilldownChart::handleClicked);
90//! [3]
91
92//! [4]
93 // Fill monthly and weekly series with data
94 for (const QString &plant : plants) {
95 QBarSet *monthlyCrop = new QBarSet(plant);
96 for (int month = 0; month < months.count(); month++) {
97 QBarSet *weeklyCrop = new QBarSet(plant);
98 for (int week = 0; week < weeks.count(); week++)
99 *weeklyCrop << QRandomGenerator::global()->bounded(highest: 20);
100 // Get the drilldown series from season series and add crop to it.
101 seasonSeries->drilldownSeries(index: month)->append(set: weeklyCrop);
102 *monthlyCrop << weeklyCrop->sum();
103 }
104 seasonSeries->append(set: monthlyCrop);
105 }
106//! [4]
107
108//! [5]
109 // Show season series in initial view
110 drilldownChart->changeSeries(series: seasonSeries);
111 drilldownChart->setTitle(seasonSeries->name());
112//! [5]
113
114//! [6]
115 drilldownChart->axes(orientation: Qt::Horizontal).first()->setGridLineVisible(false);
116 drilldownChart->legend()->setVisible(true);
117 drilldownChart->legend()->setAlignment(Qt::AlignBottom);
118//! [6]
119
120 QChartView *chartView = new QChartView(drilldownChart);
121 window.setCentralWidget(chartView);
122 window.resize(w: 480, h: 300);
123 window.show();
124
125 return a.exec();
126}
127
128

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