D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jjcruzhernandez
Full window
Github gist
scatterPlotBivariate_D3
<!-- Used example https://bl.ocks.org/d3noob/38744a17f9c0141bcd04 --> <!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 = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; // set the ranges for X and Y axis var x = d3.scaleLinear() .range([0, width]); var y = d3.scaleLinear() .range([height, 0]); var valueline = d3.line() .x(function(d) {return x(d.Robbery);}) .y(function(d) {return y(d.Assault);}); // 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; // formatting data data.forEach(function(d) { d.Robbery = +d.Robbery; d.Assault = +d.Assault; }); console.log(data); // Scale the range of the data for X and Y axis x.domain(d3.extent(data, function(d) { return d.Robbery; })); y.domain([0, d3.max(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); }); // add a-axis svg.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); // add x-axis label svg.append("text") .attr("text-anchor", "end") .attr("x", width/2) .attr("y", height + 50) .text("# Robbery"); // add the y-xis 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("# Assault"); //adding title svg.append("text") .attr("text-anchor", "end") .attr("x", width/2 + 50) .attr("y", -15) .text("Alabaster PD Assault v Robbery"); }); </script> </body>
https://d3js.org/d3.v4.min.js