gpt4 book ai didi

javascript - 如何用 Canvas 制作动画条形图?

转载 作者:太空宇宙 更新时间:2023-11-04 11:54:42 25 4
gpt4 key购买 nike

在这里查看我正在尝试执行的操作的示例:http://www.chartjs.org/ .我正在尝试复制标题,使条形图始终移动。我试图让它工作,但它似乎不起作用。我可以看到他们有

<div class="aspect-ratio">
<canvas width='1200' height='480' id="hero-bar"></canvas>
<header>
</header>
</div>

我认为这是让它工作的 js:

<script type="text/javascript">
(function(){
var data = [],
barsCount = 50,
labels = new Array(barsCount),
updateDelayMax = 500,
$id = function(id){
return document.getElementById(id);
},
random = function(max){ return Math.round(Math.random()*100)},
helpers = Chart.helpers;


Chart.defaults.global.responsive = true;


for (var i = barsCount - 1; i >= 0; i--) {
data.push(Math.round(Math.random() * 100));
};
new Chart($id('hero-bar').getContext('2d')).Bar({
labels : labels,
datasets : [{
fillColor : '#2B303B',
data : data
}]
},{
showScale : false,
barShowStroke : false,
barValueSpacing: 1,
showTooltips : false,
onAnimationComplete : function(){
// Get scope of the hero chart during updates
var heroChart = this,
timeout;
// Stop this running every time the update is fired
this.options.onAnimationComplete = randomUpdate;

this.options.animationEasing = 'easeOutQuint';

randomUpdate();

function randomUpdate(){
heroChart.stop();
clearTimeout(timeout);
// Get a random bar
timeout = setTimeout(function(){
var randomNumberOfBars = Math.floor(Math.random() * barsCount),
i;
for (i = randomNumberOfBars - 1; i >= 0; i--) {
heroChart.datasets[0].bars[Math.floor(Math.random() * barsCount)].value = Math.round(Math.random() * 100);
};
heroChart.update();
},Math.random() * updateDelayMax);
};
}
});
});
</script>

关于如何使这项工作有任何想法吗?

最佳答案

这是来自 ChartJS.org 的随机更改 Hero 的工作版本

var data = [],
barsCount = 50,
labels = new Array(barsCount),
updateDelayMax = 500,
$id = function(id){
return document.getElementById(id);
},
random = function(max){ return Math.round(Math.random()*100)},
helpers = Chart.helpers;


Chart.defaults.global.responsive = true;


for (var i = barsCount - 1; i >= 0; i--) {
data.push(Math.round(Math.random() * 100));
};
new Chart($id('hero-bar').getContext('2d')).Bar({
labels : labels,
datasets : [{
fillColor : '#2B303B',
data : data
}]
},{
showScale : false,
barShowStroke : false,
barValueSpacing: 1,
showTooltips : false,
onAnimationComplete : function(){
// Get scope of the hero chart during updates
var heroChart = this,
timeout;
// Stop this running every time the update is fired
this.options.onAnimationComplete = randomUpdate;

this.options.animationEasing = 'easeOutQuint';

randomUpdate();

function randomUpdate(){
heroChart.stop();
clearTimeout(timeout);
// Get a random bar
timeout = setTimeout(function(){
var randomNumberOfBars = Math.floor(Math.random() * barsCount),
i;
for (i = randomNumberOfBars - 1; i >= 0; i--) {
heroChart.datasets[0].bars[Math.floor(Math.random() * barsCount)].value = Math.round(Math.random() * 100);
};
heroChart.update();
},Math.random() * updateDelayMax);
};
}
});
body{ background-color: ivory; }
#canvas{border:1px solid red;}
.aspect-ratio{background-color:#232830}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
<div class="aspect-ratio">
<canvas width='1200' height='480' id="hero-bar"></canvas>
<header></header>
</div>

关于javascript - 如何用 Canvas 制作动画条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30352528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com