// William Liu's Block 803b712a4d6efbf7bdb4 // http://bl.ocks.org/WilliamQLiu/803b712a4d6efbf7bdb4 var data_scatter = data; var canvas_height = height; var canvas_width = width; var padding = 25; // Define Scales - scales map an input domain with an output range var xScale = d3.scaleLinear() .domain([d3.min(data_scatter, function(d) { return d[0]; // get the input domain as first column of array }), d3.max(data_scatter, function(d) { return d[0]; // get the input domain as first column of array })]) .range([padding, canvas_width - padding * 2]) // set the output range .nice(); // Make decimals round up nicely var yScale = d3.scaleLinear() .domain([d3.min(data_scatter, function(d) { return d[1]; // get the input domain as first column of array }), d3.max(data_scatter, function(d) { return d[1]; // gets the input domain as the second column of array })]) .range([canvas_height - padding, padding]) // set the output range .nice(); // Make decimals round up nicely // Add circles from data svg.selectAll("circle") .data(data_scatter) .enter() .append("circle") .attr("x", function(d) { return xScale(d[0]); // Location of x }) .attr("y", function(d) { return yScale(d[1]); // Location of y }) .attr("r", 4) // Radius .attr("cx", function(d) { return xScale(d[0]); // Returns scaled circle x }) .attr("cy", function(d) { return yScale(d[1]); // Returns scaled circle y }); // Add Text Labels svg.selectAll("text") .data(data_scatter) .enter() .append("text") .text(function(d) { return d[0] + "," + d[1]; }) .attr("x", function(d) { return xScale(d[0]); // Returns scaled location of x }) .attr("y", function(d) { return yScale(d[1]); // Returns scaled circle y }) .attr("font_family", "sans-serif") // Font type .attr("font-size", "11px") // Font size .attr("fill", "darkgreen"); // Font color // Define X axis and attach to graph var xAxis = d3.axisBottom() // Create an x axis .scale(xScale) // Scale x axis .ticks(10); // Set rough # of ticks (optional) svg.append("g") // Append a group element (itself invisible, but helps 'group' elements) .attr("class", "axis") // Assign the 'axis' CSS .attr("transform", "translate(0," + (canvas_height - padding) + ")") // Place axis at bottom .call(xAxis); // Call function to create axis // Define Y axis and attach to graph var yAxis = d3.axisLeft() // Create a y axis .scale(yScale) // Scale y axis .ticks(5); // Set rough # of ticks (optional) svg.append("g") .attr("class", "axis") .attr("transform", "translate(" + padding + ",0)") .call(yAxis);