chart.js load totally new data

前端 未结 20 1235
有刺的猬
有刺的猬 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:21

    I tried neaumusic solution, but later found out that the only problem with destroy is the scope.

    var chart;
    
    function renderGraph() {
        // Destroy old graph
        if (chart) {
            chart.destroy();
        }
    
        // Render chart
        chart = new Chart(
            document.getElementById(idChartMainWrapperCanvas),
            chartOptions
        );
    }
    

    Moving my chart variable outside the function scope, got it working for me.

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

    My solution to this is pretty simple. (VERSION 1.X)

    getDataSet:function(valuesArr1,valuesArr2){
            var dataset = [];
            var arr1 = {
                label: " (myvalues1)",
                fillColor: "rgba(0, 138, 212,0.5)",
                strokeColor: "rgba(220,220,220,0.8)",
                highlightFill: "rgba(0, 138, 212,0.75)",
                highlightStroke: "rgba(220,220,220,1)",
                data: valuesArr1
            };
            var arr2 = {
                label: " (myvalues2)",
                fillColor: "rgba(255, 174, 087,0.5)",
                strokeColor: "rgba(220,220,220,0.8)",
                highlightFill: "rgba(255, 174, 087,0.75)",
                highlightStroke: "rgba(220,220,220,1)",
                data: valuesArr2
            };
            /*Example conditions*/
            if(condition 1)
              dataset.push(arr1);
            }
            if(condition 2){
              dataset.push(arr1);
              dataset.push(arr2);
            }
    
            return dataset;
        }
    
    var data = {
        labels: mylabelone,
        datasets: getDataSet()
    };
    if(myBarChart != null) // i initialize myBarChart var with null
        myBarChart.destroy(); // if not null call destroy
        myBarChart = new Chart(ctxmini).Bar(data, options);//render it again ...
    

    No flickering or problems. getDataSet is a function to control what dataset I need to present

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

    It is an old thread, but in the current version (as of 1-feb-2017), it easy to replace datasets plotted on chart.js:

    suppose your new x-axis values are in array x and y-axis values are in array y, you can use below code to update the chart.

    var x = [1,2,3];
    var y = [1,1,1];
    
    chart.data.datasets[0].data = y;
    chart.data.labels = x;
    
    chart.update();
    
    0 讨论(0)
  • 2020-11-28 21:29

    I ran into the same issue, I have 6 pie charts on a page which can all be updated at the same time. I am using the following function to reset chart data.

    // sets chart segment data for previously rendered charts
    function _resetChartData(chart, new_segments) {
        // remove all the segments
        while (chart.segments.length) {
            chart.removeData();
        };
    
        // add the new data fresh
        new_segments.forEach (function (segment, index) {
            chart.addData(segment, index);
        });
    };
    
    // when I want to reset my data I call
    _resetChartData(some_chart, new_data_segments);
    some_chart.update();

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

    When creating the chart object you need to save the instance in a variable.

    var currentChart = new Chart(ctx, ...);
    

    And before loading new data, you need to destroy it:

    currentChart.destroy();
    
    0 讨论(0)
  • 2020-11-28 21:32

    I had huge problems with this

    First I tried .clear() then I tried .destroy() and I tried setting my chart reference to null

    What finally fixed the issue for me: deleting the <canvas> element and then reappending a new <canvas> to the parent container


    There's a million ways to do this:

    var resetCanvas = function () {
      $('#results-graph').remove(); // this is my <canvas> element
      $('#graph-container').append('<canvas id="results-graph"><canvas>');
      canvas = document.querySelector('#results-graph'); // why use jQuery?
      ctx = canvas.getContext('2d');
      ctx.canvas.width = $('#graph').width(); // resize to parent width
      ctx.canvas.height = $('#graph').height(); // resize to parent height
    
      var x = canvas.width/2;
      var y = canvas.height/2;
      ctx.font = '10pt Verdana';
      ctx.textAlign = 'center';
      ctx.fillText('This text is centered on the canvas', x, y);
    };
    
    0 讨论(0)
提交回复
热议问题