Skip to content

Commit cdab9df

Browse files
committed
flatten scatter cross_trace_calc functions
to avoid future accidental use of outer-scope vars
1 parent c602148 commit cdab9df

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

src/traces/scatter/cross_trace_calc.js

+45-45
Original file line numberDiff line numberDiff line change
@@ -30,48 +30,6 @@ module.exports = function crossTraceCalc(gd, plotinfo) {
3030
var groupOpts, interpolate, groupnorm, posAttr, valAttr;
3131
var hasAnyBlanks;
3232

33-
function insertBlank(calcTrace, index, position, traceIndex) {
34-
hasAnyBlanks[traceIndex] = true;
35-
var newEntry = {
36-
i: null,
37-
gap: true,
38-
s: 0
39-
};
40-
newEntry[posAttr] = position;
41-
calcTrace.splice(index, 0, newEntry);
42-
// Even if we're not interpolating, if one trace has multiple
43-
// values at the same position and this trace only has one value there,
44-
// we just duplicate that one value rather than insert a zero.
45-
// We also make it look like a real point - because it's ambiguous which
46-
// one really is the real one!
47-
if(index && position === calcTrace[index - 1][posAttr]) {
48-
var prevEntry = calcTrace[index - 1];
49-
newEntry.s = prevEntry.s;
50-
// TODO is it going to cause any problems to have multiple
51-
// calcdata points with the same index?
52-
newEntry.i = prevEntry.i;
53-
newEntry.gap = prevEntry.gap;
54-
}
55-
else if(interpolate) {
56-
newEntry.s = getInterp(calcTrace, index, position);
57-
}
58-
if(!index) {
59-
// t and trace need to stay on the first cd entry
60-
calcTrace[0].t = calcTrace[1].t;
61-
calcTrace[0].trace = calcTrace[1].trace;
62-
delete calcTrace[1].t;
63-
delete calcTrace[1].trace;
64-
}
65-
}
66-
67-
function getInterp(calcTrace, index, position) {
68-
var pt0 = calcTrace[index - 1];
69-
var pt1 = calcTrace[index + 1];
70-
if(!pt1) return pt0.s;
71-
if(!pt0) return pt1.s;
72-
return pt0.s + (pt1.s - pt0.s) * (position - pt0[posAttr]) / (pt1[posAttr] - pt0[posAttr]);
73-
}
74-
7533
for(var stackGroup in subplotStackOpts) {
7634
groupOpts = subplotStackOpts[stackGroup];
7735
var indices = groupOpts.traceIndices;
@@ -111,20 +69,20 @@ module.exports = function crossTraceCalc(gd, plotinfo) {
11169
posj = cd[j][posAttr];
11270
for(; posj > allPositions[k] && k < allPositions.length; k++) {
11371
// the current trace is missing a position from some previous trace(s)
114-
insertBlank(cd, j, allPositions[k], i);
72+
insertBlank(cd, j, allPositions[k], i, hasAnyBlanks, interpolate, posAttr);
11573
j++;
11674
}
11775
if(posj !== allPositions[k]) {
11876
// previous trace(s) are missing a position from the current trace
11977
for(i2 = 0; i2 < i; i2++) {
120-
insertBlank(calcTraces[indices[i2]], k, posj, i2);
78+
insertBlank(calcTraces[indices[i2]], k, posj, i2, hasAnyBlanks, interpolate, posAttr);
12179
}
12280
allPositions.splice(k, 0, posj);
12381
}
12482
k++;
12583
}
12684
for(; k < allPositions.length; k++) {
127-
insertBlank(cd, j, allPositions[k], i);
85+
insertBlank(cd, j, allPositions[k], i, hasAnyBlanks, interpolate, posAttr);
12886
j++;
12987
}
13088
}
@@ -179,3 +137,45 @@ module.exports = function crossTraceCalc(gd, plotinfo) {
179137
}
180138
}
181139
};
140+
141+
function insertBlank(calcTrace, index, position, traceIndex, hasAnyBlanks, interpolate, posAttr) {
142+
hasAnyBlanks[traceIndex] = true;
143+
var newEntry = {
144+
i: null,
145+
gap: true,
146+
s: 0
147+
};
148+
newEntry[posAttr] = position;
149+
calcTrace.splice(index, 0, newEntry);
150+
// Even if we're not interpolating, if one trace has multiple
151+
// values at the same position and this trace only has one value there,
152+
// we just duplicate that one value rather than insert a zero.
153+
// We also make it look like a real point - because it's ambiguous which
154+
// one really is the real one!
155+
if(index && position === calcTrace[index - 1][posAttr]) {
156+
var prevEntry = calcTrace[index - 1];
157+
newEntry.s = prevEntry.s;
158+
// TODO is it going to cause any problems to have multiple
159+
// calcdata points with the same index?
160+
newEntry.i = prevEntry.i;
161+
newEntry.gap = prevEntry.gap;
162+
}
163+
else if(interpolate) {
164+
newEntry.s = getInterp(calcTrace, index, position, posAttr);
165+
}
166+
if(!index) {
167+
// t and trace need to stay on the first cd entry
168+
calcTrace[0].t = calcTrace[1].t;
169+
calcTrace[0].trace = calcTrace[1].trace;
170+
delete calcTrace[1].t;
171+
delete calcTrace[1].trace;
172+
}
173+
}
174+
175+
function getInterp(calcTrace, index, position, posAttr) {
176+
var pt0 = calcTrace[index - 1];
177+
var pt1 = calcTrace[index + 1];
178+
if(!pt1) return pt0.s;
179+
if(!pt0) return pt1.s;
180+
return pt0.s + (pt1.s - pt0.s) * (position - pt0[posAttr]) / (pt1[posAttr] - pt0[posAttr]);
181+
}

0 commit comments

Comments
 (0)