Repository navigation
Fix tex hover label name truncation and rotated label placement #8138
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
base: main
Are you sure you want to change the base?
Changes from all commits
0a1c4c8
fc4da96
2c24c29
86b0c3e
406c21c
152d4bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| - Fix hover labels that are a single tex expression being truncated by `hoverlabel.namelength` [[#8138](https://github.com/plotly/plotly.js/pull/8138)] | ||
| - Fix tex in hover labels being misplaced when labels are rotated in `hovermode: 'y'` [[#8138](https://github.com/plotly/plotly.js/pull/8138)] |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -38,22 +38,16 @@ const isPureTex = (str) => { | |||||||
| exports.isPureTex = isPureTex; | ||||||||
|
|
||||||||
| /** | ||||||||
| * Positions a MathJax-typeset group against its source <text> node's x/y | ||||||||
| * and text-anchor. | ||||||||
| * Measures the typeset glyphs of a MathJax svg. | ||||||||
| * | ||||||||
| * @param {d3 selection} _context: the source <text> element | ||||||||
| * @param {string} svgClass: the '<baseClass>-math' class used on newSvg | ||||||||
| * @param {d3 selection} mathjaxGroup: the '<svgClass>-group' wrapper | ||||||||
| * @param {d3 selection} newSvg: the typeset <svg>, already sized | ||||||||
| * @param {string} textAnchor: _context's text-anchor to position against | ||||||||
| * @param {number} fontSize: _context's font size, for the baseline offset | ||||||||
| * @param {d3 selection} newSvg: the typeset <svg> | ||||||||
| * @param {number} [width0]: the SVG's own reported width, to detect the | ||||||||
| * Firefox v82+ overflow bug (see link below); omit to skip that check | ||||||||
| * @param {number} [height0]: same, for height | ||||||||
| * @return {{width: number, height: number}} | ||||||||
| */ | ||||||||
| function positionMathGroup(_context, svgClass, mathjaxGroup, newSvg, textAnchor, fontSize, width0, height0) { | ||||||||
| var g = newSvg.select('g'); | ||||||||
| var bb = g.node().getBoundingClientRect(); | ||||||||
| function measureMath(newSvg, width0, height0) { | ||||||||
| var bb = newSvg.select('g').node().getBoundingClientRect(); | ||||||||
| var w = bb.width; | ||||||||
| var h = bb.height; | ||||||||
| var w0 = width0 === undefined ? w : width0; | ||||||||
|
|
@@ -68,6 +62,37 @@ function positionMathGroup(_context, svgClass, mathjaxGroup, newSvg, textAnchor, | |||||||
| h = bb.height; | ||||||||
| } | ||||||||
|
|
||||||||
| return {width: w, height: h}; | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * The size of a typeset group in its own units, which unlike its client rect | ||||||||
| * does not change when the group, or something above it, is rotated or scaled. | ||||||||
| * | ||||||||
| * @param {d3 selection} mathjaxGroup: the '<svgClass>-group' wrapper | ||||||||
| * @return {{width: number, height: number}} | ||||||||
| */ | ||||||||
| function getLocalSize(mathjaxGroup) { | ||||||||
| var bb = mathjaxGroup.node().getBBox(); | ||||||||
| return {width: bb.width, height: bb.height}; | ||||||||
| } | ||||||||
|
Comment on lines
+68
to
+78
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think we don't need to add this function: we can simply reuse |
||||||||
|
|
||||||||
| /** | ||||||||
| * Positions a MathJax-typeset group against its source <text> node's x/y | ||||||||
| * and text-anchor. | ||||||||
| * | ||||||||
| * @param {d3 selection} _context: the source <text> element | ||||||||
| * @param {string} svgClass: the '<baseClass>-math' class used on newSvg | ||||||||
| * @param {d3 selection} mathjaxGroup: the '<svgClass>-group' wrapper | ||||||||
| * @param {d3 selection} newSvg: the typeset <svg>, already sized | ||||||||
| * @param {string} textAnchor: _context's text-anchor to position against | ||||||||
| * @param {number} fontSize: _context's font size, for the baseline offset | ||||||||
| * @param {{width: number, height: number}} size: the typeset glyphs' size | ||||||||
| */ | ||||||||
| function positionMathGroup(_context, svgClass, mathjaxGroup, newSvg, textAnchor, fontSize, size) { | ||||||||
| var w = size.width; | ||||||||
| var h = size.height; | ||||||||
|
|
||||||||
| var x = +_context.attr('x'); | ||||||||
| var y = +_context.attr('y'); | ||||||||
|
|
||||||||
|
|
@@ -231,7 +256,7 @@ exports.convertToTspans = function(_context, gd, _callback) { | |||||||
| var g = newSvg.select('g'); | ||||||||
| g.attr({fill: fill, stroke: fill}); | ||||||||
|
|
||||||||
| positionMathGroup(_context, svgClass, mathjaxGroup, newSvg, textAnchor, fontSize, w0, h0); | ||||||||
| positionMathGroup(_context, svgClass, mathjaxGroup, newSvg, textAnchor, fontSize, measureMath(newSvg, w0, h0)); | ||||||||
|
|
||||||||
| if(_callback) _callback.call(_context, mathjaxGroup); | ||||||||
| resolve(mathjaxGroup); | ||||||||
|
|
@@ -247,38 +272,51 @@ exports.convertToTspans = function(_context, gd, _callback) { | |||||||
| * convertToTspans, to match its source <text> node's *current* x/y and | ||||||||
| * text-anchor. | ||||||||
| * | ||||||||
| * The size is measured in the group's own units, so it does not change with | ||||||||
| * the rotation or scale of whatever the group sits in. | ||||||||
| * | ||||||||
|
Comment on lines
+275
to
+277
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| * @param {d3 selection} _context: the source <text> element | ||||||||
| * @param {{width: number, height: number}} [size]: a size returned by an | ||||||||
| * earlier call, to use instead of measuring again | ||||||||
| * @return {?{width: number, height: number}} the size used, or null if there | ||||||||
| * is no typeset group to reposition | ||||||||
| */ | ||||||||
| exports.repositionMathGroup = function(_context) { | ||||||||
| if(_context.empty()) return; | ||||||||
| exports.repositionMathGroup = function(_context, size) { | ||||||||
| if(_context.empty()) return null; | ||||||||
|
|
||||||||
| var svgClass = (_context.attr('class') ? _context.attr('class').split(' ')[0] : 'text') + '-math'; | ||||||||
| var parent = d3.select(_context.node().parentNode); | ||||||||
| var mathjaxGroup = parent.select('g.' + svgClass + '-group'); | ||||||||
| var newSvg = mathjaxGroup.select('svg.' + svgClass); | ||||||||
| if(mathjaxGroup.empty() || newSvg.empty()) return; | ||||||||
| if(mathjaxGroup.empty() || newSvg.empty()) return null; | ||||||||
|
|
||||||||
| size = size || getLocalSize(mathjaxGroup); | ||||||||
| var textAnchor = _context.attr('text-anchor'); | ||||||||
| var fontSize = parseInt(_context.node().style.fontSize, 10); | ||||||||
| positionMathGroup(_context, svgClass, mathjaxGroup, newSvg, textAnchor, fontSize); | ||||||||
| positionMathGroup(_context, svgClass, mathjaxGroup, newSvg, textAnchor, fontSize, size); | ||||||||
| return size; | ||||||||
| }; | ||||||||
|
|
||||||||
| /** | ||||||||
| * Finds the node whose bounding box represents a source <text> element's | ||||||||
| * rendered content: the typeset MathJax group if convertToTspans replaced | ||||||||
| * the text with one (the source itself is display:none in that case), or | ||||||||
| * the <text> node otherwise. | ||||||||
| * The client rect of a source <text> element's rendered content: the typeset | ||||||||
| * glyphs if convertToTspans replaced the text with a MathJax group (the source | ||||||||
| * itself is display:none in that case), or the <text> node otherwise. | ||||||||
| * | ||||||||
| * @param {d3 selection} textSel: the source <text> element | ||||||||
| * @return {?Element} the node to measure, or null if textSel is empty | ||||||||
| * @return {?DOMRect} null if textSel is empty | ||||||||
| */ | ||||||||
| exports.getMathOrTextNode = function(textSel) { | ||||||||
| exports.getTextClientRect = function(textSel) { | ||||||||
| var node = textSel.node(); | ||||||||
| if(!node) return null; | ||||||||
|
|
||||||||
| var svgClass = (textSel.attr('class') ? textSel.attr('class').split(' ')[0] : 'text') + '-math'; | ||||||||
| var mathGroup = d3.select(node.parentNode).select('g.' + svgClass + '-group'); | ||||||||
| return mathGroup.empty() ? node : mathGroup.node(); | ||||||||
| if(mathGroup.empty()) return node.getBoundingClientRect(); | ||||||||
|
|
||||||||
| // Firefox can report a near-empty rect for the group itself (seen on | ||||||||
| // rotated hover labels), but measures the glyphs inside it correctly | ||||||||
| var glyphs = mathGroup.select('svg g'); | ||||||||
|
Comment on lines
+316
to
+318
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think we can avoid a special workaround for Firefox here: if we pre-measure and record the label size, the helper wouldn't need to ask the browser to measure any on-screen tex. |
||||||||
| return (glyphs.empty() ? mathGroup : glyphs).node().getBoundingClientRect(); | ||||||||
| }; | ||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes
hoverlabel.namelengthhave no effect on a name that is a single tex expression, causing it to disagree with the attribute description.With
namelength: 0, a plain name is hidden, but a tex name is now shown in full.We should update attr description. Note the description is part of the schema, so
npm run schemaneeds to be re-run after this.