Skip to content

ohlc: use all coords on given x-axis to compute min coord diff #1066

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
Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
55 changes: 41 additions & 14 deletions src/traces/ohlc/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ exports.calcTransform = function calcTransform(gd, trace, opts) {

var xa = axisIds.getFromTrace(gd, trace, 'x'),
ya = axisIds.getFromTrace(gd, trace, 'y'),
tickWidth = convertTickWidth(gd, xa, trace._fullInput.tickwidth);
tickWidth = convertTickWidth(gd, xa, trace);

var open = trace.open,
high = trace.high,
Expand Down Expand Up @@ -191,23 +191,50 @@ exports.calcTransform = function calcTransform(gd, trace, opts) {
trace.text = textOut;
};

function convertTickWidth(gd, xa, tickWidth) {
var fullData = gd._fullData,
coords = [];
function convertTickWidth(gd, xa, trace) {
var fullInput = trace._fullInput,
tickWidth = fullInput.tickwidth,
minDiff = fullInput._minDiff;

// find all coordinates attached to this x-axis
for(var i = 0; i < fullData.length; i++) {
var trace = fullData[i]._fullInput;
if(!minDiff) {
var fullData = gd._fullData,
ohlcTracesOnThisXaxis = [];

if(trace.type === 'ohlc' &&
trace.visible === true &&
trace.xaxis === xa._id &&
trace.x
) {
coords = coords.concat(trace.x.map(xa.d2c));
minDiff = Infinity;

// find min x-coordinates difference of all traces
// attached to this x-axis and stash the result

var i;

for(i = 0; i < fullData.length; i++) {
var _trace = fullData[i]._fullInput;

if(_trace.type === 'ohlc' &&
_trace.visible === true &&
_trace.xaxis === xa._id
) {
ohlcTracesOnThisXaxis.push(_trace);

// - _trace.x may be undefined here,
// it is filled later in calcTransform
//
// - handle trace of length 1 separately.

if(_trace.x && _trace.x.length > 1) {
var _minDiff = Lib.distinctVals(_trace.x.map(xa.d2c)).minDiff;
minDiff = Math.min(minDiff, _minDiff);
}
}
}

// if minDiff is still Infinity here, set it to 1
if(minDiff === Infinity) minDiff = 1;

for(i = 0; i < ohlcTracesOnThisXaxis.length; i++) {
ohlcTracesOnThisXaxis[i]._minDiff = minDiff;
}
}

var minDiff = Lib.distinctVals(coords).minDiff;
return minDiff * tickWidth;
}
72 changes: 72 additions & 0 deletions test/jasmine/tests/finance_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,78 @@ describe('finance charts calc transforms:', function() {
32.87, 33.5, 33.37, 33.37, 33.37, 33.62
]);
});

it('should use the smallest trace minimum x difference to convert *tickwidth* to data coords for all traces attached to a given x-axis', function() {
var trace0 = Lib.extendDeep({}, mock1, {
type: 'ohlc',
tickwidth: 0.5
});

var trace1 = Lib.extendDeep({}, mock1, {
type: 'ohlc',
tickwidth: 0.5
});

// shift time coordinates by 10 hours
trace1.x = trace1.x.map(function(d) {
return d + ' 10:00';
});

var out = _calc([trace0, trace1]);

expect(out[0].x.map(ms2DateTime)).toEqual([
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 I think this is what you wanted. Can you confirm?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, looks great. Thanks!

'2016-08-31 12', '2016-09-01', '2016-09-01', '2016-09-01', '2016-09-01', '2016-09-01 12', null,
'2016-09-03 12', '2016-09-04', '2016-09-04', '2016-09-04', '2016-09-04', '2016-09-04 12', null,
'2016-09-05 12', '2016-09-06', '2016-09-06', '2016-09-06', '2016-09-06', '2016-09-06 12', null,
'2016-09-09 12', '2016-09-10', '2016-09-10', '2016-09-10', '2016-09-10', '2016-09-10 12', null
]);

expect(out[1].x.map(ms2DateTime)).toEqual([
'2016-09-01 12', '2016-09-02', '2016-09-02', '2016-09-02', '2016-09-02', '2016-09-02 12', null,
'2016-09-02 12', '2016-09-03', '2016-09-03', '2016-09-03', '2016-09-03', '2016-09-03 12', null,
'2016-09-04 12', '2016-09-05', '2016-09-05', '2016-09-05', '2016-09-05', '2016-09-05 12', null,
'2016-09-06 12', '2016-09-07', '2016-09-07', '2016-09-07', '2016-09-07', '2016-09-07 12', null
]);

expect(out[2].x.map(ms2DateTime)).toEqual([
'2016-08-31 22', '2016-09-01 10', '2016-09-01 10', '2016-09-01 10', '2016-09-01 10', '2016-09-01 22', null,
'2016-09-03 22', '2016-09-04 10', '2016-09-04 10', '2016-09-04 10', '2016-09-04 10', '2016-09-04 22', null,
'2016-09-05 22', '2016-09-06 10', '2016-09-06 10', '2016-09-06 10', '2016-09-06 10', '2016-09-06 22', null,
'2016-09-09 22', '2016-09-10 10', '2016-09-10 10', '2016-09-10 10', '2016-09-10 10', '2016-09-10 22', null
]);

expect(out[3].x.map(ms2DateTime)).toEqual([
'2016-09-01 22', '2016-09-02 10', '2016-09-02 10', '2016-09-02 10', '2016-09-02 10', '2016-09-02 22', null,
'2016-09-02 22', '2016-09-03 10', '2016-09-03 10', '2016-09-03 10', '2016-09-03 10', '2016-09-03 22', null,
'2016-09-04 22', '2016-09-05 10', '2016-09-05 10', '2016-09-05 10', '2016-09-05 10', '2016-09-05 22', null,
'2016-09-06 22', '2016-09-07 10', '2016-09-07 10', '2016-09-07 10', '2016-09-07 10', '2016-09-07 22', null
]);
});

it('should fallback to a minimum x difference of 0.5 in one-item traces', function() {
var trace0 = Lib.extendDeep({}, mock1, {
type: 'ohlc',
tickwidth: 0.5
});
trace0.x = [ '2016-01-01' ];

var trace1 = Lib.extendDeep({}, mock0, {
type: 'ohlc',
tickwidth: 0.5
});
trace1.x = [ 10 ];

var out = _calc([trace0, trace1]);

var x0 = out[0].x;
expect(x0[x0.length - 2] - x0[0]).toEqual(1);

var x2 = out[2].x;
expect(x2[x2.length - 2] - x2[0]).toEqual(1);

expect(out[1].x).toEqual([]);
expect(out[3].x).toEqual([]);
});
});

describe('finance charts updates:', function() {
Expand Down