xxxxxxxxxx
<meta charset="utf-8">
<style>
.line { fill: none;
stroke: steelblue;
stroke-width: 2px;
}
</style>
<body>
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
// Set graph dimensions and margins
var margin = {top: 50, right: 20, bottom: 110, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// Targets for filtering data
var targetYear = "2004";
var targetAirport = "ATL";
// Time parser
var parseTime = d3.timeParse("%m");
// Set ranges
var x = d3.scaleTime()
.range([0, width])
var y = d3.scaleLinear()
.range([height, 0]);
// Define line
var line = d3.line()
.x(function(d) {
return x(d.Month);
})
.y(function(d) {
return y(d.Delayed);
});
// Appends an svg to the body and a group which is moved to the top left margin
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 + ")");
// Get data
d3.csv("airlines.csv", function(error, data) {
if (error) throw error;
// Filters the data to a manageable chunk
var newData = data.filter(function filterCriteria(d) {
return (d.Year == targetYear) && (d.Code == targetAirport);
});
// Formats data
newData.forEach(function(d) {
d.Month = parseTime(d.Month);
d.Delayed = +d.Delayed;
});
// Scale the range of the data in the domains
x.domain(d3.extent(newData, function(d) {
return d.Month;
}));
y.domain([0, d3.max(newData, function(d) {
return d.Delayed;
})]);
// Appends the path of the line
svg.append("path")
.data([newData])
.attr("class", "line")
.attr("d", line);
// X axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Y Axis
svg.append("g")
.call(d3.axisLeft(y));
// Make a title
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "20px")
.text("Number of Delayed Flights for " + (targetAirport) + " in " + targetYear);
});
</script>
</body>
https://d3js.org/d3.v4.min.js