D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
cpang4
Full window
Github gist
D3: SPLOM for Delays due to Carrier, Weather, Security
<!DOCTYPE html> <html lang = "en"> <head> <meta charset="utf-8"> <title> SPLOM </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 = 800 - margin.top - margin.bottom; // divide into scales for X and Y for carrier, security, and weather var xScaleSecurity = d3.scaleLinear() .range([0, (width/3)]); var xScaleWeather = d3.scaleLinear() .range([(width/3), 2*(width/3)]); var xScaleCarrier = d3.scaleLinear() .range([2*(width/3), width]); var yScaleCarrier = d3.scaleLinear() .range([(height/3), 0]); var yScaleWeather = d3.scaleLinear() .range([2*(height/3), (height/3)]); var yScaleSecurity = d3.scaleLinear() .range([height, 2*(height/3)]); // now make all the axes var xAxisCarrier = d3.axisBottom() .scale(xScaleCarrier) .ticks(5); var xAxisSecurity = d3.axisBottom() .scale(xScaleSecurity) .ticks(4); var xAxisWeather = d3.axisBottom() .scale(xScaleWeather) .ticks(5); var yAxisSecurity = d3.axisLeft() .scale(yScaleSecurity) .ticks(4); var yAxisCarrier = d3.axisLeft() .scale(yScaleCarrier) .ticks(5); var yAxisWeather = d3.axisLeft() .scale(yScaleWeather) .ticks(5); 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({carrier:parseInt(d["# of Delays.Carrier"]), weather:parseInt(d["# of Delays.Weather"]), security:parseInt(d["# of Delays.Security"])}); } }); xScaleCarrier.domain([d3.min(dataset, function(d) { return d.carrier; }), d3.max(dataset, function(d) { return d.carrier; })]); xScaleWeather.domain([d3.min(dataset, function(d) { return d.weather; }), d3.max(dataset, function(d) { return d.weather; })]); xScaleSecurity.domain([d3.min(dataset, function(d) { return d.security; }), d3.max(dataset, function(d) { return d.security; })]); yScaleCarrier.domain([d3.min(dataset, function(d) { return d.carrier; }), d3.max(dataset, function(d) { return d.carrier; })]); yScaleWeather.domain([d3.min(dataset, function(d) { return d.weather; }), d3.max(dataset, function(d) { return d.weather; })]); yScaleSecurity.domain([d3.min(dataset, function(d) { return d.security; }), d3.max(dataset, function(d) { return d.security; })]); // adding x axes, need to translate to the bottom of graph svg.append("g") .attr("class", "axis") .attr("transform", "translate(0," + height + ")") .call(xAxisCarrier); svg.append("g") .attr("class", "axis") .attr("transform", "translate(0," + height + ")") .call(xAxisWeather); svg.append("g") .attr("class", "axis") .attr("transform", "translate(0," + height + ")") .call(xAxisSecurity); // adding y axes svg.append("g") .attr("class", "axis") .call(yAxisSecurity); svg.append("g") .attr("class", "axis") .call(yAxisWeather); svg.append("g") .attr("class", "axis") .call(yAxisCarrier); // dividers (rectangles) for (var counter = 0; counter < 3; counter++){ svg.append("rect") .attr("x", counter*(width/3)) .attr("y", 0) .attr("height", height/3) .attr("width", width/3) .style("stroke", "black") .style("fill", "none"); } for (var counter = 0; counter < 3; counter++){ svg.append("rect") .attr("x", counter*(width/3)) .attr("y", height/3) .attr("height", height/3) .attr("width", width/3) .style("stroke", "black") .style("fill", "none"); } for (var counter = 0; counter < 3; counter++){ svg.append("rect") .attr("x", counter*(width/3)) .attr("y", 2*(height/3)) .attr("height", height/3) .attr("width", width/3) .style("stroke", "black") .style("fill", "none"); } // where C = carrier, W = weather, S = security // C v S means carrier (y) vs security (x) svg.selectAll("CvS") .data(dataset) .enter() .append("circle") .attr("cx", function(d) { return xScaleSecurity(d.security); }) .attr("cy", function(d) { return yScaleCarrier(d.carrier); }) .attr("r", 1.5) .attr("fill", "black"); svg.selectAll("CvW") .data(dataset) .enter() .append("circle") .attr("cx", function(d) { return xScaleWeather(d.weather); }) .attr("cy", function(d) { return yScaleCarrier(d.carrier); }) .attr("r", 1.5) .attr("fill", "black"); svg.selectAll("CvC") .data(dataset) .enter() .append("circle") .attr("cx", function(d) { return xScaleCarrier(d.carrier); }) .attr("cy", function(d) { return yScaleCarrier(d.carrier); }) .attr("r", 1.5) .attr("fill", "black"); svg.selectAll("WvS") .data(dataset) .enter() .append("circle") .attr("cx", function(d) { return xScaleSecurity(d.security); }) .attr("cy", function(d) { return yScaleWeather(d.weather); }) .attr("r", 1.5) .attr("fill", "black"); svg.selectAll("WvW") .data(dataset) .enter() .append("circle") .attr("cx", function(d) { return xScaleWeather(d.weather); }) .attr("cy", function(d) { return yScaleWeather(d.weather); }) .attr("r", 1.5) .attr("fill", "black"); svg.selectAll("WvC") .data(dataset) .enter() .append("circle") .attr("cx", function(d) { return xScaleCarrier(d.carrier); }) .attr("cy", function(d) { return yScaleWeather(d.weather); }) .attr("r", 1.5) .attr("fill", "black"); svg.selectAll("SvS") .data(dataset) .enter() .append("circle") .attr("cx", function(d) { return xScaleSecurity(d.security); }) .attr("cy", function(d) { return yScaleSecurity(d.security); }) .attr("r", 1.5) .attr("fill", "black"); svg.selectAll("SvW") .data(dataset) .enter() .append("circle") .attr("cx", function(d) { return xScaleWeather(d.weather); }) .attr("cy", function(d) { return yScaleSecurity(d.security); }) .attr("r", 1.5) .attr("fill", "black"); svg.selectAll("SvC") .data(dataset) .enter() .append("circle") .attr("cx", function(d) { return xScaleCarrier(d.carrier); }) .attr("cy", function(d) { return yScaleSecurity(d.security); }) .attr("r", 1.5) .attr("fill", "black"); //title svg.append("text") .attr("transform", "translate(" + width/2 + "," + -20 + ")") .style("text-anchor", "middle") .attr("font-family", "sans-serif") .text("SPLOM for Delays from SFO"); // text label weather - 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("Weather"); // text label weather - 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("Weather"); // text label carrier - y axis svg.append("text") .attr("transform", "rotate(-90)") .attr("y", 0 - margin.left) .attr("x",0 - (height/6)) .attr("dy", "1em") .style("text-anchor", "middle") .attr("font-family", "sans-serif") .style("font-size","11px") .text("Carrier"); // text label carrier - x axis svg.append("text") .attr("transform", "translate(" + (width/1.2) + " ," + (height + 40) + ")") .style("text-anchor", "middle") .attr("font-family", "sans-serif") .style("font-size","11px") .text("Carrier"); // text label security - y axis svg.append("text") .attr("transform", "rotate(-90)") .attr("y", 0 - margin.left) .attr("x",0 - (height/1.2)) .attr("dy", "1em") .style("text-anchor", "middle") .attr("font-family", "sans-serif") .style("font-size","11px") .text("Security"); // text label security - x axis svg.append("text") .attr("transform", "translate(" + (width/6) + " ," + (height + 40) + ")") .style("text-anchor", "middle") .attr("font-family", "sans-serif") .style("font-size","11px") .text("Security"); }); </script> </body> </html>
https://d3js.org/d3.v4.min.js