|
| 1 | +import QtQuick |
| 2 | +import QtQuick.Controls |
| 3 | +import QtCharts |
| 4 | + |
| 5 | +import EasyApp.Gui.Animations as EaAnimations |
| 6 | +import EasyApp.Gui.Style as EaStyle |
| 7 | +import EasyApp.Gui.Charts as EaCharts |
| 8 | + |
| 9 | + |
| 10 | +ChartView { |
| 11 | + id: chartView |
| 12 | + |
| 13 | + property alias axisX: axisX |
| 14 | + property alias axisY: axisY |
| 15 | + |
| 16 | + property bool useOpenGL: false |
| 17 | + property bool allowZoom: true |
| 18 | + property bool allowHover: true |
| 19 | + |
| 20 | + anchors.top: parent.top |
| 21 | + anchors.bottom: parent.bottom |
| 22 | + anchors.left: parent.left |
| 23 | + anchors.right: parent.right |
| 24 | + anchors.margins: -12 // Reset default margins |
| 25 | + |
| 26 | + antialiasing: true |
| 27 | + |
| 28 | + legend.visible: false |
| 29 | + legend.alignment: Qt.AlignBottom |
| 30 | + legend.font.family: EaStyle.Fonts.fontFamily |
| 31 | + legend.font.pixelSize: EaStyle.Sizes.fontPixelSize |
| 32 | + legend.markerShape: Legend.MarkerShapeRectangle |
| 33 | + legend.labelColor: EaStyle.Colors.chartForeground |
| 34 | + Behavior on legend.labelColor { EaAnimations.ThemeChange {} } |
| 35 | + |
| 36 | + backgroundRoundness: 0 |
| 37 | + backgroundColor: EaStyle.Colors.chartBackground |
| 38 | + Behavior on backgroundColor { EaAnimations.ThemeChange {} } |
| 39 | + |
| 40 | + titleFont.family: EaStyle.Fonts.fontFamily |
| 41 | + titleFont.pixelSize: EaStyle.Sizes.fontPixelSize |
| 42 | + titleFont.bold: true |
| 43 | + titleColor: EaStyle.Colors.chartForeground |
| 44 | + /* BREAKS ANIMATION ! |
| 45 | + Behavior on titleColor { Animations.ThemeChange {} } |
| 46 | + */ |
| 47 | + |
| 48 | + plotAreaColor: EaStyle.Colors.chartPlotAreaBackground |
| 49 | + Behavior on plotAreaColor { EaAnimations.ThemeChange {} } |
| 50 | + |
| 51 | + animationOptions: ChartView.SeriesAnimations |
| 52 | + animationDuration: EaStyle.Times.chartAnimation |
| 53 | + |
| 54 | + // X-axis |
| 55 | + EaCharts.QtCharts1dValueAxis { |
| 56 | + id: axisX |
| 57 | + } |
| 58 | + |
| 59 | + // Y-axis |
| 60 | + EaCharts.QtCharts1dLogValueAxis { |
| 61 | + id: axisY |
| 62 | + } |
| 63 | + |
| 64 | + // Zoom rectangle |
| 65 | + Rectangle{ |
| 66 | + id: recZoom |
| 67 | + |
| 68 | + property int xScaleZoom: 0 |
| 69 | + property int yScaleZoom: 0 |
| 70 | + |
| 71 | + visible: false |
| 72 | + transform: Scale { |
| 73 | + origin.x: 0 |
| 74 | + origin.y: 0 |
| 75 | + xScale: recZoom.xScaleZoom |
| 76 | + yScale: recZoom.yScaleZoom |
| 77 | + } |
| 78 | + border.color: EaStyle.Colors.appBorder |
| 79 | + border.width: 1 |
| 80 | + opacity: 0.9 |
| 81 | + color: "transparent" |
| 82 | + |
| 83 | + Rectangle { |
| 84 | + anchors.fill: parent |
| 85 | + opacity: 0.5 |
| 86 | + color: recZoom.border.color |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Zoom with left mouse button |
| 91 | + MouseArea { |
| 92 | + id: zoomMouseArea |
| 93 | + |
| 94 | + enabled: allowZoom |
| 95 | + anchors.fill: chartView |
| 96 | + acceptedButtons: Qt.LeftButton |
| 97 | + onPressed: { |
| 98 | + recZoom.x = mouseX |
| 99 | + recZoom.y = mouseY |
| 100 | + recZoom.visible = true |
| 101 | + } |
| 102 | + onMouseXChanged: { |
| 103 | + if (mouseX > recZoom.x) { |
| 104 | + recZoom.xScaleZoom = 1 |
| 105 | + recZoom.width = Math.min(mouseX, chartView.width) - recZoom.x |
| 106 | + } else { |
| 107 | + recZoom.xScaleZoom = -1 |
| 108 | + recZoom.width = recZoom.x - Math.max(mouseX, 0) |
| 109 | + } |
| 110 | + } |
| 111 | + onMouseYChanged: { |
| 112 | + if (mouseY > recZoom.y) { |
| 113 | + recZoom.yScaleZoom = 1 |
| 114 | + recZoom.height = Math.min(mouseY, chartView.height) - recZoom.y |
| 115 | + } else { |
| 116 | + recZoom.yScaleZoom = -1 |
| 117 | + recZoom.height = recZoom.y - Math.max(mouseY, 0) |
| 118 | + } |
| 119 | + } |
| 120 | + onReleased: { |
| 121 | + const x = Math.min(recZoom.x, mouseX) - chartView.anchors.leftMargin |
| 122 | + const y = Math.min(recZoom.y, mouseY) - chartView.anchors.topMargin |
| 123 | + const width = recZoom.width |
| 124 | + const height = recZoom.height |
| 125 | + chartView.zoomIn(Qt.rect(x, y, width, height)) |
| 126 | + recZoom.visible = false |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Pan with left mouse button |
| 131 | + MouseArea { |
| 132 | + property real pressedX |
| 133 | + property real pressedY |
| 134 | + property int threshold: 1 |
| 135 | + |
| 136 | + enabled: !zoomMouseArea.enabled |
| 137 | + anchors.fill: chartView |
| 138 | + acceptedButtons: Qt.LeftButton |
| 139 | + onPressed: { |
| 140 | + pressedX = mouseX |
| 141 | + pressedY = mouseY |
| 142 | + } |
| 143 | + onMouseXChanged: Qt.callLater(update) |
| 144 | + onMouseYChanged: Qt.callLater(update) |
| 145 | + |
| 146 | + function update() { |
| 147 | + const dx = mouseX - pressedX |
| 148 | + const dy = mouseY - pressedY |
| 149 | + pressedX = mouseX |
| 150 | + pressedY = mouseY |
| 151 | + |
| 152 | + if (dx > threshold) |
| 153 | + chartView.scrollLeft(dx) |
| 154 | + else if (dx < -threshold) |
| 155 | + chartView.scrollRight(-dx) |
| 156 | + if (dy > threshold) |
| 157 | + chartView.scrollUp(dy) |
| 158 | + else if (dy < -threshold) |
| 159 | + chartView.scrollDown(-dy) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + // Reset axes with right mouse button |
| 164 | + MouseArea { |
| 165 | + anchors.fill: chartView |
| 166 | + acceptedButtons: Qt.RightButton |
| 167 | + onClicked: resetAxes() |
| 168 | + } |
| 169 | + |
| 170 | + // Logic |
| 171 | + |
| 172 | + function resetAxes() { |
| 173 | + //chartView.zoomReset() |
| 174 | + axisX.min = axisX.minAfterReset |
| 175 | + axisX.max = axisX.maxAfterReset |
| 176 | + axisY.min = axisY.minAfterReset |
| 177 | + axisY.max = axisY.maxAfterReset |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments