From 8a882dec92a4dedf8d0f6440994e3f5c1293c6b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Thu, 14 Feb 2019 14:48:09 -0500 Subject: [PATCH] fix finance trace auto-range - by keeping track of position associated with null values - by considering all positions in position-axis autorange --- src/traces/box/cross_trace_calc.js | 9 ++++--- src/traces/ohlc/calc.js | 2 +- test/jasmine/tests/finance_test.js | 40 +++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/traces/box/cross_trace_calc.js b/src/traces/box/cross_trace_calc.js index 2d42b0b0c74..8c5d4d2fc7e 100644 --- a/src/traces/box/cross_trace_calc.js +++ b/src/traces/box/cross_trace_calc.js @@ -192,11 +192,12 @@ function setPositionOffset(traceType, gd, boxList, posAxis) { vpadminus = pushminus; } - // calcdata[i][j] are in ascending order - var firstPos = calcTrace[0].pos; - var lastPos = calcTrace[calcTrace.length - 1].pos; + var pos = new Array(calcTrace.length); + for(j = 0; j < calcTrace.length; j++) { + pos[j] = calcTrace[j].pos; + } - trace._extremes[axId] = Axes.findExtremes(posAxis, [firstPos, lastPos], { + trace._extremes[axId] = Axes.findExtremes(posAxis, pos, { padded: padded, vpadminus: vpadminus, vpadplus: vpadplus, diff --git a/src/traces/ohlc/calc.js b/src/traces/ohlc/calc.js index 0f69e97811c..ea8545e0d56 100644 --- a/src/traces/ohlc/calc.js +++ b/src/traces/ohlc/calc.js @@ -90,7 +90,7 @@ function calcCommon(gd, trace, x, ya, ptFunc) { cd.push(pt); } else { - cd.push({empty: true}); + cd.push({pos: xi, empty: true}); } } diff --git a/test/jasmine/tests/finance_test.js b/test/jasmine/tests/finance_test.js index 48bc5305254..52ce663fb57 100644 --- a/test/jasmine/tests/finance_test.js +++ b/test/jasmine/tests/finance_test.js @@ -428,7 +428,7 @@ describe('finance charts calc', function() { addJunk(trace1); var out = _calcRaw([trace0, trace1]); - var indices = [0, 1, 2, 3, 4, 5, 6, 7, undefined, undefined, undefined, undefined]; + var indices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; var i = 'increasing'; var d = 'decreasing'; var directions = [i, d, d, i, d, i, d, i, undefined, undefined, undefined, undefined]; @@ -646,6 +646,44 @@ describe('finance charts calc', function() { }); }); +describe('finance charts auto-range', function() { + var gd; + + beforeEach(function() { gd = createGraphDiv(); }); + + afterEach(destroyGraphDiv); + + describe('should give correct results with trailing nulls', function() { + var base = { + x: ['time1', 'time2', 'time3'], + high: [10, 11, null], + close: [5, 6, null], + low: [3, 3, null], + open: [4, 4, null] + }; + + it('- ohlc case', function(done) { + var trace = Lib.extendDeep({}, base, {type: 'ohlc'}); + + Plotly.plot(gd, [trace]).then(function() { + expect(gd._fullLayout.xaxis.range).toBeCloseToArray([-0.5, 2.5], 1); + }) + .catch(failTest) + .then(done); + }); + + it('- candlestick case', function(done) { + var trace = Lib.extendDeep({}, base, {type: 'candlestick'}); + + Plotly.plot(gd, [trace]).then(function() { + expect(gd._fullLayout.xaxis.range).toBeCloseToArray([-0.5, 2.5], 1); + }) + .catch(failTest) + .then(done); + }); + }); +}); + describe('finance charts updates:', function() { 'use strict';