Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How disable drag of points in the x axis on chartview
Forum Updated to NodeBB v4.3 + New Features

How disable drag of points in the x axis on chartview

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 422 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pehi
    wrote on last edited by
    #1

    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;
                            }
                        }
                    }
                }
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved