D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
cpang4
Full window
Github gist
D3: Bivariate Scatterplot - Cancelled vs Delayed Flights from SFO
<!DOCTYPE html> <html lang = "en"> <head> <meta charset="utf-8"> <title> Cancelled vs Delayed Flights from SFO </title> <style> </style></head> <body> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var margin = {top: 50, right: 50, bottom: 50, left: 50}, width = 1000 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var xScale = d3.scaleLinear() .range([0, width]); var yScale = d3.scaleLinear() .range([height, 0]); var xAxis = d3.axisBottom() .scale(xScale); var yAxis = d3.axisLeft() .scale(yScale); 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 + ")"); var dataset = []; d3.csv("airlines.csv", function(data) { // only want values from SFO data.forEach(function(d) { if (d.Code == "SFO"){ dataset.push({delays:parseInt(d.Delayed), cancellations:parseInt(d.Cancelled)}); } }); console.log(dataset); xScale.domain([d3.min(dataset, function(d) { return d.delays; }), d3.max(dataset, function(d) { return d.delays; })]); yScale.domain([d3.min(dataset, function(d) { return d.cancellations; }), d3.max(dataset, function(d) { return d.cancellations; })]); svg.append("g") .attr("class", "axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "axis") .call(yAxis); svg.selectAll("points") .data(dataset) .enter() .append("circle") .attr("cx", function(d) { return xScale(d.delays); }) .attr("cy", function(d) { return yScale(d.cancellations); }) .attr("r", 1.5); // text label for the y axis svg.append("text") .attr("transform", "rotate(-90)") .attr("y", 0 - margin.left) .attr("x",0 - (height / 2)) .attr("dy", "1em") .style("text-anchor", "middle") .attr("font-family", "sans-serif") .style("font-size","11px") .text("Number of Flights Cancelled"); // text label for the x axis svg.append("text") .attr("transform", "translate(" + (width/2) + " ," + (height + 40) + ")") .style("text-anchor", "middle") .attr("font-family", "sans-serif") .style("font-size","11px") .text("Number of Delayed Flights"); svg.append("text") .attr("transform", "translate(" + width/2 + "," + -20 + ")") .style("text-anchor", "middle") .attr("font-family", "sans-serif") .text("Number of Flights Cancelled vs Delayed Flights from SFO"); }); </script> </body> </html>
https://d3js.org/d3.v4.min.js