1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| <script>
var databases = [20, 10, 34, 53, 60, 90, 110, 230, 340, 550];
var width = 1400; var height = 700;
var svg = d3.select("body") .append("svg") .attr("width", width) .attr("height", height)
var padding = { top: 20, right: 20, bottom: 20, left: 20 }; var rectStep = 60; var rectWidth = 35;
function draw() { var updateRect = svg.selectAll("rect") .data(databases) var enterRect = updateRect.enter() var exitRect = updateRect.exit() updateRect.attr("fill","blue") .attr("x",function(d,i) { return padding.left + i * rectStep; }) .attr("y",function(d) { return height - padding.bottom - d; }) .attr("width",rectWidth) .attr("height",function(d) { return d; })
enterRect.append("rect") .attr("fill","blue") .attr("x", function(d,i) { return padding.left + i * rectStep; }) .attr("y",function(d) { return height - padding.bottom - d; }) .attr("width",rectWidth) .attr("height",function(d) { return d; })
exitRect.remove()
var updateText = svg.selectAll("text") .data(databases) var enterText = updateText.enter() var exitText = updateText.exit()
updateText.attr("fill","blue") .attr("font-size","14px") .attr("text-anchor","middle") .attr("x", function(d,i) { return padding.left + i * rectStep; }) .attr("y", function(d) { return height - padding-bottom - d }) .attr("dx",rectWidth/2) .attr("dx","-1em") .text(function(d) { return d; }) enterText.append("text") .attr("fill","blue") .attr("font-size","14px") .attr("text-anchor","middle") .attr("x", function(d,i) { return padding.left + i * rectStep; }) .attr("y",function(d) { return height - padding.bottom -d; }) .attr("dx",rectWidth/2) .attr("dy","-1em") .text(function(d) { return d }) exitText.remove() }
draw(); function mysort() { databases.sort(d3.ascending); draw() } </script> <button type="submit" onclick="mysort()">Go</button>
|