Skip to content

Commit

Permalink
fix: auto fit (#696)
Browse files Browse the repository at this point in the history
* fix: auto fit

* fix: 兼容旧图表
  • Loading branch information
lxfu1 authored Jun 17, 2021
1 parent 5842e47 commit eb5fc51
Show file tree
Hide file tree
Showing 20 changed files with 39 additions and 21 deletions.
6 changes: 6 additions & 0 deletions docs/.g2plot-plot-api/indented-tree-graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ Whether to display tail arrows, default to 'true'.

Whether to turn on animation, default to 'true'.

#### autoFit

<description>**optional** _Boolean_</description>

Whether to scale node size adaptive container. Default value is 'true '.

#### layout

<description>**optional** _object_</description>
Expand Down
6 changes: 6 additions & 0 deletions docs/.g2plot-plot-api/indented-tree-graph.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ collapseExpand 为 true 时生效, 默认值 `right`,可配合 layout 使用

是否开启动画,默认值 `true`

#### autoFit

<description>**optional** _Boolean_</description>

是否缩放节点大小自适应容器,默认值 `true`

#### layout

<description>**optional** _object_</description>
Expand Down
2 changes: 1 addition & 1 deletion docs/.g2plot-plot-api/organizational-graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ Whether to turn on animation, default to 'true'.

<description>**optional** _Boolean_</description>

Whether to automatically adjust the layout after updating data. Default value is 'true '.
Whether to scale node size adaptive container. Default value is 'true '.

#### minimapCfg

Expand Down
2 changes: 1 addition & 1 deletion docs/.g2plot-plot-api/organizational-graph.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ interface Data {

<description>**optional** _Boolean_</description>

更新数据后是否自动调整布局,默认为 true。
是否缩放节点大小自适应容器,默认为 true。

#### minimapCfg

Expand Down
2 changes: 1 addition & 1 deletion docs/.g2plot-plot-api/radial-graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Whether to turn on animation, default to 'true'.

<description>**optional** _Boolean_</description>

Whether to automatically adjust the layout after updating data. Default value is 'true '.
Whether to scale node size adaptive container. Default value is 'true '.

#### layout

Expand Down
2 changes: 1 addition & 1 deletion docs/.g2plot-plot-api/radial-graph.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ interface Data {

<description>**optional** _Boolean_</description>

更新数据后是否自动调整布局,默认值 `true`
是否缩放节点大小自适应容器,默认值 `true`

#### layout

Expand Down
1 change: 1 addition & 0 deletions docs/demos/indented-tree-graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const DemoIndentedTree: React.FC = () => {

const config = {
data,
autoFit: false,
behaviors: ['drag-canvas', 'zoom-canvas', 'drag-node'],
};

Expand Down
1 change: 1 addition & 0 deletions docs/demos/indented-tree-graph.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const DemoIndentedTree: React.FC = () => {

const config = {
data,
autoFit: false,
behaviors: ['drag-canvas', 'zoom-canvas', 'drag-node'],
};

Expand Down
3 changes: 2 additions & 1 deletion src/graph/dagre.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const DagreGraph: React.FC<RelationGraph> = (props) => {
edgeStyle = defaultEdgeStyle,
nodeStateStyles = defaultStateStyles,
edgeStateStyles = defaultStateStyles,
autoFit = true,
graphRef,
onReady,
loading,
Expand Down Expand Up @@ -95,7 +96,7 @@ const DagreGraph: React.FC<RelationGraph> = (props) => {

processMinimap(minimapCfg, graph);

renderGraph(graph, data);
renderGraph(graph, data, autoFit);

if (onReady) {
onReady(graph);
Expand Down
4 changes: 2 additions & 2 deletions src/graph/dagreFundFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const DagreFundFlowGraph: React.FC<RelationGraph> = (props) => {
nodeStateStyles = defaultStateStyles,
edgeStateStyles = defaultStateStyles,
colorMap = {},
autoFit = true,
graphRef,
onReady,
loading,
Expand Down Expand Up @@ -119,8 +120,7 @@ const DagreFundFlowGraph: React.FC<RelationGraph> = (props) => {
},
});
});

graph.fitView();
autoFit ? graph.fitView() : graph.fitCenter();
bindEvents(graph, props);
return () => {
if (graphs[graphId]) {
Expand Down
3 changes: 2 additions & 1 deletion src/graph/indented.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const IndentedTree: React.FC<RelationGraph> = (props) => {
nodeStateStyles = defaultStateStyles,
edgeStateStyles = defaultStateStyles,
collapseExpand = true,
autoFit = true,
handleNodeClick,
graphRef,
onReady,
Expand Down Expand Up @@ -109,7 +110,7 @@ const IndentedTree: React.FC<RelationGraph> = (props) => {

processMinimap(minimapCfg, graph);

renderGraph(graph, data);
renderGraph(graph, data, autoFit);
if (onReady) {
onReady(graph);
}
Expand Down
3 changes: 2 additions & 1 deletion src/graph/organizationTreeGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const OrganizationTreeGraphComponent: React.FC<RelationGraph> = (props) => {
edgeStyle = defaultEdgeStyle,
nodeStateStyles = defaultStateStyles,
edgeStateStyles = defaultStateStyles,
autoFit = true,
graphRef,
onReady,
loading,
Expand Down Expand Up @@ -116,7 +117,7 @@ const OrganizationTreeGraphComponent: React.FC<RelationGraph> = (props) => {
}

processMinimap(minimapCfg, graph);
renderGraph(graph, data);
renderGraph(graph, data, autoFit);
if (onReady) {
onReady(graph);
}
Expand Down
2 changes: 1 addition & 1 deletion src/graph/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ export interface RelationGraph extends ContainerConfig {
graphRef?: React.MutableRefObject<IGraph | undefined>;
/** 图表渲染完成回调 */
onReady?: (graph: IGraph) => void;
/** 更新数据后是否自动调整布局 */
/** 是否缩放节点大小自适应容器 */
autoFit?: boolean;
}
4 changes: 2 additions & 2 deletions src/graph/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export const getGraphId = (graph: { current?: string }) => {
return graph.current;
};

export const renderGraph = (graph: IGraph, data: any) => {
export const renderGraph = (graph: IGraph, data: any, autoFit: boolean) => {
const originData = deepClone(data);
graph.data(originData);
graph.render();
graph.fitView();
autoFit ? graph.fitView() : graph.fitCenter();
};

// 事件绑定,兼容历史数据
Expand Down
3 changes: 2 additions & 1 deletion src/graphs/indentedTreeGraph/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const IndentedTreeGraph: React.FC<IndentedTreeGraphConfig> = (props) => {
footerValueStyle,
showArrow = true,
arrowType = 'vee',
autoFit = true,
onReady,
loading,
loadingTemplate,
Expand Down Expand Up @@ -149,7 +150,7 @@ const IndentedTreeGraph: React.FC<IndentedTreeGraphConfig> = (props) => {
if (collapseExpand) {
bindDefaultEvents(graph, collapseExpand);
}
renderGraph(graph, data);
renderGraph(graph, data, autoFit);

if (onReady) {
onReady(graph);
Expand Down
3 changes: 2 additions & 1 deletion src/graphs/organizationalGraph/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const OrganizationalGraph: React.FC<OrganizationalGraphConfig> = (props) => {
markerStyle,
nodeStateStyles = defaultStateStyles,
edgeStateStyles = defaultStateStyles,
autoFit = true,
onReady,
loading,
loadingTemplate,
Expand Down Expand Up @@ -142,7 +143,7 @@ const OrganizationalGraph: React.FC<OrganizationalGraphConfig> = (props) => {
});

processMinimap(minimapCfg, graph);
renderGraph(graph, data);
renderGraph(graph, data, autoFit);
if (onReady) {
onReady(graph);
}
Expand Down
3 changes: 2 additions & 1 deletion src/graphs/radialGraph/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const RadialGraph: React.FC<RadialGraphConfig> = (props) => {
edgeStateStyles = defaultStateStyles,
showArrow = false,
arrowType = 'triangle',
autoFit = true,
onReady,
loading,
loadingTemplate,
Expand Down Expand Up @@ -108,7 +109,7 @@ const RadialGraph: React.FC<RadialGraphConfig> = (props) => {
};
});

renderGraph(graph, data);
renderGraph(graph, data, autoFit);

if (onReady) {
onReady(graph);
Expand Down
2 changes: 1 addition & 1 deletion src/graphs/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export interface CommonConfig extends ContainerConfig {
layout?: any;
/** 是否开启动画 */
animate?: boolean;
/** 更新数据后是否自动调整布局 */
/** 是否缩放节点大小自适应容器 */
autoFit?: boolean;
/** 图表渲染完成回调 */
onReady?: (graph: IGraph) => void;
Expand Down
4 changes: 2 additions & 2 deletions src/graphs/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ export const getGraphId = (graph: { current?: string }) => {
return graph.current;
};

export const renderGraph = (graph: IGraph, data: any) => {
export const renderGraph = (graph: IGraph, data: any, autoFit: boolean) => {
const originData = deepClone(data);
graph.data(originData);
graph.render();
graph.fitView();
autoFit ? graph.fitView() : graph.fitCenter();
};

export const processMinimap = (cfg: MiniMapConfig | undefined = {}, graph: Graph) => {
Expand Down
4 changes: 1 addition & 3 deletions src/hooks/useGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export default function useInit(
useEffect(() => {
if (graphInstance && !graphInstance.destroyed) {
graphInstance.changeData(data);
if (autoFit) {
graphInstance.fitView();
}
autoFit ? graphInstance.fitView() : graphInstance.fitCenter();
}
}, [data]);

Expand Down

0 comments on commit eb5fc51

Please sign in to comment.