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. Regarding dynamic graph creation using Shape qml component.
Qt 6.11 is out! See what's new in the release blog

Regarding dynamic graph creation using Shape qml component.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 294 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.
  • S Offline
    S Offline
    Savyasachi
    wrote on last edited by
    #1

    I tried to create a dynamic graph using Shape and PathPolyline. when it reaches to end of the graph area, it starts from left end of graph again clearing some space. My Issue is when it reaches right end and going to left end it drawing a line. I wanted to disconnect those two points without disturbing other points.

    My QML code sample.

    import QtQuick 2.15
    import QtQuick.Controls 2.15
    import QtQuick.Shapes 1.15

    ApplicationWindow {
    visible: true
    width: 500
    height: 300

    Rectangle {
        id: graphContainer
        width: parent.width
        height: parent.height
        color: "black"
    
        Shape {
            id: graph
            anchors.fill: parent
    
            ShapePath{
                strokeWidth: 2
                strokeColor: "green"
                fillColor:"transparent"
    
                startX: 0
                startY: graphContainer.height / 2
    
                PathLine{
                    id:line11
                    x:graphContainer.width
                    y:graphContainer.height/2
                }
            }
    
            ShapePath {
                id:line12
                strokeWidth: 2
                strokeColor: "red"
                fillColor: "transparent"
                startX: 0
                startY: graphContainer.height / 2
    
                PathPolyline {
                    id: graphPath
                    path: [] // Empty initially
                }
                PathMove{
                    id:pathMove
                    x:0
                    y:graphContainer.height / 2
                }
            }
        }
    
        Connections {
            target: dataSender
            function onDataChanged() {
                if (!graphContainer || !dataSender) return;
    
                var xStep = graphContainer.width / 50; // Step size for X-axis
                var centerY = graphContainer.height / 2;
    
                if (graphPath.path.length === 0) {
                    graphPath.path = [];
                }
    
                var lastX = graphPath.path.length > 0 ? graphPath.path[graphPath.path.length - 1].x : 0;
    
                var newX,newY;
    
                if (lastX >= graphContainer.width) {
                    newX =0;
                }
                else{
                    newX =lastX + xStep;
                }
                newY = centerY - dataSender.data[dataSender.data.length - 1] / 2;
    
                var newPath = graphPath.path.concat([Qt.point(newX, newY)]);
    
                if(newPath.length > 50){
                    newPath.shift();
                }
    
                graphPath.path = newPath; // Assign updated path
            }
        }
    
    }
    

    }

    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