// Retrieve canvas element
var el = document.getElementById('graph');
// Define custom options
var options = {
percent: el.getAttribute('data-percent') || 45,
size: el.getAttribute('data-size') || 220,
lineWidth: el.getAttribute('data-line') || 25,
rotate: el.getAttribute('data-rotate') || 200
}
// Create new canvas element
var canvas = document.createElement('canvas');
var span = document.createElement('span');
span.textContent = el.getAttribute('data-text') + ' $';
if (typeof(G_vmlCanvasManager) !== 'undefined') {
G_vmlCanvasManager.initElement(canvas);
}
var ctx = canvas.getContext('2d');
canvas.width = canvas.height = options.size;
el.appendChild(span);
el.appendChild(canvas);
ctx.translate(options.size / 2, options.size / 2);
ctx.rotate((-1 / 2 + options.rotate / 180) * Math.PI);
var radius = (options.size - options.lineWidth) / 2;
var drawCircle = function(color, lineWidth, percent) {
percent = Math.min(Math.max(0, percent || 1), 1);
ctx.beginPath();
ctx.arc(0, 0, radius, 0, Math.PI * 2 * percent, false);
ctx.strokeStyle = color;
ctx.lineCap = 'square';
ctx.lineWidth = lineWidth;
ctx.stroke();
};
drawCircle('#bbbec3', options.lineWidth, 100 / 100);
drawCircle('#c21b17', options.lineWidth, options.percent / 100);
// Apply mask over the chart
var applyMask = function(numLines, lineWidth) {
ctx.save();
ctx.beginPath();
ctx.rotate(-1 * options.rotate * Math.PI / 180);
ctx.globalCompositeOperation = 'destination-out';
ctx.lineWidth = lineWidth;
for (var i = 0; i < numLines; i++) {
ctx.rotate(Math.PI / (numLines / 2));
ctx.moveTo(0, 0);
ctx.lineTo(0, 120);
ctx.stroke();
}
ctx.restore();
}
applyMask(8, 4);
.chart {
position: relative;
width: 220px;
text-align: center;
}
canvas {
display: block;
position:absolute;
top:0;
left:0;
}
span {
color:#555;
display:block;
line-height:220px;
text-align:center;
width:220px;
font-family:sans-serif;
font-size:30px;
font-weight:100;
margin-left:5px;
}
.centerbox{
display: inline-block;
bottom: 0;
width: 45px;
position: absolute;
z-index: 1;
margin-left: -50px;
border-bottom: 35px solid #fff;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
}
<div class="chart" id="graph" data-text="123123" data-percent="55"><label class="centerbox"><i class="fa fa-usd" aria-hidden="true" style="padding-top: 10px;position: absolute;"></i></label></div>