Skip to content

Commit c4f7703

Browse files
committed
isolating state logic in getFilterFn
1 parent 20494db commit c4f7703

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

src/traces/candlestick/transform.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,10 @@ exports.calcTransform = function calcTransform(gd, trace, opts) {
114114
y.push(l, o, c, c, c, h);
115115
};
116116

117-
var isPrevThisDirection = null;
118-
119117
for(var i = 0; i < len; i++) {
120-
if(filterFn(open[i], close[i], isPrevThisDirection, open[i - 1], close[i - 1])) {
118+
if(filterFn(open[i], close[i])) {
121119
appendX(i);
122120
appendY(open[i], high[i], low[i], close[i]);
123-
isPrevThisDirection = true;
124-
} else {
125-
isPrevThisDirection = false;
126-
// not adding this candle to this direction bunch
127121
}
128122
}
129123

src/traces/ohlc/helpers.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,15 @@ exports.makeTransform = function(traceIn, state, direction) {
9595
};
9696

9797
exports.getFilterFn = function(direction) {
98+
99+
var fn;
100+
var isPrevThisDirection = null;
101+
var oprev = null;
102+
var cprev = null;
103+
98104
switch(direction) {
99105
case 'increasing':
100-
return function(o, c, isPrevThisDirection, oprev, cprev) {
106+
var fn = function(o, c) {
101107
if(o === c) {
102108
if(c > cprev) {
103109
return true; // increasing
@@ -115,9 +121,10 @@ exports.getFilterFn = function(direction) {
115121
}
116122
return o < c;
117123
};
124+
break;
118125

119126
case 'decreasing':
120-
return function(o, c, isPrevThisDirection, oprev, cprev) {
127+
var fn = function(o, c) {
121128
if(o === c) {
122129
if(c > cprev) {
123130
return false; // increasing
@@ -135,7 +142,17 @@ exports.getFilterFn = function(direction) {
135142
}
136143
return o > c;
137144
};
145+
break
138146
}
147+
148+
return function(o, c) {
149+
var out = fn(o, c);
150+
isPrevThisDirection = !!out;
151+
oprev = o;
152+
cprev = c;
153+
return out;
154+
};
155+
139156
};
140157
exports.addRangeSlider = function(data, layout) {
141158
var hasOneVisibleTrace = false;

src/traces/ohlc/transform.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,11 @@ exports.calcTransform = function calcTransform(gd, trace, opts) {
194194
textOut.push(_t, _t, _t, _t, _t, _t, null);
195195
};
196196

197-
var isPrevThisDirection = null;
198197
for(var i = 0; i < len; i++) {
199-
if(filterFn(open[i], close[i], isPrevThisDirection, open[i - 1], close[i - 1])) {
198+
if(filterFn(open[i], close[i])) {
200199
appendX(i);
201200
appendY(open[i], high[i], low[i], close[i]);
202201
appendText(i, open[i], high[i], low[i], close[i]);
203-
isPrevThisDirection = true;
204-
} else {
205-
isPrevThisDirection = false;
206-
// not adding this candle to this direction bunch
207202
}
208203
}
209204

0 commit comments

Comments
 (0)