diff options
author | Andrii Staikov <andrii.staikov@scythe-studio.com> | 2021-07-02 16:56:36 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-09-24 11:16:10 +0000 |
commit | d8bb227290f2f464e239a0b2c93290c50278ba90 (patch) | |
tree | 10be12508112aee1e203a6e8ed5f946ef9170ff7 /examples | |
parent | e942e61b39b2988129ade944179a0c2c1bcdd6f0 (diff) |
Add spline-series example
Example shows using of points selection and adding light markers to plot.
User can select shapes for markers and light markers and set images for them.
[QtCharts][Example] Added Example
Task-number: QTBUG-89445
Task-number: QTBUG-92884
Change-Id: Id0c9a5a7b696b7a51804575f645578d06c717d4e
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
(cherry picked from commit 5070d1fc3ee4b63c8a04f1fdf9226b779a6cb167)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/charts/CMakeLists.txt | 1 | ||||
-rw-r--r-- | examples/charts/charts.pro | 3 | ||||
-rw-r--r-- | examples/charts/pointsselectionandmarkers/CMakeLists.txt | 54 | ||||
-rw-r--r-- | examples/charts/pointsselectionandmarkers/blue_triangle.png | bin | 0 -> 2220 bytes | |||
-rw-r--r-- | examples/charts/pointsselectionandmarkers/green_triangle.png | bin | 0 -> 1389 bytes | |||
-rw-r--r-- | examples/charts/pointsselectionandmarkers/main.cpp | 158 | ||||
-rw-r--r-- | examples/charts/pointsselectionandmarkers/pointsselectionandmarkers.pro | 15 | ||||
-rw-r--r-- | examples/charts/pointsselectionandmarkers/pointsselectionandmarkers.qrc | 6 | ||||
-rw-r--r-- | examples/charts/pointsselectionandmarkers/utilities.cpp | 140 | ||||
-rw-r--r-- | examples/charts/pointsselectionandmarkers/utilities.h | 66 |
10 files changed, 442 insertions, 1 deletions
diff --git a/examples/charts/CMakeLists.txt b/examples/charts/CMakeLists.txt index a2122782..45e3f577 100644 --- a/examples/charts/CMakeLists.txt +++ b/examples/charts/CMakeLists.txt @@ -18,6 +18,7 @@ if(QT_FEATURE_charts_spline_chart) add_subdirectory(dynamicspline) add_subdirectory(multiaxis) add_subdirectory(splinechart) + add_subdirectory(pointsselectionandmarkers) endif() if(QT_FEATURE_charts_area_chart AND QT_FEATURE_charts_spline_chart) add_subdirectory(chartthemes) diff --git a/examples/charts/charts.pro b/examples/charts/charts.pro index cd92cfe2..bac912e5 100644 --- a/examples/charts/charts.pro +++ b/examples/charts/charts.pro @@ -21,7 +21,8 @@ qtConfig(charts-spline-chart) { SUBDIRS += \ dynamicspline \ multiaxis \ - splinechart + splinechart \ + pointsselectionandmarkers qtConfig(charts-area-chart) { SUBDIRS += \ chartthemes \ diff --git a/examples/charts/pointsselectionandmarkers/CMakeLists.txt b/examples/charts/pointsselectionandmarkers/CMakeLists.txt new file mode 100644 index 00000000..c2e68bd3 --- /dev/null +++ b/examples/charts/pointsselectionandmarkers/CMakeLists.txt @@ -0,0 +1,54 @@ +# Generated from pointsselectionandmarkers.pro. + +cmake_minimum_required(VERSION 3.14) +project(pointsselectionandmarkers 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/pointsselectionandmarkers") + +find_package(Qt6 COMPONENTS Core) +find_package(Qt6 COMPONENTS Gui) +find_package(Qt6 COMPONENTS Charts) + +qt_add_executable(pointsselectionandmarkers + utilities.h utilities.cpp + main.cpp +) + +set_target_properties(pointsselectionandmarkers PROPERTIES + WIN32_EXECUTABLE TRUE + MACOSX_BUNDLE TRUE +) + +target_link_libraries(pointsselectionandmarkers PUBLIC + Qt::Charts + Qt::Core + Qt::Gui +) + +set(pointsselectionandmarkers_resource_files + "blue_triangle.png" + "green_triangle.png" +) + +qt6_add_resources(pointsselectionandmarkers "images" + PREFIX + "/" + FILES + ${pointsselectionandmarkers_resource_files} +) + +install(TARGETS pointsselectionandmarkers + RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" + LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" +) diff --git a/examples/charts/pointsselectionandmarkers/blue_triangle.png b/examples/charts/pointsselectionandmarkers/blue_triangle.png Binary files differnew file mode 100644 index 00000000..7790453c --- /dev/null +++ b/examples/charts/pointsselectionandmarkers/blue_triangle.png diff --git a/examples/charts/pointsselectionandmarkers/green_triangle.png b/examples/charts/pointsselectionandmarkers/green_triangle.png Binary files differnew file mode 100644 index 00000000..29ae043f --- /dev/null +++ b/examples/charts/pointsselectionandmarkers/green_triangle.png diff --git a/examples/charts/pointsselectionandmarkers/main.cpp b/examples/charts/pointsselectionandmarkers/main.cpp new file mode 100644 index 00000000..376d49f9 --- /dev/null +++ b/examples/charts/pointsselectionandmarkers/main.cpp @@ -0,0 +1,158 @@ +/**************************************************************************** +** +** 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 <QApplication> +#include <QChart> +#include <QChartView> +#include <QCheckBox> +#include <QComboBox> +#include <QGridLayout> +#include <QHBoxLayout> +#include <QLabel> +#include <QMainWindow> +#include <QSplineSeries> + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + QMainWindow mainWindow; + mainWindow.setWindowTitle("Chart"); + + //![1] + constexpr qreal marker_size = 20.; + + QSplineSeries *series = new QSplineSeries(); + series->append({QPointF(0., 0.), + QPointF(0.5, 2.27), + QPointF(1.5, 2.2), + QPointF(3.3, 1.7), + QPointF(4.23, 3.1), + QPointF(5.3, 2.3), + QPointF(6.47, 4.1) + }); + series->setMarkerSize(marker_size); + series->setLightMarker(Utilities::redRectangle(marker_size)); + series->setSelectedLightMarker(Utilities::blueTriangle(marker_size)); + QObject::connect(series, &QXYSeries::clicked, series, [&](const QPointF &point) { + int index = series->points().indexOf(point); + if (index != -1) + series->toggleSelection({index}); + }); + //![1] + + //![2] + QChart *chart = new QChart(); + chart->addSeries(series); + chart->createDefaultAxes(); + chart->legend()->setVisible(false); + + QChartView *chartView = new QChartView(chart); + chartView->setRenderHint(QPainter::Antialiasing); + + QWidget *controlWidget = new QWidget(&mainWindow); + QGridLayout *controlLayout = new QGridLayout(controlWidget); + //![2] + + //![3] + QComboBox *charPointCombobox = new QComboBox(); + QComboBox *charPointSelectedCombobox = new QComboBox(); + QComboBox *lineColorCombobox = new QComboBox(); + QCheckBox *showUnselectedPointsCheckbox = new QCheckBox(); + //![3] + + //![4] + QLabel *charPoint = new QLabel(QCoreApplication::tr("Char point: ")); + charPointCombobox->addItems({QCoreApplication::tr("Red rectangle"), + QCoreApplication::tr("Green triangle"), + QCoreApplication::tr("Orange circle") + }); + QObject::connect(charPointCombobox, &QComboBox::currentIndexChanged, series, [&](const int index) { + if (showUnselectedPointsCheckbox->isChecked()) + series->setLightMarker(Utilities::getPointRepresentation(Utilities::PointType(index), marker_size)); + }); + //![4] + + //![5] + QLabel *charPointSelected = new QLabel(QCoreApplication::tr("Char point selected: ")); + charPointSelectedCombobox->addItems({QCoreApplication::tr("Blue triangle"), + QCoreApplication::tr("Yellow rectangle"), + QCoreApplication::tr("Lavender circle") + }); + QObject::connect(charPointSelectedCombobox, &QComboBox::currentIndexChanged, series, [&](const int index) { + series->setSelectedLightMarker(Utilities::getSelectedPointRepresentation(Utilities::SelectedPointType(index), marker_size)); + }); + + QLabel *lineColorLabel = new QLabel(QCoreApplication::tr("Line color: ")); + lineColorCombobox->addItems({QCoreApplication::tr("Blue"), + QCoreApplication::tr("Black"), + QCoreApplication::tr("Mint") + }); + QObject::connect(lineColorCombobox, &QComboBox::currentIndexChanged, series, [&](const int index) { + series->setColor(Utilities::makeLineColor(Utilities::LineColor(index))); + }); + //![5] + + //![6] + QLabel *showUnselectedPointsLabel = new QLabel(QCoreApplication::tr("Display unselected points: ")); + showUnselectedPointsCheckbox->setChecked(true); + QObject::connect(showUnselectedPointsCheckbox, &QCheckBox::stateChanged, series, [&](const int state) { + if (state) { + series->setLightMarker(Utilities::getPointRepresentation(Utilities::PointType(charPointCombobox->currentIndex()), marker_size)); + } else { + series->setLightMarker(QImage()); + } + }); + //![6] + + //![7] + controlLayout->addWidget(charPoint, 0, 0); + controlLayout->addWidget(charPointCombobox, 0, 1); + + controlLayout->addWidget(charPointSelected, 1, 0); + controlLayout->addWidget(charPointSelectedCombobox, 1, 1); + + controlLayout->addWidget(lineColorLabel, 2, 0); + controlLayout->addWidget(lineColorCombobox, 2, 1); + + controlLayout->addWidget(showUnselectedPointsLabel, 3, 0); + controlLayout->addWidget(showUnselectedPointsCheckbox, 3, 1, 1, 2); + + QWidget *mainWidget = new QWidget(&mainWindow); + QHBoxLayout *mainLayout = new QHBoxLayout(mainWidget); + mainLayout->addWidget(chartView); + mainLayout->addWidget(controlWidget); + + mainWindow.setCentralWidget(mainWidget); + mainWindow.resize(1080, 720); + mainWindow.show(); + return a.exec(); + //![7] +} diff --git a/examples/charts/pointsselectionandmarkers/pointsselectionandmarkers.pro b/examples/charts/pointsselectionandmarkers/pointsselectionandmarkers.pro new file mode 100644 index 00000000..ac8adffb --- /dev/null +++ b/examples/charts/pointsselectionandmarkers/pointsselectionandmarkers.pro @@ -0,0 +1,15 @@ +QT += charts \ + widgets + +SOURCES += \ + main.cpp \ + utilities.cpp + +HEADERS += \ + utilities.h + +RESOURCES += \ + pointsselectionandmarkers.qrc + +target.path = $$[QT_INSTALL_EXAMPLES]/charts/pointsselectionandmarkers +INSTALLS += target diff --git a/examples/charts/pointsselectionandmarkers/pointsselectionandmarkers.qrc b/examples/charts/pointsselectionandmarkers/pointsselectionandmarkers.qrc new file mode 100644 index 00000000..920bf0cc --- /dev/null +++ b/examples/charts/pointsselectionandmarkers/pointsselectionandmarkers.qrc @@ -0,0 +1,6 @@ +<RCC> + <qresource prefix="/"> + <file>blue_triangle.png</file> + <file>green_triangle.png</file> + </qresource> +</RCC> diff --git a/examples/charts/pointsselectionandmarkers/utilities.cpp b/examples/charts/pointsselectionandmarkers/utilities.cpp new file mode 100644 index 00000000..7b3ef7f4 --- /dev/null +++ b/examples/charts/pointsselectionandmarkers/utilities.cpp @@ -0,0 +1,140 @@ +/**************************************************************************** +** +** 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 <QBrush> +#include <QList> +#include <QPainter> +#include <QPainterPath> + +namespace Utilities +{ + QImage redRectangle(qreal imageSize) + { + QImage image(imageSize, imageSize, QImage::Format_RGB32); + QPainter painter; + painter.begin(&image); + painter.fillRect(0, 0, imageSize, imageSize, Qt::red); + painter.end(); + return image; + } + + QImage yellowRectangle(qreal imageSize) + { + QImage image(imageSize, imageSize, QImage::Format_RGB32); + QPainter painter; + painter.begin(&image); + painter.fillRect(0, 0, imageSize, imageSize, Qt::yellow); + painter.end(); + return image; + } + + QImage blueTriangle(qreal imageSize) + { + return QImage(":/blue_triangle.png").scaled(imageSize, imageSize); + } + + QImage greenTriangle(qreal imageSize) + { + return QImage(":/green_triangle.png").scaled(imageSize, imageSize); + } + + QImage orangeCircle(qreal imageSize) + { + QImage image(imageSize, imageSize, QImage::Format_ARGB32); + image.fill(QColor(0, 0, 0, 0)); + QPainter paint; + paint.begin(&image); + paint.setBrush(QColor(255,127,80)); + QPen pen = paint.pen(); + pen.setWidth(0); + paint.setPen(pen); + paint.drawEllipse(0, 0, imageSize * 0.9, imageSize * 0.9); + paint.end(); + return image; + } + + QImage lavenderCircle(qreal imageSize) + { + QImage image(imageSize, imageSize, QImage::Format_ARGB32); + image.fill(QColor(0, 0, 0, 0)); + QPainter paint; + paint.begin(&image); + paint.setBrush(QColor(147,112,219)); + QPen pen = paint.pen(); + pen.setWidth(0); + paint.setPen(pen); + paint.drawEllipse(0, 0, imageSize * 0.9, imageSize * 0.9); + paint.end(); + return image; + } + + QImage getPointRepresentation(PointType pointType, int imageSize) + { + switch (pointType) { + case Utilities::PointType::RedRectangle: + return redRectangle(imageSize); + case Utilities::PointType::GreenTriangle: + return greenTriangle(imageSize); + case Utilities::PointType::OrangeCircle: + return orangeCircle(imageSize); + default: + return redRectangle(imageSize); + } + } + + QImage getSelectedPointRepresentation(SelectedPointType pointType, int imageSize) + { + switch (pointType) { + case Utilities::SelectedPointType::BlueTriangle: + return blueTriangle(imageSize); + case Utilities::SelectedPointType::YellowRectangle: + return yellowRectangle(imageSize); + case Utilities::SelectedPointType::LavenderCircle: + return lavenderCircle(imageSize); + default: + return blueTriangle(imageSize); + } + } + + QColor makeLineColor(LineColor lineColor) + { + switch (lineColor) { + case Utilities::LineColor::Blue: + return QColor(65, 105, 225); + case Utilities::LineColor::Black: + return QColor(0,0,0); + case Utilities::LineColor::Mint: + return QColor(70, 203, 155); + default: + return QColor(0, 0, 0); + } + } +} diff --git a/examples/charts/pointsselectionandmarkers/utilities.h b/examples/charts/pointsselectionandmarkers/utilities.h new file mode 100644 index 00000000..79a6a0c6 --- /dev/null +++ b/examples/charts/pointsselectionandmarkers/utilities.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** 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 <QImage> + +namespace Utilities +{ + enum class PointType { + RedRectangle, + GreenTriangle, + OrangeCircle + }; + + enum class SelectedPointType { + BlueTriangle, + YellowRectangle, + LavenderCircle + }; + + enum class LineColor { + Blue, + Black, + Mint + }; + + QImage redRectangle(qreal imageSize); + QImage yellowRectangle(qreal imageSize); + QImage blueTriangle(qreal imageSize); + QImage greenTriangle(qreal imageSize); + QImage orangeCircle(qreal imageSize); + QImage lavenderCircle(qreal imageSize); + QImage getPointRepresentation(PointType pointType, int imageSize); + QImage getSelectedPointRepresentation(SelectedPointType pointType, int imageSize); + QColor makeLineColor(LineColor lineColor); +} + +#endif // UTILITIES_H |