Skip to content

Commit 370af1a

Browse files
committed
HoverLabels: Check for <= 0 error values for log axes
1 parent 96c0157 commit 370af1a

File tree

2 files changed

+74
-20
lines changed

2 files changed

+74
-20
lines changed

src/plots/cartesian/graph_interact.js

+20-20
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ fx.getClosest = function(cd, distfn, pointData) {
628628
};
629629

630630
function cleanPoint(d, hovermode) {
631-
d.posref = hovermode==='y' ? (d.x0+d.x1)/2 : (d.y0+d.y1)/2;
631+
d.posref = hovermode === 'y' ? (d.x0 + d.x1) / 2 : (d.y0 + d.y1) / 2;
632632

633633
// then constrain all the positions to be on the plot
634634
d.x0 = Lib.constrain(d.x0, 0, d.xa._length);
@@ -639,36 +639,36 @@ function cleanPoint(d, hovermode) {
639639
// and convert the x and y label values into objects
640640
// formatted as text, with font info
641641
var logOffScale;
642-
if(d.xLabelVal!==undefined) {
643-
logOffScale = (d.xa.type==='log' && d.xLabelVal<=0);
642+
if(d.xLabelVal !== undefined) {
643+
logOffScale = (d.xa.type === 'log' && d.xLabelVal <= 0);
644644
var xLabelObj = Axes.tickText(d.xa,
645645
d.xa.c2l(logOffScale ? -d.xLabelVal : d.xLabelVal), 'hover');
646646
if(logOffScale) {
647-
if(d.xLabelVal===0) d.xLabel = '0';
647+
if(d.xLabelVal === 0) d.xLabel = '0';
648648
else d.xLabel = '-' + xLabelObj.text;
649649
}
650650
else d.xLabel = xLabelObj.text;
651651
d.xVal = d.xa.c2d(d.xLabelVal);
652652
}
653653

654-
if(d.yLabelVal!==undefined) {
655-
logOffScale = (d.ya.type==='log' && d.yLabelVal<=0);
654+
if(d.yLabelVal !== undefined) {
655+
logOffScale = (d.ya.type === 'log' && d.yLabelVal <= 0);
656656
var yLabelObj = Axes.tickText(d.ya,
657657
d.ya.c2l(logOffScale ? -d.yLabelVal : d.yLabelVal), 'hover');
658658
if(logOffScale) {
659-
if(d.yLabelVal===0) d.yLabel = '0';
659+
if(d.yLabelVal === 0) d.yLabel = '0';
660660
else d.yLabel = '-' + yLabelObj.text;
661661
}
662662
else d.yLabel = yLabelObj.text;
663663
d.yVal = d.ya.c2d(d.yLabelVal);
664664
}
665665

666-
if(d.zLabelVal!==undefined) d.zLabel = String(d.zLabelVal);
666+
if(d.zLabelVal !== undefined) d.zLabel = String(d.zLabelVal);
667667

668668
// for box means and error bars, add the range to the label
669-
if(d.xerr!==undefined) {
669+
if(!isNaN(d.xerr) && !(d.xa.type === 'log' && d.xerr <= 0)) {
670670
var xeText = Axes.tickText(d.xa, d.xa.c2l(d.xerr), 'hover').text;
671-
if(d.xerrneg!==undefined) {
671+
if(d.xerrneg !== undefined) {
672672
d.xLabel += ' +' + xeText + ' / -' +
673673
Axes.tickText(d.xa, d.xa.c2l(d.xerrneg), 'hover').text;
674674
}
@@ -677,27 +677,27 @@ function cleanPoint(d, hovermode) {
677677
// small distance penalty for error bars, so that if there are
678678
// traces with errors and some without, the error bar label will
679679
// hoist up to the point
680-
if(hovermode==='x') d.distance += 1;
680+
if(hovermode === 'x') d.distance += 1;
681681
}
682-
if(d.yerr!==undefined) {
682+
if(!isNaN(d.yerr) && !(d.ya.type === 'log' && d.yerr <= 0)) {
683683
var yeText = Axes.tickText(d.ya, d.ya.c2l(d.yerr), 'hover').text;
684-
if(d.yerrneg!==undefined) {
684+
if(d.yerrneg !== undefined) {
685685
d.yLabel += ' +' + yeText + ' / -' +
686686
Axes.tickText(d.ya, d.ya.c2l(d.yerrneg), 'hover').text;
687687
}
688688
else d.yLabel += ' &plusmn; ' + yeText;
689689

690-
if(hovermode==='y') d.distance += 1;
690+
if(hovermode === 'y') d.distance += 1;
691691
}
692692

693693
var infomode = d.trace.hoverinfo;
694-
if(infomode!=='all') {
694+
if(infomode !== 'all') {
695695
infomode = infomode.split('+');
696-
if(infomode.indexOf('x')===-1) d.xLabel = undefined;
697-
if(infomode.indexOf('y')===-1) d.yLabel = undefined;
698-
if(infomode.indexOf('z')===-1) d.zLabel = undefined;
699-
if(infomode.indexOf('text')===-1) d.text = undefined;
700-
if(infomode.indexOf('name')===-1) d.name = undefined;
696+
if(infomode.indexOf('x') === -1) d.xLabel = undefined;
697+
if(infomode.indexOf('y') === -1) d.yLabel = undefined;
698+
if(infomode.indexOf('z') === -1) d.zLabel = undefined;
699+
if(infomode.indexOf('text') === -1) d.text = undefined;
700+
if(infomode.indexOf('name') === -1) d.name = undefined;
701701
}
702702
return d;
703703
}

test/jasmine/tests/hover_label_test.js

+54
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,60 @@ describe('hover info', function() {
214214
});
215215
});
216216

217+
describe('hover error x text (log axis positive)', function() {
218+
var mockCopy = Lib.extendDeep({}, mock);
219+
220+
mockCopy.data[0].error_x = { array: [] };
221+
mockCopy.data[0].error_x.array[17] = 1;
222+
223+
beforeEach(function(done) {
224+
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
225+
});
226+
227+
it('responds to hover x+text', function() {
228+
Fx.hover('graph', evt, 'xy');
229+
230+
expect(d3.selectAll('g.axistext').size()).toEqual(1);
231+
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('0.388 ± 1');
232+
});
233+
});
234+
235+
describe('hover error text (log axis 0)', function() {
236+
var mockCopy = Lib.extendDeep({}, mock);
237+
238+
mockCopy.data[0].error_x = { array: [] };
239+
mockCopy.data[0].error_x.array[17] = 0;
240+
241+
beforeEach(function(done) {
242+
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
243+
});
244+
245+
it('responds to hover x+text', function() {
246+
Fx.hover('graph', evt, 'xy');
247+
248+
expect(d3.selectAll('g.axistext').size()).toEqual(1);
249+
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('0.388');
250+
});
251+
});
252+
253+
describe('hover error text (log axis negative)', function() {
254+
var mockCopy = Lib.extendDeep({}, mock);
255+
256+
mockCopy.data[0].error_x = { array: [] };
257+
mockCopy.data[0].error_x.array[17] = -1;
258+
259+
beforeEach(function(done) {
260+
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
261+
});
262+
263+
it('responds to hover x+text', function() {
264+
Fx.hover('graph', evt, 'xy');
265+
266+
expect(d3.selectAll('g.axistext').size()).toEqual(1);
267+
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('0.388');
268+
});
269+
});
270+
217271
describe('hover info text with html', function() {
218272
var mockCopy = Lib.extendDeep({}, mock);
219273

0 commit comments

Comments
 (0)