Skip to content

lasso #730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed

lasso #730

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ export {plot, Mark, marks} from "./plot.js";
export {Area, area, areaX, areaY} from "./marks/area.js";
export {Arrow, arrow} from "./marks/arrow.js";
export {BarX, BarY, barX, barY} from "./marks/bar.js";
export {brush, brushX, brushY} from "./marks/brush.js";
export {Brush, brush, brushX, brushY} from "./marks/brush.js";
export {Cell, cell, cellX, cellY} from "./marks/cell.js";
export {Dot, dot, dotX, dotY} from "./marks/dot.js";
export {Frame, frame} from "./marks/frame.js";
export {Image, image} from "./marks/image.js";
export {Lasso, lasso} from "./marks/lasso.js";
export {Line, line, lineX, lineY} from "./marks/line.js";
export {Link, link} from "./marks/link.js";
export {Pointer, pointer, pointerX, pointerY} from "./marks/pointer.js";
export {Rect, rect, rectX, rectY} from "./marks/rect.js";
export {RuleX, RuleY, ruleX, ruleY} from "./marks/rule.js";
export {Text, text, textX, textY} from "./marks/text.js";
Expand Down
4 changes: 2 additions & 2 deletions src/marks/brush.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ export function brush(data, {x, y, ...options} = {}) {
}

export function brushX(data, {x = identity, ...options} = {}) {
return new Brush(data, {...options, x, y: null});
return new Brush(data, {...options, x});
}

export function brushY(data, {y = identity, ...options} = {}) {
return new Brush(data, {...options, x: null, y});
return new Brush(data, {...options, y});
}
169 changes: 169 additions & 0 deletions src/marks/lasso.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import {create, select, dispatch as dispatcher, line, pointer, polygonContains, curveNatural} from "d3";
import {maybeTuple} from "../options.js";
import {Mark} from "../plot.js";
import {selection, selectionEquals} from "../selection.js";
import {applyIndirectStyles} from "../style.js";

const defaults = {
ariaLabel: "lasso",
fill: "#777",
fillOpacity: 0.3,
stroke: "#666",
strokeWidth: 2
};

export class Lasso extends Mark {
constructor(data, {x, y, ...options} = {}) {
super(
data,
[
{name: "x", value: x, scale: "x"},
{name: "y", value: y, scale: "y"}
],
options,
defaults
);
this.activeElement = null;
}

// The lasso polygons follow the even-odd rule in css, matching the way
// they are computed by polygonContains.
render(index, {x, y}, {x: X, y: Y}, dimensions) {
const margin = 5;
const {ariaLabel, ariaDescription, ariaHidden, fill, fillOpacity, stroke, strokeWidth} = this;
const {marginLeft, width, marginRight, marginTop, height, marginBottom} = dimensions;

const path = line().curve(curveNatural);
const g = create("svg:g")
.call(applyIndirectStyles, {ariaLabel, ariaDescription, ariaHidden, fill, fillOpacity, stroke, strokeWidth});
g.append("rect")
.attr("x", marginLeft)
.attr("y", marginTop)
.attr("width", width - marginLeft - marginRight)
.attr("height", height - marginTop - marginBottom)
.attr("stroke", "none")
.attr("fill", "none")
.attr("cursor", "cross") // TODO
.attr("pointer-events", "all")
.attr("fill-rule", "evenodd");

g.call(lassoer()
.extent([[marginLeft - margin, marginTop - margin], [width - marginRight + margin, height - marginBottom + margin]])
.on("start lasso end cancel", (polygons) => {
g.selectAll("path")
.data(polygons)
.join("path")
.attr("d", path);
let S = null;
let activePolygons = polygons.filter(polygon => polygon.length > 2);
if (activePolygons.length > 0) {
let bw;
if (x.bandwidth && (bw = x.bandwidth() / 2)) activePolygons = activePolygons.map(polygon => polygon.map(p => [p[0] - bw, p[1]]));
if (y.bandwidth && (bw = y.bandwidth() / 2)) activePolygons = activePolygons.map(polygon => polygon.map(p => [p[0], p[1] - bw]));
S = index.filter(i => activePolygons.some(polygon => polygonContains(polygon, [X[i], Y[i]])));

}
if (!selectionEquals(node[selection], S)) {
node[selection] = S;
node.dispatchEvent(new Event("input", {bubbles: true}));
}
}));
const node = g.node();
node[selection] = null;
return node;
}
}

export function lasso(data, {x, y, ...options} = {}) {
([x, y] = maybeTuple(x, y));
return new Lasso(data, {...options, x, y});
}

// set up listeners that will follow this gesture all along
// (even outside the target canvas)
// TODO: in a supporting file
function trackPointer(e, { start, move, out, end }) {
const tracker = {},
id = (tracker.id = e.pointerId),
target = e.target;
tracker.point = pointer(e, target);
target.setPointerCapture(id);

select(target)
.on(`pointerup.${id} pointercancel.${id}`, e => {
if (e.pointerId !== id) return;
tracker.sourceEvent = e;
select(target).on(`.${id}`, null);
target.releasePointerCapture(id);
end && end(tracker);
})
.on(`pointermove.${id}`, e => {
if (e.pointerId !== id) return;
tracker.sourceEvent = e;
tracker.prev = tracker.point;
tracker.point = pointer(e, target);
move && move(tracker);
})
.on(`pointerout.${id}`, e => {
if (e.pointerId !== id) return;
tracker.sourceEvent = e;
tracker.point = null;
out && out(tracker);
});

start && start(tracker);
}

function lassoer() {
const polygons = [];
const dispatch = dispatcher("start", "lasso", "end", "cancel");
let extent;
const lasso = selection => {
const node = selection.node();
let currentPolygon;

selection
.on("touchmove", e => e.preventDefault()) // prevent scrolling
.on("pointerdown", e => {
const p = pointer(e, node);
for (let i = polygons.length - 1; i >= 0; --i) {
if (polygonContains(polygons[i], p)) {
polygons.splice(i, 1);
dispatch.call("cancel", node, polygons);
return;
}
}
trackPointer(e, {
start: p => {
currentPolygon = [constrainExtent(p.point)];
polygons.push(currentPolygon);
dispatch.call("start", node, polygons);
},
move: p => {
currentPolygon.push(constrainExtent(p.point));
dispatch.call("lasso", node, polygons);
},
end: () => {
dispatch.call("end", node, polygons);
}
});
});
};
lasso.on = function(type, _) {
return _ ? (dispatch.on(...arguments), lasso) : dispatch.on(...arguments);
};
lasso.extent = function(_) {
return _ ? (extent = _, lasso) : extent;
};

function constrainExtent(p) {
if (!extent) return p;
return [clamp(p[0], extent[0][0], extent[1][0]), clamp(p[1], extent[0][1], extent[1][1])];
}

function clamp(x, a, b) {
return x < a ? a : x > b ? b : x;
}

return lasso;
}
Loading