summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorAndrii Staikov <andrii.staikov@scythe-studio.com>2021-06-27 00:20:47 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-08-30 13:01:41 +0000
commit9edcfe0d3099459aa36e69f6ab4605593cefc2ef (patch)
tree6f9fe9e42f5f373000fda0f32e7651dac97d5814 /examples
parentd421f565e9190b5e9e3af19b60e67bf85c8eb2cf (diff)
Add bar chart example
Example shows using setBarSelected(). Selecting bars changes their color and information of selected/unselected bars. [QtCharts][Example] Added Example Task-number: QTBUG-89445 Change-Id: Ic863e763d2cfa552629f42afd1232c1f77b95e95 Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io> (cherry picked from commit 72d69b22d8da004fcb27e1a549cdda3f52b9c4af) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'examples')
-rw-r--r--examples/charts/CMakeLists.txt1
-rw-r--r--examples/charts/charts.pro3
-rw-r--r--examples/charts/selectedbar/CMakeLists.txt40
-rw-r--r--examples/charts/selectedbar/main.cpp146
-rw-r--r--examples/charts/selectedbar/selectedbar.pro11
-rw-r--r--examples/charts/selectedbar/utilities.cpp73
-rw-r--r--examples/charts/selectedbar/utilities.h46
7 files changed, 319 insertions, 1 deletions
diff --git a/examples/charts/CMakeLists.txt b/examples/charts/CMakeLists.txt
index 8cd0211f..a2122782 100644
--- a/examples/charts/CMakeLists.txt
+++ b/examples/charts/CMakeLists.txt
@@ -34,6 +34,7 @@ if(QT_FEATURE_charts_bar_chart)
add_subdirectory(percentbarchart)
add_subdirectory(legend)
add_subdirectory(temperaturerecords)
+ add_subdirectory(selectedbar)
endif()
if(QT_FEATURE_charts_pie_chart)
add_subdirectory(donutchart)
diff --git a/examples/charts/charts.pro b/examples/charts/charts.pro
index 1e60be5a..cd92cfe2 100644
--- a/examples/charts/charts.pro
+++ b/examples/charts/charts.pro
@@ -39,7 +39,8 @@ qtConfig(charts-bar-chart) {
stackedbarchartdrilldown \
percentbarchart \
legend \
- temperaturerecords
+ temperaturerecords \
+ selectedbar
}
qtConfig(charts-pie-chart) {
SUBDIRS += \
diff --git a/examples/charts/selectedbar/CMakeLists.txt b/examples/charts/selectedbar/CMakeLists.txt
new file mode 100644
index 00000000..55542a42
--- /dev/null
+++ b/examples/charts/selectedbar/CMakeLists.txt
@@ -0,0 +1,40 @@
+# Generated from selectedbar.pro.
+
+cmake_minimum_required(VERSION 3.14)
+project(selectedbar LANGUAGES CXX)
+
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
+
+set(CMAKE_AUTOMOC ON)
+set(CMAKE_AUTORCC ON)
+set(CMAKE_AUTOUIC ON)
+
+if(NOT DEFINED INSTALL_EXAMPLESDIR)
+ set(INSTALL_EXAMPLESDIR "examples")
+endif()
+
+set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/charts/selectedbar")
+
+find_package(Qt6 COMPONENTS Core)
+find_package(Qt6 COMPONENTS Gui)
+find_package(Qt6 COMPONENTS Charts)
+
+qt_add_executable(selectedbar
+ utilities.h utilities.cpp
+ main.cpp
+)
+set_target_properties(selectedbar PROPERTIES
+ WIN32_EXECUTABLE TRUE
+ MACOSX_BUNDLE TRUE
+)
+target_link_libraries(selectedbar PUBLIC
+ Qt::Charts
+ Qt::Core
+ Qt::Gui
+)
+
+install(TARGETS selectedbar
+ RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
+ LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+)
diff --git a/examples/charts/selectedbar/main.cpp b/examples/charts/selectedbar/main.cpp
new file mode 100644
index 00000000..9ed62441
--- /dev/null
+++ b/examples/charts/selectedbar/main.cpp
@@ -0,0 +1,146 @@
+/****************************************************************************
+**
+** Copyright (C) 2021 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt Charts module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 or (at your option) any later version
+** approved by the KDE Free Qt Foundation. The licenses are as published by
+** the Free Software Foundation and appearing in the file LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "utilities.h"
+#include <QBarSet>
+#include <QApplication>
+#include <QBarSeries>
+#include <QList>
+#include <QChart>
+#include <QBarCategoryAxis>
+#include <QValueAxis>
+#include <QChartView>
+#include <QWindow>
+#include <QHBoxLayout>
+#include <QLabel>
+#include <QMainWindow>
+
+int main(int argc, char *argv[])
+{
+ QApplication a(argc, argv);
+ QMainWindow mainWindow;
+ mainWindow.setWindowTitle(QCoreApplication::tr("Meat consumption series"));
+
+ //![1]
+ QBarSet *setChicken = Utilities::createChickenSet();
+ QBarSet *setPork = Utilities::createPorkSet();
+ QBarSet *setTurkey = Utilities::createTurkeySet();
+ QBarSet *setHam = Utilities::createHamSet();
+ qreal totalSum = setChicken->sum() + setPork->sum() + setTurkey->sum() + setHam->sum();
+ QList<QBarSet *> setList = QList<QBarSet *>{setChicken, setPork, setTurkey, setHam};
+
+ QBarSeries *series = new QBarSeries();
+ series->append(setList);
+ //![1]
+
+ //![2]
+ QChart *chart = new QChart();
+ chart->addSeries(series);
+ chart->setTitle(QCoreApplication::tr("Meat consumption"));
+ chart->setAnimationOptions(QChart::SeriesAnimations);
+ chart->legend()->setVisible(true);
+ chart->legend()->setAlignment(Qt::AlignBottom);
+ //![2]
+
+ //![3]
+ for (QBarSet *barSet : series->barSets())
+ barSet->setSelectedColor(barSet->brush().color().darker());
+ //![3]
+
+ //![4]
+ QStringList categories = Utilities::createYearCategories();
+ QBarCategoryAxis *axisX = new QBarCategoryAxis();
+ axisX->setCategories(categories);
+ chart->addAxis(axisX, Qt::AlignBottom);
+ series->attachAxis(axisX);
+
+ QValueAxis *axisY = new QValueAxis();
+ axisY->setRange(0, 20);
+ axisY->setTitleText(QCoreApplication::tr("Tons"));
+ axisY->setLabelsAngle(-90);
+ axisY->setTitleVisible(true);
+ chart->addAxis(axisY, Qt::AlignLeft);
+ series->attachAxis(axisY);
+ //![4]
+
+ //![5]
+ QChartView *chartView = new QChartView(chart);
+ chartView->setRenderHint(QPainter::Antialiasing);
+ //![5]
+
+ //![6]
+ QWidget *labelWidget = new QWidget(&mainWindow);
+ QHBoxLayout *labelLayout = new QHBoxLayout(labelWidget);
+ labelLayout->setAlignment(Qt::AlignCenter);
+
+ QLabel *totalSumLabel = new QLabel(QCoreApplication::tr("Total sum: %1 T").arg(totalSum));
+ labelLayout->addWidget(totalSumLabel);
+ totalSumLabel->setContentsMargins(0, 0, 54, 0);
+
+ QLabel *selectedSumLabel = new QLabel(QCoreApplication::tr("Selected sum: 0 T"));
+ labelLayout->addWidget(selectedSumLabel);
+
+ QLabel *unselectedSumLabel = new QLabel(QCoreApplication::tr("Unselected sum: %1 T").arg(totalSum));
+ 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(QCoreApplication::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(
+ QCoreApplication::tr("Unselected sum: %1 T")
+ .arg(unselectedSum)
+ );
+ });
+ //![7]
+
+ //![8]
+ QWidget *mainWidget = new QWidget(&mainWindow);
+ QVBoxLayout *mainLayout = new QVBoxLayout(mainWidget);
+
+ mainLayout->addWidget(chartView);
+ mainLayout->addWidget(labelWidget);
+
+ mainWindow.setCentralWidget(mainWidget);
+ mainWindow.resize(800, 600);
+
+ mainWindow.show();
+ return a.exec();
+ //![8]
+}
diff --git a/examples/charts/selectedbar/selectedbar.pro b/examples/charts/selectedbar/selectedbar.pro
new file mode 100644
index 00000000..24d0de08
--- /dev/null
+++ b/examples/charts/selectedbar/selectedbar.pro
@@ -0,0 +1,11 @@
+QT += charts widgets
+
+SOURCES += \
+ main.cpp \
+ utilities.cpp
+
+HEADERS += \
+ utilities.h
+
+target.path = $$[QT_INSTALL_EXAMPLES]/charts/selectedbar
+INSTALLS += target
diff --git a/examples/charts/selectedbar/utilities.cpp b/examples/charts/selectedbar/utilities.cpp
new file mode 100644
index 00000000..4bbf2c74
--- /dev/null
+++ b/examples/charts/selectedbar/utilities.cpp
@@ -0,0 +1,73 @@
+/****************************************************************************
+**
+** Copyright (C) 2021 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt Charts module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 or (at your option) any later version
+** approved by the KDE Free Qt Foundation. The licenses are as published by
+** the Free Software Foundation and appearing in the file LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "utilities.h"
+#include <QCoreApplication>
+
+namespace Utilities
+{
+
+QBarSet *createChickenSet()
+{
+ QBarSet *set = new QBarSet(QCoreApplication::tr("Chicken"));
+ set->append({15.0, 12.0, 8.0, 11.5, 13.7});
+ return set;
+}
+
+QBarSet *createPorkSet()
+{
+ QBarSet *set = new QBarSet(QCoreApplication::tr("Pork"));
+ set->append({9.0, 11.0, 9.0, 7, 12.2});
+ return set;
+}
+
+QBarSet *createTurkeySet()
+{
+ QBarSet *set = new QBarSet(QCoreApplication::tr("Turkey"));
+ set->append({5.0, 7.6, 9.3, 8, 8.1});
+ return set;
+}
+
+QBarSet *createHamSet()
+{
+ QBarSet *set = new QBarSet(QCoreApplication::tr("Ham"));
+ set->append({5.4, 7.1, 9.3, 12.3, 11.3});
+ return set;
+}
+
+QStringList createYearCategories()
+{
+ return QStringList{QCoreApplication::tr("2017"),
+ QCoreApplication::tr("2018"),
+ QCoreApplication::tr("2019"),
+ QCoreApplication::tr("2020"),
+ QCoreApplication::tr("2021")};
+}
+
+}
diff --git a/examples/charts/selectedbar/utilities.h b/examples/charts/selectedbar/utilities.h
new file mode 100644
index 00000000..41fe9610
--- /dev/null
+++ b/examples/charts/selectedbar/utilities.h
@@ -0,0 +1,46 @@
+/****************************************************************************
+**
+** Copyright (C) 2021 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt Charts module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 or (at your option) any later version
+** approved by the KDE Free Qt Foundation. The licenses are as published by
+** the Free Software Foundation and appearing in the file LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef UTILITIES_H
+#define UTILITIES_H
+
+#include <QList>
+#include <QBarSet>
+
+namespace Utilities
+{
+ QBarSet *createChickenSet();
+ QBarSet *createPorkSet();
+ QBarSet *createTurkeySet();
+ QBarSet *createHamSet();
+ QStringList createYearCategories();
+ qreal totalSum(const QList<QBarSet *> &setList);
+}
+
+#endif // UTILITIES_H