Chart.jsで以前描画したものが残る問題を解決する
var chart; //グローバル変数にする const display_graph = function(scores, labels, colors, date){ const doughnutData = { labels : labels, datasets : [{ fill:false, tension:0, label: date, backgroundColor: colors, data : scores }] } const ctx = document.getElementById("chart-area").getContext("2d"); ctx.canvas.width = 600; ctx.canvas.height = 400; if(chart){ chart.destroy(); //すでにグラフが存在すれば消す } chart = new Chart(ctx, { type:'doughnut', data:doughnutData, options: { legend: { labels: { fontSize: 24 } } } }); };
この二つをやればいい。