Skip to content

Commit 89bbbd0

Browse files
Anush2303Anush
andauthored
fix(react-charts): fix callout position issues (#34868)
Co-authored-by: Anush <anushgupta@microsoft.com>
1 parent a529750 commit 89bbbd0

11 files changed

Lines changed: 108 additions & 124 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "fix callout position issues",
4+
"packageName": "@fluentui/react-charts",
5+
"email": "anushgupta@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

packages/charts/react-charts/library/src/components/AreaChart/AreaChart.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ export const AreaChart: React.FunctionComponent<AreaChartProps> = React.forwardR
638638
opacity={layerOpacity}
639639
fillOpacity={_getOpacity(points[index]!.legend)}
640640
onMouseMove={event => _onRectMouseMove(event)}
641+
onFocus={event => _handleFocus(event, index, 0, `${_circleId}_${index}`)}
641642
onMouseOut={_onRectMouseOut}
642643
onMouseOver={event => _onRectMouseMove(event)}
643644
/>
@@ -701,7 +702,7 @@ export const AreaChart: React.FunctionComponent<AreaChartProps> = React.forwardR
701702
onMouseOut={_onRectMouseOut}
702703
onMouseOver={event => _onRectMouseMove(event)}
703704
onClick={() => _onDataPointClick(points[index]!.data[pointIndex].onDataPointClick!)}
704-
onFocus={() => _handleFocus(index, pointIndex, circleId)}
705+
onFocus={event => _handleFocus(event, index, pointIndex, circleId)}
705706
onBlur={_handleBlur}
706707
{...getSecureProps(pointOptions)}
707708
r={_getCircleRadius(xDataPoint, circleRadius, circleId, legend)}
@@ -731,6 +732,7 @@ export const AreaChart: React.FunctionComponent<AreaChartProps> = React.forwardR
731732
fill={_updateCircleFillColor(xDataPoint, lineColor, circleId)}
732733
onMouseOut={_onRectMouseOut}
733734
onMouseOver={event => _onRectMouseMove(event)}
735+
onFocus={event => _handleFocus(event, index, pointIndex, circleId)}
734736
onClick={() => _onDataPointClick(points[index]!.data[pointIndex].onDataPointClick!)}
735737
{...getSecureProps(pointOptions)}
736738
r={_getCircleRadius(xDataPoint, circleRadius, circleId, legend)}
@@ -834,7 +836,20 @@ export const AreaChart: React.FunctionComponent<AreaChartProps> = React.forwardR
834836
: [];
835837
}
836838

837-
function _handleFocus(lineIndex: number, pointIndex: number, circleId: string) {
839+
function _handleFocus(
840+
event: React.FocusEvent<SVGCircleElement, Element>,
841+
lineIndex: number,
842+
pointIndex: number,
843+
circleId: string,
844+
) {
845+
let cx = 0;
846+
let cy = 0;
847+
848+
const targetRect = (event.target as SVGCircleElement).getBoundingClientRect();
849+
cx = targetRect.left + targetRect.width / 2;
850+
cy = targetRect.top + targetRect.height / 2;
851+
_updatePosition(cx, cy);
852+
838853
const { x, y, xAxisCalloutData } = props.data.lineChartData![lineIndex].data[pointIndex];
839854
const formattedDate = x instanceof Date ? formatDate(x, props.useUTC) : x;
840855
const modifiedXVal = x instanceof Date ? x.getTime() : x;

packages/charts/react-charts/library/src/components/DonutChart/Arc/Arc.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export const Arc: React.FunctionComponent<ArcProps> = React.forwardRef<HTMLDivEl
2222
_updateChart(props);
2323
}, [props]);
2424

25-
function _onFocus(data: ChartDataPoint, id: string): void {
26-
props.onFocusCallback!(data, id, currentRef.current);
25+
function _onFocus(data: ChartDataPoint, id: string, event: React.FocusEvent<SVGPathElement, Element>): void {
26+
props.onFocusCallback!(data, id, event, currentRef.current);
2727
}
2828

2929
function _hoverOn(data: ChartDataPoint, mouseEvent: React.MouseEvent<SVGPathElement>): void {
@@ -128,10 +128,10 @@ export const Arc: React.FunctionComponent<ArcProps> = React.forwardRef<HTMLDivEl
128128
}
129129
className={classes.root}
130130
style={{ fill: props.color, cursor: href ? 'pointer' : 'default' }}
131-
onFocus={_onFocus.bind(this, props.data!.data, id)}
131+
onFocus={event => _onFocus(props.data!.data, id, event)}
132132
data-is-focusable={props.activeArc === props.data!.data.legend || props.activeArc === ''}
133-
onMouseOver={_hoverOn.bind(this, props.data!.data)}
134-
onMouseMove={_hoverOn.bind(this, props.data!.data)}
133+
onMouseOver={event => _hoverOn(props.data!.data, event)}
134+
onMouseMove={event => _hoverOn(props.data!.data, event)}
135135
onMouseLeave={_hoverOff}
136136
tabIndex={_shouldHighlightArc(props.data!.data.legend!) ? 0 : undefined}
137137
onBlur={_onBlur}

packages/charts/react-charts/library/src/components/DonutChart/DonutChart.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,14 @@ export const DonutChart: React.FunctionComponent<DonutChartProps> = React.forwar
118118
return legends;
119119
}
120120

