forked from perspective-dev/perspective
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainAxis.js
More file actions
62 lines (51 loc) · 1.84 KB
/
mainAxis.js
File metadata and controls
62 lines (51 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/
import * as d3 from "d3";
import * as fc from "d3fc";
import {extentLinear as customExtent} from "../d3fc/extent/extentLinear";
export const scale = () => d3.scaleLinear();
export const domain = () => {
let valueName = "mainValue";
const extentLinear = customExtent()
.pad([0, 0.1])
.padUnit("percent");
const _domain = function(data) {
const extent = getDataExtentFromArray(data, valueName);
return extentLinear(extent);
};
fc.rebindAll(_domain, extentLinear);
_domain.valueName = (...args) => {
if (!args.length) {
return valueName;
}
valueName = args[0];
return _domain;
};
return _domain;
};
const getDataExtentFromArray = (array, valueName) => {
const dataExtent = array.map(v => getDataExtentFromValue(v, valueName));
const extent = flattenExtent(dataExtent);
return extent;
};
const getDataExtentFromValue = (value, valueName) => {
if (Array.isArray(value)) {
return getDataExtentFromArray(value, valueName);
}
return [value[valueName], value[valueName]];
};
export const label = settings => settings.mainValues.map(v => v.name).join(", ");
function flattenExtent(array) {
const withUndefined = fn => (a, b) => {
if (a === undefined) return b;
if (b === undefined) return a;
return fn(a, b);
};
return array.reduce((r, v) => [withUndefined(Math.min)(r[0], v[0]), withUndefined(Math.max)(r[1], v[1])], [undefined, undefined]);
}