chart.js load totally new data

前端 未结 20 1237
有刺的猬
有刺的猬 2020-11-28 21:12

The API for chart.js allows one to edit points of the datasets loaded into it, for example:

.update( )

Calling update() on

相关标签:
20条回答
  • 2020-11-28 21:33

    You need to clean old data. No need to re initialize:

    for (i in myChartLine.datasets[0].points)
        myChartLine.removeData();
    
    0 讨论(0)
  • 2020-11-28 21:34

    None of the above answers helped my particular situation in a very clean way with minimal code. I needed to remove all datasets and then loop to add in several datasets dynamically. So this snipped is for those that make it all the way to the bottom of the page without finding their answer :)

    Note: make sure to call chart.update() once you have loaded all of your new data into the dataset object. Hope this helps somebody

    function removeData(chart) {
       chart.data.datasets.length = 0;
    }
    
    function addData(chart, data) {
      chart.data.datasets.push(data);
    }
    
    0 讨论(0)
  • 2020-11-28 21:36

    You need to destroy:

    myLineChart.destroy();
    

    Then re-initialize the chart:

    var ctx = document.getElementById("myChartLine").getContext("2d");
    myLineChart = new Chart(ctx).Line(data, options);
    
    0 讨论(0)
  • 2020-11-28 21:37

    The only solution I can find so far for myself is to re-initialize the chart from scratch:

    var myLineChart = new Chart(ctx).Line(data, options);
    

    However this seems a bit hokey to me. Any better, more standard solution anybody?

    0 讨论(0)
  • 2020-11-28 21:37

    There is a way to do this without clearing the canvas or starting over, but you have to man handle the creation of the chart so that the data is in the same format for when you update.

    Here is how I did it.

        var ctx = document.getElementById("myChart").getContext("2d");
        if (chartExists) {
            for (i=0;i<10;i++){
                myNewChart.scale.xLabels[i]=dbLabels[i]; 
                myNewChart.datasets[0].bars[i].value=dbOnAir[i];
            }
            myNewChart.update();
          }else{
              console.log('chart doesnt exist');
              myNewChart = new Chart(ctx).Bar(dataNew);
              myNewChart.removeData();
              for (i=0;i<10;i++){
                  myNewChart.addData([10],dbLabels[i]);
              }
              for (i=0;i<10;i++){      
                  myNewChart.datasets[0].bars[i].value=dbOnAir[i];
              }
              myNewChart.update();
              chartExists=true;
            }
    

    I basically scrap the data loaded in at creation, and then reform with the add data method. This means that I can then access all the points. Whenever I have tried to access the data structure that is created by the:

    Chart(ctx).Bar(dataNew);
    

    command, I can't access what I need. This means you can change all the data points, in the same way you created them, and also call update() without animating completely from scratch.

    0 讨论(0)
  • 2020-11-28 21:39

    Please Learn how Chart.js (version 2 here) works and do it for whatever attribute you want:


    1.Please suppose you have a bar chart like the below in your HTML:

    <canvas id="your-chart-id" height="your-height" width="your-width"></canvas>
    

    2.Please suppose you have a javascript code that fills your chart first time (for example when page is loaded):

    var ctx = document.getElementById('your-chart-id').getContext('2d');
    var chartInstance = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: your-lables-array,
            datasets: [{
                data: your-data-array,
                /*you can create random colors dynamically by ColorHash library [https://github.com/zenozeng/color-hash]*/
                backgroundColor: your-lables-array.map(function (item) {
                    return colorHash.hex(item);
                })
            }]
        },
        options: {
            maintainAspectRatio: false,
            scales: {
                yAxes: [ { ticks: {beginAtZero: true} } ]
            },
            title: {display: true, fontSize: 16, text: 'chart title'},
            legend: {display: false}
        }
    });
    
    

    Please suppose you want to update fully your dataset. It is very simple. Please look at the above code and see how is the path from your chart variable to data and then follow the below path:

    • select chartInstance var.
    • Then select data node inside the chartInstance.
    • Then select datasets node inside the data node.
      (note: As you can see, the datasets node is an array. so you have to specify which element of this array you want. here we have only one element in the datasets node. so we use datasets[0]
    • So select datasets[0]
    • Then select data node inside in the datasets[0].


    This steps gives you chartInstance.data.datasets[0].data and you can set new data and update the chart:

    chartInstance.data.datasets[0].data = NEW-your-data-array
    //finally update chart var:
    chartInstance.update();
    


    Note: By following the above algorithm, you can simply achieve to each node you want.

    0 讨论(0)
提交回复
热议问题