Ajax重新加载数据聊天.js显示错误的鼠标悬停在图形上

Ajax reload data chat.js showing error on mouse hover on graph

本文关键字:错误 显示 鼠标 悬停 图形 js 聊天 新加载 加载 数据 Ajax      更新时间:2023-09-26

请运行代码片段

当我点击重新加载按钮并在图表上移动鼠标时。图表改变了它的数据和颜色。它显示了以前的数据图表。

谁来帮我解决这个问题?我不知道如何解决它。

$('documnet').ready(function(){
     $('#reload').on("click",function(){
     	piechart();
     });
    
  piechart();  
function piechart(){
// Pie chart or doughnut chart with chart.js
var ctx = document.getElementById("myChart").getContext("2d");
var data = [
     { value: 300, color:"#F7464A", highlight: "#FF5A5E", label: "sample" },
] ;
// data is empty. 
// we'll thow in between 10 and 50 items randomly generated
entries = 10 + Math.floor(Math.random() * 40);
for (i = 0; i < entries; i++) {
    r = Math.floor(Math.random() * 200);
    g = Math.floor(Math.random() * 200);
    b = Math.floor(Math.random() * 200);
    v = Math.floor(Math.random() * 500);
    c = 'rgb(' + r + ', ' + g + ', ' + b + ')';
    h = 'rgb(' + (r+20) + ', ' + (g+20) + ', ' + (b+20) + ')';
    data.push( {
      value : v,
      label : 'item ' + i,
      color: c,
      highlight: h
    }) ;
};
var options = { } ;
if ( entries % 2 ) {
var myChart = new Chart(ctx).Doughnut(data,options);
}
else {
var myChart = new Chart(ctx).Doughnut(data,options);
}
    
}
    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
<button id="reload">Reload</button>
<canvas id="myChart" width="600" height="400"></canvas>

此处输入代码

我建议您为画布和您的甜甜圈图使用不同的名称。如果您的甜甜圈图名称为"myChart",请尝试:

function piechart(){
myChart.destroy();
// Pie chart or doughnut chart with chart.js

您可以使用以下代码:

$('#myChart').replaceWith('<canvas id="myChart" width="600" height="400"></canvas>');