Important: Please read the Qt Code of Conduct -

[Solved]How to push data to inside element of a variable in QML?

  • I would like to push data to the inside element **data **of datasets of ChartLineData.Can any one please tell me how to do that?

    var ChartLineData = {
          labels: ["January","February","March","April","May","June","July"],
        datasets: [{
            fillColor : "rgba(220,220,220,0.2)",
            strokeColor : "rgba(220,220,220,1)",
            pointColor : "rgba(220,220,220,1)",
            pointStrokeColor : "#fff",
            pointHighlightFill : "#fff",
            pointHighlightStroke : "rgba(220,220,220,1)",
            **data **: [20,20,40,50,4,35,100,200]

    I am able to push data to labels.
    but;// It says cannot call method push of undefined.

  • Moderators

    @vishnu Well as ChartLineData is an object one way to access the key-value pairs is using [] where you can pass the key to access its value.
    So ChartLineData["labels"] will give you the labels value viz. an array. In the similar way you can access datasets.
    It contains an array of objects and arrays individual values can be accessed by index. But that value itself in an object. And we know its values can be accessed using key. So to sum up

    ChartLineData["datasets"][0]["data"].push(500) //access data and push a new value
    console.log(ChartLineData["datasets"][0]["data"]) //check the new array

  • @p3c0
    perfect explanation.Thanks a lot

Log in to reply