-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcheater_basis.js
66 lines (57 loc) · 2.09 KB
/
cheater_basis.js
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
63
64
65
66
/**
* Copyright 2012-2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isArrayOrTypedArray = require('../../lib').isArrayOrTypedArray;
/*
* Construct a 2D array of cheater values given a, b, and a slope.
* If
*/
module.exports = function(a, b, cheaterslope) {
var i, j, ascal, bscal, aval, bval;
var data = [];
var na = isArrayOrTypedArray(a) ? a.length : a;
var nb = isArrayOrTypedArray(b) ? b.length : b;
var adata = isArrayOrTypedArray(a) ? a : null;
var bdata = isArrayOrTypedArray(b) ? b : null;
// If we're using data, scale it so that for data that's just barely
// not evenly spaced, the switch to value-based indexing is continuous.
// This means evenly spaced data should look the same whether value
// or index cheatertype.
if(adata) {
ascal = (adata.length - 1) / (adata[adata.length - 1] - adata[0]) / (na - 1);
}
if(bdata) {
bscal = (bdata.length - 1) / (bdata[bdata.length - 1] - bdata[0]) / (nb - 1);
}
var xval;
var xmin = Infinity;
var xmax = -Infinity;
for(j = 0; j < nb; j++) {
data[j] = [];
bval = bdata ? (bdata[j] - bdata[0]) * bscal : j / (nb - 1);
for(i = 0; i < na; i++) {
aval = adata ? (adata[i] - adata[0]) * ascal : i / (na - 1);
xval = aval - bval * cheaterslope;
xmin = Math.min(xval, xmin);
xmax = Math.max(xval, xmax);
data[j][i] = xval;
}
}
// Normalize cheater values to the 0-1 range. This comes into play when you have
// multiple cheater plots. After careful consideration, it seems better if cheater
// values are normalized to a consistent range. Otherwise one cheater affects the
// layout of other cheaters on the same axis.
var slope = 1.0 / (xmax - xmin);
var offset = -xmin * slope;
for(j = 0; j < nb; j++) {
for(i = 0; i < na; i++) {
data[j][i] = slope * data[j][i] + offset;
}
}
return data;
};