summaryrefslogtreecommitdiffstats
path: root/examples/charts/chartsgallery/selectedbarwidget.cpp
blob: d54d220186b02089418833f89b6ff587652f1b90 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "selectedbarwidget.h"

#include <QBarCategoryAxis>
#include <QBarSeries>
#include <QBarSet>
#include <QChart>
#include <QChartView>
#include <QGraphicsLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QValueAxis>

SelectedBarWidget::SelectedBarWidget(QWidget *parent)
    : ContentWidget(parent)
{
    //![1]
    QBarSet *setChicken = createChickenSet();
    QBarSet *setPork = createPorkSet();
    QBarSet *setTurkey = createTurkeySet();
    QBarSet *setHam = createHamSet();
    qreal totalSum = setChicken->sum() + setPork->sum() + setTurkey->sum() + setHam->sum();
    QList<QBarSet *> setList = QList<QBarSet *>{setChicken, setPork, setTurkey, setHam};

    auto series = new QBarSeries;
    series->append(setList);
    //![1]

    //![2]
    auto chart = new QChart;
    chart->addSeries(series);
    chart->setTitle(tr("Meat Consumption (Click on bars to select them)"));
    chart->setAnimationOptions(QChart::SeriesAnimations);
    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignBottom);
    chart->layout()->setContentsMargins(0, 0, 0, 0);
    //![2]

    //![3]
    const auto barSets = series->barSets();
    for (QBarSet *barSet : barSets)
        barSet->setSelectedColor(barSet->brush().color().darker());
    //![3]

    //![4]
    QStringList categories = createYearCategories();
    auto axisX = new QBarCategoryAxis;
    axisX->setCategories(categories);
    chart->addAxis(axisX, Qt::AlignBottom);
    series->attachAxis(axisX);

    auto axisY = new QValueAxis;
    axisY->setRange(0, 20);
    axisY->setTitleText(tr("Tons"));
    axisY->setLabelsAngle(-90);
    axisY->setTitleVisible(true);
    chart->addAxis(axisY, Qt::AlignLeft);
    series->attachAxis(axisY);
    //![4]

    //![5]
    auto chartView = new QChartView(chart, this);
    chartView->setRenderHint(QPainter::Antialiasing);
    //![5]

    //![6]
    auto labelWidget = new QWidget(this);
    auto labelLayout = new QHBoxLayout(labelWidget);
    labelLayout->setAlignment(Qt::AlignCenter);

    auto totalSumLabel = new QLabel(tr("Total sum: %1 T").arg(totalSum), this);
    labelLayout->addWidget(totalSumLabel);
    totalSumLabel->setContentsMargins(0, 0, 54, 0);

    auto selectedSumLabel = new QLabel(tr("Selected sum: 0 T"), this);
    labelLayout->addWidget(selectedSumLabel);

    auto unselectedSumLabel = new QLabel(tr("Unselected sum: %1 T").arg(totalSum), this);
    labelLayout->addWidget(unselectedSumLabel);
    unselectedSumLabel->setContentsMargins(54, 0, 0, 0);
    //![6]

    //![7]
    QObject::connect(series, &QAbstractBarSeries::clicked, series, [=](int index, QBarSet *set) {
        set->toggleSelection({index});
        qreal selectedSum = 0.;
        for (int i = 0; i < setList.size(); ++i) {
            auto selectedIndices = setList.at(i)->selectedBars();
            for (int k = 0; k < selectedIndices.size(); ++k)
                selectedSum += setList.at(i)->at(selectedIndices.at(k));
        }
        selectedSumLabel->setText(tr("Selected sum: %1 T").arg(selectedSum));
        // Because of rounding errors, selectedSum can result in being bigger than total sum
        qreal unselectedSum = totalSum - selectedSum < 0 ? 0. : totalSum - selectedSum;
        unselectedSumLabel->setText(
            tr("Unselected sum: %1 T")
                .arg(unselectedSum)
            );
    });
    //![7]

    //![8]
    auto mainLayout = new QVBoxLayout(this);

    mainLayout->addWidget(chartView);
    mainLayout->addWidget(labelWidget);
    //![8]
}

QBarSet *SelectedBarWidget::createChickenSet()
{
    auto set = new QBarSet(tr("Chicken"));
    set->append({15.0, 12.0, 8.0, 11.5, 13.7});
    return set;
}

QBarSet *SelectedBarWidget::createPorkSet()
{
    auto set = new QBarSet(tr("Pork"));
    set->append({9.0, 11.0, 9.0, 7, 12.2});
    return set;
}

QBarSet *SelectedBarWidget::createTurkeySet()
{
    auto set = new QBarSet(tr("Turkey"));
    set->append({5.0, 7.6, 9.3, 8, 8.1});
    return set;
}

QBarSet *SelectedBarWidget::createHamSet()
{
    auto set = new QBarSet(tr("Ham"));
    set->append({5.4, 7.1, 9.3, 12.3, 11.3});
    return set;
}

QStringList SelectedBarWidget::createYearCategories()
{
    return QStringList{tr("2017"),
                       tr("2018"),
                       tr("2019"),
                       tr("2020"),
                       tr("2021")};
}