From 9edcfe0d3099459aa36e69f6ab4605593cefc2ef Mon Sep 17 00:00:00 2001 From: Andrii Staikov Date: Sun, 27 Jun 2021 00:20:47 +0200 Subject: 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 (cherry picked from commit 72d69b22d8da004fcb27e1a549cdda3f52b9c4af) Reviewed-by: Qt Cherry-pick Bot --- examples/charts/CMakeLists.txt | 1 + examples/charts/charts.pro | 3 +- examples/charts/selectedbar/CMakeLists.txt | 40 +++++++ examples/charts/selectedbar/main.cpp | 146 +++++++++++++++++++++++++ examples/charts/selectedbar/selectedbar.pro | 11 ++ examples/charts/selectedbar/utilities.cpp | 73 +++++++++++++ examples/charts/selectedbar/utilities.h | 46 ++++++++ src/charts/doc/images/examples_selectedbar.png | Bin 0 -> 29668 bytes src/charts/doc/src/examples-selectedbar.qdoc | 69 ++++++++++++ 9 files changed, 388 insertions(+), 1 deletion(-) create mode 100644 examples/charts/selectedbar/CMakeLists.txt create mode 100644 examples/charts/selectedbar/main.cpp create mode 100644 examples/charts/selectedbar/selectedbar.pro create mode 100644 examples/charts/selectedbar/utilities.cpp create mode 100644 examples/charts/selectedbar/utilities.h create mode 100644 src/charts/doc/images/examples_selectedbar.png create mode 100644 src/charts/doc/src/examples-selectedbar.qdoc 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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 setList = QList{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 + +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 +#include + +namespace Utilities +{ + QBarSet *createChickenSet(); + QBarSet *createPorkSet(); + QBarSet *createTurkeySet(); + QBarSet *createHamSet(); + QStringList createYearCategories(); + qreal totalSum(const QList &setList); +} + +#endif // UTILITIES_H diff --git a/src/charts/doc/images/examples_selectedbar.png b/src/charts/doc/images/examples_selectedbar.png new file mode 100644 index 00000000..0b425aeb Binary files /dev/null and b/src/charts/doc/images/examples_selectedbar.png differ diff --git a/src/charts/doc/src/examples-selectedbar.qdoc b/src/charts/doc/src/examples-selectedbar.qdoc new file mode 100644 index 00000000..72c6a34c --- /dev/null +++ b/src/charts/doc/src/examples-selectedbar.qdoc @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** 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 Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: https://www.gnu.org/licenses/fdl-1.3.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example selectedbar + \title SelectedBar Example + \ingroup qtcharts_examples + + \brief The example shows how to use a selection feature for the bar chart. + + The example shows changing the color and state of bars using the selection feature. + + \image examples_selectedbar.png + + \include examples-run.qdocinc + + \section1 Using setBarSelected() + + We create the sets and fill them with the data. Then we create a series and append data to it. + \snippet selectedbar/main.cpp 1 + + We create the chart and add series to it. + Also, we add a title to the chart, set animation for the chart, and align the legend. + \snippet selectedbar/main.cpp 2 + + Here we set the color for the selected bars. + \snippet selectedbar/main.cpp 3 + + Next step is adding axes: + QBarCategoryAxis for years of measurements and QValueAxis for values range. + \snippet selectedbar/main.cpp 4 + + Then we add the chart view to put the chart in. + \snippet selectedbar/main.cpp 5 + + Here we create a widget for labels of values of selected and unselected bars. + \snippet selectedbar/main.cpp 6 + + We connect selecting of a specific bar with labels of values using a lambda. + \c {set->toggleSelection({index})} sets the bar selected. + \snippet selectedbar/main.cpp 7 + + Finally, we create the main widget and add other layouts to it and run the application. + \snippet selectedbar/main.cpp 8 +*/ -- cgit v1.2.3