Skip to content

Commit

Permalink
docs: add flow direction graph demo
Browse files Browse the repository at this point in the history
  • Loading branch information
yvonneyx committed Oct 31, 2024
1 parent 92e2373 commit 2376356
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { FlowDirectionGraph, type FlowDirectionGraphOptions } from '@ant-design/graphs';
import React from 'react';

const blue = '#6395FA';
const green = '#64DAAB';
const gray = '#595959';

const data = {
"nodes": [
{ "id": "-3", "data": { type: 'source', "title": "来源页面A" } },
{ "id": "-2", "data": { type: 'source', "title": "来源页面B" } },
{ "id": "-1", "data": { type: 'source', "title": "来源页面C" } },
{ "id": "0", "data": { type: 'activity', "title": "活动页面" } },
{ "id": "1", "data": { type: 'destination', "title": "去向页面A", } },
{ "id": "2", "data": { type: 'destination', "title": "去向页面B", } },
{ "id": "3", "data": { type: 'destination', "title": "去向页面C", } },
{ "id": "4", "data": { type: 'destination', "title": "去向页面D", } },
],
"edges": [
{ "source": "-3", "target": "0", data: { type: 'in', value: 40 } },
{ "source": "-2", "target": "0", data: { type: 'in', value: 35 } },
{ "source": "-1", "target": "0", data: { type: 'in', value: 25 } },
{ "source": "0", "target": "1", data: { type: 'out', value: 22 } },
{ "source": "0", "target": "2", data: { type: 'out', value: 18 } },
{ "source": "0", "target": "3", data: { type: 'out', value: 32 } },
{ "source": "0", "target": "4", data: { type: 'out', value: 38 } },
]
}

const hexToRgba = (hex: string, alpha: number): string => {
hex = hex.replace('#', '');
if (hex.length === 3) {
hex = hex.split('').map(char => char + char).join('');
}
const r = parseInt(hex.substring(0, 2), 16);
const g = parseInt(hex.substring(2, 4), 16);
const b = parseInt(hex.substring(4, 6), 16);
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
};

const CustomNode = ({ text, isActive, color }: { text: string, isActive: boolean, color: string }) => {
const style = {
height: 'inherit',
width: 'inherit',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
borderRadius: '4px',
backgroundColor: hexToRgba(color, isActive ? 1 : 0.2),
color: isActive ? '#fff' : '#000',
};

return <div style={style}>{text}</div>
}

export default () => {
const options: FlowDirectionGraphOptions = {
containerStyle: { height: '380px' },
autoFit: 'view',
data,
node: {
style: {
component: (d) => {
const isActive = d.states?.includes('active');
const color = d.data.type === 'source' ? blue : (d.data.type === 'destination' ? green : gray);
return <CustomNode text={d.data.title} isActive={isActive} color={color} />
},
size: [130, 58],
},
},
edge: {
style: {
stroke: (d) =>
d.data.type === 'in' ? blue : green,
strokeOpacity: 0.2,
},
state: {
active: {
halo: false,
strokeOpacity: 1,
}
}
},
transforms: (transforms) => [
...transforms,
{
type: 'map-edge-line-width',
key: 'map-edge-line-width',
value: (d) => d.data.value,
minValue: 0,
maxValue: 100,
minLineWidth: 1,
maxLineWidth: 32,
},
],
behaviors: (behaviors) => [...behaviors, {
type: 'hover-activate-chain',
onHover: (e) => {
e.view.setCursor('pointer');
},
onHoverEnd: (e) => {
e.view.setCursor('default');
}
}],
layout: {
type: 'antv-dagre',
nodesep: -3,
ranksep: 60,
},
animation: false
};

return <FlowDirectionGraph {...options} />;
};
8 changes: 5 additions & 3 deletions site/docs/options/graphs/flow-direction-graph.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ Ideal for complex information that requires clear logical relationships or decis

## Examples

<code id="demo-flow-direction-graph-default" src="./demos/flow-direction-graph/default.tsx" description="A simple demonstration.<br> Adjust `edge.style.lineWidth` via the interaction `map-edge-line-width`. It includes the following properties:<br> - `value` (number or function to compute the value of the edge)<br> - `minValue` and `maxValue` (optional, minimum and maximum values, can be numbers or functions)<br> - `minLineWidth` and `maxLineWidth` (optional, minimum and maximum line widths, can be numbers or functions)<br> - `scale` (optional, interpolation function to map values to line widths, supports `'linear'`, `'log'`, `'pow'`, `'sqrt'`, and custom interpolation functions)">Basic Usage</code>
<code id="demo-flow-direction-graph-default" src="./demos/flow-direction-graph/default.tsx" description="A simple demonstration.<br> Adjust `edge.style.lineWidth` via the behavior `map-edge-line-width`. It includes the following properties:<br> - `value` (number or function to compute the value of the edge)<br> - `minValue` and `maxValue` (optional, minimum and maximum values, can be numbers or functions)<br> - `minLineWidth` and `maxLineWidth` (optional, minimum and maximum line widths, can be numbers or functions)<br> - `scale` (optional, interpolation function to map values to line widths, supports `'linear'`, `'log'`, `'pow'`, `'sqrt'`, and custom interpolation functions)">Basic Usage</code>

