Javascript Chart.js update()方法最后只工作一次,而不是更新每次迭代

Javascript Chart.js update()方法最后只工作一次,而不是更新每次迭代,javascript,chart.js,insertion-sort,Javascript,Chart.js,Insertion Sort,函数insertSort(inputArray=myChart.data.datasets[0].data){ 让inputArray长度=inputArray.length; 设outerIndex=1; 设innerIndex=0; while(outerIndex=0){ if(inputArray[temp]数学层(Math.random()*10+1)); 让myChart=新图表(ctx{ 类型:'bar', 数据:{ 标签:[“红色”、“蓝色”、“黄色”、“绿色”、“紫色”、“橙色

函数insertSort(inputArray=myChart.data.datasets[0].data){
让inputArray长度=inputArray.length;
设outerIndex=1;
设innerIndex=0;
while(outerIndex=0){
if(inputArray[temp]数学层(Math.random()*10+1));
让myChart=新图表(ctx{
类型:'bar',
数据:{
标签:[“红色”、“蓝色”、“黄色”、“绿色”、“紫色”、“橙色”、“蓝色”、“蓝色”、“蓝色”、“蓝色”],
数据集:[{
标签:“#投票数”,
数据:随机数组,
背景颜色:[
"rgba(255,99,132,0.2)",,
“rgba(54162235,0.2)”,
"rgba(255,206,86,0.2)",,
“rgba(751921920.2)”,
“rgba(153102255,0.2)”,
“rgba(54162235,0.2)”,
“rgba(54162235,0.2)”,
“rgba(54162235,0.2)”,
“rgba(54162235,0.2)”,
‘rgba(54162235,0.2)’
],
边框宽度:1
}]
},
选项:{
比例:{
雅克斯:[{
滴答声:{
贝吉纳泽罗:是的
}
}]
}
}
});

你好,世界!
你好,世界!
开始排序

您的代码会阻塞UI,因此控制台日志消息和更新的图表只能在其执行完成后显示(请参阅)

与其使用自己的
sleep
函数,不如使用它来安排各个工作部分。下面的runnable code说明了在函数
insertSort
被简化且不再像原来那样工作的情况下,这是如何工作的

函数insertSort(inputArray=myChart.data.datasets[0].data){
让inputArray长度=inputArray.length;
设outerIndex=1;
while(outerIndex{
myChart.data.datasets[0].data=Array.from({length:10},()=>Math.floor(Math.random()*10+1));
console.log('中间结果为:',myChart.data.dataset[0].data.join(',);
myChart.update();
},1000*outerIndex);
outerIndex++;
}
log('最终结果是:',inputArray.join(',);
返回true;
}
设ctx=document.getElementById('myChart').getContext('2d');
让randomArray=Array.from({length:10},()=>Math.floor(Math.random()*10+1));
让myChart=新图表(ctx{
类型:'bar',
数据:{
标签:[“红色”、“蓝色”、“黄色”、“绿色”、“紫色”、“橙色”、“蓝色”、“蓝色”、“蓝色”、“蓝色”],
数据集:[{
标签:“#投票数”,
数据:随机数组,
背景颜色:[
"rgba(255,99,132,0.2)",,
“rgba(54162235,0.2)”,
"rgba(255,206,86,0.2)",,
“rgba(751921920.2)”,
“rgba(153102255,0.2)”,
“rgba(54162235,0.2)”,
“rgba(54162235,0.2)”,
“rgba(54162235,0.2)”,
“rgba(54162235,0.2)”,
‘rgba(54162235,0.2)’
],
边框宽度:1
}]
},
选项:{
比例:{
雅克斯:[{
滴答声:{
贝吉纳泽罗:是的
}
}]
}
}
});

开始排序

您的代码会阻塞UI,因此控制台日志消息和更新的图表只能在其执行完成后显示(请参阅)

与其使用自己的
sleep
函数,不如使用它来安排各个工作部分。下面的runnable code说明了在函数
insertSort
被简化且不再像原来那样工作的情况下,这是如何工作的

函数insertSort(inputArray=myChart.data.datasets[0].data){
让inputArray长度=inputArray.length;
设outerIndex=1;
while(outerIndex{
myChart.data.datasets[0].data=Array.from({length:10},()=>Math.floor(Math.random()*10+1));
console.log('中间结果为:',myChart.data.dataset[0].data.join(',);
myChart.update();
},1000*outerIndex);
outerIndex++;
}
log('最终结果是:',inputArray.join(',);
返回true;
}
设ctx=document.getElementById('myChart').getContext('2d');
让randomArray=Array.from({length:10},()=>Math.floor(Math.random()*10+1));
让myChart=新图表(ctx{
类型:'bar',
数据:{
标签:[“红色”、“蓝色”、“黄色”、“绿色”、“紫色”、“橙色”、“蓝色”、“蓝色”、“蓝色”、“蓝色”],
数据集:[{
标签:“#投票数”,
数据:随机数组,
背景颜色:[
"rgba(255,99,132,0.2)",,
“rgba(54162235,0.2)”,
"rgba(255,206,86,0.2)",,
“rgba(751921920.2)”,
“rgba(153102255,0.2)”,
“rgba(54162235,0.2)”,
“rgba(54162235,0.2)”,
“rgba(54162235,0.2)”,
“rgba(54162235,0.2)”,
‘rgba(54162235,0.2)’
],
边框宽度:1
}]
},
选项:{
比例:{
雅克斯:[{
滴答声:{
贝吉纳泽罗:是的
}
}]
}
}
});

开始排序