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 all 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
34 changes: 24 additions & 10 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 @@ -72,22 +73,35 @@ exports.sorterDes = function(a, b) { return b - a; };
*/
exports.distinctVals = function(valsIn) {
var vals = valsIn.slice(); // otherwise we sort the original array...
vals.sort(exports.sorterAsc);
vals.sort(exports.sorterAsc); // undefined listed in the end - also works on IE11

var l = vals.length - 1;
var minDiff = (vals[l] - vals[0]) || 1;
var errDiff = minDiff / (l || 1) / 10000;
var v2 = [vals[0]];
var last;
for(last = vals.length - 1; last > -1; last--) {
if(vals[last] !== BADNUM) break;
}

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

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.
Binary file added test/image/baselines/axes_breaks-candlestick2.png
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
]
}
]
}
}
}
23 changes: 23 additions & 0 deletions test/image/mocks/axes_breaks-candlestick2.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions test/jasmine/tests/mock_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ var list = [
'autorange-tozero-rangemode',
'axes_booleans',
'axes_breaks',
'axes_breaks-candlestick',
'axes_breaks-candlestick2',
'axes_breaks-finance',
'axes_breaks-gridlines',
'axes_breaks-night_autorange-reversed',
Expand Down Expand Up @@ -1078,6 +1080,8 @@ 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-candlestick2'] = require('@mocks/axes_breaks-candlestick2');
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