From 002c403bacdd704c1ba063aae3258be72d73290a Mon Sep 17 00:00:00 2001 From: Anders Dalvander Date: Tue, 29 May 2018 22:09:55 +0200 Subject: [PATCH 1/2] treat zero as valid fixes #2660 --- src/components/drawing/index.js | 2 +- src/components/fx/hover.js | 2 +- test/jasmine/tests/hover_label_test.js | 40 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/components/drawing/index.js b/src/components/drawing/index.js index 0e2dd1b10b8..ae9c8120e16 100644 --- a/src/components/drawing/index.js +++ b/src/components/drawing/index.js @@ -650,7 +650,7 @@ drawing.textPointStyle = function(s, trace, gd) { var p = d3.select(this); var text = Lib.extractOption(d, trace, 'tx', 'text'); - if(!text) { + if(!text && text !== 0) { p.remove(); return; } diff --git a/src/components/fx/hover.js b/src/components/fx/hover.js index 0da92d4a893..ff8175c69c5 100644 --- a/src/components/fx/hover.js +++ b/src/components/fx/hover.js @@ -842,7 +842,7 @@ function createHoverText(hoverData, opts, gd) { else if(d.yLabel === undefined) text = d.xLabel; else text = '(' + d.xLabel + ', ' + d.yLabel + ')'; - if(d.text && !Array.isArray(d.text)) { + if((d.text || d.text === 0) && !Array.isArray(d.text)) { text += (text ? '
' : '') + d.text; } diff --git a/test/jasmine/tests/hover_label_test.js b/test/jasmine/tests/hover_label_test.js index aa015178067..8d2a33376b3 100644 --- a/test/jasmine/tests/hover_label_test.js +++ b/test/jasmine/tests/hover_label_test.js @@ -132,6 +132,46 @@ describe('hover info', function() { }); }); + describe('hover info text with 0', function() { + var mockCopy = Lib.extendDeep({}, mock); + + mockCopy.data[0].text = []; + // we treat number 0 as valid text + // see https://github.com/plotly/plotly.js/issues/2660 + mockCopy.data[0].text[17] = 0; + mockCopy.data[0].hoverinfo = 'text'; + mockCopy.data[0].mode = 'lines+markers+text'; + + beforeEach(function(done) { + Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done); + }); + + it('responds to hover text', function() { + var gd = document.getElementById('graph'); + Fx.hover('graph', evt, 'xy'); + + var hoverTrace = gd._hoverdata[0]; + + expect(hoverTrace.curveNumber).toEqual(0); + expect(hoverTrace.pointNumber).toEqual(17); + expect(hoverTrace.x).toEqual(0.388); + expect(hoverTrace.y).toEqual(1); + expect(hoverTrace.text).toEqual(0); + + var txs = d3.select(gd).selectAll('.textpoint text'); + + expect(txs.size()).toEqual(1); + + txs.each(function() { + expect(d3.select(this).text()).toEqual('0'); + }); + + assertHoverLabelContent({ + nums: '0' + }); + }); + }); + describe('hover info all', function() { var mockCopy = Lib.extendDeep({}, mock); From fc05ca0e582243c4a369915d73dbacff46307573 Mon Sep 17 00:00:00 2001 From: Anders Dalvander Date: Wed, 30 May 2018 20:46:18 +0200 Subject: [PATCH 2/2] PR suggestions: toBe instead of toEqual --- test/jasmine/tests/hover_label_test.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/jasmine/tests/hover_label_test.js b/test/jasmine/tests/hover_label_test.js index 8d2a33376b3..af16777fbba 100644 --- a/test/jasmine/tests/hover_label_test.js +++ b/test/jasmine/tests/hover_label_test.js @@ -152,18 +152,18 @@ describe('hover info', function() { var hoverTrace = gd._hoverdata[0]; - expect(hoverTrace.curveNumber).toEqual(0); - expect(hoverTrace.pointNumber).toEqual(17); - expect(hoverTrace.x).toEqual(0.388); - expect(hoverTrace.y).toEqual(1); - expect(hoverTrace.text).toEqual(0); + expect(hoverTrace.curveNumber).toBe(0); + expect(hoverTrace.pointNumber).toBe(17); + expect(hoverTrace.x).toBe(0.388); + expect(hoverTrace.y).toBe(1); + expect(hoverTrace.text).toBe(0); var txs = d3.select(gd).selectAll('.textpoint text'); - expect(txs.size()).toEqual(1); + expect(txs.size()).toBe(1); txs.each(function() { - expect(d3.select(this).text()).toEqual('0'); + expect(d3.select(this).text()).toBe('0'); }); assertHoverLabelContent({