From 7b75871739a0639fffc065d649848a356baf1d26 Mon Sep 17 00:00:00 2001 From: Anush Gupta <74965306+Anush2303@users.noreply.github.com> Date: Mon, 16 Dec 2024 17:01:29 +0530 Subject: [PATCH] Enable Controlled Legend working in Declarative HBC (#33476) --- ...-f8073f84-d26a-4b28-992e-379202a63f5d.json | 7 +++++++ .../HorizontalBarChartWithAxis.base.tsx | 14 +++++++++++-- .../HorizontalBarChartWithAxis.test.tsx.snap | 10 ++++++++++ ...orizontalBarChartWithAxisRTL.test.tsx.snap | 20 +++++++++++++++++++ .../schema/fluent_horizontalbar.json | 2 +- 5 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 change/@fluentui-react-charting-f8073f84-d26a-4b28-992e-379202a63f5d.json diff --git a/change/@fluentui-react-charting-f8073f84-d26a-4b28-992e-379202a63f5d.json b/change/@fluentui-react-charting-f8073f84-d26a-4b28-992e-379202a63f5d.json new file mode 100644 index 00000000000000..4ee5b0e301fd36 --- /dev/null +++ b/change/@fluentui-react-charting-f8073f84-d26a-4b28-992e-379202a63f5d.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "Enable Controlled Legends working in declarative HBC", + "packageName": "@fluentui/react-charting", + "email": "74965306+Anush2303@users.noreply.github.com", + "dependentChangeType": "patch" +} diff --git a/packages/charts/react-charting/src/components/HorizontalBarChartWithAxis/HorizontalBarChartWithAxis.base.tsx b/packages/charts/react-charting/src/components/HorizontalBarChartWithAxis/HorizontalBarChartWithAxis.base.tsx index 10246111177c29..8cb522e55edd42 100644 --- a/packages/charts/react-charting/src/components/HorizontalBarChartWithAxis/HorizontalBarChartWithAxis.base.tsx +++ b/packages/charts/react-charting/src/components/HorizontalBarChartWithAxis/HorizontalBarChartWithAxis.base.tsx @@ -84,7 +84,7 @@ export class HorizontalBarChartWithAxisBase extends React.Component< color: '', dataForHoverCard: 0, isCalloutVisible: false, - isLegendSelected: false, + isLegendSelected: props.legendProps?.selectedLegend !== undefined, isLegendHovered: false, refSelected: null, selectedLegendTitle: props.legendProps?.selectedLegend ?? '', @@ -583,6 +583,15 @@ export class HorizontalBarChartWithAxisBase extends React.Component< const { xBarScale, yBarScale } = this._getScales(containerHeight, containerWidth, false); const { useSingleColor = false } = this.props; const bars = this._points.map((point: IHorizontalBarChartWithAxisDataPoint, index: number) => { + let shouldHighlight = true; + if (this.state.isLegendHovered || this.state.isLegendSelected) { + shouldHighlight = this.state.selectedLegendTitle === point.legend; + } + this._classNames = getClassNames(this.props.styles!, { + theme: this.props.theme!, + legendColor: this.state.color, + shouldHighlight, + }); const barHeight: number = Math.max(yBarScale(point.y), 0); if (barHeight < 1) { return ; @@ -627,6 +636,7 @@ export class HorizontalBarChartWithAxisBase extends React.Component< diff --git a/packages/charts/react-charting/src/components/HorizontalBarChartWithAxis/__snapshots__/HorizontalBarChartWithAxis.test.tsx.snap b/packages/charts/react-charting/src/components/HorizontalBarChartWithAxis/__snapshots__/HorizontalBarChartWithAxis.test.tsx.snap index eeabb9f133817c..ae589f61329a71 100644 --- a/packages/charts/react-charting/src/components/HorizontalBarChartWithAxis/__snapshots__/HorizontalBarChartWithAxis.test.tsx.snap +++ b/packages/charts/react-charting/src/components/HorizontalBarChartWithAxis/__snapshots__/HorizontalBarChartWithAxis.test.tsx.snap @@ -2128,6 +2128,11 @@ exports[`HorizontalBarChartWithAxis snapShot testing renders showToolTipForYAxis