Skip to content

Handle rangebreak gaps in candlestick & ohlc #4814

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

Merged
merged 4 commits into from
May 7, 2020
Merged
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
38 changes: 29 additions & 9 deletions src/lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
var isNumeric = require('fast-isnumeric');
var loggers = require('./loggers');
var identity = require('./identity');
var BADNUM = require('../constants/numerical').BADNUM;

// don't trust floating point equality - fraction of bin size to call
// "on the line" and ensure that they go the right way specified by
Expand Down Expand Up @@ -74,20 +75,39 @@ exports.distinctVals = function(valsIn) {
var vals = valsIn.slice(); // otherwise we sort the original array...
vals.sort(exports.sorterAsc);

var l = vals.length - 1;
var minDiff = (vals[l] - vals[0]) || 1;
var errDiff = minDiff / (l || 1) / 10000;
var v2 = [vals[0]];
var first;
for(first = 0; first < vals.length; first++) {
if(vals[first] !== BADNUM) break;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh interesting, I wouldn't have guessed that the problem traced back to distinctVals, nice find!

TIL:

all undefined elements are sorted to the end of the array, with no call to compareFunction

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

So perhaps all we need is the loop to find last and the rest of this can stay as it was, as we'll never have undefined anywhere else after a sort?

Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe worth testing this in IE before relying on it. I've confirmed in Chrome, FF, Safari on my mac so seems robust on modern browsers.

If it were to fail on IE, I wouldn't trust that sorting works at all then with interspersed undefined values, and better would be to filter these out before even starting (instead of the slice() call)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alexcjohnson good catch! Not working on IE11.

Copy link
Contributor Author

@archmoj archmoj May 7, 2020

Choose a reason for hiding this comment

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

@alexcjohnson Correction. It appear to be working on IE11.

IE11

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alexcjohnson correction number 2: It is sometimes working and sometime not.
So let's fix that for IE11.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. The logic is modified by 2f3b098 & tested in IE11 and it works.


var last;
for(last = vals.length - 1; last > -1; last--) {
if(vals[last] !== BADNUM) break;
}

var minDiff = (vals[last] - vals[first]) || 1;
var errDiff = minDiff / ((last - first) || 1) / 10000;
var newVals = [];
var preV;
for(var i = first; i <= last; i++) {
var v = vals[i];
if(v === BADNUM) continue;

for(var i = 0; i < l; i++) {
// make sure values aren't just off by a rounding error
if(vals[i + 1] > vals[i] + errDiff) {
minDiff = Math.min(minDiff, vals[i + 1] - vals[i]);
v2.push(vals[i + 1]);
var diff = v - preV;

if(preV === undefined) {
newVals.push(v);
preV = v;
} else if(diff > errDiff) {
minDiff = Math.min(minDiff, diff);

newVals.push(v);
preV = v;
}
}

return {vals: v2, minDiff: minDiff};
return {vals: newVals, minDiff: minDiff};
};

/**
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions test/image/mocks/axes_breaks-candlestick.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"data": [
{
"type": "candlestick",
"x": [
Copy link
Contributor

Choose a reason for hiding this comment

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

in this mock, it would be great to see some data in the next visible range, like after 9am the next day, just to capture some of the tick labelling etc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I added another mock in 8a4bae3.
And we are going to revise tick behaviour (including these mocks) in #4734 after this PR is merged.

"2020-01-02 17:00",
"2020-01-02 17:10",
"2020-01-02 17:20",
"2020-01-02 17:30",
"2020-01-02 17:40",
"2020-01-02 17:50",
"2020-01-02 18:00",
"2020-01-02 18:10"
],
"open": [
10,
10,
10,
10,
10,
10,
10,
10
],
"high": [
12,
12,
12,
12,
12,
12,
12,
14
],
"low": [
8,
8,
8,
8,
8,
8,
3,
8
],
"close": [
12,
7,
11,
10.5,
9,
8.5,
3,
14
]
}
],
"layout": {
"width": 600,
"height": 400,
"title": {
"text": "Candlestick with rangebreaks"
},
"xaxis": {
"rangebreaks": [
{
"pattern": "hour",
"bounds": [
18,
9
]
}
]
}
}
}
2 changes: 2 additions & 0 deletions test/jasmine/tests/mock_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var list = [
'autorange-tozero-rangemode',
'axes_booleans',
'axes_breaks',
'axes_breaks-candlestick',
'axes_breaks-finance',
'axes_breaks-gridlines',
'axes_breaks-night_autorange-reversed',
Expand Down Expand Up @@ -1078,6 +1079,7 @@ figs['automargin-title-standoff'] = require('@mocks/automargin-title-standoff');
figs['autorange-tozero-rangemode'] = require('@mocks/autorange-tozero-rangemode');
// figs['axes_booleans'] = require('@mocks/axes_booleans');
figs['axes_breaks'] = require('@mocks/axes_breaks');
figs['axes_breaks-candlestick'] = require('@mocks/axes_breaks-candlestick');
figs['axes_breaks-finance'] = require('@mocks/axes_breaks-finance');
figs['axes_breaks-gridlines'] = require('@mocks/axes_breaks-gridlines');
figs['axes_breaks-night_autorange-reversed'] = require('@mocks/axes_breaks-night_autorange-reversed');
Expand Down