Home
<html> <head> <script src="https://d3js.org/d3.v4.min.js"></script> <style> svg rect { fill: orange; } svg text { fill:white; font: 10px sans-serif; text-anchor: end; } </style> </head> <body> <script> var data = [5, 10, 12]; var width = 200, scaleFactor = 10, barHeight = 20; var graph = d3.select("body") .append("svg") .attr("width", width) .attr("height", barHeight * data.length); var bar = graph.selectAll("g") .data(data) .enter() .append("g") .attr("transform", function(d, i) { return "translate(0," + i * barHeight + ")"; }); bar.append("rect") .attr("width", function(d) { return d * scaleFactor; }) .attr("height", barHeight - 1); bar.append("text") .attr("x", function(d) { return (d*scaleFactor); }) .attr("y", barHeight / 2) .attr("dy", ".35em") .text(function(d) { return d; }); </script> </body> </html>
Result: