Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML ChartView dynamically adding and removing LineSeries



  • Hello, I'm trying to dynamically add new serieses to a ChartView. Adding the series with createSeries works without a problem. But when I call removeSeries or removeAllSeries the Y-axis added to the series still remains in the ChartView and can't be deleted. How do I remove an axis from a chartView which was added with the createSeries function?



  • Hi @bozo1 . The only way I found it's to set manually the ChartView axes. Here an example. I hope you find it useful.

    There is two buttons. One to add a lineseries, and one to remove all series.

    import QtQuick 2.11
    import QtQuick.Controls 2.2
    import QtQuick.Window 2.11
    import QtCharts 2.0
    
    Window {
        id: window
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        ChartView {
            id: myChart
            title: "Line"
            anchors.fill: parent
            antialiasing: true
    
            ValueAxis {
                id: axisY
                gridVisible: true
                tickCount: 5
                min: 0
                max: 1
            }
    
            ValueAxis {
                id: axisX
                min: 0
                max: 1
                gridVisible: true
                tickCount: 5
            }
        }
    
        Button{
            id: addButton
            width: 100
            height: 20
            text: "Add"
            anchors.top: parent.top
            anchors.topMargin: 10
            anchors.left: parent.left
            anchors.leftMargin: 10
    
            onClicked: add()
        }
    
        Button{
            id: eliminateButton
            width: 100
            height: 20
            text: "Eliminate"
            anchors.top: parent.top
            anchors.topMargin: 10
            anchors.left: parent.left
            anchors.leftMargin: 120
    
            onClicked: eliminate()
        }
    
        function add()
        {
            var line = myChart.createSeries(ChartView.SeriesTypeLine, "Line series", axisX, axisY);
    
            line.append(0,0)
            line.append(1.1,2.1)
            line.append(4.5,4.5)
    
            axisX.min = 0;
            axisX.max = 5;
    
            axisY.min = 0;
            axisY.max = 5;
    
        }
    
        function eliminate()
        {
            myChart.removeAllSeries();
    
            axisX.min = 0;
            axisX.max = 1;
    
            axisY.min = 0;
            axisY.max = 1;
    
            myChart.axisX(axisX)
            myChart.axisY(axisY)
        }
    
    }
    

Log in to reply