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

Sankey revisit ankit #25455

Merged
merged 23 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6aa878c
Sankey chart changes for the new design
ankityadav4 Sep 30, 2022
7529b16
Sankey changes for updated design
ankityadav4 Oct 31, 2022
a00f600
Adding change file
ankityadav4 Oct 31, 2022
1ceea77
Fixing build failure
ankityadav4 Nov 1, 2022
7436cb1
Fixing build failure
ankityadav4 Nov 1, 2022
c29f10c
fixing build failure
ankityadav4 Nov 1, 2022
792d6f0
fixing build failure
ankityadav4 Nov 1, 2022
8b7321b
fixing build failure
ankityadav4 Nov 1, 2022
f0513a3
Adding border color and reverting changes for package.json
ankityadav4 Nov 2, 2022
f394b7b
Adding changes for opacity changes for links,font size for text,diffe…
ankityadav4 Nov 14, 2022
be038e4
Resolving review comments and removing the examples used for testing
ankityadav4 Nov 23, 2022
0107a6b
Fixing the gradient issue
ankityadav4 Nov 24, 2022
c403724
Fixing the id issues
ankityadav4 Nov 24, 2022
250c852
Fixing the tooltip position issue
ankityadav4 Nov 24, 2022
c9862c6
Fixing the tooltip position issue
ankityadav4 Nov 24, 2022
4e9f39c
Adding comment for tooltip fix
ankityadav4 Nov 24, 2022
574b984
Fixing the tooltip position issue
ankityadav4 Nov 24, 2022
0c2214b
Fixing the tooltip position issue
ankityadav4 Nov 24, 2022
d03f6df
Incorporating review comments and improving code efficiency followimg…
ankityadav4 Dec 18, 2022
522ff90
Adding Unit Test cases for Sankey Chart
ankityadav4 Dec 28, 2022
254ab3d
Resolving review comments
ankityadav4 Jan 17, 2023
ec845ef
Minor fixes
ankityadav4 Jan 17, 2023
ee1e123
Resolving review comments
ankityadav4 Jan 30, 2023
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 @@ -5,7 +5,7 @@ import { IProcessedStyleSet } from '@fluentui/react/lib/Styling';
import * as d3Sankey from 'd3-sankey';
import { area as d3Area, curveBumpX as d3CurveBasis } from 'd3-shape';
import { sum as d3Sum } from 'd3-array';
import { ChartHoverCard, IBasestate, SLink, SNode } from '../../index';
import { ChartHoverCard, SLink, SNode } from '../../index';
import { Callout, DirectionalHint } from '@fluentui/react/lib/Callout';
import { select, selectAll } from 'd3-selection';
import { FocusZone, FocusZoneDirection, FocusZoneTabbableElements } from '@fluentui/react-focus';
Expand All @@ -17,7 +17,7 @@ const PADDING_PERCENTAGE = 0.3;
type NodesInColumns = { [key: number]: SNode[] };
type NodeColors = { fillColor: string; borderColor: string };

export interface ISankeyChartState extends IBasestate {
export interface ISankeyChartState {
containerWidth: number;
containerHeight: number;
selectedState: boolean;
Expand Down Expand Up @@ -74,14 +74,12 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
selectedLinks: new Set<number>(),
selectedNodes: new Set<number>(),
shouldOverflow: false,
isCalloutVisible: false,
};
this._calloutId = getId('callout');
this._linkId = getId('link');
this._margins = { top: 36, right: 48, bottom: 32, left: 48 };
this._preRenderLayout();
ankityadav4 marked this conversation as resolved.
Show resolved Hide resolved
this._nodesInColumn = this._populateNodeInColumns(this._sankey);
this._adjustOnePercentHeightNodes(this._nodesInColumn);
}
public componentDidMount(): void {
this._fitParentContainer();
Expand All @@ -91,6 +89,11 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
if (prevProps.shouldResize !== this.props.shouldResize) {
this._fitParentContainer();
}
if (prevProps.data.SankeyChartData !== this.props.data.SankeyChartData) {
this._preRenderLayout();
this._nodesInColumn = this._populateNodeInColumns(this._sankey);
this._adjustOnePercentHeightNodes(this._nodesInColumn);
}
}
public componentWillUnmount(): void {
cancelAnimationFrame(this._reqID);
Expand All @@ -105,17 +108,7 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
className,
});
// We are using the this._margins.left and this._margins.top in sankey extent while constructing the layout
const width = this.state.containerWidth - this._margins.right!;
const height =
this.state.containerHeight - this._margins.bottom! > 0 ? this.state.containerHeight - this._margins.bottom! : 0;

