summaryrefslogtreecommitdiffstats
path: root/examples/charts/gallery/piedrilldownwidget.cpp
blob: 59555f726f5b1fc90f666ebe4255ae6628dad203 (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
39
40
41
42
43
44
45
46
47
48
49
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "piedrilldownchart.h"
#include "piedrilldownslice.h"
#include "piedrilldownwidget.h"

#include <QChart>
#include <QLegend>
#include <QPieSeries>
#include <QRandomGenerator>

PieDrilldownWidget::PieDrilldownWidget(QWidget *parent)
    : ContentWidget(parent)
{
    auto *chart = new PieDrilldownChart;
    chart->setTheme(QChart::ChartThemeLight);
    chart->setAnimationOptions(QChart::AllAnimations);
    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignRight);

    auto yearSeries = new QPieSeries(this);
    yearSeries->setName("Sales by year - All (Click on slice to drill down)");

    const QStringList months = {
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    };
    const QStringList names = {
        "Jane", "John", "Axel", "Mary", "Susan", "Bob"
    };

    for (const QString &name : names) {
        auto series = new QPieSeries(this);
        series->setName("Sales by month - " + name);

        for (const QString &month : months)
            *series << new PieDrilldownSlice(QRandomGenerator::global()->bounded(1000), month, yearSeries);

        QObject::connect(series, &QPieSeries::clicked, chart, &PieDrilldownChart::handleSliceClicked);

        *yearSeries << new PieDrilldownSlice(series->sum(), name, series);
    }

    QObject::connect(yearSeries, &QPieSeries::clicked, chart, &PieDrilldownChart::handleSliceClicked);

    chart->changeSeries(yearSeries);

    createDefaultChartView(chart);
}