Skip to content

Fixing ohlc showing wrong color when opening equals closing #1619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/traces/candlestick/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,16 @@ exports.calcTransform = function calcTransform(gd, trace, opts) {
y.push(l, o, c, c, c, h);
};

var isPrevThisDirection = null;

for(var i = 0; i < len; i++) {
if(filterFn(open[i], close[i])) {
if(filterFn(open[i], close[i], isPrevThisDirection, open[i - 1], close[i - 1])) {
appendX(i);
appendY(open[i], high[i], low[i], close[i]);
isPrevThisDirection = true;
} else {
isPrevThisDirection = false;
// not adding this candle to this direction bunch
}
}

Expand Down
39 changes: 36 additions & 3 deletions src/traces/ohlc/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,46 @@ exports.makeTransform = function(traceIn, state, direction) {
exports.getFilterFn = function(direction) {
switch(direction) {
case 'increasing':
return function(o, c) { return o <= c; };
return function(o, c, isPrevThisDirection, oprev, cprev) {
Copy link
Contributor

@etpinard etpinard Apr 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The algo looks good 👍

It might worth the time to make it a little DRYer by wrapping isPrevThisDirestion, oprev and cprev in a closure. That is, something like:

exports.getFilterFn = function(direction) {
  return new _getFilterFn(direction);
};

function _getFilterFn(directon) {
  var isPrevThisDirection = null;
  var oprev = null;
  var cprev = null;
  
  var fn;
  switch(direction) {
    case 'increasing':
      fn = function(o, c) { /* */ };
      break;
    case 'decreasing':
      fn = function(o, c) { /* */ };
      break;
  }

  return function(o, c) {
    var out = fn(o, c);
    isPrevThisDirection = !!out;
    oprev = o;
    cprev = c;
    return out;
  };
};

that way we wouldn't have to patch candlestick/transform.js and ohlc/transform.js.

if(o === c) {
if(c > cprev) {
return true; // increasing
} else if(c < cprev) {
return false; // decreasing
} else {
if(isPrevThisDirection === true) {
return true; // determine by last candle
} else if(isPrevThisDirection === false) {
return false; // determine by last candle
} else {
return true; // If we don't have previous data, assume it was increasing
}
}
}
return o < c;
};

case 'decreasing':
return function(o, c) { return o > c; };
return function(o, c, isPrevThisDirection, oprev, cprev) {
if(o === c) {
if(c > cprev) {
return false; // increasing
} else if(c < cprev) {
return true; // decreasing
} else {
if(isPrevThisDirection === true) {
return true; // determine by last candle
} else if(isPrevThisDirection === false) {
return false; // determine by last candle
} else {
return false; // If we don't have previous data, assume it was increasing
}
}
}
return o > c;
};
}
};

exports.addRangeSlider = function(data, layout) {
var hasOneVisibleTrace = false;

Expand Down
7 changes: 6 additions & 1 deletion src/traces/ohlc/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,16 @@ exports.calcTransform = function calcTransform(gd, trace, opts) {
textOut.push(_t, _t, _t, _t, _t, _t, null);
};

var isPrevThisDirection = null;
for(var i = 0; i < len; i++) {
if(filterFn(open[i], close[i])) {
if(filterFn(open[i], close[i], isPrevThisDirection, open[i - 1], close[i - 1])) {
appendX(i);
appendY(open[i], high[i], low[i], close[i]);
appendText(i, open[i], high[i], low[i], close[i]);
isPrevThisDirection = true;
} else {
isPrevThisDirection = false;
// not adding this candle to this direction bunch
}
}

Expand Down