this._sankey = d3Sankey
.sankey()
.nodeWidth(124)
.extent([
[this._margins.left!, this._margins.top!],
[width - 1, height - 6],
]);
const { height, width } = this._preRenderLayout();
let nodePadding = 8;
nodePadding = this._adjustPadding(this._sankey, height - 6, this._nodesInColumn);

Expand Down Expand Up @@ -147,7 +140,7 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
);
}

private _preRenderLayout() {
private _preRenderLayout(): { height: number; width: number } {
const width = this.state.containerWidth - this._margins.right!;
const height =
this.state.containerHeight - this._margins.bottom! > 0 ? this.state.containerHeight - this._margins.bottom! : 0;
Expand All @@ -159,6 +152,8 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
[this._margins.left!, this._margins.top!],
[width - 1, height - 6],
]);

return { height, width };
}

/**
Expand Down Expand Up @@ -274,15 +269,14 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
if (this.props.data.SankeyChartData) {
this.props.data.SankeyChartData.links.forEach((singleLink: SLink, index: number) => {
const calloutProps = {
isCalloutVisible:
(this.state.selectedState && this.state.selectedLink) ||
(this.state.isCalloutVisible && this.state.selectedLink),
directionalHint: DirectionalHint.bottomCenter,
isCalloutVisible: this.state.selectedLink,
directionalHint: DirectionalHint.topAutoEdge,
id: `toolTip${this._calloutId}`,
target: this.state.linkElement,
isBeakVisible: false,
gapSpace: 15,
onDismiss: this._onCloseCallout,
className: this._classNames.calloutContentRoot,
};
const onMouseOut = () => {
this._onStreamLeave(singleLink);
Expand Down Expand Up @@ -326,8 +320,15 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
onBlur={this._onBlur}
fillOpacity={this._getOpacityStream(singleLink)}
data-is-focusable={true}
aria-labelledby={`toolTip${this._calloutId}`}
role="text"
aria-label={
'link from' +
(singleLink.source as SNode).name +
'to' +
(singleLink.target as SNode).name +
'with weight' +
singleLink!.unnormalizedValue
}
role="img"
>
{calloutProps.isCalloutVisible && (
<Callout {...calloutProps}>
Expand Down Expand Up @@ -361,15 +362,14 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
let padding = 8;
const calloutProps = {
isCalloutVisible:
this.state.selectedState &&
this.state.selectedNode &&
this.state.selectedNode!.y1! - this.state.selectedNode!.y0! < MIN_HEIGHT_FOR_TYPE,
directionalHint: DirectionalHint.topAutoEdge,
id: `toolTip${getId('nodeGElement')}`,
target: this.state.nodeElement,
isBeakVisible: false,
gapSpace: 15,
preventDismissOnLostFocus: true,
className: this._classNames.calloutContentRoot,
};
let textLengthForNodeWeight = 0;

Expand Down Expand Up @@ -412,8 +412,8 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
strokeWidth="2"
opacity="1"
data-is-focusable={true}
aria-label={`${singleNode.name} ${singleNode.actualValue}`}
role="text"
aria-label={'node' + `${singleNode.name}` + 'with weight' + `${singleNode.actualValue}`}
role="img"
/>

{calloutProps.isCalloutVisible && (
Expand All @@ -437,6 +437,7 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
dx={'0.4em'}
textAnchor="start"
fontWeight="regular"
aria-hidden="true"
fill={
!(
!this.state.selectedState ||
Expand Down Expand Up @@ -465,6 +466,7 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
dx={height > MIN_HEIGHT_FOR_DOUBLINE_TYPE ? '0.4em' : '0em'}
textAnchor="start"
fontWeight="bold"
aria-hidden="true"
fill={
!(
!this.state.selectedState ||
Expand Down Expand Up @@ -527,7 +529,6 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
const { selectedLinks, selectedNodes } = this._getSelectedLinksforStreamHover(singleLink);
this.setState({
selectedState: true,
isCalloutVisible: true,
selectedNodes: new Set<number>(Array.from(selectedNodes).map(node => node.index!)),
selectedLinks: new Set<number>(Array.from(selectedLinks).map(link => link.index!)),
linkElement: mouseEvent,
Expand All @@ -540,7 +541,6 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
private _onStreamLeave(singleLink: SLink) {
if (this.state.selectedState) {
this.setState({
isCalloutVisible: false,
selectedState: false,
selectedNodes: new Set<number>(),
selectedLinks: new Set<number>(),
Expand All @@ -554,7 +554,6 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
private _onFocusLink(singleLink: SLink, element: React.FocusEvent<SVGElement>): void {
element.persist();
this.setState({
isCalloutVisible: true,
linkElement: element.currentTarget,
selectedLink: singleLink,
});
Expand All @@ -563,7 +562,6 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _onCloseCallout = () => {
this.setState({
isCalloutVisible: false,
selectedLink: undefined,
linkElement: '',
});
Expand Down Expand Up @@ -794,6 +792,11 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
.attr('font-size', '10')
ankityadav4 marked this conversation as resolved.
Show resolved Hide resolved
.append('tspan')
.text(null);
tspan.text(text);
if (tspan.node() !== null && tspan.node()!.getComputedTextLength() <= textLengthForNodeName) {
return text;
}
tspan.text(null);
tspan.text('...');
if (tspan.node() !== null) {
elipsisLength = tspan.node()!.getComputedTextLength();
ankityadav4 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -805,11 +808,8 @@ export class SankeyChartBase extends React.Component<ISankeyChartProps, ISankeyC
tspan.text(line);
if (tspan.node() !== null) {
const w = tspan.node()!.getComputedTextLength();
if (w > textLengthForNodeName) {
while (tspan.node()!.getComputedTextLength() > textLengthForNodeName - elipsisLength) {
line = line.slice(0, -1);
tspan.text(line);
}
if (w >= textLengthForNodeName - elipsisLength) {
line = line.slice(0, -1);
line += '...';
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HighContrastSelectorBlack } from '@fluentui/react/lib/Styling';

export const getStyles = (props: ISankeyChartStyleProps): ISankeyChartStyles => {
const { className, theme, pathColor } = props;
const { effects } = theme;
return {
root: [
theme.fonts.medium,
Expand All @@ -17,7 +18,7 @@ export const getStyles = (props: ISankeyChartStyleProps): ISankeyChartStyles =>
],
links: {
stroke: pathColor ? pathColor : theme.palette.black,
fill: '#F5F5F5',
fill: theme ? theme.semanticColors.bodyBackground : '#F5F5F5',
strokeWidth: 3,
selectors: {
[HighContrastSelectorBlack]: {
Expand Down Expand Up @@ -61,5 +62,8 @@ export const getStyles = (props: ISankeyChartStyleProps): ISankeyChartStyles =>
marginBottom: '4px',
marginRight: '8px',
},
calloutContentRoot: {
boxShadow: effects.elevation4,
ankityadav4 marked this conversation as resolved.
Show resolved Hide resolved
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,8 @@ describe('Render calling with respective to props', () => {
height: 500,
width: 800,
};
const component = mount(<SankeyChart {...props} />);
component.setProps({ ...props });
expect(renderMock).toHaveBeenCalledTimes(2);
mount(<SankeyChart {...props} />);
expect(renderMock).toHaveBeenCalledTimes(1);
renderMock.mockRestore();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,10 @@ export interface ISankeyChartStyles {
* Style for the tooltip ,when user hover over the truncated node detail.
*/
toolTip?: IStyle;

/**
* Style for the tooltip ,when user hover over the truncated node detail.
*/

calloutContentRoot?: IStyle;
}
Loading