├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── example ├── CMakeLists.txt └── zoomlinechart │ ├── CMakeLists.txt │ └── main.cpp ├── rangelimitedvalueaxis.cpp ├── rangelimitedvalueaxis.h ├── res ├── horizontal_zoom.png ├── pan.png ├── rectangle_zoom.png ├── vertical_zoom.png ├── zoom_to_fit_all.png └── zoomable_chart_widget_resources.qrc ├── screenshots ├── hover.png ├── promote_to.png └── zoom_selector.png ├── zoomable_chart_widget.pri ├── zoomablechart.cpp ├── zoomablechart.h ├── zoomablechartview.cpp ├── zoomablechartview.h ├── zoomablechartwidget.cpp ├── zoomablechartwidget.h └── zoomablechartwidget.ui /.gitignore: -------------------------------------------------------------------------------- 1 | # C++ objects and libs 2 | *.slo 3 | *.lo 4 | *.o 5 | *.a 6 | *.la 7 | *.lai 8 | *.so 9 | *.dll 10 | *.dylib 11 | 12 | # Qt-es 13 | object_script.*.Release 14 | object_script.*.Debug 15 | *_plugin_import.cpp 16 | /.qmake.cache 17 | /.qmake.stash 18 | *.pro.user 19 | *.pro.user.* 20 | *.qbs.user 21 | *.qbs.user.* 22 | *.moc 23 | moc_*.cpp 24 | moc_*.h 25 | qrc_*.cpp 26 | ui_*.h 27 | *.qmlc 28 | *.jsc 29 | Makefile* 30 | *build-* 31 | 32 | # Qt unit tests 33 | target_wrapper.* 34 | 35 | # QtCreator 36 | *.autosave 37 | 38 | # QtCreator Qml 39 | *.qmlproject.user 40 | *.qmlproject.user.* 41 | 42 | # QtCreator CMake 43 | CMakeLists.txt.user* 44 | /build 45 | /.cache 46 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10.0) 2 | project(zoomablechartwidget VERSION 0.1.0 LANGUAGES C CXX) 3 | 4 | # Determine if zoomablechart is built as a subproject (using add_subdirectory) 5 | # or if it is the master project. 6 | if (NOT DEFINED ZOOMABLECHART_MASTER_PROJECT) 7 | set(ZOOMABLECHART_MASTER_PROJECT OFF) 8 | if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) 9 | set(ZOOMABLECHART_MASTER_PROJECT ON) 10 | message(STATUS "CMake version: ${CMAKE_VERSION}") 11 | endif () 12 | endif () 13 | 14 | 15 | find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Charts) 16 | find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Charts) 17 | 18 | add_library(zoomablechart 19 | rangelimitedvalueaxis.cpp 20 | rangelimitedvalueaxis.h 21 | 22 | zoomablechart.cpp 23 | zoomablechart.h 24 | 25 | zoomablechartview.cpp 26 | zoomablechartview.h 27 | 28 | zoomablechartwidget.cpp 29 | zoomablechartwidget.h 30 | zoomablechartwidget.ui 31 | ) 32 | 33 | set_target_properties(zoomablechart PROPERTIES 34 | AUTOMOC ON 35 | AUTOUIC ON 36 | AUTORCC ON 37 | ) 38 | 39 | target_link_libraries(zoomablechart PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Charts) 40 | target_include_directories(zoomablechart PUBLIC ./) 41 | 42 | if(ZOOMABLECHART_MASTER_PROJECT) 43 | add_subdirectory(example) 44 | endif() 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Zoomable chart widget for Qt 2 | 3 | Wrapper widget around QChartView which provides basic zoom/pan/legend handling stuff 4 | 5 | ## Why? 6 | 7 | Because I got rid of configuring the QtCharts widget in each of my applications 8 | 9 | ### How to use it? 10 | 11 | Add this project to your Qt application as a submodule with: 12 | 13 | ``` 14 | git submodule add https://github.com/martonmiklos/qt_zoomable_chart_widget.git 15 | ``` 16 | 17 | Include it into your project by adding the follwing to your .pro file: 18 | 19 | ``` 20 | include(qt_zoomable_chart_widget/zoomable_chart_widget.pri) 21 | ``` 22 | 23 | Create you UI with QtDesigner (or Designer plugin in the QtCreator). 24 | Add your chart as a QWidget and then promote to the ZoomableChartWidget: 25 | 26 | ![Promoting widget](https://raw.githubusercontent.com/martonmiklos/qt_zoomable_chart_widget/master/screenshots/promote_to.png "Promoting widget") 27 | 28 | You can access the underlying QtChart object via 29 | 30 | ``` 31 | ui->whateverIsMyWidgetName->chart() 32 | ``` 33 | 34 | You can add/remove series,axes do whatever else you do with a QtChart. 35 | 36 | ### Features 37 | 38 | #### Zoom mode 39 | There is a zoom mode selector combobox above the chart. I know this is a space wasting thing, I am planning to move this feature to a hamburger style button to the graph area's corner. 40 | 41 | ![Zoom mode selector](https://raw.githubusercontent.com/martonmiklos/qt_zoomable_chart_widget/master/screenshots/zoom_selector.png "Zoom mode selector") 42 | 43 | You can select 4 zoom modes with it: 44 | 45 | - ![pan icon](https://raw.githubusercontent.com/martonmiklos/qt_zoomable_chart_widget/master/res/pan.png) Pan mode: you can drag the graph content with pressed mouse button along the horizontal axes. If you press the Ctrl key you can do panning along the vertical axes. 46 | - ![rectangular icon](https://raw.githubusercontent.com/martonmiklos/qt_zoomable_chart_widget/master/res/rectangle_zoom.png) Rectangular zoom mode 47 | - ![horizontal icon](https://raw.githubusercontent.com/martonmiklos/qt_zoomable_chart_widget/master/res/horizontal_zoom.png) You can zoom into the graph contents on the horizontal axis with the mouse wheel. If you press the Ctrl key the zoom will be performed on the vertical axis. 48 | - ![vertical icon](https://raw.githubusercontent.com/martonmiklos/qt_zoomable_chart_widget/master/res/vertical_zoom.png) You can zoom into the graph contents on the vertical axis with the mouse wheel. If you press the Ctrl key the zoom will be performed on the horizontal axis. 49 | 50 | #### Legend show/hide 51 | You can show/hide series by clicking on the Legend markers. Inspired by the [Legend markers Qt example)](https://doc.qt.io/qt-5/qtcharts-legendmarkers-example.html) 52 | 53 | Protip: by pressing the Shift you can show/hide multiple series by a single click 54 | 55 | #### Legend hover highlight 56 | When hovering the mouse over the legend markers the respective series line width will be doubled: 57 | ![Legend hover](https://raw.githubusercontent.com/martonmiklos/qt_zoomable_chart_widget/master/screenshots/hover.png "Legend hover") 58 | 59 | #### Limiting scrolling/zooming of QValueAxis 60 | You can limit the scroll/zooming ranges of QValueAxes by using RangeLimitedValueAxis. 61 | 62 | If you find this feature useful to be included in the Qt itself [please comment/vote on this issue.](https://bugreports.qt.io/browse/QTBUG-81759) 63 | -------------------------------------------------------------------------------- /example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | add_subdirectory(zoomlinechart) 4 | 5 | -------------------------------------------------------------------------------- /example/zoomlinechart/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Charts) 3 | find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Charts) 4 | 5 | add_executable(zoomlinechart main.cpp) 6 | 7 | set_target_properties(zoomlinechart PROPERTIES 8 | AUTOMOC ON 9 | AUTOUIC ON 10 | AUTORCC ON 11 | ) 12 | 13 | target_link_libraries(zoomlinechart PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Charts zoomablechart) -------------------------------------------------------------------------------- /example/zoomlinechart/main.cpp: -------------------------------------------------------------------------------- 1 | // based on https://doc.qt.io/Qt-6/qtcharts-zoomlinechart-example.html 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | int main(int argc, char *argv[]) 14 | { 15 | QApplication a(argc, argv); 16 | 17 | auto series = new QLineSeries; 18 | for (int i = 0; i < 500; i++) { 19 | QPointF p((qreal) i, qSin(M_PI / 50 * i) * 100); 20 | p.ry() += QRandomGenerator::global()->bounded(20); 21 | *series << p; 22 | } 23 | 24 | auto chart = new ZoomableChart; 25 | chart->addSeries(series); 26 | chart->setTitle("Zoom in/out example"); 27 | chart->setAnimationOptions(QChart::SeriesAnimations); 28 | chart->legend()->hide(); 29 | chart->createDefaultAxes(); 30 | 31 | 32 | QMainWindow window; 33 | window.resize(800, 600); 34 | window.grabGesture(Qt::PanGesture); 35 | window.grabGesture(Qt::PinchGesture); 36 | 37 | auto chartView = new ZoomableChartView(chart); 38 | chartView->setRenderHint(QPainter::Antialiasing); 39 | 40 | window.setCentralWidget(chartView); 41 | 42 | window.show(); 43 | 44 | return a.exec(); 45 | } 46 | -------------------------------------------------------------------------------- /rangelimitedvalueaxis.cpp: -------------------------------------------------------------------------------- 1 | #include "rangelimitedvalueaxis.h" 2 | 3 | RangeLimitedValueAxis::RangeLimitedValueAxis(QObject *parent) : 4 | QValueAxis(parent) 5 | { 6 | setProperty("rangeLimited", true); // This is a hack 7 | } 8 | 9 | void RangeLimitedValueAxis::setLowerLimit(qreal value) 10 | { 11 | if (m_limitLowerRange && value > m_upLimit) { 12 | m_limitLowerRange = false; 13 | return; 14 | } 15 | m_lowLimit = value; 16 | m_limitLowerRange = true; 17 | } 18 | 19 | void RangeLimitedValueAxis::setUpperLimit(qreal value) 20 | { 21 | if (m_limitLowerRange && m_lowLimit > value) { 22 | m_limitUpperRange = false; 23 | return; 24 | } 25 | m_upLimit = value; 26 | m_limitUpperRange = true; 27 | } 28 | 29 | void RangeLimitedValueAxis::disableLowerLimit() 30 | { 31 | m_limitLowerRange = false; 32 | } 33 | 34 | void RangeLimitedValueAxis::disableUpperLimit() 35 | { 36 | m_limitUpperRange = false; 37 | } 38 | 39 | bool RangeLimitedValueAxis::isLowerRangeLimited() const 40 | { 41 | return m_limitLowerRange; 42 | } 43 | 44 | bool RangeLimitedValueAxis::isUpperRangeLimited() const 45 | { 46 | return m_limitUpperRange; 47 | } 48 | 49 | qreal RangeLimitedValueAxis::lowerLimit() const 50 | { 51 | return m_lowLimit; 52 | } 53 | 54 | qreal RangeLimitedValueAxis::upperLimit() const 55 | { 56 | return m_upLimit; 57 | } 58 | -------------------------------------------------------------------------------- /rangelimitedvalueaxis.h: -------------------------------------------------------------------------------- 1 | #ifndef RANGELIMITEDVALUEAXIS_H 2 | #define RANGELIMITEDVALUEAXIS_H 3 | 4 | #include 5 | 6 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 7 | QT_CHARTS_USE_NAMESPACE 8 | #endif 9 | 10 | 11 | class RangeLimitedValueAxis : public QValueAxis 12 | { 13 | public: 14 | RangeLimitedValueAxis(QObject *parent = nullptr); 15 | 16 | void setLowerLimit(qreal value); 17 | void setUpperLimit(qreal value); 18 | 19 | void disableLowerLimit(); 20 | void disableUpperLimit(); 21 | 22 | bool isLowerRangeLimited() const; 23 | bool isUpperRangeLimited() const; 24 | 25 | qreal lowerLimit() const; 26 | qreal upperLimit() const; 27 | 28 | protected: 29 | bool m_limitLowerRange = false; 30 | bool m_limitUpperRange = false; 31 | 32 | qreal m_lowLimit = 0.0; 33 | qreal m_upLimit = 0.0; 34 | }; 35 | 36 | #endif // RANGELIMITEDVALUEAXIS_H 37 | -------------------------------------------------------------------------------- /res/horizontal_zoom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martonmiklos/qt_zoomable_chart_widget/f610926f0c4411dd34c836b1cb7f66ca3e1631eb/res/horizontal_zoom.png -------------------------------------------------------------------------------- /res/pan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martonmiklos/qt_zoomable_chart_widget/f610926f0c4411dd34c836b1cb7f66ca3e1631eb/res/pan.png -------------------------------------------------------------------------------- /res/rectangle_zoom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martonmiklos/qt_zoomable_chart_widget/f610926f0c4411dd34c836b1cb7f66ca3e1631eb/res/rectangle_zoom.png -------------------------------------------------------------------------------- /res/vertical_zoom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martonmiklos/qt_zoomable_chart_widget/f610926f0c4411dd34c836b1cb7f66ca3e1631eb/res/vertical_zoom.png -------------------------------------------------------------------------------- /res/zoom_to_fit_all.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martonmiklos/qt_zoomable_chart_widget/f610926f0c4411dd34c836b1cb7f66ca3e1631eb/res/zoom_to_fit_all.png -------------------------------------------------------------------------------- /res/zoomable_chart_widget_resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | horizontal_zoom.png 4 | pan.png 5 | rectangle_zoom.png 6 | vertical_zoom.png 7 | zoom_to_fit_all.png 8 | 9 | 10 | -------------------------------------------------------------------------------- /screenshots/hover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martonmiklos/qt_zoomable_chart_widget/f610926f0c4411dd34c836b1cb7f66ca3e1631eb/screenshots/hover.png -------------------------------------------------------------------------------- /screenshots/promote_to.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martonmiklos/qt_zoomable_chart_widget/f610926f0c4411dd34c836b1cb7f66ca3e1631eb/screenshots/promote_to.png -------------------------------------------------------------------------------- /screenshots/zoom_selector.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martonmiklos/qt_zoomable_chart_widget/f610926f0c4411dd34c836b1cb7f66ca3e1631eb/screenshots/zoom_selector.png -------------------------------------------------------------------------------- /zoomable_chart_widget.pri: -------------------------------------------------------------------------------- 1 | 2 | 3 | FORMS += \ 4 | $$PWD/zoomablechartwidget.ui 5 | 6 | HEADERS += \ 7 | $$PWD/rangelimitedvalueaxis.h \ 8 | $$PWD/zoomablechart.h \ 9 | $$PWD/zoomablechartview.h \ 10 | $$PWD/zoomablechartwidget.h 11 | 12 | SOURCES += \ 13 | $$PWD/rangelimitedvalueaxis.cpp \ 14 | $$PWD/zoomablechart.cpp \ 15 | $$PWD/zoomablechartview.cpp \ 16 | $$PWD/zoomablechartwidget.cpp 17 | 18 | INCLUDEPATH += $$PWD 19 | 20 | RESOURCES += \ 21 | $$PWD/res/zoomable_chart_widget_resources.qrc 22 | -------------------------------------------------------------------------------- /zoomablechart.cpp: -------------------------------------------------------------------------------- 1 | #include "zoomablechart.h" 2 | 3 | ZoomableChart::ZoomableChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : 4 | QChart(parent, wFlags) 5 | { 6 | 7 | } 8 | 9 | void ZoomableChart::addSeries(QAbstractSeries *series) 10 | { 11 | QChart::addSeries(series); 12 | emit seriesAdded(series); 13 | } 14 | 15 | void ZoomableChart::removeSeries(QAbstractSeries *series) 16 | { 17 | QChart::removeSeries(series); 18 | emit seriesRemoved(series); 19 | } 20 | 21 | void ZoomableChart::addAxis(QAbstractAxis *axis, Qt::Alignment alignment) 22 | { 23 | QChart::addAxis(axis, alignment); 24 | #if (QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)) 25 | axis->setLabelsEditable(); 26 | #endif 27 | } 28 | -------------------------------------------------------------------------------- /zoomablechart.h: -------------------------------------------------------------------------------- 1 | #ifndef ZOOMABLECHART_H 2 | #define ZOOMABLECHART_H 3 | 4 | #include 5 | 6 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 7 | QT_CHARTS_USE_NAMESPACE 8 | #endif 9 | 10 | 11 | class ZoomableChart : public QChart 12 | { 13 | Q_OBJECT 14 | public: 15 | ZoomableChart(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = Qt::WindowFlags()); 16 | void addSeries(QAbstractSeries *series); 17 | void removeSeries(QAbstractSeries *series); 18 | void addAxis(QAbstractAxis *axis, Qt::Alignment alignment); 19 | signals: 20 | void seriesAdded(QAbstractSeries *series); 21 | void seriesRemoved(QAbstractSeries *series); 22 | }; 23 | 24 | #endif // ZOOMABLECHART_H 25 | -------------------------------------------------------------------------------- /zoomablechartview.cpp: -------------------------------------------------------------------------------- 1 | #include "zoomablechartview.h" 2 | #include 3 | #include 4 | #include 5 | 6 | #include "rangelimitedvalueaxis.h" 7 | 8 | #include 9 | 10 | ZoomableChartView::ZoomableChartView(QWidget *parent) : 11 | QChartView(parent) 12 | { 13 | setRubberBand(QChartView::RectangleRubberBand); 14 | } 15 | 16 | 17 | ZoomableChartView::ZoomableChartView(QChart *chart, QWidget *parent) : 18 | QChartView(chart,parent) 19 | { 20 | setRubberBand(QChartView::RectangleRubberBand); 21 | } 22 | 23 | void ZoomableChartView::mousePressEvent(QMouseEvent *event) 24 | { 25 | m_isTouching = true; 26 | m_lastMousePos = event->localPos(); 27 | qWarning() << "Press" << m_lastMousePos; 28 | QChartView::mousePressEvent(event); 29 | } 30 | 31 | void ZoomableChartView::mouseMoveEvent(QMouseEvent *event) 32 | { 33 | if (!m_isTouching) 34 | return; 35 | 36 | if (dragMode() == ScrollHandDrag) { 37 | if (event->buttons() & Qt::LeftButton) { 38 | bool moveHorizontalAxis = false; 39 | for (const auto *axis : chart()->axes()) { 40 | if (axis->orientation() == Qt::Horizontal && isAxisTypeZoomableWithMouse(axis->type())) { 41 | moveHorizontalAxis = true; 42 | break; 43 | } 44 | } 45 | 46 | if (QGuiApplication::keyboardModifiers() & Qt::KeyboardModifier::ControlModifier) 47 | moveHorizontalAxis = !moveHorizontalAxis; 48 | 49 | if (moveHorizontalAxis) { 50 | qreal dx = -(event->localPos().x() - m_lastMousePos.x()); 51 | for (auto *series : this->chart()->series()) { 52 | for (const auto *axis : series->attachedAxes()) { 53 | if (axis->orientation() != Qt::Horizontal) 54 | continue; 55 | if (axis->property("rangeLimited").toBool()) { 56 | const auto *rangeLimitedAxis = static_cast(axis); 57 | if (rangeLimitedAxis->orientation() != Qt::Horizontal) 58 | continue; 59 | QPointF oldPoint = getSeriesCoordFromChartCoord(m_lastMousePos, series); 60 | QPointF newPoint = getSeriesCoordFromChartCoord(event->localPos(), series); 61 | qreal dxAxis = -(newPoint.x() - oldPoint.x()); 62 | if (rangeLimitedAxis->isLowerRangeLimited() 63 | && (rangeLimitedAxis->min() + dxAxis) < rangeLimitedAxis->lowerLimit()) { 64 | dxAxis = -(rangeLimitedAxis->min() - rangeLimitedAxis->lowerLimit()); 65 | if (qFuzzyIsNull(dxAxis)) { 66 | dx = 0.0; 67 | } else { 68 | QPointF dummyCoord(oldPoint.x() - dxAxis, m_lastMousePos.y()); 69 | dummyCoord = getChartCoordFromSeriesCoord(dummyCoord, series); 70 | dx = (m_lastMousePos.x() - dummyCoord.x()); 71 | } 72 | } 73 | 74 | 75 | if (rangeLimitedAxis->isUpperRangeLimited() 76 | && (rangeLimitedAxis->max() + dxAxis) > rangeLimitedAxis->upperLimit()) { 77 | dxAxis = -(rangeLimitedAxis->upperLimit() - rangeLimitedAxis->max()); 78 | if (qFuzzyIsNull(dxAxis)) { 79 | dx = 0.0; 80 | } else { 81 | QPointF dummyCoord(oldPoint.x() - dxAxis, m_lastMousePos.y()); 82 | dummyCoord = getChartCoordFromSeriesCoord(dummyCoord, series); 83 | dx = -(m_lastMousePos.x() - dummyCoord.x()); 84 | } 85 | } 86 | } 87 | } 88 | } 89 | chart()->scroll(dx, 0); 90 | } else { 91 | qreal dy = event->pos().y() - m_lastMousePos.y(); 92 | for (const auto series : this->chart()->series()) { 93 | for (const auto axis : series->attachedAxes()) { 94 | if (axis->orientation() != Qt::Vertical) 95 | continue; 96 | if (axis->property("rangeLimited").toBool()) { 97 | RangeLimitedValueAxis *rangeLimitedAxis = static_cast(axis); 98 | if (rangeLimitedAxis->orientation() != Qt::Vertical) 99 | continue; 100 | QPointF oldPoint = getSeriesCoordFromChartCoord(m_lastMousePos, series); 101 | QPointF newPoint = getSeriesCoordFromChartCoord(event->localPos(), series); 102 | qreal dyAxis = -(newPoint.y() - oldPoint.y()); 103 | if (rangeLimitedAxis->isLowerRangeLimited() 104 | && (rangeLimitedAxis->min() + dyAxis) < rangeLimitedAxis->lowerLimit()) { 105 | dyAxis = rangeLimitedAxis->min() - rangeLimitedAxis->lowerLimit(); 106 | if (qFuzzyIsNull(dyAxis)) { 107 | dy = 0.0; 108 | } else { 109 | QPointF dummyCoord(m_lastMousePos.x(), oldPoint.y() - dyAxis); 110 | dummyCoord = getChartCoordFromSeriesCoord(dummyCoord, series); 111 | dy = (m_lastMousePos.y() - dummyCoord.y()); 112 | } 113 | } 114 | 115 | if (rangeLimitedAxis->isUpperRangeLimited() 116 | && (rangeLimitedAxis->max() + dyAxis) > rangeLimitedAxis->upperLimit()) { 117 | dyAxis = rangeLimitedAxis->upperLimit() - rangeLimitedAxis->max(); 118 | if (qFuzzyIsNull(dyAxis)) { 119 | dy = 0.0; 120 | } else { 121 | QPointF dummyCoord(m_lastMousePos.x(), oldPoint.y() - dyAxis); 122 | dummyCoord = getChartCoordFromSeriesCoord(dummyCoord, series); 123 | dy = -(m_lastMousePos.y() - dummyCoord.y()); 124 | } 125 | } 126 | } 127 | } 128 | } 129 | chart()->scroll(0, dy); 130 | } 131 | } 132 | m_lastMousePos = event->pos(); 133 | } 134 | 135 | QChartView::mouseMoveEvent(event); 136 | } 137 | 138 | void ZoomableChartView::wheelEvent(QWheelEvent *event) 139 | { 140 | bool zoomHorizontalAxis = false; 141 | for (auto axis : chart()->axes()) { 142 | if (axis->orientation() == Qt::Horizontal && isAxisTypeZoomableWithMouse(axis->type())) { 143 | zoomHorizontalAxis = true; 144 | break; 145 | } 146 | } 147 | 148 | if (QGuiApplication::keyboardModifiers() & Qt::KeyboardModifier::ControlModifier) 149 | zoomHorizontalAxis = !zoomHorizontalAxis; 150 | 151 | if (zoomHorizontalAxis) { 152 | if (event->angleDelta().y() > 0) { 153 | zoomX(2, event->position().x() - chart()->plotArea().x()); 154 | } else if (event->angleDelta().y() < 0) { 155 | zoomX(0.5, event->position().x() - chart()->plotArea().x()); 156 | } 157 | } else { 158 | if (event->angleDelta().y() > 0) { 159 | zoomY(2, event->position().y() - chart()->plotArea().y()); 160 | } else if (event->angleDelta().y() < 0) { 161 | zoomY(0.5, event->position().y() - chart()->plotArea().y()); 162 | } 163 | } 164 | } 165 | 166 | bool ZoomableChartView::isAxisTypeZoomableWithMouse(const QAbstractAxis::AxisType type) 167 | { 168 | return (type == QAbstractAxis::AxisTypeValue 169 | || type == QAbstractAxis::AxisTypeLogValue 170 | || type == QAbstractAxis::AxisTypeDateTime); 171 | } 172 | 173 | QPointF ZoomableChartView::getSeriesCoordFromChartCoord(const QPointF &chartPos, QAbstractSeries *series) const 174 | { 175 | auto const chartItemPos = chart()->mapFromScene(chartPos); 176 | auto const valueGivenSeries = chart()->mapToValue(chartItemPos, series); 177 | return valueGivenSeries; 178 | } 179 | 180 | QPointF ZoomableChartView::getChartCoordFromSeriesCoord(const QPointF &seriesPos, QAbstractSeries *series) const 181 | { 182 | QPointF ret = chart()->mapToPosition(seriesPos, series); 183 | ret = chart()->mapFromScene(ret); 184 | return ret; 185 | } 186 | 187 | ZoomableChartView::ZoomMode ZoomableChartView::zoomMode() const 188 | { 189 | return m_zoomMode; 190 | } 191 | 192 | void ZoomableChartView::setZoomMode(const ZoomMode &zoomMode) 193 | { 194 | if (m_zoomMode != zoomMode) { 195 | m_zoomMode = zoomMode; 196 | switch (zoomMode) { 197 | case Pan: 198 | setRubberBand(QChartView::NoRubberBand); 199 | setDragMode(QChartView::ScrollHandDrag); 200 | break; 201 | case RectangleZoom: 202 | setRubberBand(QChartView::RectangleRubberBand); 203 | setDragMode(QChartView::NoDrag); 204 | break; 205 | case HorizontalZoom: 206 | setRubberBand(QChartView::HorizontalRubberBand); 207 | setDragMode(QChartView::NoDrag); 208 | break; 209 | case VerticalZoom: 210 | setRubberBand(QChartView::VerticalRubberBand); 211 | setDragMode(QChartView::NoDrag); 212 | break; 213 | } 214 | } 215 | } 216 | 217 | void ZoomableChartView::zoomX(qreal factor, qreal xcenter) 218 | { 219 | QRectF rect = chart()->plotArea(); 220 | qreal widthOriginal = rect.width(); 221 | rect.setWidth(widthOriginal / factor); 222 | qreal centerScale = (xcenter / widthOriginal); 223 | 224 | qreal leftOffset = (xcenter - (rect.width() * centerScale) ); 225 | 226 | rect.moveLeft(rect.x() + leftOffset); 227 | chart()->zoomIn(rect); 228 | } 229 | 230 | void ZoomableChartView::zoomX(qreal factor) 231 | { 232 | QRectF rect = chart()->plotArea(); 233 | qreal widthOriginal = rect.width(); 234 | QPointF center_orig = rect.center(); 235 | 236 | rect.setWidth(widthOriginal / factor); 237 | 238 | rect.moveCenter(center_orig); 239 | 240 | chart()->zoomIn(rect); 241 | } 242 | 243 | void ZoomableChartView::zoomY(qreal factor, qreal ycenter) 244 | { 245 | QRectF rect = chart()->plotArea(); 246 | qreal heightOriginal = rect.height(); 247 | rect.setHeight(heightOriginal / factor); 248 | qreal centerScale = (ycenter / heightOriginal); 249 | 250 | qreal topOffset = (ycenter - (rect.height() * centerScale) ); 251 | 252 | rect.moveTop(rect.x() + topOffset); 253 | chart()->zoomIn(rect); 254 | } 255 | 256 | void ZoomableChartView::zoomY(qreal factor) 257 | { 258 | QRectF rect = chart()->plotArea(); 259 | qreal heightOriginal = rect.height(); 260 | QPointF center_orig = rect.center(); 261 | 262 | rect.setHeight(heightOriginal / factor); 263 | 264 | rect.moveCenter(center_orig); 265 | 266 | chart()->zoomIn(rect); 267 | } 268 | 269 | 270 | void ZoomableChartView::mouseReleaseEvent(QMouseEvent *event) 271 | { 272 | m_isTouching = false; 273 | QChartView::mouseReleaseEvent(event); 274 | } 275 | 276 | //![1] 277 | void ZoomableChartView::keyPressEvent(QKeyEvent *event) 278 | { 279 | auto resetZoom = [this]()-> void 280 | { 281 | // return if empty 282 | if(chart()->series().empty()) return; 283 | 284 | auto seriesAbstract = chart()->series().at(0); 285 | auto xySeries = dynamic_cast(seriesAbstract); 286 | if(xySeries) 287 | { 288 | auto const points = xySeries->points(); 289 | auto [point_minX, point_maxX] = std::minmax_element(points.begin(), points.end(), 290 | [](const QPointF p1, const QPointF p2){ 291 | return p1.x() < p2.y(); 292 | }); 293 | 294 | auto [point_minY, point_maxY] = std::minmax_element(points.begin(), points.end(), 295 | [](const QPointF p1, const QPointF p2){ 296 | return p1.y() < p2.y(); 297 | }); 298 | 299 | chart()->axes(Qt::Horizontal).first()->setRange(point_minX->x() , point_maxX->x()); 300 | chart()->axes(Qt::Vertical).first()->setRange(point_minY->y(), point_maxY->y()); 301 | } 302 | }; 303 | 304 | switch (event->key()) { 305 | case Qt::Key_Plus: 306 | chart()->zoomIn(); 307 | break; 308 | case Qt::Key_Minus: 309 | chart()->zoomOut(); 310 | break; 311 | //![1] 312 | case Qt::Key_Left: 313 | chart()->scroll(-10, 0); 314 | break; 315 | case Qt::Key_Right: 316 | chart()->scroll(10, 0); 317 | break; 318 | case Qt::Key_Up: 319 | chart()->scroll(0, 10); 320 | break; 321 | case Qt::Key_Down: 322 | chart()->scroll(0, -10); 323 | break; 324 | case Qt::Key_Question: 325 | resetZoom(); 326 | break; 327 | default: 328 | QGraphicsView::keyPressEvent(event); 329 | break; 330 | } 331 | } 332 | -------------------------------------------------------------------------------- /zoomablechartview.h: -------------------------------------------------------------------------------- 1 | #ifndef CHARTVIEW_H 2 | #define CHARTVIEW_H 3 | 4 | #include 5 | #include 6 | 7 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 8 | QT_CHARTS_USE_NAMESPACE 9 | #endif 10 | 11 | //![1] 12 | class ZoomableChartView : public QChartView 13 | //![1] 14 | { 15 | public: 16 | enum ZoomMode { 17 | Pan, 18 | RectangleZoom, 19 | VerticalZoom, 20 | HorizontalZoom 21 | }; 22 | 23 | ZoomableChartView(QWidget *parent = 0); 24 | ZoomableChartView(QChart *chart, QWidget *parent = nullptr); 25 | 26 | void zoomX(qreal factor, qreal xcenter); 27 | void zoomX(qreal factor); 28 | 29 | void zoomY(qreal factor, qreal ycenter); 30 | void zoomY(qreal factor); 31 | 32 | //![2] 33 | ZoomMode zoomMode() const; 34 | void setZoomMode(const ZoomMode &zoomMode); 35 | 36 | 37 | protected: 38 | void mousePressEvent(QMouseEvent *event); 39 | void mouseMoveEvent(QMouseEvent *event); 40 | void mouseReleaseEvent(QMouseEvent *event); 41 | void keyPressEvent(QKeyEvent *event); 42 | void wheelEvent(QWheelEvent *event); 43 | //![2] 44 | 45 | private: 46 | 47 | bool m_isTouching = false; 48 | QPointF m_lastMousePos; 49 | ZoomMode m_zoomMode = RectangleZoom; 50 | 51 | static bool isAxisTypeZoomableWithMouse(const QAbstractAxis::AxisType type); 52 | QPointF getSeriesCoordFromChartCoord(const QPointF & mousePos, QAbstractSeries *series) const; 53 | QPointF getChartCoordFromSeriesCoord(const QPointF & seriesPos, QAbstractSeries *series) const; 54 | }; 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /zoomablechartwidget.cpp: -------------------------------------------------------------------------------- 1 | #include "zoomablechartwidget.h" 2 | #include "ui_zoomablechartwidget.h" 3 | 4 | #include 5 | #include 6 | 7 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 8 | QT_CHARTS_USE_NAMESPACE 9 | #endif 10 | 11 | 12 | 13 | ZoomableChartWidget::ZoomableChartWidget(QWidget *parent) : 14 | QWidget(parent), 15 | ui(new Ui::ZoomableChartWidget) 16 | { 17 | ui->setupUi(this); 18 | 19 | QIcon panIcon; 20 | panIcon.addFile(QStringLiteral(":/icons/pan.png"), QSize(), QIcon::Normal, QIcon::Off); 21 | ui->comboBoxZoomMode->addItem(panIcon, QString(), ZoomableChartView::Pan); 22 | 23 | QIcon rectZoomIcon; 24 | rectZoomIcon.addFile(QStringLiteral(":/icons/rectangle_zoom.png"), QSize(), QIcon::Normal, QIcon::Off); 25 | ui->comboBoxZoomMode->addItem(rectZoomIcon, QString(), ZoomableChartView::RectangleZoom); 26 | 27 | QIcon vZoomIcon; 28 | vZoomIcon.addFile(QStringLiteral(":/icons/vertical_zoom.png"), QSize(), QIcon::Normal, QIcon::Off); 29 | ui->comboBoxZoomMode->addItem(vZoomIcon, QString(), ZoomableChartView::VerticalZoom); 30 | 31 | QIcon hZoomIcon; 32 | hZoomIcon.addFile(QStringLiteral(":/icons/horizontal_zoom.png"), QSize(), QIcon::Normal, QIcon::Off); 33 | ui->comboBoxZoomMode->addItem(hZoomIcon, QString(), ZoomableChartView::HorizontalZoom); 34 | 35 | ui->chartView->setZoomMode(ZoomableChartView::Pan); 36 | 37 | m_chart = new ZoomableChart(); 38 | ui->chartView->setChart(m_chart); 39 | connect(m_chart, &ZoomableChart::seriesAdded, this, &ZoomableChartWidget::seriesAdded); 40 | connect(m_chart, &ZoomableChart::seriesRemoved, this, &ZoomableChartWidget::seriesRemoved); 41 | } 42 | 43 | ZoomableChartWidget::~ZoomableChartWidget() 44 | { 45 | delete m_chart; 46 | delete ui; 47 | } 48 | 49 | void ZoomableChartWidget::on_comboBoxZoomMode_activated(int index) 50 | { 51 | ui->chartView->setZoomMode(static_cast(ui->comboBoxZoomMode->itemData(index).toInt())); 52 | } 53 | 54 | void ZoomableChartWidget::legendMarkerClicked() 55 | { 56 | static QLegendMarker *m_lastClickedMarker = nullptr; 57 | auto* marker = qobject_cast(sender()); 58 | Q_ASSERT(marker); 59 | 60 | // Toggle visibility of series 61 | setSeriesVisible(marker->series(), !marker->series()->isVisible()); 62 | if (m_lastClickedMarker 63 | && (m_lastClickedMarker->series()->isVisible() == marker->series()->isVisible()) 64 | && (QApplication::keyboardModifiers() & Qt::ShiftModifier)) { 65 | auto markers = m_chart->legend()->markers(); 66 | int startIndex = markers.indexOf(marker); 67 | int lastIndex = markers.indexOf(m_lastClickedMarker); 68 | if (startIndex != -1 && lastIndex != -1) { 69 | if (startIndex > lastIndex) { 70 | auto tmp = lastIndex; 71 | lastIndex = startIndex; 72 | startIndex = tmp; 73 | } 74 | 75 | for (int i = (startIndex + 1); iseries(), !markers[i]->series()->isVisible()); 77 | } 78 | } 79 | } 80 | m_lastClickedMarker = marker; 81 | connect(m_lastClickedMarker, &QLegendMarker::destroyed, this, [this]() {m_lastClickedMarker = nullptr;}); 82 | } 83 | 84 | void ZoomableChartWidget::legendMarkerHovered(bool hover) 85 | { 86 | auto* marker = qobject_cast(sender()); 87 | Q_ASSERT(marker); 88 | 89 | QFont font = marker->font(); 90 | font.setBold(hover); 91 | marker->setFont(font); 92 | 93 | if (marker->series()->type() == QAbstractSeries::SeriesTypeLine) { 94 | auto series = qobject_cast(marker->series()); 95 | auto pen = series->pen(); 96 | pen.setWidth(hover ? (pen.width() * 2) : (pen.width() / 2)); 97 | series->setPen(pen); 98 | } 99 | } 100 | 101 | ZoomableChart *ZoomableChartWidget::chart() const 102 | { 103 | return m_chart; 104 | } 105 | 106 | ZoomableChartView *ZoomableChartWidget::chartView() const 107 | { 108 | return ui->chartView; 109 | } 110 | 111 | void ZoomableChartWidget::addToolWidget(QWidget *widget) 112 | { 113 | ui->horizontalLayoutToolbar->insertWidget(1, widget); 114 | } 115 | 116 | void ZoomableChartWidget::setSeriesVisible(QAbstractSeries *series, bool visible) 117 | { 118 | series->setVisible(visible); 119 | const auto markers = m_chart->legend()->markers(series); 120 | for (auto *marker : markers) { 121 | // Turn legend marker back to visible, since hiding series also hides the marker 122 | // and we don't want it to happen now. 123 | marker->setVisible(true); 124 | 125 | // Dim the marker, if series is not visible 126 | qreal alpha = visible ? 1.0 : 0.5; 127 | QColor color; 128 | QBrush brush = marker->labelBrush(); 129 | color = brush.color(); 130 | color.setAlphaF(alpha); 131 | brush.setColor(color); 132 | marker->setLabelBrush(brush); 133 | 134 | brush = marker->brush(); 135 | color = brush.color(); 136 | color.setAlphaF(alpha); 137 | brush.setColor(color); 138 | marker->setBrush(brush); 139 | 140 | QPen pen = marker->pen(); 141 | color = pen.color(); 142 | color.setAlphaF(alpha); 143 | pen.setColor(color); 144 | marker->setPen(pen); 145 | } 146 | 147 | for (QAbstractAxis *axis : m_chart->axes(Qt::Vertical)) { 148 | bool hideAxis = true; 149 | for (auto *series : m_chart->series()) { 150 | for (const auto *attachedAxis : series->attachedAxes()) { 151 | if (series->isVisible() && attachedAxis == axis) { 152 | hideAxis = false; 153 | break; 154 | } 155 | } 156 | if (!hideAxis) 157 | break; 158 | } 159 | axis->setVisible(!hideAxis); 160 | } 161 | } 162 | 163 | void ZoomableChartWidget::seriesAdded(QAbstractSeries *series) 164 | { 165 | // Connect all markers to handler 166 | const auto markers = m_chart->legend()->markers(series); 167 | for (auto marker : markers) { 168 | QObject::connect(marker, &QLegendMarker::clicked, 169 | this, &ZoomableChartWidget::legendMarkerClicked); 170 | QObject::connect(marker, &QLegendMarker::hovered, 171 | this, &ZoomableChartWidget::legendMarkerHovered); 172 | } 173 | } 174 | 175 | void ZoomableChartWidget::seriesRemoved(QAbstractSeries *series) 176 | { 177 | // Connect all markers to handler 178 | const auto markers = m_chart->legend()->markers(series); 179 | for (auto marker : markers) { 180 | QObject::disconnect(marker, &QLegendMarker::clicked, 181 | this, &ZoomableChartWidget::legendMarkerClicked); 182 | QObject::disconnect(marker, &QLegendMarker::hovered, 183 | this, &ZoomableChartWidget::legendMarkerHovered); 184 | } 185 | } 186 | 187 | 188 | void ZoomableChartWidget::on_toolButtonFitInView_clicked() 189 | { 190 | QHash xmin, xmax, ymin, ymax; 191 | // loop on all axes (both vetival and horizontal) 192 | for (const QAbstractAxis *axis : m_chart->axes()) { 193 | // only QValueAxis supported at the moment 194 | if (axis->type() != QAbstractAxis::AxisTypeValue) 195 | continue; 196 | 197 | // loop on all series attached to the axes and look for each min max point 198 | for (auto *series : m_chart->series()) { 199 | // at the moment only QLineSeries is supported for zoom to fit 200 | if (series->type() != QAbstractSeries::SeriesTypeLine) 201 | continue; 202 | 203 | if (!xmin.contains(axis)) 204 | xmin[axis] = std::numeric_limits::max(); 205 | if (!xmax.contains(axis)) 206 | xmax[axis] = -std::numeric_limits::max(); 207 | if (!ymin.contains(axis)) 208 | ymin[axis] = std::numeric_limits::max(); 209 | if (!ymax.contains(axis)) 210 | ymax[axis] = -std::numeric_limits::max(); 211 | 212 | for (const auto *attachedAxis : series->attachedAxes()) { 213 | if (series->isVisible() && attachedAxis == axis) { 214 | auto lineSeries = static_cast(series); 215 | for (const auto pt : lineSeries->points()) { 216 | if (pt.x() < xmin[axis]) 217 | xmin[axis] = pt.x(); 218 | if (pt.x() > xmax[axis]) 219 | xmax[axis] = pt.x(); 220 | if (pt.y() < ymin[axis]) 221 | ymin[axis] = pt.y(); 222 | if (pt.y() > ymax[axis]) 223 | ymax[axis] = pt.y(); 224 | } 225 | break; 226 | } 227 | } 228 | } 229 | } 230 | 231 | for (QAbstractAxis *axis : m_chart->axes()) { 232 | if (axis->type() != QAbstractAxis::AxisTypeValue) 233 | continue; 234 | if (axis->alignment() == Qt::Horizontal) { 235 | if (xmin.contains(axis)) 236 | axis->setMin(xmin[axis]); 237 | 238 | if (xmax.contains(axis)) 239 | axis->setMax(xmax[axis]); 240 | } else if (axis->alignment() == Qt::Vertical) { 241 | if (ymin.contains(axis)) 242 | axis->setMin(ymin[axis]); 243 | 244 | if (ymax.contains(axis)) 245 | axis->setMax(ymax[axis]); 246 | } 247 | } 248 | } 249 | -------------------------------------------------------------------------------- /zoomablechartwidget.h: -------------------------------------------------------------------------------- 1 | #ifndef ZOOMABLECHARTWIDGET_H 2 | #define ZOOMABLECHARTWIDGET_H 3 | 4 | #include "zoomablechart.h" 5 | #include "zoomablechartview.h" 6 | 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 14 | QT_CHARTS_USE_NAMESPACE 15 | #endif 16 | 17 | namespace Ui { 18 | class ZoomableChartWidget; 19 | } 20 | 21 | class ZoomableChartWidget : public QWidget 22 | { 23 | Q_OBJECT 24 | 25 | public: 26 | explicit ZoomableChartWidget(QWidget *parent = nullptr); 27 | ~ZoomableChartWidget(); 28 | 29 | ZoomableChart *chart() const; 30 | ZoomableChartView *chartView() const; 31 | 32 | void addToolWidget(QWidget *widget); 33 | 34 | public slots: 35 | void setSeriesVisible(QAbstractSeries *series, bool visible = true); 36 | 37 | private slots: 38 | void on_comboBoxZoomMode_activated(int index); 39 | void legendMarkerClicked(); 40 | void legendMarkerHovered(bool hover); 41 | void seriesAdded(QAbstractSeries *series); 42 | void seriesRemoved(QAbstractSeries *series); 43 | void on_toolButtonFitInView_clicked(); 44 | 45 | private: 46 | Ui::ZoomableChartWidget *ui; 47 | ZoomableChart *m_chart = nullptr; 48 | }; 49 | 50 | #endif // ZOOMABLECHARTWIDGET_H 51 | -------------------------------------------------------------------------------- /zoomablechartwidget.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | ZoomableChartWidget 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 300 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | Qt::Horizontal 23 | 24 | 25 | 26 | 40 27 | 20 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | Adjust zoom to fit all 36 | 37 | 38 | ... 39 | 40 | 41 | 42 | :/icons/zoom_to_fit_all.png:/icons/zoom_to_fit_all.png 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 48 51 | 16777215 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | ZoomableChartView 66 | QGraphicsView 67 |
zoomablechartview.h
68 |
69 |
70 | 71 | 72 | 73 | 74 |
75 | --------------------------------------------------------------------------------