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
Open
SergeyAxiomatic
wants to merge
7
commits into
plotly:main
Choose a base branch
from
SergeyAxiomatic:fix/hover-mathjax-name-and-y-followups
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0a1c4c8
Fix tex hover label name truncation and rotated label placement
SergeyAxiomatic fc4da96
Rename draftlog to the PR number
SergeyAxiomatic 2c24c29
Limit rotation-safe tex measurement to repositionMathGroup
SergeyAxiomatic 86b0c3e
Measure tex hover labels by their glyphs, not the wrapping group
SergeyAxiomatic 406c21c
Measure tex hover labels once and reuse the size
SergeyAxiomatic 152d4bf
Measure repositioned tex in its own units
SergeyAxiomatic af75a87
Reuse measureMath when repositioning tex
SergeyAxiomatic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,25 @@ function positionMathGroup(_context, svgClass, mathjaxGroup, newSvg, textAnchor, | |
| h = bb.height; | ||
| } | ||
|
|
||
| return {width: w, height: h}; | ||
| } | ||
|
|
||
| /** | ||
| * 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 +244,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); | ||
|
|
@@ -248,37 +261,47 @@ exports.convertToTspans = function(_context, gd, _callback) { | |
| * text-anchor. | ||
| * | ||
| * @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 || measureMath(newSvg); | ||
| 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
+301
to
+303
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(); | ||
| }; | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.