Skip to content

Commit 9ba4be9

Browse files
committed
update scatterternary for Plotly.react
1 parent 0752aa5 commit 9ba4be9

File tree

3 files changed

+42
-31
lines changed

3 files changed

+42
-31
lines changed

src/traces/scatterternary/calc.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,35 @@ var dataArrays = ['a', 'b', 'c'];
2020
var arraysToFill = {a: ['b', 'c'], b: ['a', 'c'], c: ['a', 'b']};
2121

2222
module.exports = function calc(gd, trace) {
23-
var ternary = gd._fullLayout[trace.subplot],
24-
displaySum = ternary.sum,
25-
normSum = trace.sum || displaySum;
23+
var ternary = gd._fullLayout[trace.subplot];
24+
var displaySum = ternary.sum;
25+
var normSum = trace.sum || displaySum;
26+
var arrays = {a: trace.a, b: trace.b, c: trace.c};
2627

2728
var i, j, dataArray, newArray, fillArray1, fillArray2;
2829

2930
// fill in one missing component
3031
for(i = 0; i < dataArrays.length; i++) {
3132
dataArray = dataArrays[i];
32-
if(trace[dataArray]) continue;
33+
if(arrays[dataArray]) continue;
3334

34-
fillArray1 = trace[arraysToFill[dataArray][0]];
35-
fillArray2 = trace[arraysToFill[dataArray][1]];
35+
fillArray1 = arrays[arraysToFill[dataArray][0]];
36+
fillArray2 = arrays[arraysToFill[dataArray][1]];
3637
newArray = new Array(fillArray1.length);
3738
for(j = 0; j < fillArray1.length; j++) {
3839
newArray[j] = normSum - fillArray1[j] - fillArray2[j];
3940
}
40-
trace[dataArray] = newArray;
41+
arrays[dataArray] = newArray;
4142
}
4243

4344
// make the calcdata array
44-
var serieslen = trace.a.length;
45+
var serieslen = trace._length;
4546
var cd = new Array(serieslen);
4647
var a, b, c, norm, x, y;
4748
for(i = 0; i < serieslen; i++) {
48-
a = trace.a[i];
49-
b = trace.b[i];
50-
c = trace.c[i];
49+
a = arrays.a[i];
50+
b = arrays.b[i];
51+
c = arrays.c[i];
5152
if(isNumeric(a) && isNumeric(b) && isNumeric(c)) {
5253
a = +a;
5354
b = +b;

src/traces/scatterternary/defaults.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
5555
return;
5656
}
5757

58-
// cut all data arrays down to same length
59-
if(a && len < a.length) traceOut.a = a.slice(0, len);
60-
if(b && len < b.length) traceOut.b = b.slice(0, len);
61-
if(c && len < c.length) traceOut.c = c.slice(0, len);
58+
traceOut._length = len;
6259

6360
coerce('sum');
6461

test/jasmine/tests/scatterternary_test.js

+29-16
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,23 @@ describe('scatterternary defaults', function() {
101101
expect(traceOut.visible).toBe(false);
102102
});
103103

104-
it('should truncate data arrays to the same length (\'c\' is shortest case)', function() {
104+
it('should not truncate data arrays to the same length (\'c\' is shortest case)', function() {
105+
// this is handled at the calc step now via _length.
105106
traceIn = {
106107
a: [1, 2, 3],
107108
b: [1, 2],
108109
c: [1]
109110
};
110111

111112
supplyDefaults(traceIn, traceOut, defaultColor, layout);
112-
expect(traceOut.a).toEqual([1]);
113-
expect(traceOut.b).toEqual([1]);
113+
expect(traceOut.a).toEqual([1, 2, 3]);
114+
expect(traceOut.b).toEqual([1, 2]);
114115
expect(traceOut.c).toEqual([1]);
116+
expect(traceOut._length).toBe(1);
115117
});
116118

117-
it('should truncate data arrays to the same length (\'a\' is shortest case)', function() {
119+
it('should not truncate data arrays to the same length (\'a\' is shortest case)', function() {
120+
// this is handled at the calc step now via _length.
118121
traceIn = {
119122
a: [1],
120123
b: [1, 2, 3],
@@ -123,21 +126,24 @@ describe('scatterternary defaults', function() {
123126

124127
supplyDefaults(traceIn, traceOut, defaultColor, layout);
125128
expect(traceOut.a).toEqual([1]);
126-
expect(traceOut.b).toEqual([1]);
127-
expect(traceOut.c).toEqual([1]);
129+
expect(traceOut.b).toEqual([1, 2, 3]);
130+
expect(traceOut.c).toEqual([1, 2]);
131+
expect(traceOut._length).toBe(1);
128132
});
129133

130-
it('should truncate data arrays to the same length (\'a\' is shortest case)', function() {
134+
it('should not truncate data arrays to the same length (\'a\' is shortest case)', function() {
135+
// this is handled at the calc step now via _length.
131136
traceIn = {
132137
a: [1, 2],
133138
b: [1],
134139
c: [1, 2, 3]
135140
};
136141

137142
supplyDefaults(traceIn, traceOut, defaultColor, layout);
138-
expect(traceOut.a).toEqual([1]);
143+
expect(traceOut.a).toEqual([1, 2]);
139144
expect(traceOut.b).toEqual([1]);
140-
expect(traceOut.c).toEqual([1]);
145+
expect(traceOut.c).toEqual([1, 2, 3]);
146+
expect(traceOut._length).toBe(1);
141147
});
142148

143149
it('should include \'name\' in \'hoverinfo\' default if multi trace graph', function() {
@@ -218,32 +224,39 @@ describe('scatterternary calc', function() {
218224

219225
trace = {
220226
subplot: 'ternary',
221-
sum: 1
227+
sum: 1,
228+
_length: 3
222229
};
223230
});
224231

232+
function get(cd, component) {
233+
return cd.map(function(v) {
234+
return v[component];
235+
});
236+
}
237+
225238
it('should fill in missing component (case \'c\')', function() {
226239
trace.a = [0.1, 0.3, 0.6];
227240
trace.b = [0.3, 0.6, 0.1];
228241

229-
calc(gd, trace);
230-
expect(trace.c).toBeCloseToArray([0.6, 0.1, 0.3]);
242+
cd = calc(gd, trace);
243+
expect(get(cd, 'c')).toBeCloseToArray([0.6, 0.1, 0.3]);
231244
});
232245

233246
it('should fill in missing component (case \'b\')', function() {
234247
trace.a = [0.1, 0.3, 0.6];
235248
trace.c = [0.1, 0.3, 0.2];
236249

237-
calc(gd, trace);
238-
expect(trace.b).toBeCloseToArray([0.8, 0.4, 0.2]);
250+
cd = calc(gd, trace);
251+
expect(get(cd, 'b')).toBeCloseToArray([0.8, 0.4, 0.2]);
239252
});
240253

241254
it('should fill in missing component (case \'a\')', function() {
242255
trace.b = [0.1, 0.3, 0.6];
243256
trace.c = [0.8, 0.4, 0.1];
244257

245-
calc(gd, trace);
246-
expect(trace.a).toBeCloseToArray([0.1, 0.3, 0.3]);
258+
cd = calc(gd, trace);
259+
expect(get(cd, 'a')).toBeCloseToArray([0.1, 0.3, 0.3]);
247260
});
248261

249262
it('should skip over non-numeric values', function() {

0 commit comments

Comments
 (0)