| 1 |
// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
// Copyright 2019 Juan Palacios <[email protected]> |
| 3 |
|
| 4 |
import QtQuick 2.15 |
| 5 |
import QtQuick.Controls 2.15 |
| 6 |
import QtCharts 2.15 |
| 7 |
import "Style.js" as Style |
| 8 |
|
| 9 |
ChartView { |
| 10 |
property real minXDistance: 1.0 |
| 11 |
property bool clampPointsYCoordinate: false |
| 12 |
|
| 13 |
property alias xTickCount: xAxis.tickCount |
| 14 |
property alias xMinorTickCount: xAxis.minorTickCount |
| 15 |
property alias yTickCount: yAxis.tickCount |
| 16 |
property alias yMinorTickCount: yAxis.minorTickCount |
| 17 |
|
| 18 |
id: chart |
| 19 |
|
| 20 |
antialiasing: true |
| 21 |
legend.visible: false |
| 22 |
backgroundColor: "#00000000" |
| 23 |
|
| 24 |
margins.top: 0 |
| 25 |
margins.bottom: 0 |
| 26 |
margins.left: 0 |
| 27 |
margins.right: 0 |
| 28 |
|
| 29 |
axes: [ValueAxis { |
| 30 |
id: xAxis |
| 31 |
labelFormat: Style.CurveControl.axis_label_format |
| 32 |
minorGridVisible: true |
| 33 |
minorTickCount: 1 |
| 34 |
tickCount: 5 |
| 35 |
}, |
| 36 |
ValueAxis { |
| 37 |
id: yAxis |
| 38 |
labelFormat: Style.CurveControl.axis_label_format |
| 39 |
minorGridVisible: true |
| 40 |
minorTickCount: 1 |
| 41 |
tickCount: 3 |
| 42 |
}] |
| 43 |
|
| 44 |
signal curveChanged(var name, var oldPoint, var newPoint) |
| 45 |
|
| 46 |
function configureAxes(xName, xUnit, xMin, xMax, yName, yUnit, yMin, yMax) { |
| 47 |
|
| 48 |
p.axisInfo = { |
| 49 |
x: { |
| 50 |
name: xName, |
| 51 |
unit: xUnit, |
| 52 |
min: xMin, |
| 53 |
max: xMax |
| 54 |
}, |
| 55 |
y: { |
| 56 |
name: yName, |
| 57 |
unit: yUnit, |
| 58 |
min: yMin, |
| 59 |
max: yMax |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
p.refreshAxes(p.axisInfo, enabled) |
| 64 |
|
| 65 |
// update outer range line series points |
| 66 |
p.curves.forEach(series => { |
| 67 |
const oldFirst = series.line.at(0) |
| 68 |
const oldLast = series.line.at(line.count - 1) |
| 69 |
series.line.replace(oldFirst.x, oldFirst.y, xMin - 1, oldFirst.y) |
| 70 |
series.line.replace(oldLast.x, oldLast.y, xMax + 1, oldLast.y) |
| 71 |
}) |
| 72 |
} |
| 73 |
|
| 74 |
function addCurve(name, color, points, vertical) { |
| 75 |
// remove existing curve |
| 76 |
if (p.curves.has(name)) |
| 77 |
removeCurve(name) |
| 78 |
|
| 79 |
const line = p.createLineSeries(name, color, points, vertical) |
| 80 |
const controls = p.createControlSeries(name, color, points) |
| 81 |
|
| 82 |
p.curves.set(name, { |
| 83 |
line: line, |
| 84 |
points: controls, |
| 85 |
vertical: vertical |
| 86 |
}) |
| 87 |
} |
| 88 |
|
| 89 |
function removeCurve(name) { |
| 90 |
if (p.curves.has(name)) { |
| 91 |
const {line, points} = p.curves.get(name) |
| 92 |
chart.removeSeries(line) |
| 93 |
chart.removeSeries(points) |
| 94 |
p.curves.delete(name) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
onEnabledChanged: { p.onEnableChanged(enabled) } |
| 99 |
|
| 100 |
QtObject { // private stuff |
| 101 |
id: p |
| 102 |
|
| 103 |
property var curves: new Map() |
| 104 |
property var selection: undefined |
| 105 |
property var axisInfo: undefined |
| 106 |
|
| 107 |
function createLineSeries(name, color, points, vertical) { |
| 108 |
var series = chart.createSeries(ChartView.SeriesTypeLine, |
| 109 |
name, xAxis, yAxis) |
| 110 |
series.name = name |
| 111 |
series.color = color |
| 112 |
series.opacity = chart.enabled ? Style.CurveControl.curve_opacity |
| 113 |
: Style.CurveControl.curve_opacity_alt |
| 114 |
series.width = 2 |
| 115 |
|
| 116 |
// line first outer range point |
| 117 |
if (vertical) |
| 118 |
series.append(points[0].x, axisInfo.y.min - 1) |
| 119 |
else |
| 120 |
series.append(axisInfo.x.min - 1, points[0].y) |
| 121 |
|
| 122 |
for (var i = 0; i < points.length; ++i) |
| 123 |
series.append(points[i].x, points[i].y) |
| 124 |
|
| 125 |
// line last outer range point |
| 126 |
if (vertical) |
| 127 |
series.append(points[points.length - 1].x, axisInfo.y.max + 1) |
| 128 |
else |
| 129 |
series.append(axisInfo.x.max + 1, points[points.length - 1].y) |
| 130 |
|
| 131 |
return series |
| 132 |
} |
| 133 |
|
| 134 |
function createControlSeries(name, color, points) { |
| 135 |
var series = chart.createSeries(ChartView.SeriesTypeScatter, |
| 136 |
name, xAxis, yAxis) |
| 137 |
series.name = name |
| 138 |
series.color = color |
| 139 |
series.opacity = chart.enabled ? Style.CurveControl.curve_opacity |
| 140 |
: Style.CurveControl.curve_opacity_alt |
| 141 |
series.borderColor = color |
| 142 |
series.markerSize = 10 |
| 143 |
series.borderWidth = 0 |
| 144 |
|
| 145 |
for (var i = 0; i < points.length; ++i) |
| 146 |
series.append(points[i].x, points[i].y) |
| 147 |
|
| 148 |
return series |
| 149 |
} |
| 150 |
|
| 151 |
function onEnableChanged(enabled) { |
| 152 |
refreshAxes(axisInfo, enabled) |
| 153 |
|
| 154 |
// series opacity |
| 155 |
var opacity = enabled ? Style.CurveControl.curve_opacity |
| 156 |
: Style.CurveControl.curve_opacity_alt |
| 157 |
curves.forEach(series => { |
| 158 |
series.line.opacity = opacity |
| 159 |
series.points.opacity = opacity |
| 160 |
}) |
| 161 |
} |
| 162 |
|
| 163 |
function hasCurves() { |
| 164 |
return curves.size > 0 |
| 165 |
} |
| 166 |
|
| 167 |
function hasSelection() { |
| 168 |
return selection !== undefined |
| 169 |
} |
| 170 |
|
| 171 |
function mapToChart(point) { |
| 172 |
// use the first available point series to map the point |
| 173 |
for (const series of curves.values()) |
| 174 |
return chart.mapToValue(point, series.points) |
| 175 |
} |
| 176 |
|
| 177 |
function refreshAxes({x, y}, enabled) { |
| 178 |
xAxis.min = x.min |
| 179 |
xAxis.max = x.max |
| 180 |
xAxis.titleText = "<font color='"+ (enabled ? Style.CurveControl.axis_title_color |
| 181 |
: Style.CurveControl.axis_title_color_alt) |
| 182 |
+ "'>" + x.name + " (" + x.unit + ")</font>" |
| 183 |
xAxis.labelsColor = enabled ? Style.CurveControl.axis_label_color |
| 184 |
: Style.CurveControl.axis_label_color_alt |
| 185 |
xAxis.color = enabled ? Style.CurveControl.axis_color |
| 186 |
: Style.CurveControl.axis_color_alt |
| 187 |
xAxis.gridLineColor = enabled ? Style.CurveControl.axis_grid_color |
| 188 |
: Style.CurveControl.axis_grid_color_alt |
| 189 |
xAxis.minorGridLineColor = enabled ? Style.CurveControl.axis_grid_minor_color |
| 190 |
: Style.CurveControl.axis_grid_minor_color_alt |
| 191 |
yAxis.min = y.min |
| 192 |
yAxis.max = y.max |
| 193 |
yAxis.titleText = "<font color='"+ (enabled ? Style.CurveControl.axis_title_color |
| 194 |
: Style.CurveControl.axis_title_color_alt) |
| 195 |
+ "'>" + y.name + " (" + y.unit + ")</font>" |
| 196 |
yAxis.labelsColor = enabled ? Style.CurveControl.axis_label_color |
| 197 |
: Style.CurveControl.axis_label_color_alt |
| 198 |
yAxis.color = enabled ? Style.CurveControl.axis_color |
| 199 |
: Style.CurveControl.axis_color_alt |
| 200 |
yAxis.gridLineColor = enabled ? Style.CurveControl.axis_grid_color |
| 201 |
: Style.CurveControl.axis_grid_color_alt |
| 202 |
yAxis.minorGridLineColor = enabled ? Style.CurveControl.axis_grid_minor_color |
| 203 |
: Style.CurveControl.axis_grid_minor_color_alt |
| 204 |
} |
| 205 |
|
| 206 |
function updateCurvePoint(curve, oldPoint, newPoint) { |
| 207 |
curve.line.replace(oldPoint.x, oldPoint.y, newPoint.x, newPoint.y) |
| 208 |
curve.points.replace(oldPoint.x, oldPoint.y, newPoint.x, newPoint.y) |
| 209 |
} |
| 210 |
|
| 211 |
function updateLineOuterRangePoint(curve, pointIndex, oldPoint, newPoint) { |
| 212 |
if (curve.vertical) { |
| 213 |
if (pointIndex === 0) |
| 214 |
curve.line.replace(oldPoint.x, axisInfo.y.min - 1, newPoint.x, axisInfo.y.min - 1) |
| 215 |
if (pointIndex === curve.points.count - 1) |
| 216 |
curve.line.replace(oldPoint.x, axisInfo.y.max + 1, newPoint.x, axisInfo.y.max + 1) |
| 217 |
} else { |
| 218 |
if (pointIndex === 0) |
| 219 |
curve.line.replace(axisInfo.x.min - 1, oldPoint.y, axisInfo.x.min - 1, newPoint.y) |
| 220 |
if (pointIndex === curve.points.count - 1) |
| 221 |
curve.line.replace(axisInfo.x.max + 1, oldPoint.y, axisInfo.x.max + 1, newPoint.y) |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
function moveSelection(point) { |
| 226 |
// clamp point coordinates to axes range |
| 227 |
point.x = Math.min(Math.max(point.x, axisInfo.x.min), axisInfo.x.max) |
| 228 |
point.y = Math.min(Math.max(point.y, axisInfo.y.min), axisInfo.y.max) |
| 229 |
|
| 230 |
const curve = curves.get(selection.curve) |
| 231 |
|
| 232 |
// limit x movement between points using minXDistance |
| 233 |
if (selection.index > 0) |
| 234 |
point.x = Math.max(point.x, selection.prevX + minXDistance) |
| 235 |
if (selection.index < curve.points.count - 1) |
| 236 |
point.x = Math.min(point.x, selection.nextX - minXDistance) |
| 237 |
|
| 238 |
if (clampPointsYCoordinate) |
| 239 |
clampOtherPointsYCoordinate(curve, selection) |
| 240 |
|
| 241 |
updateCurvePoint(curve, selection.point, point) |
| 242 |
updateLineOuterRangePoint(curve, selection.index, selection.point, point) |
| 243 |
|
| 244 |
// emit curveChanged signal |
| 245 |
curveChanged(selection.curve, selection.point, point) |
| 246 |
|
| 247 |
selection.point = point |
| 248 |
|
| 249 |
return point |
| 250 |
} |
| 251 |
|
| 252 |
function clampOtherPointsYCoordinate(curve, selection) { |
| 253 |
var points = curve.points |
| 254 |
for (var i = 0; i < points.count; ++i) { |
| 255 |
// skip selected point |
| 256 |
if (selection.index === i) |
| 257 |
continue |
| 258 |
|
| 259 |
var point = points.at(i) |
| 260 |
if ((i < selection.index && point.y > selection.point.y) || |
| 261 |
(i > selection.index && point.y < selection.point.y)) { |
| 262 |
point.y = selection.point.y |
| 263 |
var oldPoint = points.at(i) |
| 264 |
updateCurvePoint(curve, oldPoint, point) |
| 265 |
curveChanged(selection.curve, oldPoint, point) |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
function findCloserIndex(points, target) { |
| 271 |
var distRange = (xAxis.max - xAxis.min) / 20 |
| 272 |
var closerPointIndex = -1 |
| 273 |
var minDist = 10000 |
| 274 |
for (var i = 0; i < points.count; ++i) { |
| 275 |
var point = points.at(i) |
| 276 |
var distance = Math.sqrt(Math.pow(point.x - target.x, 2) + |
| 277 |
Math.pow(point.y - target.y, 2)) |
| 278 |
|
| 279 |
if (distance < minDist && distance < distRange) { |
| 280 |
minDist = distance |
| 281 |
closerPointIndex = i |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
return closerPointIndex |
| 286 |
} |
| 287 |
|
| 288 |
function closerPointTo(point) { |
| 289 |
for (const [curve, series] of curves) { |
| 290 |
const selectedPointIndex = findCloserIndex(series.points, point) |
| 291 |
if (selectedPointIndex !== -1) |
| 292 |
return { |
| 293 |
curve: curve, |
| 294 |
point: series.points.at(selectedPointIndex), |
| 295 |
index: selectedPointIndex |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
return undefined |
| 300 |
} |
| 301 |
|
| 302 |
function select(pointInfo) { |
| 303 |
if (pointInfo !== undefined) { |
| 304 |
const { curve, point, index } = pointInfo |
| 305 |
const points = curves.get(curve).points |
| 306 |
|
| 307 |
selection = { |
| 308 |
curve, |
| 309 |
point, |
| 310 |
index, |
| 311 |
prevX: index > 0 ? points.at(index - 1).x : undefined, |
| 312 |
nextX: index < points.count - 1 ? points.at(index + 1).x : undefined |
| 313 |
} |
| 314 |
} |
| 315 |
else { |
| 316 |
selection = undefined |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
ToolTip { |
| 322 |
id: tooltip |
| 323 |
delay: 0 |
| 324 |
timeout: -1 |
| 325 |
margins: -1 |
| 326 |
|
| 327 |
function updatePosition(x, y) { |
| 328 |
x = Math.max(0, Math.min(x, Math.round(parent.width - width))) |
| 329 |
y = Math.max(0, Math.min(y + 25, Math.round(parent.height - height))) |
| 330 |
|
| 331 |
tooltip.x = x |
| 332 |
tooltip.y = y |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
MouseArea { |
| 337 |
id: mouseArea |
| 338 |
anchors.fill: parent |
| 339 |
|
| 340 |
onPressed: mouse => { |
| 341 |
if (p.hasCurves()) { |
| 342 |
var outerPoint = p.mapToChart(Qt.point(mouse.x, mouse.y)) |
| 343 |
|
| 344 |
var pointInfo = p.closerPointTo(outerPoint) |
| 345 |
p.select(pointInfo) |
| 346 |
|
| 347 |
// update tooltip |
| 348 |
if (pointInfo !== undefined) { |
| 349 |
const { point } = pointInfo |
| 350 |
|
| 351 |
tooltip.updatePosition(mouse.x, mouse.y) |
| 352 |
tooltip.text = Math.round(point.x) + p.axisInfo.x.unit + " " + Math.round(point.y) + p.axisInfo.y.unit |
| 353 |
tooltip.visible = true |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
onReleased: mouse => { |
| 359 |
if (p.hasCurves()) { |
| 360 |
p.select(undefined) |
| 361 |
tooltip.visible = false |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
onPositionChanged: mouse => { |
| 366 |
if (p.hasCurves() && p.hasSelection()) { |
| 367 |
var point = p.mapToChart(Qt.point(mouse.x, mouse.y)) |
| 368 |
point = p.moveSelection(point) |
| 369 |
|
| 370 |
// update tooltip |
| 371 |
tooltip.updatePosition(mouse.x, mouse.y) |
| 372 |
tooltip.text = Math.round(point.x) + p.axisInfo.x.unit + " " + Math.round(point.y) + p.axisInfo.y.unit |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
} |