Revision control

Copy as Markdown

// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
/*!
\page stackedbarchartdrilldown
\title Implementing Drilldown
\brief Shows how to implement drilldown using a stacked barchart.
\include gallery.qdocinc
Here we create a stacked bar chart, which shows the harvest of various chili
peppers during season. In season view the harvest is grouped by month. To drill down to weekly
view, right-click the selected month. On weekly view, the harvest of the month clicked is shown
by week.
The season view looks like this:
\image examples_stackedbarchartdrilldown1.png {Screenshot showing a stacked bar chart of the harvest of Habanero, Lemon Drop, Starfish, and Aji
Amarillo across five months from May to September}
Clicking on a month shows that month's harvest:
\image examples_stackedbarchartdrilldown2.png {Screenshot showing a stacked bar chart of the harvest of Habanero, Lemon Drop, Starfish, and Aji
Amarillo in June}
First we define a drilldown series class, which adds categories to the stacked bar series and mapping for categories to other drilldown series.
The purpose of the drilldown series is to contain knowledge of the drilldown structure. The mapDrilldownSeries function maps the category to a given series. We can request the mapping for a category with the drilldownSeries(int category) function.
\snippet chartsgallery/stackeddrilldownseries.h 1
Next we define our own drilldown chart, which implements the handler for the mouse click. All QBarSeries derived classes send out the clicked(QBarSet*, int) signal when a series is clicked with the mouse. The parameter QBarSet contains the pointer to the clicked bar set and parameter int contains the index of the clicked category.
\snippet chartsgallery/stackeddrilldownchart.h 1
Now we have our drilldown classes and we can start using them.
First create the chart.
\snippet chartsgallery/stackeddrilldownwidget.cpp 1
We define the categories from which the drilldown series will be constructed.
\snippet chartsgallery/stackeddrilldownwidget.cpp 2
To create the drilldown structure, we first create our top level series, which we call seasonSeries. For each month in seasonSeries we create a drilldown series called weeklySeries which contains more detailed data for that month.
In weeklySeries, we use the drilldown handler to bring us back to seasonSeries. To do so we add mapping to the series. The seasonSeries is mapped to weeklySeries for each month. Every weeklySeries is mapped back to the seasonSeries.
To make mapping work, we connect the clicked signals from our series to the drilldownChart.
\snippet chartsgallery/stackeddrilldownwidget.cpp 3
When we have our drilldown structure ready, we can add the data to it. Here we generate a random crop for each plant in each week. The monthly crop is calculated from weekly crops and is set as value to the monthly series.
\snippet chartsgallery/stackeddrilldownwidget.cpp 4
Here we set the chart to show the top level series initially.
\snippet chartsgallery/stackeddrilldownwidget.cpp 5
*/