D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
adurivault
Full window
Github gist
Iris Regression
Built with
blockbuilder.org
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>D3 Page Template</title> <script src="https://d3js.org/d3.v4.min.js"></script> <style> div.bar { display: inline-block; width: 20px; height: 75px; /* We'll override height later */ background-color: teal; margin-right: 2px; } </style> </head> <body> <script type="text/javascript"> //Définition des variables globales var w = 1000; var h = 500; var scalePadding = 80; var axisPadding = 60; var dataset; var xScale; var yScale; var xMax; var xMin; var yMax; var yMin; var xAxis; var yAxis; //Chargement des données d3.csv("iris.csv", function(error, data) { if (error) { console.log(error); } else { dataset = data; defineScales(); drawScatterPlot(); addAxes(); draw_regression("setosa", "orange"); draw_regression("versicolor", "red"); draw_regression("virginica", "blue"); } function defineScales(){ // Défintions valeurs extr^mes données xMin = d3.min(dataset, function(d) {return d["sepal_length"]}); xMax = d3.max(dataset, function(d) {return d["sepal_length"]}); yMin = d3.min(dataset, function(d) {return d["sepal_width"]}); yMax = d3.max(dataset, function(d) {return d["sepal_width"]}); xScale = d3.scaleLinear() .domain([xMin, xMax]) .range([scalePadding, w - scalePadding]) yScale = d3.scaleLinear() .domain([yMin, yMax]) .range([h - scalePadding, scalePadding]) } function drawScatterPlot(){ // Axes xAxis = d3.axisBottom() .scale(xScale); yAxis = d3.axisLeft() .scale(yScale); // Scatterplot values var points = svg.selectAll(".point") .data(dataset) .enter() .append("path") .attr("fill", function(d){ if (d["species"] == "setosa") {return "orange"} else if (d["species"] == "versicolor") {return "red"} else if (d["species"] == "virginica") {return "blue"} }) .attr("d", d3.symbol().type(function(d) { if (d["species"] == "setosa") {return d3.symbolStar} else if (d["species"] == "versicolor") {return d3.symbolCross} else if (d["species"] == "virginica") {return d3.symbolCircle} })) .attr("transform", function(d) {return "translate(" + xScale(d["sepal_length"]) + "," + yScale(d["sepal_width"]) + ")";}); } function draw_regression(species, color) { // Filtered dataset with only selected species var ds = dataset.filter(function(d) {return d["species"] == species}); var x_mean = d3.mean(ds, function(d) {return d["sepal_length"]}); var y_mean = d3.mean(ds, function(d) {return d["sepal_width"]}); var term1 = 0; var term2 = 0; for (i = 0; i < ds.length; i++) { xr = ds[i]["sepal_length"] - x_mean; yr = ds[i]["sepal_width"] - y_mean; term1 += xr * yr; term2 += xr * xr; } //Regression coeffs var b1 = term1 / term2; var b0 = y_mean - (b1 * x_mean); // Plot the line for the regression console.log(xScale) var lines = svg.append("line") .attr("x1", xScale(xMin)) .attr("y1", yScale(b0 + b1 * xMin)) .attr("x2", xScale(d3.max(dataset, function(d) {return d["sepal_length"]}))) .attr("y2", yScale(b0 + b1 * d3.max(dataset, function(d) {return d["sepal_length"]}))) .attr("stroke", color) .attr("stroke-width", 2); } function addAxes(){ svg.append("g") .attr("transform", "translate(0," + (h - axisPadding) + ")") .call(xAxis); svg.append("g") .attr("transform", "translate(" + axisPadding + ",0)") .call(yAxis); svg.append("text") .attr("text-anchor", "middle") .attr("transform", "translate("+ (scalePadding/3) +","+(h/2)+")rotate(-90)") .attr("font-family", "sans-serif") .text("Sepal Width"); svg.append("text") .attr("text-anchor", "middle") .attr("transform", "translate("+ (w/2) +","+(h-(scalePadding/3))+")") .attr("font-family", "sans-serif") .text("Sepal Length"); } }); svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); </script> </body> </html>
https://d3js.org/d3.v4.min.js