|
| 1 | +/** |
| 2 | +* Copyright 2012-2016, Plotly, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the MIT license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. |
| 7 | +*/ |
| 8 | + |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +var isNumeric = require('fast-isnumeric'); |
| 13 | + |
| 14 | +var Axes = require('../../plots/cartesian/axes'); |
| 15 | +var Lib = require('../../lib'); |
| 16 | + |
| 17 | +var subTypes = require('./subtypes'); |
| 18 | +var calcMarkerColorscale = require('../scatter/marker_colorscale_calc'); |
| 19 | + |
| 20 | +var dataArrays = ['a', 'b', 'c']; |
| 21 | +var arraysToFill = {a: ['b', 'c'], b: ['a', 'c'], c: ['a', 'b']}; |
| 22 | + |
| 23 | + |
| 24 | +module.exports = function calc(gd, trace) { |
| 25 | + var ternary = gd._fullLayout[trace.subplot], |
| 26 | + displaySum = ternary.sum, |
| 27 | + normSum = trace.sum || displaySum; |
| 28 | + |
| 29 | + var i, j, dataArray, newArray, fillArray1, fillArray2; |
| 30 | + |
| 31 | + // fill in one missing component |
| 32 | + for(i = 0; i < dataArrays.length; i++) { |
| 33 | + dataArray = dataArrays[i]; |
| 34 | + if(trace[dataArray] && trace[dataArray.length]) continue; |
| 35 | + |
| 36 | + fillArray1 = arraysToFill[dataArray][0]; |
| 37 | + fillArray2 = arraysToFill[dataArray][1]; |
| 38 | + newArray = new Array(fillArray1.length); |
| 39 | + for(j = 0; j < fillArray1.length; j++) { |
| 40 | + newArray[j] = normSum - fillArray1[j] - fillArray2[j]; |
| 41 | + } |
| 42 | + trace[dataArray] = newArray; |
| 43 | + } |
| 44 | + |
| 45 | + // make the calcdata array |
| 46 | + var serieslen = trace.a.length; |
| 47 | + var cd = new Array(serieslen); |
| 48 | + var a, b, c, norm, x, y; |
| 49 | + for(i = 0; i < serieslen; i++) { |
| 50 | + a = trace.a[i]; |
| 51 | + b = trace.b[i]; |
| 52 | + c = trace.c[i]; |
| 53 | + if(isNumeric(a) && isNumeric(b) && isNumeric(c)) { |
| 54 | + a = Number(a); |
| 55 | + b = Number(b); |
| 56 | + c = Number(c); |
| 57 | + norm = displaySum / (a + b + c); |
| 58 | + if(norm !== 1) { |
| 59 | + a *= norm; |
| 60 | + b *= norm; |
| 61 | + c *= norm; |
| 62 | + } |
| 63 | + // map a, b, c onto x and y where the full scale of y |
| 64 | + // is [0, sum], and x is [-sum, sum] |
| 65 | + // TODO: this makes `a` always the top, `b` the bottom left, |
| 66 | + // and `c` the bottom right. Do we want options to rearrange |
| 67 | + // these? |
| 68 | + y = a; |
| 69 | + x = c - b; |
| 70 | + cd[i] = {x: x, y: y, a: a, b: b, c: c}; |
| 71 | + } |
| 72 | + else cd[i] = {x: false, y: false}; |
| 73 | + } |
| 74 | + |
| 75 | + // fill in some extras |
| 76 | + var marker, s; |
| 77 | + if(subTypes.hasMarkers(trace)) { |
| 78 | + // Treat size like x or y arrays --- Run d2c |
| 79 | + // this needs to go before ppad computation |
| 80 | + marker = trace.marker; |
| 81 | + s = marker.size; |
| 82 | + |
| 83 | + if(Array.isArray(s)) { |
| 84 | + var ax = {type: 'linear'}; |
| 85 | + Axes.setConvert(ax); |
| 86 | + s = ax.makeCalcdata(trace.marker, 'size'); |
| 87 | + if(s.length > serieslen) s.splice(serieslen, s.length - serieslen); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + calcMarkerColorscale(trace); |
| 92 | + |
| 93 | + // this has migrated up from arraysToCalcdata as we have a reference to 's' here |
| 94 | + if(typeof s !== undefined) Lib.mergeArray(s, cd, 'ms'); |
| 95 | + |
| 96 | + return cd; |
| 97 | +}; |
0 commit comments