D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
skokenes
Full window
Github gist
Scatterplot with Lasso on points
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .dot { stroke: #000; } .lasso path { stroke: rgb(80,80,80); stroke-width:2px; } .lasso .drawn { fill-opacity:.05 ; } .lasso .loop_close { fill:none; stroke-dasharray: 4,4; } .lasso .origin { fill:#3399FF; fill-opacity:.5; } .not_possible { fill:rgb(200,200,200); } .possible { fill:#EC888C; } svg { margin-left: 30px; margin-top: 30px; } </style> <body> <script src="https://d3js.org/d3.v3.min.js"></script> <script src = "lasso.js"></script> <script> 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.scale.linear() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var color = d3.scale.category10(); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); 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 + ")"); // Create the area where the lasso event can be triggered svg.append("rect") .attr('class','lassoable') .attr("width",width) .attr("height",height) .style("opacity",0); // Lasso functions to execute while lassoing var lasso_start = function() { lasso.items() .attr("r",3.5) // reset size .style("fill",null) // clear all of the fills .classed({"not_possible":true,"selected":false}); // style as not possible }; var lasso_draw = function() { // Style the possible dots lasso.items().filter(function(d) {return d.possible===true}) .classed({"not_possible":false,"possible":true}); // Style the not possible dot lasso.items().filter(function(d) {return d.possible===false}) .classed({"not_possible":true,"possible":false}); }; var lasso_end = function() { // Reset the color of all dots lasso.items() .style("fill", function(d) { return color(d.species); }); // Style the selected dots lasso.items().filter(function(d) {return d.selected===true}) .classed({"not_possible":false,"possible":false}) .attr("r",7); // Reset the style of the not selected dots lasso.items().filter(function(d) {return d.selected===false}) .classed({"not_possible":false,"possible":false}) .attr("r",3.5); }; // Define the lasso var lasso = d3.lasso() .closePathDistance(75) // max distance for the lasso loop to be closed .closePathSelect(true) // can items be selected by closing the path? .hoverSelect(true) // can items by selected by hovering over them? .on("start",lasso_start) // lasso start function .on("draw",lasso_draw) // lasso draw function .on("end",lasso_end); // lasso end function d3.tsv("data.tsv", function(error, data) { data.forEach(function(d) { d.sepalLength = +d.sepalLength; d.sepalWidth = +d.sepalWidth; }); x.domain(d3.extent(data, function(d) { return d.sepalWidth; })).nice(); y.domain(d3.extent(data, function(d) { return d.sepalLength; })).nice(); 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)") var dot_g = svg.append('g') .attr('class','lassoable'); dot_g.selectAll(".dot") .data(data) .enter().append("circle") .attr("id",function(d,i) {return "dot_" + i;}) // added .attr("class", "dot") .attr("r", 3.5) .attr("cx", function(d) { return x(d.sepalWidth); }) .attr("cy", function(d) { return y(d.sepalLength); }) .style("fill", function(d) { return color(d.species); }); lasso.items(d3.selectAll(".dot")); //lasso.items(d3.selectAll("#dot_118")); 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; }); lasso.area(svg.selectAll('.lassoable')); // Init the lasso on the svg:g that contains the dots svg.call(lasso); }); </script>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js