diff --git a/packages/charts/react-charting/src/components/VerticalBarChart/VerticalBarChart.base.tsx b/packages/charts/react-charting/src/components/VerticalBarChart/VerticalBarChart.base.tsx index 9e793a25b87c6..8644255fe92af 100644 --- a/packages/charts/react-charting/src/components/VerticalBarChart/VerticalBarChart.base.tsx +++ b/packages/charts/react-charting/src/components/VerticalBarChart/VerticalBarChart.base.tsx @@ -72,6 +72,7 @@ export interface IVerticalBarChartState extends IBasestate { hoverXValue?: string | number | null; callOutAccessibilityData?: IAccessibilityProps; calloutLegend: string; + selectedLegends: string[]; } type ColorScale = (_p?: number) => string; @@ -110,8 +111,8 @@ export class VerticalBarChartBase extends React.Component 1) && { isCalloutForStack: true })} legendBars={legendBars} datasetForXAxisDomain={this._xAxisLabels} barwidth={this._barWidth} @@ -382,11 +384,11 @@ export class VerticalBarChartBase extends React.Component { - const { selectedLegend, activeXdataPoint } = this.state; - if (selectedLegend !== '') { - if (xAxisPoint === activeXdataPoint && selectedLegend === legend) { + const { activeXdataPoint } = this.state; + if (!this._noLegendHighlighted()) { + if (xAxisPoint === activeXdataPoint && this._legendHighlighted(legend)) { return { visibility: CircleVisbility.show, radius: 8 }; - } else if (selectedLegend === legend) { + } else if (this._legendHighlighted(legend)) { // Don't hide the circle to keep it focusable. For more information, // see https://fuzzbomb.github.io/accessibility-demos/visually-hidden-focus-test.html return { visibility: CircleVisbility.show, radius: 0.3 }; @@ -517,7 +519,7 @@ export class VerticalBarChartBase extends React.Component { - this._onLegendClick(point.legend!); - }, hoverAction: () => { this._handleChartMouseLeave(); this._onLegendHover(point.legend!); @@ -1097,9 +1086,6 @@ export class VerticalBarChartBase extends React.Component { - this._onLegendClick(lineLegendText); - }, hoverAction: () => { this._handleChartMouseLeave(); this._onLegendHover(lineLegendText); @@ -1119,11 +1105,27 @@ export class VerticalBarChartBase extends React.Component ); return legends; }; + private _onLegendSelectionChange( + selectedLegends: string[], + event: React.MouseEvent, + currentLegend?: ILegend, + ): void { + if (this.props.legendProps?.canSelectMultipleLegends) { + this.setState({ selectedLegends }); + } else { + this.setState({ selectedLegends: selectedLegends.slice(-1) }); + } + if (this.props.legendProps?.onChange) { + this.props.legendProps.onChange(selectedLegends, event, currentLegend); + } + } + private _getAxisData = (yAxisData: IAxisData) => { if (yAxisData && yAxisData.yAxisDomainValues.length) { const { yAxisDomainValues: domainValue } = yAxisData; @@ -1138,20 +1140,25 @@ export class VerticalBarChartBase extends React.Component { - return ( - this.state.selectedLegend === legendTitle || - (this.state.selectedLegend === '' && this.state.activeLegend === legendTitle) - ); + private _legendHighlighted = (legendTitle: string | undefined) => { + return this._getHighlightedLegend().includes(legendTitle!); }; /** * This function checks if none of the legends is selected or hovered. */ private _noLegendHighlighted = () => { - return this.state.selectedLegend === '' && this.state.activeLegend === ''; + return this._getHighlightedLegend().length === 0; }; + private _getHighlightedLegend() { + return this.state.selectedLegends.length > 0 + ? this.state.selectedLegends + : this.state.activeLegend + ? [this.state.activeLegend] + : []; + } + private _getAriaLabel = (point: IVerticalBarChartDataPoint): string => { const xValue = point.xAxisCalloutData ? point.xAxisCalloutData diff --git a/packages/react-examples/src/react-charting/VerticalBarChart/VerticalBarChart.Basic.Example.tsx b/packages/react-examples/src/react-charting/VerticalBarChart/VerticalBarChart.Basic.Example.tsx index e9cbcb71d7828..045c52bbad61a 100644 --- a/packages/react-examples/src/react-charting/VerticalBarChart/VerticalBarChart.Basic.Example.tsx +++ b/packages/react-examples/src/react-charting/VerticalBarChart/VerticalBarChart.Basic.Example.tsx @@ -23,6 +23,7 @@ interface IVerticalChartState { showAxisTitles: boolean; enableGradient: boolean; roundCorners: boolean; + selectMultipleLegends: boolean; } const options: IChoiceGroupOption[] = [ @@ -42,6 +43,7 @@ export class VerticalBarChartBasicExample extends React.Component, checked: boolean) => { + this.setState({ selectMultipleLegends: checked }); + }; + private _basicExample(): JSX.Element { const points: IVerticalBarChartDataPoint[] = [ { @@ -234,6 +240,13 @@ export class VerticalBarChartBasicExample extends React.Component    +    + {this.state.showAxisTitles && (
@@ -259,6 +272,9 @@ export class VerticalBarChartBasicExample extends React.Component
)} @@ -284,6 +300,9 @@ export class VerticalBarChartBasicExample extends React.Component )}