Skip to content

Commit

Permalink
Enable multiple legend selection for Horizontal bar chart with axis
Browse files Browse the repository at this point in the history
  • Loading branch information
srmukher committed Dec 16, 2024
1 parent 7b75871 commit f255301
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export interface IHorizontalBarChartWithAxisState extends IBasestate {
callOutAccessibilityData?: IAccessibilityProps;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
tooltipElement?: any;
selectedLegends: string[];
}

type ColorScale = (_p?: number) => string;
Expand Down Expand Up @@ -93,6 +94,7 @@ export class HorizontalBarChartWithAxisBase extends React.Component<
activeXdataPoint: null,
YValueHover: [],
hoverXValue: '',
selectedLegends: [],
};
this._calloutId = getId('callout');
this._tooltipId = getId('HBCWATooltipID_');
Expand Down Expand Up @@ -337,7 +339,10 @@ export class HorizontalBarChartWithAxisBase extends React.Component<
const { YValueHover, hoverXValue } = this._getCalloutContentForBar(point);
if (
(this.state.isLegendSelected === false ||
(this.state.isLegendSelected && this.state.selectedLegendTitle === point.legend)) &&
(this.state.isLegendSelected &&
(this.state.selectedLegendTitle === point.legend ||
this.state.selectedLegends.length === 0 ||
this.state.selectedLegends.includes(point.legend!)))) &&
this._calloutAnchorPoint !== point
) {
this._calloutAnchorPoint = point;
Expand Down Expand Up @@ -457,7 +462,10 @@ export class HorizontalBarChartWithAxisBase extends React.Component<
const bars = sortedBars.map((point: IHorizontalBarChartWithAxisDataPoint, index: number) => {
let shouldHighlight = true;
if (this.state.isLegendHovered || this.state.isLegendSelected) {
shouldHighlight = this.state.selectedLegendTitle === point.legend;
shouldHighlight =
this.state.selectedLegendTitle === point.legend ||
this.state.selectedLegends.length === 0 ||
this.state.selectedLegends?.includes(point.legend!);
}
this._classNames = getClassNames(this.props.styles!, {
theme: this.props.theme!,
Expand Down Expand Up @@ -780,11 +788,17 @@ export class HorizontalBarChartWithAxisBase extends React.Component<
focusZonePropsInHoverCard={this.props.focusZonePropsForLegendsInHoverCard}
overflowText={this.props.legendsOverflowText}
{...this.props.legendProps}
canSelectMultipleLegends={this.props.legendProps?.canSelectMultipleLegends}
onChange={this._onLegendChange}
/>
);
return legends;
};

private _onLegendChange = (selectedLegends: string[]) => {
this.setState({ selectedLegends });
};

private _getAxisData = (yAxisData: IAxisData) => {
if (yAxisData && yAxisData.yAxisDomainValues.length) {
// For HBCWA x and y Values are swapped
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface IHorizontalBarChartWithAxisState {
useSingleColor: boolean;
enableGradient: boolean;
roundCorners: boolean;
selectMultipleLegends: boolean;
}

const options: IChoiceGroupOption[] = [
Expand All @@ -38,6 +39,7 @@ export class HorizontalBarChartWithAxisBasicExample extends React.Component<
useSingleColor: false,
enableGradient: false,
roundCorners: false,
selectMultipleLegends: false,
};
}

Expand Down Expand Up @@ -71,6 +73,10 @@ export class HorizontalBarChartWithAxisBasicExample extends React.Component<
this.setState({ roundCorners: checked });
};

private _onToggleRoundMultipleLegendSelection = (ev: React.MouseEvent<HTMLElement>, checked: boolean) => {
this.setState({ selectMultipleLegends: checked });
};

private _basicExample(): JSX.Element {
const points: IHorizontalBarChartWithAxisDataPoint[] = [
{
Expand Down Expand Up @@ -148,6 +154,12 @@ export class HorizontalBarChartWithAxisBasicExample extends React.Component<
<Toggle label="Enable Gradient" onText="ON" offText="OFF" onChange={this._onToggleGradient} />
&nbsp;&nbsp;
<Toggle label="Rounded Corners" onText="ON" offText="OFF" onChange={this._onToggleRoundCorners} />
<Toggle
label="Select multiple legends"
onText="ON"
offText="OFF"
onChange={this._onToggleRoundMultipleLegendSelection}
/>
</div>
<br />

Expand All @@ -168,6 +180,9 @@ export class HorizontalBarChartWithAxisBasicExample extends React.Component<
enableReflow={true}
enableGradient={this.state.enableGradient}
roundCorners={this.state.roundCorners}
legendProps={{
canSelectMultipleLegends: this.state.selectMultipleLegends,
}}
/>
</div>
</>
Expand Down

0 comments on commit f255301

Please sign in to comment.