121-
function _focusCallback(data: ChartDataPoint, id: string, element: SVGPathElement): void {
121+
function _focusCallback(data: ChartDataPoint, id: string, e: React.FocusEvent<SVGPathElement>): void {
122+
let cx = 0;
123+
let cy = 0;
124+
125+
const targetRect = (e.target as SVGPathElement).getBoundingClientRect();
126+
cx = targetRect.left + targetRect.width / 2;
127+
cy = targetRect.top + targetRect.height / 2;
128+
updatePosition(cx, cy);
122129
setPopoverOpen(selectedLegend === '' || selectedLegend === data.legend);
123130
setValue(data.data!.toString());
124131
setLegend(data.legend);

packages/charts/react-charts/library/src/components/DonutChart/Pie/Pie.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const Pie: React.FunctionComponent<PieProps> = React.forwardRef<HTMLDivEl
2929
.value((d: any) => d.data)
3030
.padAngle(0);
3131

32-
function _focusCallback(data: ChartDataPoint, id: string, e: SVGPathElement): void {
32+
function _focusCallback(data: ChartDataPoint, id: string, e: React.FocusEvent<SVGPathElement>): void {
3333
props.onFocusCallback!(data, id, e);
3434
}
3535

packages/charts/react-charts/library/src/components/GroupedVerticalBarChart/GroupedVerticalBarChart.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,19 @@ export const GroupedVerticalBarChart: React.FC<GroupedVerticalBarChartProps> = R
389389
};
390390

391391
const onBarFocus = (
392+
event: React.FocusEvent<SVGRectElement, Element>,
392393
pointData: GVBarChartSeriesPoint,
393394
// eslint-disable-next-line @typescript-eslint/no-explicit-any
394395
groupData: any,
395396
refArrayIndexNumber: number,
396397
): void => {
398+
let x = 0;
399+
let y = 0;
400+
401+
const targetRect = (event.target as SVGRectElement).getBoundingClientRect();
402+
x = targetRect.left + targetRect.width / 2;
403+
y = targetRect.top + targetRect.height / 2;
404+
updatePosition(x, y);
397405
_refArray.forEach((obj: RefArrayData, index: number) => {
398406
if (obj.index === pointData.legend && refArrayIndexNumber === index) {
399407
setPopoverOpen(_noLegendHighlighted() || _legendHighlighted(pointData.legend));
@@ -464,17 +472,16 @@ export const GroupedVerticalBarChart: React.FC<GroupedVerticalBarChartProps> = R
464472
width={_barWidth}
465473
x={xPoint}
466474
y={yPoint}
467-
data-is-focusable={!props.hideTooltip && (_legendHighlighted(pointData.legend) || _noLegendHighlighted())}
468475
opacity={_getOpacity(pointData.legend)}
469476
ref={(e: SVGRectElement | null) => {
470477
_refCallback(e!, pointData.legend, refIndexNumber);
471478
}}
472479
fill={startColor}
473480
rx={0}
474-
onMouseOver={onBarHover.bind(null, pointData, singleSet)}
475-
onMouseMove={onBarHover.bind(null, pointData, singleSet)}
481+
onMouseOver={event => onBarHover(pointData, singleSet, event)}
482+
onMouseMove={event => onBarHover(pointData, singleSet, event)}
476483
onMouseOut={_onBarLeave}
477-
onFocus={onBarFocus.bind(null, pointData, singleSet, refIndexNumber)}
484+
onFocus={event => onBarFocus(event, pointData, singleSet, refIndexNumber)}
478485
onBlur={_onBarLeave}
479486
onClick={pointData.onClick}
480487
aria-label={getAriaLabel(pointData, singleSet.xAxisPoint)}

0 commit comments

Comments
 (0)