D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jjcruzhernandez
Full window
Github gist
TrellisPlot_D3
<!-- Used example posted in Piazza --> <!DOCTYPE html> <meta charset="utf-8"> <style> </style> <body> <!-- load the d3.js library --> <script src="https://d3js.org/d3.v4.min.js"></script> <script> // set the dimensions and margins of the graph var margin = {top: 50, right: 50, bottom: 100, left: 100}, width = 350 - margin.left - margin.right, height = 350 - margin.top - margin.bottom; // set X and Y axis var x = d3.scaleLinear() .range([0, width]); var y = d3.scaleLinear() .range([height, 0]); // created 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; // formatting data data.forEach(function(d) { d.Robbery = +d.Robbery; d.Assault = +d.Assault; }); // Scale the range of data for axis x.domain(d3.extent(data, function(d) { return d.Robbery; })); y.domain(d3.extent(data, function(d) { return d.Assault; })); // Add the scatterplot svg.selectAll("dot") .data(data) .enter() .append("circle") .attr("r", 5) .attr("cx", function(d) { return x(d.Robbery); }) .attr("cy", function(d) { return y(d.Assault); }); // x-axis svg.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); //x-axis label svg.append("text") .attr("text-anchor", "end") .attr("x", width/2) .attr("y", height + 50) .text("# Robbery"); //y-axis svg.append("g") .call(d3.axisLeft(y)); //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("# Assault"); //adding title svg.append("text") .attr("text-anchor", "end") .attr("x", width/2 + 50) .attr("y", -15) .text("Alabaster PD Assault v Robbery"); }); //REPEAT 2 MORE TIMES ////////////////////////////////////////////////////////////// var x2 = d3.scaleLinear() .range([0, width]); var y2 = d3.scaleLinear() .range([height, 0]); var svg2 = 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 + ")"); d3.csv("albertvillePD.csv", function(error, data) { if (error) throw error; data.forEach(function(d) { d.Robbery = +d.Robbery; d.Assault = +d.Assault; }); x2.domain(d3.extent(data, function(d) { return d.Robbery; })); y2.domain([0, d3.max(data, function(d) { return d.Assault; })]); svg2.selectAll("dot2") .data(data) .enter().append("circle") .attr("r", 5) .attr("cx", function(d) { return x2(d.Robbery); }) .attr("cy", function(d) { return y2(d.Assault); }); svg2.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x2)); svg2.append("text") .attr("text-anchor", "end") .attr("x", width/2) .attr("y", height + 50) .text("# Robbery"); svg2.append("g") .call(d3.axisLeft(y2)); svg2.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("# Assault"); svg2.append("text") .attr("text-anchor", "end") .attr("x", width/2 + width/2) .attr("y", -15) .text("Albertville PD Assault v Robbery"); }); ///////////////////////////////////////////////////////////////////////// var x3 = d3.scaleLinear() .range([0, width]); var y3 = d3.scaleLinear() .range([height, 0]); var svg3 = 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 + ")"); d3.csv("alexanderPD.csv", function(error, data) { if (error) throw error; data.forEach(function(d) { d.Robbery = +d.Robbery; d.Assault = +d.Assault; }); x3.domain(d3.extent(data, function(d) { return d.Robbery; })); y3.domain([0, d3.max(data, function(d) { return d.Assault; })]); svg3.selectAll("dot") .data(data) .enter() .append("circle") .attr("r", 5) .attr("cx", function(d) { return x3(d.Robbery); }) .attr("cy", function(d) { return y3(d.Assault); }); svg3.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x3)); svg3.append("text") .attr("text-anchor", "end") .attr("x", width/2) .attr("y", height + 50) .text("# Robbery"); svg3.append("g") .call(d3.axisLeft(y3)); svg3.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("# Assault"); svg3.append("text") .attr("text-anchor", "end") .attr("x", width/2 + width/2) .attr("y", -15) .text("Alexander PD Assault v Robbery"); }); </script> </body>
https://d3js.org/d3.v4.min.js