- Qt 5.15
- Qt Charts
- Qt Charts Examples
- StackedBarChart Drilldown Example
- Qt Charts | Commercial or GPLv3

# StackedBarChart Drilldown Example

The example shows how to implement drilldown using a stacked barchart.

In the drilldown example 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:

Clicking on a month shows that month's harvest:

#### Running the Example

To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

#### Implementing Drilldown

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.

     class DrilldownBarSeries : public QStackedBarSeries
     {
         Q_OBJECT
     public:
         DrilldownBarSeries(QStringList categories, int maxValue, QObject *parent = 0);

         void mapDrilldownSeries(int index, DrilldownBarSeries *drilldownSeries);

         DrilldownBarSeries *drilldownSeries(int index);

         QStringList categories();

         int maxValue();

     private:
         QMap<int, DrilldownBarSeries *> m_DrilldownSeries;
         QStringList m_categories;
         int m_maxValue;
     };

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.

     class DrilldownChart : public QChart
     {
         Q_OBJECT
     public:
         explicit DrilldownChart(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = {});

         void changeSeries(DrilldownBarSeries *series);

     public Q_SLOTS:
         void handleClicked(int index, QBarSet *barset);

     private:
         DrilldownBarSeries *m_currentSeries;
         QBarCategoryAxis *m_axisX;
         QValueAxis *m_axisY;
     };

Now we have our drilldown classes and we can start using them. First create the chart.

         DrilldownChart *drilldownChart =  new DrilldownChart();
         drilldownChart->setAnimationOptions(QChart::SeriesAnimations);

We define the categories from which the drilldown series will be constructed.

         // Define categories
         const QStringList months = {
             "May", "Jun", "Jul", "Aug", "Sep"
         };
         const QStringList weeks = {
             "week 1", "week 2", "week 3", "week 4"
         };
         const QStringList plants = {
             "Habanero", "Lemon Drop", "Starfish", "Aji Amarillo"
         };

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.

         // Create drilldown structure
         DrilldownBarSeries *seasonSeries = new DrilldownBarSeries(months, 320, drilldownChart);
         seasonSeries->setName("Crop by month - Season");

         // Each month in season series has drilldown series for weekly data
         for (int month = 0; month < months.count(); month++) {

             // Create drilldown series for every week
             DrilldownBarSeries *weeklySeries = new DrilldownBarSeries(weeks, 80, drilldownChart);
             seasonSeries->mapDrilldownSeries(month, weeklySeries);

             // Drilling down from weekly data brings us back to season data.
             for (int week = 0; week < weeks.count(); week++) {
                 weeklySeries->mapDrilldownSeries(week, seasonSeries);
                 weeklySeries->setName(QString("Crop by week - " + months.at(month)));
             }

             // Use clicked signal to implement drilldown
             QObject::connect(weeklySeries, &DrilldownBarSeries::clicked,
                              drilldownChart, &DrilldownChart::handleClicked);
         }

         // Enable drilldown from season series using clicked signal
         QObject::connect(seasonSeries, &DrilldownBarSeries::clicked,
                          drilldownChart, &DrilldownChart::handleClicked);

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.

         // Fill monthly and weekly series with data
         for (const QString &plant : plants) {
             QBarSet *monthlyCrop = new QBarSet(plant);
             for (int month = 0; month < months.count(); month++) {
                 QBarSet *weeklyCrop = new QBarSet(plant);
                 for (int week = 0; week < weeks.count(); week++)
                     *weeklyCrop << QRandomGenerator::global()->bounded(20);
                 // Get the drilldown series from season series and add crop to it.
                 seasonSeries->drilldownSeries(month)->append(weeklyCrop);
                 *monthlyCrop << weeklyCrop->sum();
             }
             seasonSeries->append(monthlyCrop);
         }

Here we set the chart to show the top level series initially.

         // Show season series in initial view
         drilldownChart->changeSeries(seasonSeries);
         drilldownChart->setTitle(seasonSeries->name());

Example project @ code.qt.io