Skip to content

Instantly share code, notes, and snippets.

@junwang23
Forked from gtb104/README.markdown
Last active September 13, 2017 18:03
Show Gist options
  • Save junwang23/bfcf242c09f0aaa0d6a27cdc84285a8e to your computer and use it in GitHub Desktop.
Save junwang23/bfcf242c09f0aaa0d6a27cdc84285a8e to your computer and use it in GitHub Desktop.
d3 Polybrush with d3.js v4
license: mit

This is a modified version of the polybrush plugin, originaly created by Geoffrey T. Bell, for d3.js to make it compatible with d3-4.x-api.

The d3-3.x based plugin is forked from: https://gist.github.com/gtb104/3667340

The changes include adapting to d3-4.x-api and a few minor fixes.

Now the plugin can be instantiated like d3.brush() in d3-4.x-api.

var brush = d3.polybrush()
  .x({x_scale})
  .y({y_scale});

d3.select("svg")
  .append("g")
  .call(brush);

Please keep in mind that the extent in the polybrush is very different from that in d3-brush. So, the working area of the brush can only be assigned by the range of the {x_scale} and {y_scale}.

Three events are dispatched, which are start, brush, and end, consistent with the events of d3-brush. The event names are a bit different in the d3-3.x based version.

The rest is the same as using the d3-3.x version of the plugin.

Usage:
Click to add an anchor point, double click to close the selection.
Drag the selection to reposition it.
Double click outside the selection to clear it.

