Skip to content

Commit 569edee

Browse files
Anush2303Anush
andauthored
fix(charts): enable custom callout in scatter chart (#35565)
Co-authored-by: Anush <anushgupta@microsoft.com>
1 parent 1bd1787 commit 569edee

File tree

8 files changed

+54
-0
lines changed

8 files changed

+54
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "enable custom callout in scatter chart",
4+
"packageName": "@fluentui/react-charting",
5+
"email": "anushgupta@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "enable custom callout in scatter chart",
4+
"packageName": "@fluentui/react-charts",
5+
"email": "anushgupta@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

packages/charts/react-charting/etc/react-charting.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,7 @@ export interface IScatterChartProps extends ICartesianChartProps {
16491649
data: IChartProps;
16501650
getCalloutDescriptionMessage?: (calloutDataProps: ICustomizedCalloutData) => string | undefined;
16511651
onRenderCalloutPerDataPoint?: IRenderFunction<ICustomizedCalloutData>;
1652+
onRenderCalloutPerStack?: IRenderFunction<ICustomizedCalloutData>;
16521653
styles?: IScatterChartStyles;
16531654
}
16541655

packages/charts/react-charting/src/components/ScatterChart/ScatterChart.base.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export const ScatterChartBase: React.FunctionComponent<IScatterChartProps> = Rea
9494
const [isSelectedLegend, setIsSelectedLegend] = React.useState<boolean>(false);
9595
const [activePoint, setActivePoint] = React.useState<string>('');
9696
const [stackCalloutProps, setStackCalloutProps] = React.useState<ICustomizedCalloutData>();
97+
const [dataPointCalloutProps, setDataPointCalloutProps] = React.useState<ICustomizedCalloutData>();
9798
const [isCalloutVisible, setCalloutVisible] = React.useState(false);
9899
const [selectedLegends, setSelectedLegends] = React.useState<string[]>(props.legendProps?.selectedLegends || []);
99100
const [refSelected, setRefSelected] = React.useState<string>('');
@@ -408,6 +409,7 @@ export const ScatterChartBase: React.FunctionComponent<IScatterChartProps> = Rea
408409
setRefSelected(`#${circleId}`);
409410
setYValueHover(found.values);
410411
setStackCalloutProps(found!);
412+
setDataPointCalloutProps(found!);
411413
setActivePoint(circleId);
412414
}
413415
} else {
@@ -720,6 +722,14 @@ export const ScatterChartBase: React.FunctionComponent<IScatterChartProps> = Rea
720722
setCalloutVisible(false);
721723
}
722724

