如何使用Javascript更新chart.js

问题描述 投票:1回答:2

使用新数据更新条形图(chartJS)的正确方法是什么?

使用下面的代码,我可以使用新数据更新条形图。但仔细观察后,看起来更新的图表是原始的。当我将鼠标悬停在图表上时,它会在旧图像和新图像之间来回跳转。我很难理解文档..

也许这是我的HTML?

HTML:

<div>
    <form id="frm2">
        <h7> filter by dates </h7>
        <input type="button" onclick="runGraphButton(updatedCountList, updatedDayCount)" value="update graph">
    </form>
</div>

<div class="chartleft">
    <div id = "buildCountInfo-js">To:</div>
    <canvas id="buildCount"></canvas>
</div>

JS

//intial data
let countList = [3,4,5,4,37,8,4];
let dayCount = [1,1,3,4,5,3,2];

//updated data
let updatedCountList = [3,4,5,4,37,8,4,6,4,7,8,5];
let updatedDayCount = [1,1,3,4,5,3,2,4,5,4,3,2];

//run at page load
runGraph(countList, dayCount);

//run at button push
function runGraphButton(updatedCountList, updatedDayCount){
    runGraph(updatedCountList,updatedDayCount);
}

function runGraph(counts,days){
let ctx = document.getElementById("buildCount").getContext('2d');
let myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: counts,
        datasets: [{
            label: 'Builds per Day',
            data: days,
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {stepSize: 1,
                     beginAtZero:true
                }
            }]
        }
    }

});

removeData(myChart);
function removeData(chart) {
    chart.data.labels.pop();
    chart.data.datasets.forEach((dataset) => {
        dataset.data.pop();
    });
    chart.update();
}
}
javascript chart.js
2个回答
1
投票

chart.destroy()将销毁所创建的任何图表实例。这将清除Chart.js中存储到图表对象的任何引用,以及Chart.js附加的任何关联事件侦听器。

因此,如果要加载全新的数据集,可以事先调用chart.destroy()。我稍微重构了你的代码,这似乎有效:

let myChart; // declare chart variable

//intial data
let countList = [3, 4, 5, 4, 37, 8, 4];
let dayCount = [1, 1, 3, 4, 5, 3, 2];

//updated data
let updatedCountList = [3, 4, 5, 4, 37, 8, 4, 6, 4, 7, 8, 5];
let updatedDayCount = [1, 1, 3, 4, 5, 3, 2, 4, 5, 4, 3, 2];

//run at page load
myChart = runGraph(countList, dayCount);

//run at button push
function runGraphButton(updatedCountList, updatedDayCount) {
  myChart.destroy();  // call destroy before loading new dataset
  myChart = runGraph(updatedCountList, updatedDayCount);
}

function runGraph(counts, days) {
  let ctx = document.getElementById("buildCount").getContext('2d');
  let myChart = new Chart(ctx, {
    type: 'bar',
    data: {
      labels: counts,
      datasets: [{
        label: 'Builds per Day',
        data: days,
        backgroundColor: [
          'rgba(255, 99, 132, 0.2)',
          'rgba(54, 162, 235, 0.2)',
          'rgba(255, 206, 86, 0.2)',
          'rgba(75, 192, 192, 0.2)',
          'rgba(153, 102, 255, 0.2)',
          'rgba(255, 159, 64, 0.2)'
        ],
        borderColor: [
          'rgba(255,99,132,1)',
          'rgba(54, 162, 235, 1)',
          'rgba(255, 206, 86, 1)',
          'rgba(75, 192, 192, 1)',
          'rgba(153, 102, 255, 1)',
          'rgba(255, 159, 64, 1)'
        ],
        borderWidth: 1
      }]
    },
    options: {
      scales: {
        yAxes: [{
          ticks: {
            stepSize: 1,
            beginAtZero: true
          }
        }]
      }
    }

  });
  return myChart; // return chart object
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<div>
  <form id="frm2">
    <h7> filter by dates </h7>
    <input type="button" onclick="runGraphButton(updatedCountList, updatedDayCount)" value="update graph">
  </form>
</div>

<div class="chartleft">
  <div id="buildCountInfo-js">To:</div>
  <canvas id="buildCount"></canvas>
</div>

注意我也删除了removeData()方法,因为我正在调用destroy()


1
投票

只需更改您的更新功能即可

//run at button push
function runGraphButton(updatedCountList, updatedDayCount){
    chart.data.labels = updatedCountList;
    chart.data.datasets[0].data = updatedDayCount;
    chart.update();
}
© www.soinside.com 2019 - 2024. All rights reserved.