Built with blockbuilder.org
Last active
April 16, 2020 15:32
-
-
Save JoseJuan81/fbe707be8be446d75420b183c75f0d0d to your computer and use it in GitHub Desktop.
Dona
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> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<style> | |
body { align-items: center; display: flex; justify-content: flex-start; margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
svg { height: 400px;width: 400px} | |
</style> | |
</head> | |
<body> | |
<svg></svg> | |
<script> | |
const height = 400; | |
const width = 400; | |
const innerRadius = 170; | |
// datos | |
const fruits = [ | |
{name: "π", count: 21}, | |
{name: "π", count: 13}, | |
{name: "π", count: 8}, | |
{name: "π", count: 5}, | |
{name: "π", count: 3}, | |
{name: "π", count: 12}, | |
{name: "π", count: 10}, | |
{name: "π", count: 7} | |
] | |
// formateo de datos para la dona | |
const pieDataParsed = d3.pie().value(d => d.count)(fruits); | |
console.log(pieDataParsed) | |
// estilos de la dona | |
const arcPie = d3.arc() | |
.innerRadius(innerRadius) | |
.outerRadius(height / 3 * 2 ) | |
.padRadius(400) | |
.padAngle(4 / 300) | |
.cornerRadius(8); | |
const svg = d3.select('svg') | |
.attr('viewBox', "-320 -320 640 640") | |
// Dona | |
const path = svg.selectAll('path') | |
.data(pieDataParsed) | |
.enter().append('path') | |
.attr('d', d => arcPie(d)) | |
.attr('fill', "steelblue") | |
// texto dentro de Dona | |
pieDataParsed.map(p => { | |
const text = svg.append('text') | |
.attr('transform', 'translate(' + arcPie.centroid(p) + ')'); | |
const img = text.append('tspan').text(p.data.name); | |
const count = text.append('tspan') | |
.attr('font-size', '18') | |
.attr('font-family', 'verdana') | |
.attr('font-weight', '400') | |
.attr('dy', '1.2rem') | |
.attr('dx', '-1.2rem') | |
.attr('fill', 'white') | |
.text(p.value); | |
}) | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment