Skip to content

feat: Make hover label triangle optional #7451

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions draftlogs/7451_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add `layout.hoverlabel.showarrow` (and `trace.hoverlabel.showarrow`) attribute to allow hiding the triangular caret that appears on the hover label box [[#7451](https://github.com/plotly/plotly.js/pull/7451)]
1 change: 1 addition & 0 deletions src/components/fx/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = {
}),
align: extendFlat({}, hoverLabelAttrs.align, {arrayOk: true}),
namelength: extendFlat({}, hoverLabelAttrs.namelength, {arrayOk: true}),
showarrow: extendFlat({}, hoverLabelAttrs.showarrow),
editType: 'none'
}
};
1 change: 1 addition & 0 deletions src/components/fx/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = function calc(gd) {
fillFn(trace.hoverlabel.font.variant, cd, 'htv');
fillFn(trace.hoverlabel.namelength, cd, 'hnl');
fillFn(trace.hoverlabel.align, cd, 'hta');
fillFn(trace.hoverlabel.showarrow, cd, 'htsa');
}
};

Expand Down
32 changes: 21 additions & 11 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -1922,20 +1922,30 @@ function alignHoverText(hoverLabels, rotateLabels, scaleX, scaleY) {
var offsetY = offsets.y;

var isMiddle = anchor === 'middle';
// Get 'showarrow' attribute value from trace hoverlabel settings;
// if trace has no hoverlabel settings, we should show the arrow by default
var showArrow = 'hoverlabel' in d.trace ? d.trace.hoverlabel.showarrow : true;

g.select('path')
.attr('d', isMiddle ?
var pathStr;
if(isMiddle) {
// middle aligned: rect centered on data
('M-' + pX(d.bx / 2 + d.tx2width / 2) + ',' + pY(offsetY - d.by / 2) +
'h' + pX(d.bx) + 'v' + pY(d.by) + 'h-' + pX(d.bx) + 'Z') :
pathStr = 'M-' + pX(d.bx / 2 + d.tx2width / 2) + ',' + pY(offsetY - d.by / 2) +
'h' + pX(d.bx) + 'v' + pY(d.by) + 'h-' + pX(d.bx) + 'Z';
Comment on lines +1932 to +1933
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you be up for using a template literal here and elsewhere?

Suggested change
pathStr = 'M-' + pX(d.bx / 2 + d.tx2width / 2) + ',' + pY(offsetY - d.by / 2) +
'h' + pX(d.bx) + 'v' + pY(d.by) + 'h-' + pX(d.bx) + 'Z';
pathStr = `M-${pX(d.bx / 2 + d.tx2width / 2)},${pY(offsetY - d.by / 2)}h${pX(d.bx)}v${pY(d.by)}h-${pX(d.bx)}Z`;

Copy link
Contributor

Choose a reason for hiding this comment

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

We could leave refactoring for another PR.
Template literal may run slower: https://jsperf.app/es6-string-literals-vs-string-concatenation/43

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting. Slower in Chrome but not in Safari.
Screenshot 2025-07-25 at 4 50 20 PM
Screenshot 2025-07-25 at 4 48 45 PM

Copy link
Contributor

Choose a reason for hiding this comment

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

Have you seen any issues with this kind of calculation? I imagine that most computers are powerful enough where this isn't an issue.

} else if(showArrow) {
// left or right aligned: side rect with arrow to data
('M0,0L' + pX(horzSign * HOVERARROWSIZE + offsetX) + ',' + pY(HOVERARROWSIZE + offsetY) +
'v' + pY(d.by / 2 - HOVERARROWSIZE) +
'h' + pX(horzSign * d.bx) +
'v-' + pY(d.by) +
'H' + pX(horzSign * HOVERARROWSIZE + offsetX) +
'V' + pY(offsetY - HOVERARROWSIZE) +
'Z'));
pathStr = 'M0,0L' + pX(horzSign * HOVERARROWSIZE + offsetX) + ',' + pY(HOVERARROWSIZE + offsetY) +
'v' + pY(d.by / 2 - HOVERARROWSIZE) +
Copy link
Contributor

Choose a reason for hiding this comment

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

OMG math

Copy link
Contributor Author

@emilykl emilykl Jul 22, 2025

Choose a reason for hiding this comment

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

@gvwilson Coming up with the new SVG path string with no arrow was Claude Code's only helpful contribution to this PR (but it's a doozy, so, worth it)

'h' + pX(horzSign * d.bx) +
'v-' + pY(d.by) +
'H' + pX(horzSign * HOVERARROWSIZE + offsetX) +
'V' + pY(offsetY - HOVERARROWSIZE) +
'Z';
} else {
// left or right aligned: side rect without arrow
pathStr = 'M' + pX(horzSign * HOVERARROWSIZE + offsetX) + ',' + pY(offsetY - d.by / 2) +
'h' + pX(horzSign * d.bx) + 'v' + pY(d.by) + 'h' + pX(-horzSign * d.bx) + 'Z';
}
g.select('path').attr('d', pathStr);

