QML ChartView : mapToValue not taking very exactly?

788 views Asked by At

i have a chartView it has some random values and working good .

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
            }
        }
    }
}

in this chartview i show mouse position mapped to value on Axis by a simple Text. but it seems it has a little inexact value as shown on image :

mouseX and mouseY positions are not exactly match with Axis ! : look at this picture please

does there is some problems with mapToValue ?

Thank you for any comments and help .

EDIT : i changed code a little to show exact problem .

0

There are 0 answers