Important: Please read the Qt Code of Conduct -

QML ChartView : mapToValue not taking account of serie parameter?

  • Hi all, I'm playing with ChartView in QML.

    I want to have the Y value of a LineSeries on the corresponding X value relative to the mouse position. Reading the docs, some samples and some discussion here I come up with this:

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.3
    import QtCharts 2.1
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
            ChartView {
                id: chart
                title: "Line"
                anchors.fill: parent
                antialiasing: true
                objectName: "mychart"
                ValueAxis {
                    id: axisX
                    min: 0
                    max: 30
                    tickCount: 5
                ValueAxis {
                    id: axisY
                    min: 0
                    max: 400
                LineSeries {
                    id: test
                    XYPoint { x: 5; y: 5 }
                    XYPoint { x: 6; y: 12 }
                MouseArea {
                    anchors.fill: parent
                    hoverEnabled  : true
                    onPositionChanged: {
                        var p = Qt.point(mouse.x, mouse.y);
                        var cp = chart.mapToValue(p, test);

    But the method mapToValue always return the y value of the mouse position, not the one related to the series in that point. It basically return the same if I add the second parameter or if it is missing.
    I could write a method to get it doing some simple math using the values in the series and the current y of the mouse, but I wonder if there are a nice and ready method.
    I have misunderstood the doc of the mapToValue method?

  • Hi @michele-tameni. I copy and paste your code into Qt. When I compile and run your program: it works for me! This is the output:

    qml: 10.53061224489796
    qml: 10.591836734693878
    qml: 10.63265306122449
    qml: 10.693877551020408
    qml: 10.795918367346939
    qml: 10.816326530612244
    qml: 10.918367346938776

    I use Qt 5.11 with MinGW compiler.

    Do I miss something?

  • I too have been playing with mapToValue and am seeing the same behaviour, albeit I am using Qt 5.9.6 currently.

    It is not necessarily that the behaviour is useless for my application - I am able to obtain, from the current mouse position, an accurate coordinate in terms of the plot area axes, which is all that I needed.

    As I was writing this I had intended to say that, like the OP, it seems to me that the series argument is irrelevant but it just occurred to me that it might be relevant if different axes were associated with different series; it is possible to use two axes on the same chart. So the series argument is used to determine which of the plot coordinate systems to map to. If only single X-Y axes are present then one may arbitrarily choose 0 as the series argument (assuming there are any series of course). Is my interpretation correct?

  • hi . i don't know i have a mistake or not but it seems i have the same problem :
    there is no respond to me yet !
    and an strange thing is MapToValue Works! with another seed of Points!

    this image shows MapToValue : working strangely !

  • I have same problem. Qt5.14 + MinGW.
    mapToValue just return the chart value of the mouse positon.

Log in to reply