<code id="demo-flow-direction-graph-hover-activate-neighbor" src="./demos/flow-direction-graph/hover-activate-neighbor.tsx" description="This interaction `hover-activate-neighbors` is based on the G6 built-in interaction `hover-activate` and inherits its [configuration options](https://g6.antv.antgroup.com/en/sapi/behaviors/hover-activate).">Hover Highlight Neighbors</code>
<code id="demo-flow-direction-graph-hover-activate-neighbor" src="./demos/flow-direction-graph/hover-activate-neighbor.tsx" description="By adding the hover highlight behavior (registration type: `hover-activate-neighbors`), you can highlight the current element and its neighboring elements. This behavior is based on the G6 built-in behavior `hover-activate` and inherits its [configuration options](https://g6.antv.antgroup.com/api/behaviors/hover-activate).">Highlight Neighboring Elements</code>

<code id="demo-flow-direction-graph-hover-activate-chain" src="./demos/flow-direction-graph/hover-activate-chain.tsx" description="By adding the hover highlight behavior (registration type: `hover-activate-chain`), you can highlight the current element and its entire chain. This behavior is based on the G6 built-in behavior `hover-activate` and inherits its [configuration options](https://g6.antv.antgroup.com/api/behaviors/hover-activate).">Highlight Element and Its Chain</code>

<code id="demo-flow-direction-graph-custom" src="./demos/flow-direction-graph/custom.tsx">Custom</code>

Expand All @@ -31,6 +33,6 @@ Common props ref: [Common Graph Properties](./overview#common-graph-properties)
| Property | Description | Type | Default Value |
| --- | --- | --- | --- |
| layout | AntV Dagre layout configuration | [`AntVDagreLayoutOptions`](https://g6.antv.antgroup.com/en/api/layouts/antv-dagre-layout) | `{ type: 'antv-dagre' }` |
| behaviors | Set user interaction events, also supports G6 built-in behaviors. For more details on behaviors, refer to [here](https://g6.antv.antgroup.com/en/manual/core-concept/behavior) | [`BehaviorOptions[]`](https://g6.antv.antgroup.com/en/api/behaviors/brush-select) \| `((existingBehaviors: BehaviorOptions[]) => BehaviorOptions[])` | - |
| behaviors | Set user behavior events, also supports G6 built-in behaviors. For more details on behaviors, refer to [here](https://g6.antv.antgroup.com/en/manual/core-concept/behavior) | [`BehaviorOptions[]`](https://g6.antv.antgroup.com/en/api/behaviors/brush-select) \| `((existingBehaviors: BehaviorOptions[]) => BehaviorOptions[])` | - |
| plugins | Set canvas plugins for handling rendering logic and additional component rendering. Also supports G6 built-in plugins. For more details on plugins, refer to [here](https://g6.antv.antgroup.com/en/manual/core-concept/plugin) | [`PluginOptions[]`](https://g6.antv.antgroup.com/en/api/plugins/background) \| `((existingPlugins: PluginOptions[]) => PluginOptions[])` | - |
| transforms | Set data transformers to process user input data and convert it into internal flow data. Also supports G6 built-in data transformers. For more details on data transformation, refer to [here](https://g6.antv.antgroup.com/en/api/transforms/map-node-size) | [`TransformOptions[]`](https://g6.antv.antgroup.com/en/api/transforms/map-node-size) \| `((existingTransforms: TransformOptions[]) => TransformOptions[])` | - |
4 changes: 3 additions & 1 deletion site/docs/options/graphs/flow-direction-graph.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import { FlowDirectionGraph } from '@ant-design/graphs';

<code id="demo-flow-direction-graph-default" src="./demos/flow-direction-graph/default.tsx" description="简单的展示。<br> 通过交互 `map-edge-line-width` 来调整 `edge.style.lineWidth`。包含以下属性:<br> - `value`(数值或函数,用于计算边的数值)<br> - `minValue` 和 `maxValue`(可选,最小值和最大值,可以是数值或函数)<br> - `minLineWidth` 和 `maxLineWidth`(可选,最小线宽和最大线宽,可以是数值或函数)<br> - `scale`(可选,插值函数,用于将数值映射到线宽,支持 `'linear`'、'`log'`、`'pow'`、`'sqrt'` 和自定义插值函数)">基本用法</code>

<code id="demo-flow-direction-graph-hover-activate-neighbor" src="./demos/flow-direction-graph/hover-activate-neighbor.tsx" description="此交互 `hover-activate-neighbors` 基于 G6 内置交互 `hover-activate` 实现,并继承了其[配置项](https://g6.antv.antgroup.com/api/behaviors/hover-activate)。 ">悬浮高亮邻居</code>
<code id="demo-flow-direction-graph-hover-activate-neighbor" src="./demos/flow-direction-graph/hover-activate-neighbor.tsx" description="通过添加悬浮高亮交互(注册类型:`hover-activate-neighbors`)可以高亮当前元素及其相邻元素。该交互基于 G6 内置交互 `hover-activate` 实现,并继承了其[配置项](https://g6.antv.antgroup.com/api/behaviors/hover-activate)。">高亮相邻元素</code>

<code id="demo-flow-direction-graph-hover-activate-chain" src="./demos/flow-direction-graph/hover-activate-chain.tsx" description="通过添加悬浮高亮交互(注册类型:`hover-activate-chain`)可以高亮显示当前元素及其所在的链路。此交互基于 G6 内置交互 `hover-activate` 实现,并继承了其[配置项](https://g6.antv.antgroup.com/api/behaviors/hover-activate)。">高亮元素及其所在链路</code>

<code id="demo-flow-direction-graph-custom" src="./demos/flow-direction-graph/custom.tsx">自定义</code>

Expand Down

0 comments on commit 2376356

Please sign in to comment.