Built with blockbuilder.org
Created
May 8, 2018 12:08
-
-
Save laxmikanta415/72270bd25537bba1859a364cd1cfb00b to your computer and use it in GitHub Desktop.
fresh block
This file contains 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 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"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-legend/2.24.0/d3-legend.min.js"></script> | |
<title>Pie Chart</title> | |
<style> | |
</style> | |
</head> | |
<body> | |
<svg width="550" height="500"></svg> | |
<script> | |
var data = [ | |
{ | |
"label": "Currency", | |
"value": "318.70", | |
"isDecreasing": false, | |
"change": 13.7 | |
}, | |
{ | |
"label": "Equity", | |
"value": "36115.70", | |
"isDecreasing": false, | |
"change": 49.35 | |
}, | |
{ | |
"label": "Volatility", | |
"value": "1099.70", | |
"isDecreasing": false, | |
"change": 5.4 | |
}, | |
{ | |
"label": "Commodity", | |
"value": "1122.30", | |
"isDecreasing": true, | |
"change": 5.35 | |
}, | |
{ | |
"label": "Alternatives", | |
"value": "113.00", | |
"isDecreasing": true, | |
"change": 7.27 | |
}, | |
{ | |
"label": "Real Estate", | |
"value": "250.70", | |
"isDecreasing": false, | |
"change": 35.25 | |
}, | |
{ | |
"label": "Bond", | |
"value": "4508.10", | |
"isDecreasing": false, | |
"change": 6.57 | |
} | |
]; | |
const pieValue = d => d.value; | |
const colorValue = d => d.label+' - '+d.change+"%"+'<text>'+d.isDecreasing+'</text>'; | |
const colorLabel = 'Religion'; | |
const margin = { left: 10, right: 177, top: 1, bottom: 1 }; | |
const svg = d3.select('svg'); | |
const width = svg.attr('width'); | |
const height = svg.attr('height'); | |
const innerWidth = width - margin.left - margin.right; | |
const innerHeight = height - margin.top - margin.bottom; | |
const pie = d3.pie().value(pieValue); | |
const arc = d3.arc() | |
.innerRadius(0) | |
.outerRadius(width / 3); | |
const g = svg.append('g') | |
.attr('transform', `translate(${margin.left},${margin.top})`); | |
const pieG = g.append('g') | |
.attr('transform', `translate(${innerWidth / 2},${innerHeight / 2})`); | |
const colorLegendG = g.append('g') | |
.attr('transform', `translate(${innerWidth + 20}, 150)`); | |
colorLegendG.append('text') | |
.attr('class', 'legend-label') | |
.attr('x', -30) | |
.attr('y', -40) | |
.text(colorLabel); | |
const colorScale = d3.scaleOrdinal() | |
.range(d3.schemeCategory10); | |
const colorLegend = d3.legendColor() | |
.scale(colorScale) | |
.shape('circle'); | |
colorScale.domain(data.map(colorValue)); | |
const arcs = pie(data); | |
pieG.selectAll('path').data(arcs) | |
.enter().append('path') | |
.attr('d', arc) | |
.attr('fill', d => { | |
return colorScale(colorValue(d.data)); | |
}); | |
colorLegendG.call(colorLegend) | |
.selectAll('.cell text') | |
.attr('dy', '0.1em'); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment