-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from jtrim-ons/new-api-for-calcExtents
New API for calcExtents (Issue #44)
- Loading branch information
Showing
7 changed files
with
118 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,73 @@ | ||
/* -------------------------------------------- | ||
* | ||
* Calculate the extents of desired fields | ||
* For example, a fields object like this: | ||
* `{'x': d => d.x, 'y': d => d.y}` | ||
* For data like this: | ||
* [{ x: 0, y: -10 }, { x: 10, y: 0 }, { x: 5, y: 10 }] | ||
* Returns an object like: | ||
* `{x: [0, 10], y: [-10, 10]}` if `fields` is | ||
* `[{field:'x', accessor: d => d.x}, {field:'y', accessor: d => d.y}]` | ||
* `{ x: [0, 10], y: [-10, 10] }` | ||
* | ||
* -------------------------------------------- | ||
*/ | ||
export default function calcExtents (data, fields) { | ||
if (!Array.isArray(data) || data.length === 0) return null; | ||
if (!Array.isArray(data)) { | ||
throw new TypeError('The first argument of calcExtents() must be an array.'); | ||
} | ||
|
||
if ( | ||
Array.isArray(fields) | ||
|| fields === undefined | ||
|| fields === null | ||
) { | ||
throw new TypeError('The second argument of calcExtents() must be an ' | ||
+ 'object with field names as keys as accessor functions as values.'); | ||
} | ||
|
||
const extents = {}; | ||
const fl = fields.length; | ||
|
||
const keys = Object.keys(fields); | ||
const kl = keys.length; | ||
let i; | ||
let j; | ||
let f; | ||
let val; | ||
let k; | ||
let s; | ||
let min; | ||
let max; | ||
let acc; | ||
let val; | ||
|
||
if (fl) { | ||
for (i = 0; i < fl; i += 1) { | ||
const firstRow = fields[i].accessor(data[0]); | ||
if (firstRow === undefined || firstRow === null || Number.isNaN(firstRow) === true) { | ||
extents[fields[i].field] = [Infinity, -Infinity]; | ||
} else { | ||
extents[fields[i].field] = Array.isArray(firstRow) ? firstRow : [firstRow, firstRow]; | ||
} | ||
} | ||
const dl = data.length; | ||
for (i = 0; i < dl; i += 1) { | ||
for (j = 0; j < fl; j += 1) { | ||
f = fields[j]; | ||
val = f.accessor(data[i]); | ||
s = f.field; | ||
if (Array.isArray(val)) { | ||
const vl = val.length; | ||
for (let k = 0; k < vl; k += 1) { | ||
if (val[k] !== undefined && val[k] !== null && Number.isNaN(val[k]) === false) { | ||
if (val[k] < extents[s][0]) { | ||
extents[s][0] = val[k]; | ||
} | ||
if (val[k] > extents[s][1]) { | ||
extents[s][1] = val[k]; | ||
} | ||
const dl = data.length; | ||
for (i = 0; i < kl; i += 1) { | ||
s = keys[i]; | ||
acc = fields[s]; | ||
min = null; | ||
max = null; | ||
for (j = 0; j < dl; j += 1) { | ||
val = acc(data[j]); | ||
if (Array.isArray(val)) { | ||
const vl = val.length; | ||
for (k = 0; k < vl; k += 1) { | ||
if (val[k] !== undefined && val[k] !== null && Number.isNaN(val[k]) === false) { | ||
if (min === null || val[k] < min) { | ||
min = val[k]; | ||
} | ||
if (max === null || val[k] > max) { | ||
max = val[k]; | ||
} | ||
} | ||
} else if (val !== undefined && val !== null && Number.isNaN(val) === false) { | ||
if (val < extents[s][0]) { | ||
extents[s][0] = val; | ||
} | ||
if (val > extents[s][1]) { | ||
extents[s][1] = val; | ||
} | ||
} | ||
} else if (val !== undefined && val !== null && Number.isNaN(val) === false) { | ||
if (min === null || val < min) { | ||
min = val; | ||
} | ||
if (max === null || val > max) { | ||
max = val; | ||
} | ||
} | ||
} | ||
} else { | ||
return null; | ||
extents[s] = [min, max]; | ||
} | ||
|
||
return extents; | ||
} |
This file contains 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
File renamed without changes.
This file contains 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
This file contains 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