Built with blockbuilder.org
Created
August 21, 2019 08:53
-
-
Save dianaow/faa81e6e8e12c5b2902d6dcee2f8a10b to your computer and use it in GitHub Desktop.
SVG Donut Chart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var startOffset = 25; | |
var strokeDashOffset; | |
var firstOffset; | |
var previousValues = []; | |
var svg = d3.select('svg') | |
function drawDonutChart(){ | |
var data = initData() | |
svg.append('circle') | |
.attr('class', 'donut-hole') | |
.attr('cx', 21) | |
.attr('cy', 21) | |
.attr('r', 15.91549430918954) | |
.attr('fill', '#fff') | |
svg.append('circle') | |
.attr('class', 'donut-ring') | |
.attr('cx', 21) | |
.attr('cy', 21) | |
.attr('r', 15.91549430918954) | |
.attr('fill', 'transparent') | |
.attr('stroke', "#d2d3d4") | |
.attr('stroke-width', '3px') | |
data.map((d,i)=>{ | |
oneDonut(d,i) | |
}) | |
} | |
function initData(){ | |
const data = | |
[{ | |
"color": "#00c389", | |
"name": "Low", | |
"range_end": 30, | |
"range_start": 0, | |
"count": 217, | |
"share": 83, | |
"bar_range_start": 0, | |
"bar_range_end": 83 | |
}, | |
{ | |
"color": "#ffc800", | |
"name": "Mid", | |
"range_end": 70, | |
"range_start": 30, | |
"count": 24, | |
"share": 9, | |
"bar_range_start": 83, | |
"bar_range_end": 92 | |
}, | |
{ | |
"color": "#ff5000", | |
"name": "High", | |
"range_end": 100, | |
"range_start": 70, | |
"count": 17, | |
"share": 6, | |
"bar_range_start": 92, | |
"bar_range_end": 98 | |
}, | |
{ | |
"name": "RISK", | |
"color": "#000000", | |
"count": 4, | |
"share": 2, | |
"bar_range_start": 98, | |
"bar_range_end": 100 | |
}] | |
return data | |
} | |
function oneDonut(donut,i){ | |
svg.append('circle') | |
.attr('class', 'donut-segment') | |
.attr('cx', 21) | |
.attr('cy', 21) | |
.attr('r', 15.91549430918954) | |
.attr('fill', 'transparent') | |
.attr('stroke-width', '3px') | |
.attr('stroke', donut.color) | |
.attr('stroke-dasharray', donut.share + ' ' + (100 - donut.share)) | |
.attr('stroke-dashoffset', updatePercent(donut, i)) | |
} | |
function updatePercent(donut, i){ | |
if(i === 0){ | |
var firstOffset = donut.share | |
previousValues.push(firstOffset) | |
var strokeDashOffset = startOffset | |
}else{ | |
var strokeDashOffset = startOffset + 100 - previousValues.reduce((a, b) => a + b, 0) | |
previousValues.push(donut.share) | |
} | |
return strokeDashOffset | |
} | |
drawDonutChart() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
</style> | |
</head> | |
<body> | |
<svg width="100%" height="100%" viewBox="0 0 42 42" class="donut"></svg> | |
<script src="donutChart.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment