Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Horizontal Bar Chart With Axis] Enable multiple legend selection #33484

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface IHorizontalBarChartWithAxisState extends IBasestate {
callOutAccessibilityData?: IAccessibilityProps;
srmukher marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line @typescript-eslint/no-explicit-any
tooltipElement?: any;
selectedLegends: string[];
}

type ColorScale = (_p?: number) => string;
Expand Down Expand Up @@ -98,6 +99,7 @@ export class HorizontalBarChartWithAxisBase
activeXdataPoint: null,
YValueHover: [],
hoverXValue: '',
selectedLegends: [],
};
this._calloutId = getId('callout');
this._tooltipId = getId('HBCWATooltipID_');
Expand Down Expand Up @@ -355,8 +357,7 @@ export class HorizontalBarChartWithAxisBase

const { YValueHover, hoverXValue } = this._getCalloutContentForBar(point);
if (
(this.state.isLegendSelected === false ||
(this.state.isLegendSelected && this.state.selectedLegendTitle === point.legend)) &&
(this.state.isLegendSelected === false || this._isLegendHighlighted(point.legend)) &&
this._calloutAnchorPoint !== point
) {
this._calloutAnchorPoint = point;
Expand Down Expand Up @@ -398,8 +399,8 @@ export class HorizontalBarChartWithAxisBase
color: string,
): void => {
if (
this.state.isLegendSelected === false ||
(this.state.isLegendSelected && this.state.selectedLegendTitle === point.legend)
(this.state.isLegendSelected === false || this._isLegendHighlighted(point.legend)) &&
this._calloutAnchorPoint !== point
) {
const { YValueHover, hoverXValue } = this._getCalloutContentForBar(point);
this._refArray.forEach((obj: IRefArrayData, index: number) => {
Expand Down Expand Up @@ -476,7 +477,7 @@ export class HorizontalBarChartWithAxisBase
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._isLegendHighlighted(point.legend);
}
this._classNames = getClassNames(this.props.styles!, {
theme: this.props.theme!,
Expand Down Expand Up @@ -603,8 +604,8 @@ export class HorizontalBarChartWithAxisBase
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;
if (this._getHighlightedLegend().length > 0) {
shouldHighlight = this._isLegendHighlighted(point.legend);
}
this._classNames = getClassNames(this.props.styles!, {
theme: this.props.theme!,
Expand Down Expand Up @@ -717,28 +718,8 @@ export class HorizontalBarChartWithAxisBase
});
};

private _onLegendClick(customMessage: string): void {
if (this.state.isLegendSelected) {
if (this.state.selectedLegendTitle === customMessage) {
this.setState({
isLegendSelected: false,
selectedLegendTitle: customMessage,
});
} else {
this.setState({
selectedLegendTitle: customMessage,
});
}
} else {
this.setState({
isLegendSelected: true,
selectedLegendTitle: customMessage,
});
}
}

private _onLegendHover(customMessage: string): void {
if (this.state.isLegendSelected === false) {
if (!this._isLegendSelected()) {
this.setState({
isLegendHovered: true,
selectedLegendTitle: customMessage,
Expand All @@ -747,11 +728,11 @@ export class HorizontalBarChartWithAxisBase
}

private _onLegendLeave(isLegendFocused?: boolean): void {
if (!!isLegendFocused || this.state.isLegendSelected === false) {
if (!!isLegendFocused || !this._isLegendSelected()) {
this.setState({
isLegendHovered: false,
selectedLegendTitle: '',
isLegendSelected: isLegendFocused ? false : this.state.isLegendSelected,
isLegendSelected: isLegendFocused ? false : this._isLegendSelected(),
});
}
}
Expand All @@ -778,9 +759,6 @@ export class HorizontalBarChartWithAxisBase
const legend: ILegend = {
title: point.legend!,
color,
action: () => {
this._onLegendClick(point.legend!);
},
hoverAction: () => {
this._handleChartMouseLeave();
this._onLegendHover(point.legend!);
Expand All @@ -799,11 +777,58 @@ export class HorizontalBarChartWithAxisBase
focusZonePropsInHoverCard={this.props.focusZonePropsForLegendsInHoverCard}
overflowText={this.props.legendsOverflowText}
{...this.props.legendProps}
onChange={this._onLegendSelectionChange.bind(this)}
/>
);
return legends;
};

private _isLegendSelected = (): boolean => {
return this.state.isLegendSelected!;
};

/**
* This function checks if the given legend is highlighted or not.
* A legend can be highlighted in 2 ways:
* 1. selection: if the user clicks on it
* 2. hovering: if there is no selected legend and the user hovers over it
*/
private _isLegendHighlighted = (legend?: string) => {
return this._getHighlightedLegend().includes(legend!);
};

private _getHighlightedLegend() {
return this.state.selectedLegends.length > 0
? this.state.selectedLegends
: this.state.selectedLegendTitle
? [this.state.selectedLegendTitle]
: [];
}

private _onLegendSelectionChange(
selectedLegends: string[],
event: React.MouseEvent<HTMLButtonElement>,
currentLegend?: ILegend,
): void {
if (this.props.legendProps?.canSelectMultipleLegends) {
this.setState({
selectedLegends,
selectedLegendTitle: currentLegend?.title!,
});
} else {
this.setState({
selectedLegends: selectedLegends.slice(-1),
selectedLegendTitle: currentLegend?.title!,
});
}
this.setState({
isLegendSelected: selectedLegends.length > 0,
});
if (this.props.legendProps?.onChange) {
this.props.legendProps.onChange(selectedLegends, event, currentLegend);
}
}

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 @@ -169,6 +169,31 @@ describe('Horizontal bar chart with axis- Subcomponent Legends', () => {
expect(legendsAfterClickEvent[3]).toHaveAttribute('aria-selected', 'false');
},
);

testWithoutWait(
'Should select multiple legends on multiple mouse click on legends',
HorizontalBarChartWithAxis,
{ data: chartPointsHBCWA, legendProps: { canSelectMultipleLegends: true } },
container => {
// const legends = screen.getAllByText((content, element) => element!.tagName.toLowerCase() === 'button');
const legend1 = screen.getByText('Grapes')?.closest('button');
const legend2 = screen.getByText('Apples')?.closest('button');

expect(legend1).toBeDefined();
expect(legend2).toBeDefined();

fireEvent.click(legend1!);
fireEvent.click(legend2!);
const legendsAfterClickEvent = screen.getAllByText(
(content, element) => element!.tagName.toLowerCase() === 'button',
);
// Assert
expect(legendsAfterClickEvent[0]).toHaveAttribute('aria-selected', 'false');
expect(legendsAfterClickEvent[1]).toHaveAttribute('aria-selected', 'true');
expect(legendsAfterClickEvent[2]).toHaveAttribute('aria-selected', 'true');
expect(legendsAfterClickEvent[3]).toHaveAttribute('aria-selected', 'false');
},
);
});

describe('Horizontal bar chart with axis - Subcomponent callout', () => {
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
44 changes: 22 additions & 22 deletions packages/react/etc/react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1604,49 +1604,49 @@ export { FabricPerformance }
// @public (undocumented)
export enum FabricSlots {
// (undocumented)
black = 21,// BaseSlots.primaryColor, Shade[Shade.Unshaded]);
black = 21,
Copy link
Contributor

@AtishayMsft AtishayMsft Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert changes in this file

// (undocumented)
neutralDark = 20,// BaseSlots.primaryColor, Shade[Shade.Shade1]);
neutralDark = 20,
// (undocumented)
neutralLight = 11,// BaseSlots.primaryColor, Shade[Shade.Shade2]);
neutralLight = 11,
// (undocumented)
neutralLighter = 10,// BaseSlots.primaryColor, Shade[Shade.Shade3]);
neutralLighter = 10,
// (undocumented)
neutralLighterAlt = 9,// BaseSlots.primaryColor, Shade[Shade.Shade4]);
neutralLighterAlt = 9,
// (undocumented)
neutralPrimary = 19,// BaseSlots.primaryColor, Shade[Shade.Shade5]);
neutralPrimary = 19,
// (undocumented)
neutralPrimaryAlt = 18,// BaseSlots.primaryColor, Shade[Shade.Shade6]);
neutralPrimaryAlt = 18,
// (undocumented)
neutralQuaternary = 13,// BaseSlots.primaryColor, Shade[Shade.Shade7]);
neutralQuaternary = 13,
// (undocumented)
neutralQuaternaryAlt = 12,// BaseSlots.primaryColor, Shade[Shade.Shade8]);
neutralQuaternaryAlt = 12,
// (undocumented)
neutralSecondary = 17,// BaseSlots.backgroundColor, Shade[Shade.Shade1]);
neutralSecondary = 17,
// (undocumented)
neutralSecondaryAlt = 16,// BaseSlots.backgroundColor, Shade[Shade.Shade2]);
neutralSecondaryAlt = 16,
// (undocumented)
neutralTertiary = 15,// BaseSlots.backgroundColor, Shade[Shade.Shade3]);
neutralTertiary = 15,
// (undocumented)
neutralTertiaryAlt = 14,// BaseSlots.backgroundColor, Shade[Shade.Shade4]);
neutralTertiaryAlt = 14,
// (undocumented)
themeDark = 7,// BaseSlots.backgroundColor, Shade[Shade.Shade5]);
themeDark = 7,
// (undocumented)
themeDarkAlt = 6,// BaseSlots.backgroundColor, Shade[Shade.Shade6]); // bg6 or fg2
themeDarkAlt = 6,
// (undocumented)
themeDarker = 8,// BaseSlots.foregroundColor, Shade[Shade.Shade3]);
themeDarker = 8,
// (undocumented)
themeLight = 3,// BaseSlots.foregroundColor, Shade[Shade.Shade4]);
themeLight = 3,
// (undocumented)
themeLighter = 2,// BaseSlots.foregroundColor, Shade[Shade.Shade5]);
themeLighter = 2,
// (undocumented)
themeLighterAlt = 1,// BaseSlots.foregroundColor, Shade[Shade.Shade6]);
themeLighterAlt = 1,
// (undocumented)
themePrimary = 0,// BaseSlots.foregroundColor, Shade[Shade.Unshaded]);
themePrimary = 0,
// (undocumented)
themeSecondary = 5,// BaseSlots.foregroundColor, Shade[Shade.Shade7]);
themeSecondary = 5,
// (undocumented)
themeTertiary = 4,// BaseSlots.foregroundColor, Shade[Shade.Shade8]);
themeTertiary = 4,
// (undocumented)
white = 22
}
Expand Down
Loading