[76] | 1 | <!DOCTYPE html> |
---|
| 2 | <html> |
---|
| 3 | <head> |
---|
| 4 | <title>Bar Chart</title> |
---|
| 5 | <script type="text/javascript" src="../../d3.js"></script> |
---|
| 6 | <style type="text/css"> |
---|
| 7 | |
---|
| 8 | body { |
---|
| 9 | font: 10px sans-serif; |
---|
| 10 | } |
---|
| 11 | |
---|
| 12 | svg { |
---|
| 13 | shape-rendering: crispEdges; |
---|
| 14 | } |
---|
| 15 | |
---|
| 16 | </style> |
---|
| 17 | </head> |
---|
| 18 | <body> |
---|
| 19 | <script type="text/javascript"> |
---|
| 20 | |
---|
| 21 | var data = d3.range(10).map(Math.random); |
---|
| 22 | |
---|
| 23 | var w = 430, |
---|
| 24 | h = 230, |
---|
| 25 | x = d3.scale.linear().domain([0, 1]).range([0, w]), |
---|
| 26 | y = d3.scale.ordinal().domain(d3.range(data.length)).rangeBands([0, h], .2); |
---|
| 27 | |
---|
| 28 | var vis = d3.select("body") |
---|
| 29 | .append("svg:svg") |
---|
| 30 | .attr("width", w + 40) |
---|
| 31 | .attr("height", h + 20) |
---|
| 32 | .append("svg:g") |
---|
| 33 | .attr("transform", "translate(20,0)"); |
---|
| 34 | |
---|
| 35 | var bars = vis.selectAll("g.bar") |
---|
| 36 | .data(data) |
---|
| 37 | .enter().append("svg:g") |
---|
| 38 | .attr("class", "bar") |
---|
| 39 | .attr("transform", function(d, i) { return "translate(0," + y(i) + ")"; }); |
---|
| 40 | |
---|
| 41 | bars.append("svg:rect") |
---|
| 42 | .attr("fill", "steelblue") |
---|
| 43 | .attr("width", x) |
---|
| 44 | .attr("height", y.rangeBand()); |
---|
| 45 | |
---|
| 46 | bars.append("svg:text") |
---|
| 47 | .attr("x", x) |
---|
| 48 | .attr("y", y.rangeBand() / 2) |
---|
| 49 | .attr("dx", -6) |
---|
| 50 | .attr("dy", ".35em") |
---|
| 51 | .attr("fill", "white") |
---|
| 52 | .attr("text-anchor", "end") |
---|
| 53 | .text(x.tickFormat(100)); |
---|
| 54 | |
---|
| 55 | bars.append("svg:text") |
---|
| 56 | .attr("x", 0) |
---|
| 57 | .attr("y", y.rangeBand() / 2) |
---|
| 58 | .attr("dx", -6) |
---|
| 59 | .attr("dy", ".35em") |
---|
| 60 | .attr("text-anchor", "end") |
---|
| 61 | .text(function(d, i) { return String.fromCharCode(65 + i); }); |
---|
| 62 | |
---|
| 63 | var rules = vis.selectAll("g.rule") |
---|
| 64 | .data(x.ticks(10)) |
---|
| 65 | .enter().append("svg:g") |
---|
| 66 | .attr("class", "rule") |
---|
| 67 | .attr("transform", function(d) { return "translate(" + x(d) + ",0)"; }); |
---|
| 68 | |
---|
| 69 | rules.append("svg:line") |
---|
| 70 | .attr("y1", h) |
---|
| 71 | .attr("y2", h + 6) |
---|
| 72 | .attr("stroke", "black"); |
---|
| 73 | |
---|
| 74 | rules.append("svg:line") |
---|
| 75 | .attr("y1", 0) |
---|
| 76 | .attr("y2", h) |
---|
| 77 | .attr("stroke", "white") |
---|
| 78 | .attr("stroke-opacity", .3); |
---|
| 79 | |
---|
| 80 | rules.append("svg:text") |
---|
| 81 | .attr("y", h + 9) |
---|
| 82 | .attr("dy", ".71em") |
---|
| 83 | .attr("text-anchor", "middle") |
---|
| 84 | .text(x.tickFormat(10)); |
---|
| 85 | |
---|
| 86 | vis.append("svg:line") |
---|
| 87 | .attr("y1", 0) |
---|
| 88 | .attr("y2", h) |
---|
| 89 | .attr("stroke", "black"); |
---|
| 90 | |
---|
| 91 | </script> |
---|
| 92 | </body> |
---|
| 93 | </html> |
---|