|
| 1 | +import QtQuick 2.15 |
| 2 | +import QtQuick.Layouts 1.15 |
| 3 | +import QtQuick.Window 2.15 |
| 4 | +import QtQuick.Controls 2.15 |
| 5 | +import FluentUI 1.0 |
| 6 | +import example 1.0 |
| 7 | +import "../component" |
| 8 | + |
| 9 | +FluPage{ |
| 10 | + title: qsTr("QCustomPlot2") |
| 11 | + RealTimePlot { |
| 12 | + anchors.fill: parent // 关键:给容器确定尺寸 |
| 13 | + |
| 14 | + id: realtimePlot |
| 15 | + xAxis.visible: true |
| 16 | + yAxis.visible: true |
| 17 | + |
| 18 | + xAxis.ticker.tickCount: 2 |
| 19 | + xAxis.ticker.ticks: true |
| 20 | + xAxis.ticker.subTicks: false |
| 21 | + yAxis.ticker.tickCount: 10 |
| 22 | + yAxis.ticker.ticks: true |
| 23 | + yAxis.ticker.subTicks: false |
| 24 | + |
| 25 | + backgroundColor: FluTheme.dark ? Qt.rgba(39/255, 39/255, 39/255, 1) : "white" |
| 26 | + baseColor: FluTheme.dark ? "white" : Qt.rgba(39/255, 39/255, 39/255, 1) |
| 27 | + labelColor: FluTheme.dark ? "white" : Qt.rgba(39/255, 39/255, 39/255, 1) |
| 28 | + maxBufferPoints: 20000 |
| 29 | + refreshMs: 30 |
| 30 | + initialXRange : ({"lower":0, "upper":1.0}) |
| 31 | + initialYRange : ({"lower":-2, "upper":2}) |
| 32 | + Component.onCompleted: { |
| 33 | + realtimePlot.addGraph("main"); |
| 34 | + } |
| 35 | + |
| 36 | + Component.onDestruction: { |
| 37 | + |
| 38 | + } |
| 39 | + // 鼠标滚轮缩放 |
| 40 | + MouseArea { |
| 41 | + id: rtMouse |
| 42 | + anchors.fill: parent |
| 43 | + hoverEnabled: true |
| 44 | + property double posX : 0 |
| 45 | + property double posY : 0 |
| 46 | + // 框选缩放相关 |
| 47 | + property bool dragging: false |
| 48 | + property real startX: 0 |
| 49 | + property real startY: 0 |
| 50 | + property real curX: 0 |
| 51 | + property real curY: 0 |
| 52 | + |
| 53 | + onPositionChanged: function(mouse) { |
| 54 | + // console.log("mouse:", mouse.x, mouse.y) |
| 55 | + posX = mouse.x |
| 56 | + posY = mouse.y |
| 57 | + if (dragging) { |
| 58 | + curX = mouse.x |
| 59 | + curY = mouse.y |
| 60 | + } |
| 61 | + } |
| 62 | + onPressed: function(mouse) { |
| 63 | + if (mouse.button === Qt.LeftButton) { |
| 64 | + dragging = true |
| 65 | + startX = mouse.x |
| 66 | + startY = mouse.y |
| 67 | + curX = mouse.x |
| 68 | + curY = mouse.y |
| 69 | + } |
| 70 | + } |
| 71 | + onReleased: function(mouse) { |
| 72 | + if (dragging && mouse.button === Qt.LeftButton) { |
| 73 | + dragging = false |
| 74 | + // 计算选择框 |
| 75 | + var x1 = Math.min(startX, curX) |
| 76 | + var y1 = Math.min(startY, curY) |
| 77 | + var x2 = Math.max(startX, curX) |
| 78 | + var y2 = Math.max(startY, curY) |
| 79 | + var selW = Math.abs(x2 - x1) |
| 80 | + var selH = Math.abs(y2 - y1) |
| 81 | + // 阈值,避免误触 |
| 82 | + if (selW > 8 && selH > 8) { |
| 83 | + // 将像素坐标转换为数据坐标 |
| 84 | + realtimePlot.setRangeByPixels(x1, y1, x2, y2) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + onWheel: function(wheel){ |
| 89 | + let delta = wheel.angleDelta.y; |
| 90 | + if (wheel.modifiers & Qt.ControlModifier) { |
| 91 | + realtimePlot.zoomXY(posX, posY, delta > 0 ? 0.9 : 1.1, true); |
| 92 | + } else { |
| 93 | + realtimePlot.moveY( delta > 0 ? -0.05 : 0.05); |
| 94 | + } |
| 95 | + wheel.accepted = true; |
| 96 | + } |
| 97 | + onDoubleClicked: function(mouse) { |
| 98 | + if (mouse.button === Qt.LeftButton) { |
| 99 | + realtimePlot.resetRange(); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + // 框选可视化选区 |
| 104 | + Rectangle { |
| 105 | + id: realtimeSelection |
| 106 | + x: Math.min(rtMouse.startX, rtMouse.curX) |
| 107 | + y: Math.min(rtMouse.startY, rtMouse.curY) |
| 108 | + width: Math.abs(rtMouse.curX - rtMouse.startX) |
| 109 | + height: Math.abs(rtMouse.curY - rtMouse.startY) |
| 110 | + visible: rtMouse.dragging && width > 4 && height > 4 |
| 111 | + color: Qt.rgba(0, 120/255, 215/255, 0.15) // Windows 风格选区蓝 |
| 112 | + border.color: Qt.rgba(0, 120/255, 215/255, 0.8) |
| 113 | + border.width: 1 |
| 114 | + z: 10 |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + DataGenerator { |
| 119 | + id: dataGenerator |
| 120 | + Component.onCompleted: { |
| 121 | + dataGenerator.start(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // 使用数据的示例 |
| 126 | + Timer { |
| 127 | + interval: 50 |
| 128 | + running: true |
| 129 | + repeat: true |
| 130 | + onTriggered: { |
| 131 | + // 获取最新数据 |
| 132 | + var xData = dataGenerator.xData |
| 133 | + var yData = dataGenerator.yData |
| 134 | + realtimePlot.appendBatch(xData, yData); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments