Published
Edited
Apr 17, 2019
6 forks
19 stars
Also listed in…
Work Utils
Insert cell
Insert cell
{
const svgNode = d3.select(svg`<svg width=300 height=200></svg>`)
const circles = svgNode.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("r", 7)
.attr("cx", d=>d[1])
.attr("cy", d=>d[2])
.attr('fill','steelblue')
// ---------------- LASSO STUFF . ----------------
var lasso_start = function() {
console.log('start')
lasso.items()
.attr("r",7)
.classed("not_possible",true)
.classed("selected",false);
};

var lasso_draw = function() {
console.log('draw')
lasso.possibleItems()
.classed("not_possible",false)
.classed("possible",true);
lasso.notPossibleItems()
.classed("not_possible",true)
.classed("possible",false);
};

var lasso_end = function() {
console.log('end')
lasso.items()
.classed("not_possible",false)
.classed("possible",false);
lasso.selectedItems()
.classed("selected",true)
.attr("r",7);
lasso.notSelectedItems()
.attr("r",3.5);
};
const lasso = d3.lasso()
.closePathDistance(305)
.closePathSelect(true)
.targetArea(svgNode)
.items(circles)
.on("start",lasso_start)
.on("draw",lasso_draw)
.on("end",lasso_end);

svgNode.call(lasso);
return svgNode.node();
}
Insert cell
data = [
["Arsenal",30, 30],
["Chelsea", 50,100],
["Liverpool",100, 70],
["Manchester City", 74,140],
["Manchester United",150,30],
["Tottenham",203,100]
]
Insert cell
styles = html`
<div></div>
<style>
.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;
}
.selected {
fill: steelblue;
}
</style>
`
Insert cell
d3code = require('d3@v5')
Insert cell
d3 = {
const d3 = Object.assign(d3code,{lasso:d3lasso.lasso});
window.d3 = d3;
return d3;
}
Insert cell
d3lasso = require('d3-lasso')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more