summaryrefslogtreecommitdiffstats
path: root/examples/charts/gallery/stackeddrilldownchart.cpp
blob: 89df4ec0047131ffbb2dc108fc99c9f97f916b2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "stackeddrilldownchart.h"

#include <QBarCategoryAxis>
#include <QValueAxis>

StackedDrilldownChart::StackedDrilldownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
    : QChart(QChart::ChartTypeCartesian, parent, wFlags),
      m_axisX(new QBarCategoryAxis),
      m_axisY(new QValueAxis)
{
    addAxis(m_axisY, Qt::AlignLeft);
    addAxis(m_axisX, Qt::AlignBottom);
}

void StackedDrilldownChart::changeSeries(StackedDrilldownSeries *series)
{
    if (m_currentSeries)
        removeSeries(m_currentSeries);

    m_currentSeries = series;

    // Reset axis
    m_axisX->setCategories(m_currentSeries->categories());
    addSeries(series);
    series->attachAxis(m_axisX);
    series->attachAxis(m_axisY);
    m_axisY->setRange(0,m_currentSeries->maxValue());
    setTitle(series->name());
}

void StackedDrilldownChart::handleClicked(int index, QBarSet *)
{
    auto series = static_cast<StackedDrilldownSeries *>(sender());
    changeSeries(series->drilldownSeries(index));
}