D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Hugues-Moreau
Full window
Github gist
iris scatter plot
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <title>D3 Scatterplot with Regression Line</title> <style> .line { stroke: #E4002B; fill: none; stroke-width: 3; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-size: 10px; font-family: sans-serif; } .text-label { font-size: 10px; font-family: sans-serif; } .dot { stroke: #293b47; fill: #7A99AC } </style> <body> <script src="//d3js.org/d3.v4.js"></script> <script> function compute_reg(data) { // copier-coller éhonté de la fonction de https://bl.ocks.org/ctufts/298bfe4b11989960eeeecc9394e9f118 var x = []; var y = []; var n = data.length; var x_mean = 0; var y_mean = 0; var term1 = 0; var term2 = 0; var noise_factor = 100; var noise = 0; // create x and y values for (var i = 0; i < n; i++) { d = data[i]; x.push(+d.sepal_width); y.push(+d.sepal_length); x_mean += x[i] ; y_mean += y[i] ; } // calculate mean x and y x_mean /= data.length; y_mean /= data.length; // calculate coefficients var xr = 0; var yr = 0; for (i = 0; i < x.length; i++) { xr = x[i] - x_mean; yr = y[i] - y_mean; term1 += xr * yr; term2 += xr * xr; } var b1 = term1 / term2; var b0 = y_mean - (b1 * x_mean); // perform regression yhat = []; // fit line using coeffs for (i = 0; i < x.length; i++) { yhat.push(b0 + (x[i] * b1)); } for (i = 0; i < y.length; i++) { data[i].yhat = yhat[i]; } return (data); }; var dataset = []; d3.csv("iris.csv", function(data) { var data = compute_reg(data); dataset.push([data]); data = compute_reg(data); var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scaleLinear() .range([0, width]); var y = d3.scaleLinear() .range([height, 0]); x.domain(d3.extent(data, function(d) { return +d.sepal_width; })); y.domain(d3.extent(data, function(d) { return +d.sepal_length; })); var xAxis = d3.axisBottom() .scale(x) var yAxis = d3.axisLeft() .scale(y) var svg = d3.select("body").append("svg") .attr("width", width+ margin.left + margin.right) .attr("height", height + margin.bottom + margin.top) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var line = d3.line() .x(function(d) {return x(+d.sepal_width); }) .y(function(d) {return y(+d.yhat); }); var triangle = d3.symbol() .type(d3.symbolTriangle) .size(30); var diamond = d3.symbol() .type(d3.symbolDiamond) .size(30); var cross = d3.symbol() .type(d3.symbolCross) .size(30); svg.selectAll("path") .data(data) .enter() .append("path") .attr("d",diamond) /* .attr("d",function(d) { if (d.species == 'setosa') { return cross; } else if (d.species == 'versicolor') { return diamond; } else if (d.species == 'virginica') { return triangle; } }) */ // ne fonctionne pas .attr('fill',function(d) { if (d.species == 'setosa') { return 'red'; } else if (d.species == 'versicolor') { return 'blue'; } else if (d.species == 'virginica') { return 'yellow'; } }) .attr("transform", function(d) { return "translate(" + x(+d.sepal_width) + "," + y(+d.sepal_length) + ")"; }); svg.append("path") .datum(data) .attr("class", "line") .attr("d", line); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .append("text") .attr("class", "label") .attr("x", width) .attr("y", -6) .style("text-anchor", "end") .text("Sepal Width (cm)"); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("class", "label") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Sepal Length (cm)") }); </script> </body>
https://d3js.org/d3.v4.js