Skip to content
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

revert to d3-voronoi #1343

Merged
merged 1 commit into from
Jun 28, 2019
Merged
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
8 changes: 5 additions & 3 deletions packages/victory-core/src/victory-util/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ function omit(originalObject, keys = []) {

function getPoint(datum) {
const exists = (val) => val !== undefined;
const { _x, _x1, _x0, _y, _y1, _y0 } = datum;
const { _x, _x1, _x0, _voronoiX, _y, _y1, _y0, _voronoiY } = datum;
const defaultX = exists(_x1) ? _x1 : _x;
const defaultY = exists(_y1) ? _y1 : _y;
const point = {
x: exists(_x1) ? _x1 : _x,
x: exists(_voronoiX) ? _voronoiX : defaultX,
x0: exists(_x0) ? _x0 : _x,
y: exists(_y1) ? _y1 : _y,
y: exists(_voronoiY) ? _voronoiY : defaultY,
y0: exists(_y0) ? _y0 : _y
};
return defaults({}, point, datum);
Expand Down
2 changes: 1 addition & 1 deletion packages/victory-voronoi-container/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"author": "Formidable",
"license": "MIT",
"dependencies": {
"delaunay-find": "^0.0.2",
"d3-voronoi": "^1.1.2",
"lodash": "^4.17.5",
"prop-types": "^15.5.8",
"victory-core": "^32.3.0",
Expand Down
81 changes: 43 additions & 38 deletions packages/victory-voronoi-container/src/voronoi-helpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { Selection, Data, Helpers } from "victory-core";
import { assign, throttle, isFunction, isEmpty, includes, isString, isRegExp } from "lodash";
import {
assign,
throttle,
isFunction,
isEmpty,
groupBy,
keys,
includes,
isString,
isRegExp
} from "lodash";
import isEqual from "react-fast-compare";
import Delaunay from "delaunay-find/lib/index.js";
import { voronoi as d3Voronoi } from "d3-voronoi";
import React from "react";

const VoronoiHelpers = {
Expand Down Expand Up @@ -29,8 +39,8 @@ const VoronoiHelpers = {
const voronoiY = (+y + +y0) / 2;
return assign(
{
_voronoiX: props.voronoiDimension === "y" ? undefined : voronoiX,
_voronoiY: props.voronoiDimension === "x" ? undefined : voronoiY,
_voronoiX: props.voronoiDimension === "y" ? 0 : voronoiX,
_voronoiY: props.voronoiDimension === "x" ? 0 : voronoiY,
eventKey: index,
childName: name,
continuous,
Expand Down Expand Up @@ -69,39 +79,34 @@ const VoronoiHelpers = {
return Helpers.reduceChildren(children, iteratee, props);
},

findPoints(datasets, point) {
const x = point._voronoiX;
const y = point._voronoiY;
if (x !== undefined && y !== undefined) {
return [point];
}
return datasets.filter((d) => {
const matchesX = x === undefined || x === d._voronoiX;
const matchesY = y === undefined || y === d._voronoiY;
return matchesX && matchesY;
// returns an array of objects with point and data where point is an x, y coordinate, and data is
// an array of points belonging to that coordinate
mergeDatasets(props, datasets) {
const points = groupBy(datasets, (datum) => {
const { x, y } = Helpers.scalePoint(props, datum);
return `${x},${y}`;
});
return keys(points).map((key) => {
const point = key.split(",");
return {
x: +point[0],
y: +point[1],
points: points[key]
};
});
},

withinRadius(point, mousePosition, radius) {
if (!radius) {
return true;
}
const { x, y } = mousePosition;
const distanceSquared = Math.pow(x - point[0], 2) + Math.pow(y - point[1], 2);
return distanceSquared < Math.pow(radius, 2);
},

getVoronoiPoints(props, mousePosition) {
getVoronoi(props, mousePosition) {
const { width, height, voronoiPadding } = props;
const padding = voronoiPadding || 0;
const voronoiFunction = d3Voronoi()
.x((d) => d.x)
.y((d) => d.y)
.extent([[padding, padding], [width - padding, height - padding]]);
const datasets = this.getDatasets(props);
const scaledData = datasets.map((d) => {
const { x, y } = Helpers.scalePoint(props, d);
return [x, y];
});
const delaunay = Delaunay.from(scaledData);
const index = delaunay.find(mousePosition.x, mousePosition.y, props.vIndex);
const withinRadius = this.withinRadius(scaledData[index], mousePosition, props.radius);
const points = withinRadius ? this.findPoints(datasets, datasets[index]) : [];
return { points, index };
const voronoi = voronoiFunction(this.mergeDatasets(props, datasets));
const size = props.voronoiDimension ? undefined : props.radius;
return voronoi.find(mousePosition.x, mousePosition.y, size);
},

getActiveMutations(props, point) {
Expand Down Expand Up @@ -148,13 +153,12 @@ const VoronoiHelpers = {
});
},

// eslint-disable-next-line max-params
getParentMutation(activePoints, mousePosition, parentSVG, vIndex) {
getParentMutation(activePoints, mousePosition, parentSVG) {
return [
{
target: "parent",
eventKey: "parent",
mutation: () => ({ activePoints, mousePosition, parentSVG, vIndex })
mutation: () => ({ activePoints, mousePosition, parentSVG })
}
];
},
Expand Down Expand Up @@ -192,8 +196,9 @@ const VoronoiHelpers = {
: [];
return this.getParentMutation([], mousePosition, parentSVG).concat(...inactiveMutations);
}
const { points = [], index } = this.getVoronoiPoints(targetProps, mousePosition);
const parentMutations = this.getParentMutation(points, mousePosition, parentSVG, index);
const nearestVoronoi = this.getVoronoi(targetProps, mousePosition);
const points = nearestVoronoi ? nearestVoronoi.data.points : [];
const parentMutations = this.getParentMutation(points, mousePosition, parentSVG);
if (activePoints.length && isEqual(points, activePoints)) {
return parentMutations;
} else {
Expand Down
12 changes: 0 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4015,18 +4015,6 @@ degenerator@^1.0.4:
escodegen "1.x.x"
esprima "3.x.x"

delaunator@^2.0.0:
version "2.0.5"
resolved "https://registry.yarnpkg.com/delaunator/-/delaunator-2.0.5.tgz#c2a9ba2cf3d5aaab8fa0aa3ae82426d3fc0aeaf5"
integrity sha512-GSYmkITO56erpQzv5Pw+8Vg769kurM16IVUq/AcMb5ZCJCtV7Z2agx9lJ7EbbLno8L099iH2d+hvAK34ZnsvIQ==

delaunay-find@^0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/delaunay-find/-/delaunay-find-0.0.2.tgz#261dd25fe3f4fb31ddd016a0e20f58a21b584e0d"
integrity sha512-bg2Kx35l/1mHNzICc+QUCEq14CZzLnXSR71NmQxLwACoq38otL0c4mXCXs6xpw4v4rgLQJphjT9aHyRZcdsCnw==
dependencies:
delaunator "^2.0.0"

delayed-stream@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
Expand Down