How disable drag of points in the x axis on chartview



  • I have a qtchart with a lineseries with 5 points on the graph. I can drag these points on in both x and y axis inside min and max of axisX and axisY.
    How can disable drag of points in the x axis? so it is possible to drag points vertically on the chart. Here is my code:

    ChartView {
        id: chart
        property var selectedPoint: undefined
        anchors.fill: parent
        antialiasing: true
        property real toleranceX: 0.05
        property real toleranceY: 0.05
    
        ValueAxis {
            id: axisX
            min: 0
            max: 5
        }
    
        ValueAxis {
            id: axisY
            min: -10
            max: 10
        }
    
        LineSeries {
            id: series1
            axisX: axisX
            axisY: axisY
            pointsVisible: true            
        }
    
        MouseArea {
            anchors.fill: parent
            onPressed:
            {
                var cp = chart.mapToValue(Qt.point(mouse.x,mouse.y));
                for(var i = 0;i < series1.count;i ++)
                {
                    var p = series1.at(i);
                    if(Math.abs(cp.x - p.x) <= chart.toleranceX && Math.abs(cp.y - p.y) <= chart.toleranceY)
                    {
                        chart.selectedPoint = p;
                        break;
                    }
                }
            }
            onPositionChanged: {
                if(chart.selectedPoint != undefined) {
                    var p = Qt.point(mouse.x, mouse.y);
                    var cp = chart.mapToValue(p);
                    if(cp.x >= axisX.min && cp.x <= axisX.max && cp.y >= axisY.min && cp.y <= axisY.max) {
                        series1.replace(chart.selectedPoint.x, chart.selectedPoint.y, cp.x, cp.y);
                        chart.selectedPoint = cp;
                    }
                }
            }
    
            onReleased: {
                chart.selectedPoint = undefined;
            }
        }
    
    }
    // Add data to the series
    Component.onCompleted: {
        for (var i = 0; i <= 5; i++) {            
            series1.append(i, Math.random());
        }
    } 
    

    So far I could disable drag on x axis for one point in this case point on 3 by changing onPositionChanged to following code but there are two problems, first this is just one point, second, it does not disable the drag completely instead the point can be dragged on x axis between 2 to 4 but it cannot be dragged outside this range.

     onPositionChanged: {
                    if(chart.selectedPoint != undefined) {
                        var p = Qt.point(mouse.x, mouse.y);
                        var cp = chart.mapToValue(p);
                        if(cp.x >= axisX.min && cp.x <= axisX.max && cp.y >= axisY.min && cp.y <= axisY.max) {
                            if (Math.round(cp.x) == 3){
                                  series1.replace(chart.selectedPoint.x, chart.selectedPoint.y, cp.x, cp.y);
                                  chart.selectedPoint = cp;
                            }
                        }
                    }
                }

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.