2
기본적으로 나는 도넛 형 차트 (example 참조)의 선을 애니메이션하여 각 개체가 document.ready로 계산되도록합니다. here 및 here캔버스를 사용하여 도넛 형 차트의 선을 애니메이션으로 지정
위에서 알 수 있듯이 countUp 솔루션은 이미 잘 작동하지만 차트에 연결해야 페이지로드시에 그려집니다.
캔버스에 관한 나의 지식은 놀랍지 않습니다. 아무도 도울 수 있다면 나는 매우 행복 할 것이다. 여기 코드는 다음과 같습니다 https://jsfiddle.net/9oyoh67x/10/
$(document).ready(function() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
console.log(ctx);
ctx.lineWidth = 19;
ctx.lineCap = 'round';
ctx.shadowBlur = 10;
donutChart();
function degtoRad(degree) {
var factor = Math.PI/180; // = 1 deg = 0.01745 rad
return degree * factor; // for 360 = 6.28 = 360°
}
/*function move() {
var elem = document.getElementById("canvas");
var width = 0;
var id = setInterval(countUp, 1000); }
*/
$(function() {
var countUp = setInterval(function() { donutChart }, 40);
function count($this) {
var current = parseInt($this.html(), 10);
$this.html(++current);
if (current !== $this.data('count')) {
setTimeout(function() { count($this) }, 50);
}
}
$(".testspan1").each(function() {
$(this).data('count', parseInt($(this).html(), 10));
$(this).html('0');
count($(this));
});
$(".testspan2").each(function() {
$(this).data('count', parseInt($(this).html(), 10));
$(this).html('0');
count($(this));
});
$(".testspan3").each(function() {
$(this).data('count', parseInt($(this).html(), 10));
$(this).html('0');
count($(this));
});
$(".testspan4").each(function() {
$(this).data('count', parseInt($(this).html(), 10));
$(this).html('0');
count($(this));
});
});
function donutChart() {
var factor_calc = Math.PI/180;
var record = $('.testspan1').text(); // equal to 100% or 360°
var average = $('.testspan2').text();
var income = $('.testspan2').text();
var target = $('.testspan3').text();
// Record
ctx.strokeStyle = 'rgba(115, 100, 164, 1)';
ctx.beginPath();
ctx.arc(250, 250, 200, degtoRad(270), degtoRad(270 + (record/record) * 360));
ctx.stroke();
// Average
ctx.strokeStyle = 'rgba(125, 131, 164, 1)';
ctx.beginPath();
ctx.arc(250, 250, 170, degtoRad(270), degtoRad(270 + (average/record) * 360));
ctx.stroke();
//Income
ctx.strokeStyle = 'rgba(75, 181, 164, 1)';
ctx.beginPath();
ctx.arc(250, 250, 140, degtoRad(270), degtoRad(270 + (income/record) * 360));
ctx.stroke();
// Target
ctx.strokeStyle = 'rgba(26, 221, 164, 1)';
ctx.beginPath();
ctx.arc(250, 250, 110, degtoRad(270), degtoRad(270 + (target/record) * 360));
ctx.stroke();
// Record
ctx.font = "25px Philosopher";
ctx.fillStyle = 'rgba(115, 100, 164, 1)';
ctx.fillText(record, 220, 200);
//Average
ctx.font = "25px Philosopher";
ctx.fillStyle = 'rgba(125, 131, 164, 1)';
ctx.fillText(average, 220, 230);
//Income
ctx.font = "30px Philosopher";
ctx.fillStyle = 'rgba(75, 181, 164, 1)';
ctx.fillText(income, 220, 260);
// Target
ctx.font = "35px Philosopher";
ctx.fillStyle = 'rgba(26, 221, 164, 1)';
ctx.fillText(target, 220, 290);
// Euro
ctx.font = "60px Philosopher";
ctx.fillStyle = 'rgba(26, 221, 164, 1)';
ctx.fillText('€', 280, 255);
}
var options = {
useEasing: true,
useGrouping: true,
separator: ',',
decimal: '',
prefix: '',
suffix: '€'
};
});
HTML : 비슷한 문제를 가진 사람들을 위해
<div class="canvasdiv">
<canvas id="canvas" width="500" height="500"></canvas>
<img id="myImage" />
<span class='testspan1'>1250</span>
<span class='testspan2'>250</span>
<span class='testspan3'>450</span>
<span class='testspan4'>130</span>
</div>
, 여기에 (더 캔버스를 사용하지 않고) 몇 가지 더 관련 링크입니다 :
- https://bl.ocks.org/mattkohl/9f3a283813cf0226311f41595582c9eb
- https://codepen.io/hi-im-si/pen/uhxFn
- 좋아