forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.js
97 lines (80 loc) · 2.85 KB
/
calc.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Copyright 2012-2016, 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 isNumeric = require('fast-isnumeric');
var Axes = require('../../plots/cartesian/axes');
var Lib = require('../../lib');
var subTypes = require('../scatter/subtypes');
var calcColorscale = require('../scatter/colorscale_calc');
var dataArrays = ['a', 'b', 'c'];
var arraysToFill = {a: ['b', 'c'], b: ['a', 'c'], c: ['a', 'b']};
module.exports = function calc(gd, trace) {
var ternary = gd._fullLayout[trace.subplot],
displaySum = ternary.sum,
normSum = trace.sum || displaySum;
var i, j, dataArray, newArray, fillArray1, fillArray2;
// fill in one missing component
for(i = 0; i < dataArrays.length; i++) {
dataArray = dataArrays[i];
if(trace[dataArray]) continue;
fillArray1 = trace[arraysToFill[dataArray][0]];
fillArray2 = trace[arraysToFill[dataArray][1]];
newArray = new Array(fillArray1.length);
for(j = 0; j < fillArray1.length; j++) {
newArray[j] = normSum - fillArray1[j] - fillArray2[j];
}
trace[dataArray] = newArray;
}
// make the calcdata array
var serieslen = trace.a.length;
var cd = new Array(serieslen);
var a, b, c, norm, x, y;
for(i = 0; i < serieslen; i++) {
a = trace.a[i];
b = trace.b[i];
c = trace.c[i];
if(isNumeric(a) && isNumeric(b) && isNumeric(c)) {
a = +a;
b = +b;
c = +c;
norm = displaySum / (a + b + c);
if(norm !== 1) {
a *= norm;
b *= norm;
c *= norm;
}
// map a, b, c onto x and y where the full scale of y
// is [0, sum], and x is [-sum, sum]
// TODO: this makes `a` always the top, `b` the bottom left,
// and `c` the bottom right. Do we want options to rearrange
// these?
y = a;
x = c - b;
cd[i] = {x: x, y: y, a: a, b: b, c: c};
}
else cd[i] = {x: false, y: false};
}
// fill in some extras
var marker, s;
if(subTypes.hasMarkers(trace)) {
// Treat size like x or y arrays --- Run d2c
// this needs to go before ppad computation
marker = trace.marker;
s = marker.size;
if(Array.isArray(s)) {
var ax = {type: 'linear'};
Axes.setConvert(ax);
s = ax.makeCalcdata(trace.marker, 'size');
if(s.length > serieslen) s.splice(serieslen, s.length - serieslen);
}
}
calcColorscale(trace);
// this has migrated up from arraysToCalcdata as we have a reference to 's' here
if(typeof s !== undefined) Lib.mergeArray(s, cd, 'ms');
return cd;
};