xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Adding Tooltips</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: white;
}
svg {
background-color: #aaaaaa;
}
svg2 {
background-color: blue;
}
</style>
</head>
<body>
<script type="text/javascript">
var svg = d3.select("body")
.append("svg")
.attr("width", 700)
.attr("height", 500);
d3.csv("New Jersey Crime.csv", function(data) {
data.sort(function(a, b) {
return d3.descending(+a.Population, +b.Population)||d3.ascending(+a.Year, +b.Year);
})
var rects = svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("fill", function(d) {
if (d.Year < 2013) {return "#179BD7"}
else {return "#005191"}
;});
//.attr("fill", "#005191");
rects.attr("x", 0)
.attr("y", function(d, i) {
return i * 20;
})
.attr("width", function(d) {
return d.Population / 500;
})
.attr("height", 18)
.append("title")
.text(function(d) {
return d.City + "'s Population is " + d.Population.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,") + " in " + d.Year;
})
;
length = d3.selectAll("rect")[0].length
svg.attr("height", length*20)
var texts = svg.selectAll("text")
.data(data)
.enter()
.append("text")
.attr("fill", "white")
.attr("font-size", "11px")
.attr("font-family", "sans-serif");
texts.attr("x", 0)
.attr("y", function(d, i) {
return i * 20 + 13;
})
.text(function(d) {
return d.City; //+ " - " + d.Year;
});
svg.append("rect")
.attr("x", 400)
.attr("y", 100)
.attr("height", 20)
.attr("width", 200)
.attr("fill", "#005191");
svg.append("rect")
.attr("x", 400)
.attr("y", 120)
.attr("height", 20)
.attr("width", 200)
.attr("fill", "#179BD7");
svg.append("text")
.attr("x", 410)
.attr("y", 114)
.attr("fill", "white")
.attr("font-family", "sans-serif")
.attr("font-size", "12px")
.text("Population in 2013");
svg.append("text")
.attr("x", 410)
.attr("y", 134)
.attr("fill", "white")
.attr("font-family", "sans-serif")
.attr("font-size", "12px")
.text("Population in 2000");
});
</script>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js