- Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(tooltip): add text label support for crosshairs #7278
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: v5
Are you sure you want to change the base?
Changes from all commits
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 | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||
| import { Circle, DisplayObject, IElement, Line } from '@antv/g'; | ||||||
| import { Circle, DisplayObject, IElement, Line, Text } from '@antv/g'; | ||||||
| import { sort, group, mean, bisector, minIndex } from '@antv/vendor/d3-array'; | ||||||
| import { deepMix, lowerFirst, set, throttle, last, isNumber } from '@antv/util'; | ||||||
| import { Tooltip as TooltipComponent } from '@antv/component'; | ||||||
| | @@ -347,13 +347,22 @@ function updateRuleX( | |||||
| polar, | ||||||
| insetLeft, | ||||||
| insetTop, | ||||||
| textXposition = 'start' as const, | ||||||
| textXoffsetX = 5, | ||||||
| textXoffsetY = 0, | ||||||
| textX = undefined, | ||||||
| textY = undefined, | ||||||
| textFill = undefined, | ||||||
| ...rest | ||||||
| }, | ||||||
| ) { | ||||||
| const defaults = { | ||||||
| lineWidth: 1, | ||||||
| stroke: '#1b1e23', | ||||||
| strokeOpacity: 0.5, | ||||||
| textX, | ||||||
| textY, | ||||||
| textFill, | ||||||
| ...rest, | ||||||
| }; | ||||||
| | ||||||
| | @@ -429,6 +438,31 @@ function updateRuleX( | |||||
| ruleX.style.y1 = y1; | ||||||
| ruleX.style.y2 = y2; | ||||||
| root.ruleX = ruleX; | ||||||
| | ||||||
| // Render text if crosshairsLabelX or textX is provided | ||||||
| const labelX = defaults.textX; | ||||||
| if (labelX) { | ||||||
| const createTextX = () => { | ||||||
| const text = new Text({ | ||||||
| style: { | ||||||
| text: labelX, | ||||||
| fontSize: 12, | ||||||
| fill: defaults.textFill || defaults.stroke, | ||||||
| textAlign: textXposition, | ||||||
| textBaseline: 'middle', | ||||||
| }, | ||||||
| }); | ||||||
| root.appendChild(text); | ||||||
| return text; | ||||||
| }; | ||||||
| | ||||||
| const textX = root.textX || createTextX(); | ||||||
| // Position at end of the crosshair line (right side) | ||||||
| textX.style.x = x2 + textXoffsetX; | ||||||
| textX.style.y = (y1 + y2) / 2 + textXoffsetY; | ||||||
| textX.style.text = labelX; | ||||||
| root.textX = textX; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| | ||||||
| | @@ -446,13 +480,20 @@ function updateRuleY( | |||||
| polar, | ||||||
| insetLeft, | ||||||
| insetTop, | ||||||
| textYposition = 'end' as const, | ||||||
| textYoffsetX = 5, | ||||||
| textYoffsetY = 5, | ||||||
| textY = undefined, | ||||||
| textFill = undefined, | ||||||
| ...rest | ||||||
| }, | ||||||
| ) { | ||||||
| const defaults = { | ||||||
| lineWidth: 1, | ||||||
| stroke: '#1b1e23', | ||||||
| strokeOpacity: 0.5, | ||||||
| textY, | ||||||
| textFill, | ||||||
| ...rest, | ||||||
| }; | ||||||
| | ||||||
| | @@ -498,6 +539,31 @@ function updateRuleY( | |||||
| ruleY.style.y1 = y1; | ||||||
| ruleY.style.y2 = y2; | ||||||
| root.ruleY = ruleY; | ||||||
| | ||||||
| // Render text if textY is provided | ||||||
| const labelY = defaults.textY; | ||||||
| if (labelY) { | ||||||
| const createTextY = () => { | ||||||
| const text = new Text({ | ||||||
| style: { | ||||||
| text: labelY, | ||||||
| fontSize: 12, | ||||||
| fill: defaults.textFill || defaults.stroke, | ||||||
| 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. Similar to Suggested change
| ||||||
| textAlign: textYposition, | ||||||
| textBaseline: 'bottom', | ||||||
| }, | ||||||
| }); | ||||||
| root.appendChild(text); | ||||||
| return text; | ||||||
| }; | ||||||
| | ||||||
| const textYObj = root.textY || createTextY(); | ||||||
| // Position at end of the crosshair line (top) | ||||||
| textYObj.style.x = (x1 + x2) / 2 + textYoffsetX; | ||||||
| textYObj.style.y = y2 + textYoffsetY; | ||||||
| textYObj.style.text = labelY; | ||||||
| root.textY = textYObj; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| | ||||||
| | @@ -506,13 +572,21 @@ function hideRuleY(root) { | |||||
| root.ruleY.remove(); | ||||||
| root.ruleY = undefined; | ||||||
| } | ||||||
| if (root.textY) { | ||||||
| root.textY.remove(); | ||||||
| root.textY = undefined; | ||||||
| } | ||||||
| } | ||||||
| | ||||||
| function hideRuleX(root) { | ||||||
| if (root.ruleX) { | ||||||
| root.ruleX.remove(); | ||||||
| root.ruleX = undefined; | ||||||
| } | ||||||
| if (root.textX) { | ||||||
| root.textX.remove(); | ||||||
| root.textX = undefined; | ||||||
| } | ||||||
| } | ||||||
| | ||||||
| function updateMarker(root, { data, style, theme }) { | ||||||
| | ||||||
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.
The
fillstyle for theTextobject is set usingdefaults.textFill || defaults.stroke. However,defaults.textFillis not explicitly defined within thedefaultsobject constructed inupdateRuleX. The intended property for crosshair text fill,crosshairsTextFill, is passed through...restand would be available asdefaults.crosshairsTextFill. As a result, the text fill will always fallback todefaults.stroke, ignoring anycrosshairsTextFillconfiguration.