我有一个 chartView,它有一些随机值并且工作良好。
import QtQuick 2.9
import QtQuick.Window 2.2
import QtCharts 2.0
Item {
visible: true
width: 640
height: 480
ChartView {
id: chartView
anchors.fill: parent
theme: ChartView.ChartThemeBrownSand
antialiasing: true
LineSeries {
id: liness
name: "LineSeries"
XYPoint {
x: 0
y: 0
}
XYPoint {
x: 1.1
y: 2.1
}
XYPoint {
x: 1.9
y: 3.3
}
XYPoint {
x: 2.1
y: 2.1
}
XYPoint {
x: 2.9
y: 4.9
}
XYPoint {
x: 3.4
y: 3.0
}
XYPoint {
x: 4.1
y: 3.3
}
}
LineSeries {
name: "xPositioner"
id: xPositionerX
style: Qt.DashLine
XYPoint {
x: 0
y: 2
}
XYPoint {
x: 4.1
y: 2
}
}
LineSeries {
name: "LineSeries"
id: positionery
style: Qt.DashLine
XYPoint {
x: 2
y: 0
}
XYPoint {
x: 2
y: 4.9
}
}
MouseArea {
anchors.fill: parent
cursorShape: Qt.CrossCursor
onDoubleClicked: chartView.zoomReset()
hoverEnabled: true
onPositionChanged: {
var p = Qt.point(mouse.x, mouse.y)
var cp = chartView.mapToValue(p, liness)
xPositionerX.replace(xPositionerX.at(1).x,
xPositionerX.at(1).y,
xPositionerX.at(1).x, cp.y)
xPositionerX.replace(xPositionerX.at(0).x,
xPositionerX.at(0).y,
xPositionerX.at(0).x, cp.y)
positionery.replace(positionery.at(1).x, positionery.at(1).y,
cp.x, positionery.at(1).y)
positionery.replace(positionery.at(0).x, positionery.at(0).y,
cp.x, positionery.at(0).y)
xMagView.x = mouseX - xMagView.width / 2
xMagText.text = cp.x
yMagView.y = mouseY - yMagView.height / 2
yMagText.text = cp.y
console.debug(mouseX, mouseY, cp.y)
}
}
Rectangle {
id: xMagView
width: 50
clip: true
height: 20
color: "#592e2e2e"
y: chartView.plotArea.y + chartView.plotArea.height
x: chartView.plotArea.x
Text {
clip: true
id: xMagText
anchors.fill: parent
wrapMode: Text.WrapAnywhere
horizontalAlignment: Text.AlignHCenter
}
}
Rectangle {
id: yMagView
width: 50
clip: true
height: 20
color: "#592e2e2e"
y: chartView.plotArea.y
x: chartView.plotArea.x - 50
Text {
clip: true
id: yMagText
anchors.fill: parent
wrapMode: Text.WrapAnywhere
horizontalAlignment: Text.AlignHCenter
}
}
}
}
在这个图表视图中,我通过一个简单的文本显示了鼠标位置映射到 Axis 上的值。但它似乎有一些不精确的值,如图所示:
mouseX 和 mouseY 位置与 Axis 不完全匹配!: 请看这张图
mapToValue 有问题吗?
感谢您的任何意见和帮助。
编辑: 我稍微更改了代码以显示确切的问题。