725+
function _getCustomizedCallout() {
726+
return props.onRenderCalloutPerStack
727+
? props.onRenderCalloutPerStack(stackCalloutProps)
728+
: props.onRenderCalloutPerDataPoint
729+
? props.onRenderCalloutPerDataPoint(dataPointCalloutProps)
730+
: null;
731+
}
732+
723733
const _getNumericMinMaxOfY = React.useCallback(
724734
(points: IScatterChartPoints[], yAxisType?: YAxisType): { startValue: number; endValue: number } => {
725735
const { startValue, endValue } = findNumericMinMaxOfY(points, yAxisType, undefined, props.yScaleType);
@@ -809,6 +819,7 @@ export const ScatterChartBase: React.FunctionComponent<IScatterChartProps> = Rea
809819
datasetForXAxisDomain={_xAxisLabels}
810820
componentRef={cartesianChartRef}
811821
{...(_isScatterPolarRef.current ? { yMaxValue: 1, yMinValue: -1 } : {})}
822+
customizedCallout={_getCustomizedCallout()}
812823
/* eslint-disable react/jsx-no-bind */
813824

814825
children={(childProps: IChildProps) => {

packages/charts/react-charting/src/components/ScatterChart/ScatterChart.types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export interface IScatterChartProps extends ICartesianChartProps {
3737
*/
3838
onRenderCalloutPerDataPoint?: IRenderFunction<ICustomizedCalloutData>;
3939

40+
/**
41+
* Define a custom callout renderer for a stack; default is to render per data point
42+
*/
43+
onRenderCalloutPerStack?: IRenderFunction<ICustomizedCalloutData>;
44+
4045
/**
4146
* Callback for getting callout description message
4247
*/

packages/charts/react-charts/library/etc/react-charts.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,7 @@ export interface ScatterChartProps extends CartesianChartProps {
16501650
data: ChartProps;
16511651
getCalloutDescriptionMessage?: (calloutDataProps: CustomizedCalloutData) => string | undefined;
16521652
onRenderCalloutPerDataPoint?: RenderFunction<CustomizedCalloutData>;
1653+
onRenderCalloutPerStack?: RenderFunction<CustomizedCalloutData>;
16531654
styles?: ScatterChartStyles;
16541655
}
16551656

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export const ScatterChart: React.FunctionComponent<ScatterChartProps> = React.fo
9595
const [isSelectedLegend, setIsSelectedLegend] = React.useState<boolean>(false);
9696
const [activePoint, setActivePoint] = React.useState<string>('');
9797
const [stackCalloutProps, setStackCalloutProps] = React.useState<CustomizedCalloutData>();
98+
const [dataPointCalloutProps, setDataPointCalloutProps] = React.useState<CustomizedCalloutData>();
9899
const [clickPosition, setClickPosition] = React.useState({ x: 0, y: 0 });
99100
const [isPopoverOpen, setPopoverOpen] = React.useState(false);
100101
const [selectedLegends, setSelectedLegends] = React.useState<string[]>(props.legendProps?.selectedLegends || []);
@@ -573,6 +574,7 @@ export const ScatterChart: React.FunctionComponent<ScatterChartProps> = React.fo
573574
xAxisCalloutData ? setHoverXValue(xAxisCalloutData) : setHoverXValue('' + formattedData);
574575
setYValueHover(found.values);
575576
setStackCalloutProps(found!);
577+
setDataPointCalloutProps(found!);
576578
setActivePoint(circleId);
577579
}
578580
});
@@ -607,6 +609,7 @@ export const ScatterChart: React.FunctionComponent<ScatterChartProps> = React.fo
607609
xAxisCalloutData ? setHoverXValue(xAxisCalloutData) : setHoverXValue('' + formattedData);
608610
setYValueHover(found.values);
609611
setStackCalloutProps(found!);
612+
setDataPointCalloutProps(found!);
610613
setActivePoint(circleId);
611614
}
612615
} else {
@@ -640,6 +643,14 @@ export const ScatterChart: React.FunctionComponent<ScatterChartProps> = React.fo
640643
}
641644
}
642645

646+
function _getCustomizedCallout() {
647+
return props.onRenderCalloutPerStack
648+
? props.onRenderCalloutPerStack(stackCalloutProps)
649+
: props.onRenderCalloutPerDataPoint
650+
? props.onRenderCalloutPerDataPoint(dataPointCalloutProps)
651+
: null;
652+
}
653+
643654
/**
644655
* This function checks if the given legend is highlighted or not.
645656
* A legend can be highlighted in 2 ways:
@@ -712,6 +723,12 @@ export const ScatterChart: React.FunctionComponent<ScatterChartProps> = React.fo
712723
isCalloutForStack: true,
713724
culture: props.culture,
714725
isCartesian: true,
726+
customCallout: {
727+
customizedCallout: _getCustomizedCallout() !== null ? _getCustomizedCallout()! : undefined,
728+
customCalloutProps: props.calloutPropsPerDataPoint
729+
? props.calloutPropsPerDataPoint(dataPointCalloutProps!)
730+
: undefined,
731+
},
715732
};
716733
const tickParams = {
717734
tickValues,

packages/charts/react-charts/library/src/components/ScatterChart/ScatterChart.types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export interface ScatterChartProps extends CartesianChartProps {
3737
*/
3838
onRenderCalloutPerDataPoint?: RenderFunction<CustomizedCalloutData>;
3939

40+
/**
41+
* Define a custom callout renderer for a stack; default is to render per data point
42+
*/
43+
onRenderCalloutPerStack?: RenderFunction<CustomizedCalloutData>;
44+
4045
/**
4146
* Callback for getting callout description message
4247
*/

0 commit comments

Comments
 (0)