Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
76 changes: 75 additions & 1 deletion src/interaction/tooltip.ts
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';
Expand Down Expand Up @@ -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,
};

Expand Down Expand Up @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The fill style for the Text object is set using defaults.textFill || defaults.stroke. However, defaults.textFill is not explicitly defined within the defaults object constructed in updateRuleX. The intended property for crosshair text fill, crosshairsTextFill, is passed through ...rest and would be available as defaults.crosshairsTextFill. As a result, the text fill will always fallback to defaults.stroke, ignoring any crosshairsTextFill configuration.

Suggested change
fill: defaults.textFill || defaults.stroke,
fill: defaults.crosshairsTextFill || 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;
}
}
}

Expand All @@ -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,
};

Expand Down Expand Up @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

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

high

Similar to updateRuleX, the fill style for the Text object in updateRuleY is set using defaults.textFill || defaults.stroke. defaults.textFill is not defined in the defaults object here. It should be defaults.crosshairsTextFill to correctly apply the configured crosshair text fill. This will cause the text fill to incorrectly fallback to defaults.stroke.

Suggested change
fill: defaults.textFill || defaults.stroke,
fill: defaults.crosshairsTextFill || defaults.stroke,
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;
}
}
}

Expand All @@ -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 }) {
Expand Down
10 changes: 10 additions & 0 deletions src/spec/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ export type TooltipInteraction = {
event, // @todo
options: { title: 'string'; items: TooltipItemValue[] },
) => HTMLElement | string;
// Crosshairs text label options
crosshairsTextFill?: string;
textX?: string;
textY?: string;
textXposition?: 'start' | 'center' | 'end';
textYposition?: 'start' | 'center' | 'end';
textXoffsetX?: number;
textXoffsetY?: number;
textYoffsetX?: number;
textYoffsetY?: number;
} & Record<`crosshairs${any}`, any> &
Record<`marker${any}`, any>;

Expand Down
1 change: 1 addition & 0 deletions src/theme/dark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const Dark: TC<DarkOptions> = (options) => {
crosshairsStroke: '#fff',
crosshairsLineWidth: 1,
crosshairsStrokeOpacity: 0.25,
crosshairsTextFill: '#fff',
css: {
[g2Selector('tooltip')]: {
background: '#1f1f1f',
Expand Down
Loading