-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graphs): add hierarchical graph (#2692)
* feat: add hierarchical graph * chore: add prettier plugins * feat: add organization chart demo based on hierarchical graph * fix: remove react strict mode * fix: fix cr issues * fix: remove @ant-design/icons
- Loading branch information
Showing
36 changed files
with
818 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
packages/graphs/src/components/hierarchical-graph/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { Graph } from '@antv/g6'; | ||
import React, { forwardRef, ForwardRefExoticComponent, PropsWithChildren, PropsWithoutRef, RefAttributes } from 'react'; | ||
import { BaseGraph } from '../../core/base-graph'; | ||
import type { GraphOptions } from '../../types'; | ||
|
||
const HierarchicalGraph: ForwardRefExoticComponent< | ||
PropsWithoutRef<PropsWithChildren<GraphOptions>> & RefAttributes<Graph> | ||
> = forwardRef<Graph, PropsWithChildren<GraphOptions>>(({ children, ...props }, ref) => ( | ||
<BaseGraph type="hierarchical-graph" {...props} ref={ref}> | ||
{children} | ||
</BaseGraph> | ||
)); | ||
|
||
export default HierarchicalGraph; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export { HierarchicalGraph } from './HierarchicalGraph'; | ||
import HierarchicalGraph from './hierarchical-graph'; | ||
|
||
export { HierarchicalGraph }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { ChartLoading, ErrorBoundary } from '@ant-design/charts-util'; | ||
import type { Graph } from '@antv/g6'; | ||
import { Graphin } from '@antv/graphin'; | ||
import React, { | ||
forwardRef, | ||
ForwardRefExoticComponent, | ||
PropsWithChildren, | ||
PropsWithoutRef, | ||
RefAttributes, | ||
useImperativeHandle, | ||
useRef, | ||
} from 'react'; | ||
import type { GraphOptions, GraphType } from '../types'; | ||
import { useOptions } from './hooks'; | ||
|
||
interface BaseGraphProps extends GraphOptions { | ||
/** | ||
* 内部属性,只读 | ||
*/ | ||
readonly type: GraphType; | ||
} | ||
|
||
export const BaseGraph: ForwardRefExoticComponent< | ||
PropsWithoutRef<PropsWithChildren<BaseGraphProps>> & RefAttributes<Graph> | ||
> = forwardRef<Graph, PropsWithChildren<BaseGraphProps>>(({ children, ...props }, ref) => { | ||
const { | ||
type, | ||
containerStyle, | ||
className, | ||
onInit, | ||
onReady, | ||
onDestroy, | ||
errorTemplate, | ||
loading, | ||
loadingTemplate, | ||
...propOptions | ||
} = props; | ||
const graphRef = useRef<Graph | null>(null); | ||
|
||
const options = useOptions(type, propOptions); | ||
|
||
useImperativeHandle(ref, () => graphRef.current!); | ||
|
||
return ( | ||
<ErrorBoundary errorTemplate={errorTemplate}> | ||
{loading && <ChartLoading loadingTemplate={loadingTemplate} />} | ||
<Graphin | ||
ref={(ref) => { | ||
graphRef.current = ref; | ||
}} | ||
className={className} | ||
style={containerStyle} | ||
options={options} | ||
onInit={onInit} | ||
onReady={onReady} | ||
onDestroy={onDestroy} | ||
> | ||
{children} | ||
</Graphin> | ||
</ErrorBoundary> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { HoverActivate, idOf } from '@antv/g6'; | ||
|
||
export class HoverElement extends HoverActivate { | ||
getActiveIds(event) { | ||
const { model, graph } = this.context; | ||
const targetId = event.target.id; | ||
const targetType = graph.getElementType(targetId); | ||
|
||
const ids = [targetId]; | ||
if (targetType === 'edge') { | ||
const edge = model.getEdgeDatum(targetId); | ||
ids.push(edge.source, edge.target); | ||
} else if (targetType === 'node') { | ||
ids.push(...model.getRelatedEdgesData(targetId).map(idOf)); | ||
} | ||
|
||
graph.frontElement(ids); | ||
|
||
return ids; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { HoverElement } from './hover-element'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { withCollapsibleNode } from './with-collapsible-node'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { idOf } from '@antv/g6'; | ||
import { get, isEmpty } from 'lodash'; | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
import styled from 'styled-components'; | ||
import type { CollapsibleOptions } from '../../types'; | ||
import type { NodeProps } from '../nodes/types'; | ||
|
||
const StyledWrapper = styled.div` | ||
position: relative; | ||
height: inherit; | ||
width: inherit; | ||
.collapsible-icon { | ||
position: absolute; | ||
left: 50%; | ||
top: calc(100% + 24px); | ||
transform: translate(-50%, -50%); | ||
z-index: 1; | ||
&:hover { | ||
cursor: pointer; | ||
} | ||
} | ||
`; | ||
|
||
interface CollapsibleNodeProps extends NodeProps, CollapsibleOptions {} | ||
|
||
export const withCollapsibleNode = (NodeComponent: React.FC) => { | ||
return (props: CollapsibleNodeProps) => { | ||
const { data, graph, trigger, iconRender, iconClassName = '', iconStyle } = props; | ||
const [isCollapsed, setIsCollapsed] = useState(get(data, 'style.collapsed', false)); | ||
const wrapperRef = useRef(null); | ||
const iconRef = useRef(null); | ||
|
||
const isIconShown = trigger === 'icon' && !isEmpty(data.children); | ||
|
||
const handleClickCollapse = async (e) => { | ||
e.stopPropagation(); | ||
|
||
const toggleExpandCollapse = isCollapsed ? 'expandElement' : 'collapseElement'; | ||
await graph[toggleExpandCollapse](idOf(data)); | ||
setIsCollapsed((prev) => !prev); | ||
|
||
await graph.layout(); | ||
}; | ||
|
||
useEffect(() => { | ||
const target = trigger === 'icon' ? iconRef.current : trigger === 'node' ? wrapperRef.current : trigger; | ||
|
||
target?.addEventListener('click', handleClickCollapse); | ||
return () => { | ||
target?.removeEventListener('click', handleClickCollapse); | ||
}; | ||
}, [trigger, isCollapsed]); | ||
|
||
return ( | ||
<StyledWrapper ref={wrapperRef}> | ||
{isIconShown && ( | ||
<div ref={iconRef} className={`collapsible-icon ${iconClassName}`} style={iconStyle}> | ||
{iconRender?.(isCollapsed)} | ||
</div> | ||
)} | ||
{NodeComponent.call(graph, data)} | ||
</StyledWrapper> | ||
); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useOptions } from './useOptions'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import type { NodeData } from '@antv/g6'; | ||
import { idOf } from '@antv/g6'; | ||
import React, { useMemo } from 'react'; | ||
import type { GraphOptions, GraphType } from '../../types'; | ||
import { PlainNode } from '../nodes'; | ||
import { inferCollapsibleStyle, isCollapsible, isReactNode, parseCollapsible, upsertChildrenData } from '../utils/node'; | ||
import { mergeOptions } from '../utils/options'; | ||
|
||
const COMMON_OPTIONS: GraphOptions = { | ||
node: { | ||
type: 'react', | ||
style: {}, | ||
state: { | ||
active: { | ||
halo: false, | ||
}, | ||
selected: { | ||
halo: false, | ||
}, | ||
}, | ||
}, | ||
transforms: ['infer-react-style'], | ||
}; | ||
|
||
const hierarchicalGraphOptions: GraphOptions = { | ||
node: { | ||
style: { | ||
component: (data: NodeData) => <PlainNode text={idOf(data)} isActive={data.states?.includes('active')} />, | ||
size: [80, 40], | ||
ports: [{ placement: 'top' }, { placement: 'bottom' }], | ||
}, | ||
}, | ||
edge: { | ||
type: 'polyline', | ||
style: { | ||
router: { | ||
type: 'orth', | ||
}, | ||
}, | ||
}, | ||
layout: { | ||
type: 'antv-dagre', | ||
rankdir: 'TB', | ||
}, | ||
animation: false, | ||
}; | ||
|
||
const TEMPLATE_OPTIONS_MAP: Record<GraphType, GraphOptions> = { | ||
'hierarchical-graph': hierarchicalGraphOptions, | ||
}; | ||
|
||
export const useOptions = (type: GraphType, propOptions: GraphOptions) => { | ||
return useMemo(() => { | ||
const defaultOptions = mergeOptions(TEMPLATE_OPTIONS_MAP[type] || {}, COMMON_OPTIONS); | ||
const options = mergeOptions(propOptions, defaultOptions); | ||
|
||
if (isCollapsible(options)) { | ||
const { direction } = parseCollapsible(options.collapsible); | ||
options.data = upsertChildrenData(options.data, direction!); | ||
|
||
if (isReactNode(options)) inferCollapsibleStyle(options); | ||
} | ||
|
||
return options; | ||
}, [type, propOptions]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { OrganizationChartNode } from './organization-chart-node'; | ||
export { PlainNode } from './plain-node'; |
Oops, something went wrong.