Last active
September 27, 2016 00:02
-
-
Save stormpython/3f430f11de260ac0b3be63cd72b0b3f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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