var posX = offsetX + shiftX.textShiftX;
var posY = offsetY + d.ty0 - d.by / 2 + HOVERTEXTPAD;
Expand Down
1 change: 1 addition & 0 deletions src/components/fx/hoverlabel_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = function handleHoverLabelDefaults(contIn, contOut, coerce, opts
coerce('hoverlabel.bgcolor', opts.bgcolor);
coerce('hoverlabel.bordercolor', opts.bordercolor);
coerce('hoverlabel.namelength', opts.namelength);
coerce('hoverlabel.showarrow', opts.showarrow);
Lib.coerceFont(coerce, 'hoverlabel.font', opts.font);
coerce('hoverlabel.align', opts.align);
};
9 changes: 9 additions & 0 deletions src/components/fx/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ module.exports = {
'`namelength - 3` characters and add an ellipsis.'
].join(' ')
},
showarrow: {
valType: 'boolean',
dflt: true,
editType: 'none',
description: [
'Sets whether or not to show the hover label arrow/triangle',
'pointing to the data point.'
].join(' ')
},

editType: 'none'
},
Expand Down
6 changes: 4 additions & 2 deletions test/jasmine/tests/fx_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ describe('Fx defaults', function() {
shadow: 'auto',
},
align: 'auto',
namelength: 15
namelength: 15,
showarrow: true,
});

expect(out.data[1].hoverlabel).toEqual({
Expand All @@ -197,7 +198,8 @@ describe('Fx defaults', function() {
shadow: 'auto',
},
align: 'auto',
namelength: 15
namelength: 15,
showarrow: true,
});

expect(out.layout.annotations[0].hoverlabel).toEqual({
Expand Down
97 changes: 97 additions & 0 deletions test/jasmine/tests/hover_label_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7038,3 +7038,100 @@ describe('hover on traces with (x|y)hoverformat', function() {
.then(done, done.fail);
});
});

describe('hoverlabel.showarrow', function() {
'use strict';

var gd;

beforeEach(function() {
gd = createGraphDiv();
});

afterEach(destroyGraphDiv);

function _hover(x, y) {
mouseEvent('mousemove', x, y);
Lib.clearThrottle();
}

function getHoverPath() {
var hoverLabels = d3SelectAll('g.hovertext');
if (hoverLabels.size() === 0) return null;
return hoverLabels.select('path').attr('d');
}

it('should show hover arrow by default', function(done) {
Plotly.newPlot(gd, [{
x: [1, 2, 3],
y: [1, 2, 1],
type: 'scatter',
mode: 'markers'
}], {
width: 400,
height: 400,
margin: {l: 50, t: 50, r: 50, b: 50}
})
.then(function() {
_hover(200, 70); // Hover over middle point
})
.then(delay(HOVERMINTIME * 1.1))
.then(function() {
var pathD = getHoverPath();
expect(pathD).not.toBeNull('hover path should exist');
// Arrow paths contain 'L' commands starting from 0,0
expect(pathD).toMatch(/^M0,0L/, 'path should contain arrow (L command from 0,0)');
})
.then(done, done.fail);
});

it('should hide hover arrow when showarrow is false', function(done) {
Plotly.newPlot(gd, [{
x: [1, 2, 3],
y: [1, 2, 1],
type: 'scatter',
mode: 'markers'
}], {
width: 400,
height: 400,
margin: {l: 50, t: 50, r: 50, b: 50},
hoverlabel: { showarrow: false }
})
.then(function() {
_hover(200, 70); // Hover over middle point
})
.then(delay(HOVERMINTIME * 1.1))
.then(function() {
var pathD = getHoverPath();
expect(pathD).not.toBeNull('hover path should exist');
// No-arrow paths should be simple rectangles (no 'L' commands starting at 0,0))
expect(pathD).not.toMatch(/^M0,0L/, 'path should not start at 0,0');
expect(pathD).toMatch(/^M[\d.-]+,[\d.-]+h/, 'path should start with some numeric point and move horizontally');
})
.then(done, done.fail);
});

it('should work at trace level', function(done) {
Plotly.newPlot(gd, [{
x: [1, 2, 3],
y: [1, 2, 1],
type: 'scatter',
mode: 'markers',
hoverlabel: { showarrow: false }
}], {
width: 400,
height: 400,
margin: {l: 50, t: 50, r: 50, b: 50}
})
.then(function() {
_hover(200, 70); // Hover over middle point
})
.then(delay(HOVERMINTIME * 1.1))
.then(function() {
var pathD = getHoverPath();
expect(pathD).not.toBeNull('hover path should exist');
expect(pathD).not.toMatch(/^M0,0L/, 'trace-level showarrow:false should hide arrow');
})
.then(done, done.fail);
});
});
Loading