Skip to content

Commit

Permalink
refactor: simplify tree data conversion to graph data and adjust orga…
Browse files Browse the repository at this point in the history
…nizational chart node styles (#2716)

* refactor: treeToGraphData

* fix: modify font size

* fix: adjust node styles

* chore: update version
  • Loading branch information
yvonneyx authored Sep 27, 2024
1 parent 086b96e commit 767bfa3
Show file tree
Hide file tree
Showing 29 changed files with 51 additions and 250 deletions.
10 changes: 5 additions & 5 deletions packages/graphs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ant-design/graphs",
"version": "2.0.0-beta.2",
"version": "2.0.0-beta.3",
"description": "A React graph library based on Graphin",
"keywords": [
"antv",
Expand Down Expand Up @@ -41,16 +41,16 @@
"dev": "vite",
"lint": "eslint --cache --ext .js,.jsx,.ts,.tsx --fix --format=pretty ./src && npm run lint:prettier",
"lint:prettier": "npm run prettier && git diff && prettier --version && prettier --check \"src/**/**.{js,jsx,tsx,ts,less,md,json}\" --end-of-line auto",
"prepublishOnly": "pnpm run build",
"prettier": "prettier --write \"**/**.{js,jsx,tsx,ts,less,md,json}\"",
"profile": "webpack --config webpack.config.js --mode production --profile --json > stats.json",
"start": "npm run build:esm --w",
"test": "jest",
"prepublishOnly": "pnpm run build"
"test": "jest"
},
"dependencies": {
"@ant-design/charts-util": "workspace:*",
"@antv/g6": "^5.0.23",
"@antv/g6-extension-react": "^0.1.6",
"@antv/g6": "^5.0.24",
"@antv/g6-extension-react": "^0.1.7",
"@antv/graphin": "^3.0.2",
"lodash": "^4.17.21",
"styled-components": "^6.1.13"
Expand Down
9 changes: 4 additions & 5 deletions packages/graphs/src/components/indented-tree/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,16 @@ export const getIndentedTreeOptions = ({
};

const measureNodeSize = (data: NodeData) => {
const depth = data.data?.depth as number;
const offset = depth === 0 ? [24, 36] : [12, 24];
const font = getNodeFont(depth);
const offset = data.depth === 0 ? [24, 36] : [12, 24];
const font = getNodeFont(data.depth!);
return measureTextSize(idOf(data), offset, font, minWidth, maxWidth);
};

options = {
node: {
style: {
component: function (data: NodeData) {
const depth = data.data?.depth as number;
const depth = data.depth as number;
const color = data.style?.color as string;
const props: TextNodeProps = {
type: depth === 0 || depth === 1 ? 'filled' : 'outlined',
Expand Down Expand Up @@ -160,7 +159,7 @@ export const getIndentedTreeOptions = ({
node: {
style: {
component: function (data: NodeData) {
const depth = data.data?.depth as number;
const depth = data.depth as number;
const color = data.style?.color as string;
const props = { text: idOf(data), color, maxWidth, font: getNodeFont(depth) } as TextNodeProps;
Object.assign(
Expand Down
2 changes: 1 addition & 1 deletion packages/graphs/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { FlowGraph, type FlowGraphOptions } from './flow-graph';
export { Dendrogram, type DendrogramOptions } from './dendrogram';
export { FlowDirectionGraph, type FlowDirectionGraphOptions } from './flow-direction-graph';
export { FlowGraph, type FlowGraphOptions } from './flow-graph';
export { IndentedTree, type IndentedTreeOptions } from './indented-tree';
export { MindMap, type MindMapOptions } from './mind-map';
export { NetworkGraph, type NetworkGraphOptions } from './network-graph';
Expand Down
8 changes: 4 additions & 4 deletions packages/graphs/src/components/mind-map/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function getMindMapOptions({
};

const measureNodeSize = (data: NodeData) => {
const depth = data.data?.depth as number;
const depth = data.depth as number;
const offset = depth === 0 ? [24, 36] : [12, 24];
const font = getNodeFont(depth);
return measureTextSize(idOf(data), offset, font, minWidth, maxWidth);
Expand All @@ -101,7 +101,7 @@ export function getMindMapOptions({
node: {
style: {
component: (data: NodeData) => {
const depth = data.data?.depth as number;
const depth = data.depth as number;
const color = data.style?.color as string;
const props = { text: idOf(data), color, maxWidth, font: getNodeFont(depth) } as TextNodeProps;
Object.assign(
Expand Down Expand Up @@ -164,7 +164,7 @@ export function getMindMapOptions({
style: {
component: function (data: NodeData) {
const side = getNodeSide(this as unknown as Graph, data);
const depth = data.data?.depth as number;
const depth = data.depth as number;
const color = data.style?.color as string;
const props = { text: idOf(data), color, maxWidth, font: getNodeFont(depth) } as TextNodeProps;
Object.assign(
Expand Down Expand Up @@ -213,7 +213,7 @@ export function getMindMapOptions({
{
...(prev.find((t) => (t as any).key === 'collapse-expand-react-node') as any),
iconOffsetY: (data) => {
if (data.data?.depth === 0) return 0;
if (data.depth === 0) return 0;
return measureNodeSize(data)[1] / 2;
},
},
Expand Down
17 changes: 11 additions & 6 deletions packages/graphs/src/core/base/node/organization-chart-node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface OrganizationChartNodeProps extends Pick<React.HTMLAttributes<HT
/**
* Working status of the person
*/
status: string;
status?: string;
/**
* Whether the node is hovered
*/
Expand All @@ -33,8 +33,7 @@ const StyledWrapper = styled.div<{ $color?: string; $isActive?: boolean }>`
${(props) =>
props.$isActive &&
css`
height: calc(100% - 6px);
width: calc(100% - 6px);
transform: translate(-3px, -3px);
border: 3px solid #1783ff;
`}
Expand Down Expand Up @@ -65,25 +64,31 @@ const StyledWrapper = styled.div<{ $color?: string; $isActive?: boolean }>`
}
&-detail {
flex: 1;
width: calc(100% - 56px);
}
&-name {
color: #242424;
font-weight: 600;
font-size: 18px;
margin-bottom: 5px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
&-post {
color: #616161;
font-size: 14px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
}
`;

export const OrganizationChartNode: React.FC<OrganizationChartNodeProps> = (props) => {
const { name, position, status, isActive, className, style } = props;
const { name, position, status = 'online', isActive, className, style } = props;

const colorMap = {
online: '#1783FF',
Expand All @@ -98,7 +103,7 @@ export const OrganizationChartNode: React.FC<OrganizationChartNodeProps> = (prop
<div className="org-chart-node-content-avatar">{name.slice(0, 1)}</div>
<div className="org-chart-node-content-detail">
<div className="org-chart-node-content-name">{name}</div>
<div className="org-chart-node-content-post">{position}</div>
{position && <div className="org-chart-node-content-post">{position}</div>}
</div>
</div>
</StyledWrapper>
Expand Down
3 changes: 2 additions & 1 deletion packages/graphs/src/core/base/node/text-node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const StyledWrapper = styled.div<{
height: inherit;
width: inherit;
box-sizing: content-box;
font-size: 14px;
${({ $type, $color, $borderWidth }) => {
switch ($type) {
Expand Down Expand Up @@ -132,7 +133,7 @@ export const TextNode: FC<TextNodeProps> = (props) => {
$borderWidth={borderWidth}
$isActive={isActive}
$isSelected={isSelected}
className={className}
className={`text-node ${className}`}
style={{ ...style, ...font }}
>
<div style={isMultiLine ? { width: 'calc(100% - 12px)' } : {}}>{text}</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/graphs/src/core/transform/arrange-edge-z-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class ArrangeEdgeZIndex extends BaseTransform {
const { model } = this.context;
const { nodes, edges } = model.getData();

const oneLevelNodes = nodes.filter((node) => node.data?.depth === 1);
const oneLevelNodes = nodes.filter((node) => node.depth === 1);
const oneLevelNodeIds = oneLevelNodes.map((node) => node.id);

edges.forEach((edge) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class AssignColorByBranch extends BaseTransform {
node.children?.forEach((childId) => dfs(childId, node.style?.color as string));
};

nodes.filter((node) => node.data?.depth === 1).forEach((rootNode) => dfs(rootNode.id));
nodes.filter((node) => node.depth === 1).forEach((rootNode) => dfs(rootNode.id));

return input;
}
Expand Down
11 changes: 1 addition & 10 deletions packages/graphs/tests/demos/indented-tree-boxed.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { IndentedTreeOptions } from '@ant-design/graphs';
import { G6, IndentedTree as IndentedTreeComponent } from '@ant-design/graphs';
import type { NodeData, TreeData } from '@antv/g6';
import React from 'react';
import data from '../datasets/algorithm-category.json';

Expand All @@ -9,15 +8,7 @@ const { treeToGraphData } = G6;
export const IndentedTreeBoxed = () => {
const options: IndentedTreeOptions = {
type: 'boxed',
data: treeToGraphData(data, {
getNodeData: (datum: TreeData, depth: number) => {
datum.data ||= {};
datum.data.depth = depth;
if (!datum.children) return datum as NodeData;
const { children, ...restDatum } = datum;
return { ...restDatum, children: children.map((child) => child.id) } as NodeData;
},
}),
data: treeToGraphData(data),
};

return <IndentedTreeComponent {...options} />;
Expand Down
11 changes: 1 addition & 10 deletions packages/graphs/tests/demos/indented-tree-default.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { IndentedTreeOptions } from '@ant-design/graphs';
import { G6, IndentedTree as IndentedTreeComponent } from '@ant-design/graphs';
import type { NodeData, TreeData } from '@antv/g6';
import React from 'react';
import data from '../datasets/algorithm-category.json';

Expand All @@ -9,15 +8,7 @@ const { treeToGraphData } = G6;
export const IndentedTree = () => {
const options: IndentedTreeOptions = {
type: 'default',
data: treeToGraphData(data, {
getNodeData: (datum: TreeData, depth: number) => {
datum.data ||= {};
datum.data.depth = depth;
if (!datum.children) return datum as NodeData;
const { children, ...restDatum } = datum;
return { ...restDatum, children: children.map((child) => child.id) } as NodeData;
},
}),
data: treeToGraphData(data),
// transforms: (prev) => [
// ...prev.filter((transform) => (transform as any).type !== 'collapse-expand-react-node'),
// {
Expand Down
11 changes: 1 addition & 10 deletions packages/graphs/tests/demos/indented-tree-linear.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { IndentedTreeOptions } from '@ant-design/graphs';
import { G6, IndentedTree as IndentedTreeComponent } from '@ant-design/graphs';
import type { NodeData, TreeData } from '@antv/g6';
import React from 'react';
import data from '../datasets/algorithm-category.json';

Expand All @@ -9,15 +8,7 @@ const { treeToGraphData } = G6;
export const IndentedTreeLinear = () => {
const options: IndentedTreeOptions = {
type: 'linear',
data: treeToGraphData(data, {
getNodeData: (datum: TreeData, depth: number) => {
datum.data ||= {};
datum.data.depth = depth;
if (!datum.children) return datum as NodeData;
const { children, ...restDatum } = datum;
return { ...restDatum, children: children.map((child) => child.id) } as NodeData;
},
}),
data: treeToGraphData(data),
};

return <IndentedTreeComponent {...options} />;
Expand Down
11 changes: 1 addition & 10 deletions packages/graphs/tests/demos/mind-map-boxed.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { MindMapOptions } from '@ant-design/graphs';
import { G6, MindMap as MindMapComponent } from '@ant-design/graphs';
import type { NodeData, TreeData } from '@antv/g6';
import React from 'react';
import data from '../datasets/algorithm-category.json';

Expand All @@ -9,15 +8,7 @@ const { treeToGraphData } = G6;
export const MindMapBoxed = () => {
const options: MindMapOptions = {
type: 'boxed',
data: treeToGraphData(data, {
getNodeData: (datum: TreeData, depth: number) => {
datum.data ||= {};
datum.data.depth = depth;
if (!datum.children) return datum as NodeData;
const { children, ...restDatum } = datum;
return { ...restDatum, children: children.map((child) => child.id) } as NodeData;
},
}),
data: treeToGraphData(data),
};

return <MindMapComponent {...options} />;
Expand Down
12 changes: 2 additions & 10 deletions packages/graphs/tests/demos/mind-map-custom.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MindMapOptions } from '@ant-design/graphs';
import { G6, getNodeSide, measureTextSize, MindMap as MindMapComponent, RCNode } from '@ant-design/graphs';
import { Graph, type NodeData, type TreeData } from '@antv/g6';
import { Graph, type NodeData } from '@antv/g6';
import React from 'react';
import data from '../datasets/algorithm-category.json';

Expand All @@ -9,15 +9,7 @@ const { TextNode } = RCNode;

export const MindMapCustom = () => {
const options: MindMapOptions = {
data: treeToGraphData(data, {
getNodeData: (datum: TreeData, depth: number) => {
datum.data ||= {};
datum.data.depth = depth;
if (!datum.children) return datum as NodeData;
const { children, ...restDatum } = datum;
return { ...restDatum, children: children.map((child) => child.id) } as NodeData;
},
}),
data: treeToGraphData(data),
node: {
style: {
component: (data) => {
Expand Down
11 changes: 1 addition & 10 deletions packages/graphs/tests/demos/mind-map-default.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import type { MindMapOptions } from '@ant-design/graphs';
import { G6, MindMap as MindMapComponent } from '@ant-design/graphs';
import type { NodeData, TreeData } from '@antv/g6';
import React from 'react';
import data from '../datasets/algorithm-category.json';

const { treeToGraphData } = G6;

export const MindMap = () => {
const options: MindMapOptions = {
data: treeToGraphData(data, {
getNodeData: (datum: TreeData, depth: number) => {
datum.data ||= {};
datum.data.depth = depth;
if (!datum.children) return datum as NodeData;
const { children, ...restDatum } = datum;
return { ...restDatum, children: children.map((child) => child.id) } as NodeData;
},
}),
data: treeToGraphData(data),
transforms: (prev) => [
...prev.filter((transform) => (transform as any).type !== 'collapse-expand-react-node'),
{
Expand Down
11 changes: 1 addition & 10 deletions packages/graphs/tests/demos/mind-map-linear.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { MindMapOptions } from '@ant-design/graphs';
import { G6, MindMap as MindMapComponent } from '@ant-design/graphs';
import type { NodeData, TreeData } from '@antv/g6';
import React from 'react';
import data from '../datasets/algorithm-category.json';

Expand All @@ -9,15 +8,7 @@ const { treeToGraphData } = G6;
export const MindMapLinear = () => {
const options: MindMapOptions = {
type: 'linear',
data: treeToGraphData(data, {
getNodeData: (datum: TreeData, depth: number) => {
datum.data ||= {};
datum.data.depth = depth;
if (!datum.children) return datum as NodeData;
const { children, ...restDatum } = datum;
return { ...restDatum, children: children.map((child) => child.id) } as NodeData;
},
}),
data: treeToGraphData(data),
};

return <MindMapComponent {...options} />;
Expand Down
1 change: 1 addition & 0 deletions packages/graphs/tests/demos/organization-chart2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const OrganizationChart2 = () => {
endArrow: true,
},
},
behaviors: (prev) => [...prev, 'hover-activate-neighbors'],
transforms: (prev) => [
...prev.filter((t) => (t as any).type !== 'collapse-expand-react-node'),
{
Expand Down
13 changes: 1 addition & 12 deletions site/examples/relations/indented-tree/demo/auto-children.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,7 @@ const DemoIndentedTree = () => {
useEffect(() => {
fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/algorithm-category.json')
.then((res) => res.json())
.then((res) => {
const graphData = treeToGraphData(res, {
getNodeData: (datum, depth) => {
datum.data ||= {};
datum.data.depth = depth;
if (!datum.children) return datum;
const { children, ...restDatum } = datum;
return { ...restDatum, children: children.map((child) => child.id) };
},
});
setData(graphData);
});
.then((res) => setData(treeToGraphData(res)));
}, []);

const options = {
Expand Down
Loading

0 comments on commit 767bfa3

Please sign in to comment.