xxxxxxxxxx
<!-- Used example posted in https://blockbuilder.org/elt12njo/76b484f5187c7ecfc83070dd81897327 -->
<meta charset="utf-8">
<style>
.bar {fill:steelblue;}
</style>
<body>
<!-- load the d3.js library -->
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// set the dimensions and margins for X and Y axis
var margin = {top: 50, right: 50, bottom: 100, left: 100},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// set the ranges for X and Y axis
var x = d3.scaleBand()
.range([0, width])
.padding(0.1);
var y = d3.scaleLinear()
.range([height, 0]);
// create svg
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// getting data
d3.csv("alabasterPD.csv", function(error, data) {
if (error) throw error;
// formating data
data.forEach(function(d) {
d.Year = d.Year;
d.Population = +d.Population;
});
x.domain(data.map(function(d) { return d.Year; }));
y.domain([0, d3.max(data, function(d) { return d.Population; })]);
// append the rectangles for the bar chart
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.Year); })
.attr("width", x.bandwidth())
.attr("y", function(d) { return y(d.Population); })
.attr("height", function(d) { return height - y(d.Population); });
// add the x-axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", ".8em")
.style("font-size", "15px")
.attr("dy", ".75em")
.attr("transform", "rotate(-35)");
// add x-axis label
svg.append("text")
.attr("text-anchor", "end")
.attr("x", width/2)
.attr("y", height + 50)
.text("Year");
// add the y-axis
svg.append("g")
.call(d3.axisLeft(y));
//add y-axis label
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("x",0 - (height / 2))
.attr("y", 0 - margin.left + 20)
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Population");
//adding title
svg.append("text")
.attr("text-anchor", "end")
.attr("x", width/2 + 50)
.attr("y", 0)
.text("Alabaster Popution per Year");
});
</script>
</body>
https://d3js.org/d3.v4.min.js