Check the git repository polybrushv4

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://d3js.org/d3.v4.min.js"></script>
<script type="text/javascript" src="polybrush.js"></script>
<style type="text/css">
.brush .extent {
stroke: #000;
stroke-width: 1.5px;
fill: #000;
fill-opacity: 0.3;
}
.voronoi {
fill: steelblue;
fill-opacity: 0.4;
stroke: steelblue;
stroke-opacity: 0.5;
}
.voronoi.selected {
fill: red;
fill-opacity: 0.4;
stroke: darkred;
stroke-opacity: 0.5;
}
.point.selected {
fill: red;
stroke: darkred;
stroke-width: 1;
}
</style>
</head>
<body>
<div id="chart"></div>
<script type="text/javascript">
var w = 960,
h = 500;
var brush = d3.polybrush()
.x(d3.scaleLinear().range([0, w]))
.y(d3.scaleLinear().range([0, h]))
.on("start", function() {
svg.selectAll(".selected").classed("selected", false);
})
.on("brush", function() {
// set the 'selected' class for the circle
svg.selectAll(".point").classed("selected", function(d) {
//get the associated circle
var id = d3.select(this).attr("id");
var i = id.substr(id.indexOf("-") + 1, id.length);
var vornoi = d3.select("#path-" + i);
// set the 'selected' class for the path
if (brush.isWithinExtent(d[0], d[1])) {
vornoi.classed("selected", true);
return true;
} else {
vornoi.classed("selected", false);
return false;
}
});
});
var vertices = d3.range(200).map(function(d) {
return [Math.random() * w, Math.random() * h];
});
var svg = d3.select("#chart")
.append("svg:svg")
.attr("width", w)
.attr("height", h);
var paths, points, clips;
clips = svg.append("svg:g").attr("id", "point-clips");
points = svg.append("svg:g").attr("id", "points");
paths = svg.append("svg:g").attr("id", "point-paths");
clips.selectAll("clipPath")
.data(vertices)
.enter().append("svg:clipPath")
.attr("id", function(d, i) {
return "clip-" + i;
})
.append("svg:circle")
.attr('cx', function(d) {
return d[0];
})
.attr('cy', function(d) {
return d[1];
})
.attr('r', 20);
var voronoi = d3.voronoi()
.extent([
[-1, -1],
[w + 1, h + 1]
]);
paths.selectAll("path")
.data(voronoi.polygons(vertices))
.enter().append("svg:path")
.attr("d", function(d) {
return "M" + d.join(",") + "Z";
})
.attr("id", function(d, i) {
return "path-" + i;
})
.attr("clip-path", function(d, i) {
return "url(#clip-" + i + ")";
})
.attr("class", "voronoi");
points.selectAll("circle")
.data(vertices)
.enter().append("svg:circle")
.attr("class", "point")
.attr("id", function(d, i) {
return "point-" + i;
})
.attr("transform", function(d) {
return "translate(" + d + ")";
})
.attr("r", 2)
.attr('stroke', 'none');
svg.append("svg:g")
.attr("class", "brush")
.call(brush);
</script>
</body>
</html>
/*
Copyright (c) 2012 Geoffrey T. Bell
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function(d3) {
d3.polybrush = function() {
var dispatch = d3.dispatch("start", "brush", "end"),
el = null,
x = null,
y = null,
extent = [],
firstClick = true,
firstTime = true,
wasDragged = false,
origin = null,
line = d3.line()
.x(function(d) {
return d[0];
})
.y(function(d) {
return d[1];
});
var brush = function(g) {
el = g;
g.each(function() {
var bg, e, fg;
g = d3.select(this)
.style("pointer-events", "all")
.on("click.brush", addAnchor);
bg = g.selectAll(".background")
.data([0]).enter()
.append("rect")
.attr("class", "background")
.style("visibility", "hidden")
.style("cursor", "crosshair");
fg = g.selectAll(".extent")
.data([extent]).enter()
.append("path")
.attr("class", "extent")
.style("cursor", "move");
if (x) {
e = scaleExtent(x.range());
bg.attr("x", e[0]).attr("width", e[1] - e[0]);
}
if (y) {
e = scaleExtent(y.range());
bg.attr("y", e[0]).attr("height", e[1] - e[0]);
}
});
};
var drawPath = function() {
return el.each(function() {
d3.select(this)
.selectAll("g path").attr("d", function(d) {
return line(d) + "Z";
});
});
};
var scaleExtent = function(domain) {
var start, stop;
start = domain[0];
stop = domain[domain.length - 1];
if (start < stop) {
return [start, stop];
} else {
return [stop, start];
}
};
var withinBounds = function(point) {
var rangeX, rangeY, _x, _y;
rangeX = scaleExtent(x.range());
rangeY = scaleExtent(y.range());
_x = Math.max(rangeX[0], Math.min(rangeX[1], point[0]));
_y = Math.max(rangeY[0], Math.min(rangeY[1], point[1]));
return point[0] === _x && point[1] === _y;
};
var moveAnchor = function(target) {
var moved, point;
point = d3.mouse(target);
if (firstTime) {
extent.push(point);
firstTime = false;
} else {
if (withinBounds(point)) {
extent.splice(extent.length - 1, 1, point);
}
drawPath();
dispatch.call("brush", this);
}
};
var closePath = function() {
var w;
w = d3.select(window);
w.on("dblclick.brush", null).on("mousemove.brush", null);
firstClick = true;
if (extent.length === 2 && extent[0][0] === extent[1][0] && extent[0][1] === extent[1][1]) {
extent.splice(0, extent.length);
}
d3.select(".extent").on("mousedown.brush", moveExtent);
return dispatch.call("end", this);
};
var addAnchor = function() {
var g, w,
_this = this;
g = d3.select(this);
w = d3.select(window);
firstTime = true;
if (wasDragged) {
wasDragged = false;
return;
}
if (firstClick) {
extent.splice(0, extent.length);
firstClick = false;
d3.select(".extent").on("mousedown.brush", null);
w.on("mousemove.brush", function() {
return moveAnchor(_this);
}).on("dblclick.brush", closePath);
dispatch.call("start", this);
}
if (extent.length > 1) {
extent.pop();
}
extent.push(d3.mouse(this));
return drawPath();
};
var dragExtent = function(target) {
var checkBounds, fail, p, point, scaleX, scaleY, updateExtentPoint, _i, _j, _len, _len1;
point = d3.mouse(target);
scaleX = point[0] - origin[0];
scaleY = point[1] - origin[1];
fail = false;
origin = point;
updateExtentPoint = function(p) {
p[0] += scaleX;
p[1] += scaleY;
};
for (_i = 0, _len = extent.length; _i < _len; _i++) {
p = extent[_i];
updateExtentPoint(p);
}
checkBounds = function(p) {
if (!withinBounds(p)) {
fail = true;
}
return fail;
};
for (_j = 0, _len1 = extent.length; _j < _len1; _j++) {
p = extent[_j];
checkBounds(p);
}
if (fail) {
return;
}
drawPath();
return dispatch.call("brush", this, {
mode: "move"
});
};
var dragStop = function() {
var w;
w = d3.select(window);
w.on("mousemove.brush", null).on("mouseup.brush", null);
wasDragged = true;
return dispatch.call("end", this);
};
var moveExtent = function() {
var _this = this;
d3.event.stopPropagation();
d3.event.preventDefault();
if (firstClick && !brush.empty()) {
d3.select(window).on("mousemove.brush", function() {
return dragExtent(_this);
}).on("mouseup.brush", dragStop);
origin = d3.mouse(this);
}
};
brush.isWithinExtent = function(x, y) {
var i, j, len, p1, p2, ret, _i, _len;
len = extent.length;
j = len - 1;
ret = false;
for (i = _i = 0, _len = extent.length; _i < _len; i = ++_i) {
p1 = extent[i];
p2 = extent[j];
if ((p1[1] > y) !== (p2[1] > y) && x < (p2[0] - p1[0]) * (y - p1[1]) / (p2[1] - p1[1]) + p1[0]) {
ret = !ret;
}
j = i;
}
return ret;
};
brush.x = function(z) {
if (!arguments.length) {
return x;
}
x = z;
return brush;
};
brush.y = function(z) {
if (!arguments.length) {
return y;
}
y = z;
return brush;
};
brush.extent = function(z) {
if (!arguments.length) {
return extent;
}
extent = z;
return brush;
};
brush.clear = function() {
extent.splice(0, extent.length);
return brush;
};
brush.empty = function() {
return extent.length === 0;
};
brush.on = function(typename, callback) {
dispatch.on(typename, callback);
return brush;
};
return brush;
};
})(d3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment