Skip to content

Instantly share code, notes, and snippets.

@stormpython
Last active September 27, 2016 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stormpython/3f430f11de260ac0b3be63cd72b0b3f6 to your computer and use it in GitHub Desktop.
Save stormpython/3f430f11de260ac0b3be63cd72b0b3f6 to your computer and use it in GitHub Desktop.
import { cloneDeep, findIndex, first, isFunction } from 'lodash';
export default function () {
let x = (d) => d.x;
let y = (d) => d.y;
let xScale = d3.scale.linear();
let yScale = d3.scale.linear();
function X(d, i) {
return xScale(x.call(this, d, i));
}
function Y(d, i) {
return yScale(y.call(this, d, i));
}
function pixelatePoint(d, i) {
return `${parseInt(X.call(this, d, i))},${parseInt(Y.call(this, d, i))}`;
}
function pixelate(data) {
return data.map((d, i) => {
const datum = cloneDeep(d);
if (!datum.coords) { datum.coords = {}; }
datum.coords.x = X.call(this, d, i);
datum.coords.y = Y.call(this, d, i);
datum.coords.d = pixelatePoint(d);
return datum;
})
.filter((d, i, array) => {
const duplicatePoint = findIndex(array, (o) => {
return o.coords.d === d.coords.d;
});
if (duplicatePoint !== i && duplicatePoint !== -1) {
const o = array[duplicatePoint];
if (!o.overlaps) { o.overlaps = []; }
o.overlaps.push(d);
}
return duplicatePoint === i;
});
}
// Public API
pixelate.x = (...args) => {
if (!args.length) { return x; }
const value = first(args);
x = isFunction(value) ? value : x;
return pixelate;
};
pixelate.y = (...args) => {
if (!args.length) { return y; }
const value = first(args);
y = isFunction(value) ? value : y;
return pixelate;
};
pixelate.xScale = (...args) => {
if (!args.length) { return xScale; }
const value = first(args);
xScale = isFunction(value) ? value : xScale;
return pixelate;
};
pixelate.yScale = (...args) => {
if (!args.length) { return yScale; }
const value = first(args);
yScale = isFunction(value) ? value : yScale;
return pixelate;
};
return pixelate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment