-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: dendrogram * fix: use lodash * docs: update config * refactor: dendrogram * chore: revert version update * fix: remove unnecessary null checks
- Loading branch information
Showing
12 changed files
with
163 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Graph } from '@antv/g6'; | ||
import React, { | ||
ForwardRefExoticComponent, | ||
PropsWithChildren, | ||
PropsWithoutRef, | ||
RefAttributes, | ||
forwardRef, | ||
useMemo, | ||
} from 'react'; | ||
import { BaseGraph } from '../../core/base-graph'; | ||
import { COMMON_OPTIONS } from '../../core/constants'; | ||
import { mergeOptions } from '../../core/utils/options'; | ||
import { DEFAULT_OPTIONS, getDendrogramOptions } from './options'; | ||
import type { DendrogramOptions } from './types'; | ||
|
||
export const Dendrogram: ForwardRefExoticComponent< | ||
PropsWithoutRef<PropsWithChildren<DendrogramOptions>> & RefAttributes<Graph> | ||
> = forwardRef<Graph, PropsWithChildren<DendrogramOptions>>(({ children, ...props }, ref) => { | ||
const options = useMemo(() => { | ||
const { direction = 'horizontal', compact = false, ...restProps } = props; | ||
return mergeOptions(COMMON_OPTIONS, DEFAULT_OPTIONS, getDendrogramOptions({ direction, compact }), restProps); | ||
}, [props]); | ||
|
||
return ( | ||
<BaseGraph {...options} ref={ref}> | ||
{children} | ||
</BaseGraph> | ||
); | ||
}); | ||
|
||
export type { DendrogramOptions }; |
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,61 @@ | ||
import { isEmpty } from 'lodash'; | ||
import type { DendrogramOptions } from './types'; | ||
|
||
export const DEFAULT_OPTIONS: DendrogramOptions = { | ||
node: { | ||
type: 'circle', | ||
style: { | ||
labelText: (d) => d.id, | ||
}, | ||
}, | ||
}; | ||
|
||
export const getDendrogramOptions = ({ | ||
direction, | ||
compact, | ||
}: Pick<DendrogramOptions, 'direction' | 'compact'>): DendrogramOptions => { | ||
const isLeafNode = (d) => isEmpty(d.children); | ||
|
||
const layoutType = compact ? 'compact-box' : 'dendrogram'; | ||
if (direction === 'vertical') { | ||
return { | ||
node: { | ||
style: { | ||
labelBackground: true, | ||
labelPlacement: 'right', | ||
labelTransform: (d) => (isLeafNode(d) ? 'rotate(90deg) translate(18px)' : 'translate(18px)'), | ||
ports: [{ placement: 'top' }, { placement: 'bottom' }], | ||
}, | ||
}, | ||
edge: { type: 'cubic-vertical' }, | ||
layout: { type: layoutType, direction: 'TB', nodeSep: 40, rankSep: 140, getVGap: () => 80, getHGap: () => 20 }, | ||
}; | ||
} else if (direction === 'horizontal') { | ||
return { | ||
node: { | ||
style: { | ||
labelBackground: true, | ||
labelPlacement: (d) => (isLeafNode(d) ? 'right' : 'left'), | ||
ports: [{ placement: 'left' }, { placement: 'right' }], | ||
}, | ||
}, | ||
edge: { type: 'cubic-horizontal' }, | ||
layout: { type: layoutType, direction: 'LR', nodeSep: 40, rankSep: 200, getVGap: () => 5, getHGap: () => 100 }, | ||
}; | ||
} else { | ||
return { | ||
node: { style: { labelBackground: true } }, | ||
edge: { type: 'cubic-radial' }, | ||
layout: { | ||
type: layoutType, | ||
direction: 'RL', | ||
radial: true, | ||
nodeSep: 40, | ||
rankSep: 200, | ||
getVGap: () => 30, | ||
getHGap: () => 60, | ||
}, | ||
transforms: (prev) => [...prev, 'place-radial-labels'], | ||
}; | ||
} | ||
}; |
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,17 @@ | ||
import type { GraphOptions } from '../../types'; | ||
|
||
export interface DendrogramOptions extends GraphOptions { | ||
/** | ||
* The direction of the dendrogram. | ||
* - `'vertical'`: vertical direction (top to bottom). | ||
* - `'horizontal'`: horizontal direction (left to right). | ||
* - `'radial'`: radial direction (clockwise). | ||
* @default 'horizontal' | ||
*/ | ||
direction?: 'vertical' | 'horizontal' | 'radial'; | ||
/** | ||
* Whether to compact the layout. | ||
* @default false | ||
*/ | ||
compact?: boolean; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
import type { GraphOptions } from '../../types'; | ||
|
||
export const COMMON_OPTIONS: GraphOptions = { | ||
behaviors: ['drag-canvas', 'zoom-canvas'], | ||
behaviors: [ | ||
{ | ||
key: 'zoom-canvas', | ||
type: 'zoom-canvas', | ||
}, | ||
{ | ||
key: 'drag-canvas', | ||
type: 'drag-canvas', | ||
}, | ||
], | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Dendrogram as DendrogramComponent, DendrogramOptions } from '@ant-design/graphs'; | ||
import { treeToGraphData } from '@antv/g6'; | ||
import React from 'react'; | ||
import data from '../datasets/algorithm-category.json'; | ||
|
||
export const Dendrogram = () => { | ||
const options: DendrogramOptions = { | ||
autoFit: 'view', | ||
data: treeToGraphData(data), | ||
direction: 'vertical', | ||
compact: true, | ||
behaviors: (prev) => [ | ||
...prev, | ||
{ | ||
key: 'hover-activate', | ||
type: 'hover-activate', | ||
degree: Infinity, | ||
direction: 'in', | ||
inactiveState: 'inactive', | ||
}, | ||
], | ||
}; | ||
|
||
return <DendrogramComponent {...options} />; | ||
}; |
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