ドリルダウンの実装

注:これは 「ウィジェット付きチャート ギャラリー」のサンプルの一部です。

ここでは、シーズンごとのさまざまな唐辛子の収穫量を示す積み上げ棒グラフを作成します。シーズンビューでは、収穫量は月ごとにグループ化されています。週単位のビューにドリルダウンするには、選択した月を右クリックします。週単位のビューでは、クリックした月の収穫量が週ごとに表示されます。

シーズンビューは次のようになります:

5月から9月までの5か月間にわたる、ハバネロ、レモンドロップ、スターフィッシュ、アヒ・アマリージョの収穫量を示す積み上げ棒グラフのスクリーンショット

月をクリックすると、その月の収穫量が表示されます:

6月のハバネロ、レモンドロップ、スターフィッシュ、アヒ・アマリージョの収穫量を示す積み上げ棒グラフのスクリーンショット

まず、ドリルダウン・シリーズ・クラスを定義します。このクラスは、積み上げ棒グラフ・シリーズにカテゴリを追加し、カテゴリを他のドリルダウン・シリーズにマッピングします。ドリルダウン・シリーズの目的は、ドリルダウン構造に関する情報を保持することにあります。mapDrilldownSeries関数は、カテゴリを指定されたシリーズにマッピングします。 drilldownSeries(int category)関数を使用して、カテゴリのマッピングを要求できます。

class StackedDrilldownSeries : public QStackedBarSeries
{
    Q_OBJECT
public:
    StackedDrilldownSeries(const QStringList &categories, int maxValue, QObject *parent = nullptr);

    void mapDrilldownSeries(int index, StackedDrilldownSeries *drilldownSeries);
    StackedDrilldownSeries *drilldownSeries(int index) const;
    QStringList categories() const;
    int maxValue() const;

private:
    QMap<int, StackedDrilldownSeries *> m_drilldownSeries;
    QStringList m_categories;
    int m_maxValue = 0;
};

次に、マウスクリックのハンドラを実装する独自のドリルダウンチャートを定義します。QBarSeries から派生したすべてのクラスは、シリーズがマウスでクリックされると、clicked(QBarSet*, int) シグナルを送信します。パラメータ QBarSet にはクリックされたバーセットへのポインタが、パラメータ int にはクリックされたカテゴリのインデックスが含まれます。

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

    void changeSeries(StackedDrilldownSeries *series);

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

private:
    StackedDrilldownSeries *m_currentSeries = nullptr;
    QBarCategoryAxis *m_axisX = nullptr;
    QValueAxis *m_axisY = nullptr;
};

これでドリルダウンクラスが完成したので、使用を開始できます。まず、チャートを作成します。

auto drilldownChart = new StackedDrilldownChart;
drilldownChart->setAnimationOptions(QChart::SeriesAnimations);

ドリルダウン・シリーズを構成するカテゴリを定義します。

// 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"
};

ドリルダウン構造を作成するために、まず「seasonSeries」と呼ぶ最上位のシリーズを作成します。seasonSeriesの各月について、その月のより詳細なデータを含む「weeklySeries」というドリルダウンシリーズを作成します。 weeklySeries では、ドリルダウンハンドラを使用して seasonSeries に戻ります。そのためには、シリーズにマッピングを追加します。各月について、seasonSeries は weeklySeries にマッピングされます。すべての weeklySeries は seasonSeries にマッピングされます。マッピングを機能させるには、シリーズのクリック信号を drilldownChart に接続します。

// Create drilldown structure
auto seasonSeries = new StackedDrilldownSeries(months, 320, drilldownChart);
seasonSeries->setName("Crop by month - Season (Click on bar to drill down)");

// 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
    auto weeklySeries = new StackedDrilldownSeries(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, &StackedDrilldownSeries::clicked,
                     drilldownChart, &StackedDrilldownChart::handleClicked);
}

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

ドリルダウン構造の準備が整ったら、データを追加できます。ここでは、各週の各植物についてランダムな収穫量を生成します。月ごとの収穫量は週ごとの収穫量から計算され、月次シリーズの値として設定されます。

// Fill monthly and weekly series with data
for (const QString &plant : plants) {
    auto monthlyCrop = new QBarSet(plant);
    for (int month = 0; month < months.count(); month++) {
        auto 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);
}

ここでは、チャートが初期状態で最上位の系列を表示するように設定します。

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

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.