D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ocarneiro
Full window
Github gist
balde
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } </style> </head> <body> <div id="balde"> <div id="minha_porcentagem" style="font-size:120px"></div> <script> var svg = d3.select("#balde").append("svg") .attr("width", 220) .attr("height", 260) var balde = svg.append("g") balde.append("rect") .attr("y", 0) .attr("x", 8) .attr("width", 200) .attr("height", 250) .attr("stroke", "black") .attr("stroke-width", 20) .attr("fill", "white") balde.append("rect") .attr("y", 0) .attr("x", 0) .attr("width", 220) .attr("height", 50) .attr("fill", "white") var ALTURA_BALDE = 188; function desenhaConteudo(valorAtual, totalEsperado){ var altura = ALTURA_BALDE / totalEsperado * valorAtual; var porcentagem = valorAtual/totalEsperado; var cor = getCor(porcentagem); balde.select("#conteudoBalde").remove(); var c = balde.append("g") .attr("id", "conteudoBalde") c.append("text") .text(parseInt(porcentagem * 100) + "%") .attr("id","porcentagem") .attr("x", 110) .attr("y", 40) .style("font-size", 26) .attr("text-anchor", "middle") c.append("rect") .attr("y", 50 + (ALTURA_BALDE -altura)) .attr("x", 20) .attr("width", 176) .attr("height", altura) .attr("fill", cor) return parseInt(porcentagem * 100) + "%"; } function getCor(valor) { if (valor > 0.74) { return "green"; } else if (valor > 0.5) { return "orange"; } else { return "red"; } } p = desenhaConteudo(400,500); document.getElementById("minha_porcentagem").innerHTML = p; console.log(p); </script> </body>
https://d3js.org/d3.v4.min.js