D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
GTHamonville
Full window
Github gist
iris
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } </style> </head> <body> <script> var margin = {top: 20, right:10, bottom: 20, left: 10} var width = 960 - margin.left -margin.right, height = 500 - margin.top - margin.bottom; 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 +")") var x = d3.scaleLinear() .range([0,width]); var y = d3.scaleLinear() .range([height, 0]); var r = d3.scaleSqrt() .range([2, 10]); var color = d3.scaleOrdinal(d3.schemeCategory20); var xAxis = d3.axisBottom().scale(x); var yAxis = d3.axisLeft().scale(y); var tooltip = d3.select("body").append("div") .attr("class", "tooltip") .style("opacity", 0); function scatterplot(el, data, h, w, dim, var_x, var_y, var_c, var_r){ var svg = d3.select("body").append("svg") .attr("width", w + margin.left + margin.right) .attr("height", h + margin.top + margin.bottom).append("g") .attr("transform", "translate("+ margin.left + "," + margin.top +")") var x = d3.scaleLinear() .range([0,width]); var y = d3.scaleLinear() .range([height, 0]); var r = d3.scaleSqrt() .range([2, 10]); var color = d3.scaleOrdinal(d3.schemeCategory20); var xAxis = d3.axisBottom().scale(x); var yAxis = d3.axisLeft().scale(y); var tooltip = d3.select("body").append("div") .attr("class", "tooltip") .style("opacity", 0); x.domain(d3.extent(data, function(d) {return d. sepal_length})) y.domain(d3.extent(data, function(d) {return d. sepal_width})) r.domain(d3.extent(data, function(d) {return d. petal_length})) color.domain(d3.extent(data, function(d) {return d.species})) console.log(data) svg.selectAll(".dot").data(data).enter() .append("circle") .attr("class", "dot") .attr("cx", function(d) {return x(d.sepal_length);}) .attr("cy", function(d) {return y(d.sepal_width);}) .attr("r", function(d) {return r(d.petal_length);}) .style("fill", function(d) {return color(d.species);}) var legend = svg.selectAll(".legend") .data(color.domain()) .enter().append("g") .attr("class", "legend") .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); legend.append("rect") .attr("x", width - 18) .attr("width", 18) .attr("height", 18) .style("fill", color); legend.append("text") .attr("x", width - 24) .attr("y", 9) .attr("dy", ".35em") .style("text-anchor", "end") .text(function(d) { return d; }); } d3.csv("iris.csv",function(error, data){ scatterplot(d3.select("body", data)) data.forEach(function(d){ d.sepal_length = +d.sepal_length d.sepal_width = +d.sepal_width d.petal_length = +d.petal_length d.petal_width = +d.petal_width }); }); </script> </body>
https://d3js.org/d3.v4.min.js