Skip to content

Commit af277b6

Browse files
committed
fixing ohlc showing wrong color when open equals close
1 parent 5e7eaaf commit af277b6

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/traces/candlestick/transform.js

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

117+
var isPrevThisDirection = null;
118+
117119
for(var i = 0; i < len; i++) {
118-
if(filterFn(open[i], close[i])) {
120+
if(filterFn(open[i], close[i], isPrevThisDirection, open[i - 1], close[i - 1])) {
119121
appendX(i);
120122
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
121127
}
122128
}
123129

src/traces/ohlc/helpers.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,46 @@ exports.makeTransform = function(traceIn, state, direction) {
9797
exports.getFilterFn = function(direction) {
9898
switch(direction) {
9999
case 'increasing':
100-
return function(o, c) { return o <= c; };
100+
return function(o, c, isPrevThisDirection, oprev, cprev) {
101+
if (o == c) {
102+
if (c > cprev) {
103+
return true // increasing
104+
} else if (c < cprev) {
105+
return false // decreasing
106+
} else {
107+
if (isPrevThisDirection === true) {
108+
return true // determine by last candle
109+
} else if (isPrevThisDirection == false) {
110+
return false // determine by last candle
111+
} else {
112+
return true // If we don't have previous data, assume it was increasing
113+
}
114+
}
115+
}
116+
return o < c;
117+
};
101118

102119
case 'decreasing':
103-
return function(o, c) { return o > c; };
120+
return function(o, c, isPrevThisDirection, oprev, cprev) {
121+
if (o == c) {
122+
if (c > cprev) {
123+
return false // increasing
124+
} else if (c < cprev) {
125+
return true // decreasing
126+
} else {
127+
if (isPrevThisDirection === true) {
128+
return true // determine by last candle
129+
} else if (isPrevThisDirection == false) {
130+
return false // determine by last candle
131+
} else {
132+
return false // If we don't have previous data, assume it was increasing
133+
}
134+
}
135+
}
136+
return o > c;
137+
};
104138
}
105139
};
106-
107140
exports.addRangeSlider = function(data, layout) {
108141
var hasOneVisibleTrace = false;
109142

src/traces/ohlc/transform.js

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

197+
var isPrevThisDirection = null;
197198
for(var i = 0; i < len; i++) {
198-
if(filterFn(open[i], close[i])) {
199+
if(filterFn(open[i], close[i], isPrevThisDirection, open[i - 1], close[i - 1])) {
199200
appendX(i);
200201
appendY(open[i], high[i], low[i], close[i]);
201202
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
202207
}
203208
}
204209

0 commit comments

Comments
 (0)