0.7.3 • Published 5 months ago

@graphgl/react v0.7.3

Weekly downloads
-
License
BSD-3-Clause
Repository
-
Last release
5 months ago

graphgl/react

Layout

Built-In Layout

  • fruchterman-gpu
参数描述默认值
maxIteration最大迭代次数100
gravity重力10
speed速度0.1
textureWidth点纹理宽度16
edgeTextureWidth边纹理宽度20
  • force-gpu
参数描述默认值
maxIteration最大迭代次数100
damping阻尼系数0.9
maxSpeed最大速度1000
minMovement一次迭代的平均移动距离小于该值时停止迭代0.5
factor斥力系数1
coulombDisScale库伦系数0.005
edgeDistance理想边长10
gravity中心重力10
edgeStrength弹簧引力系数200
nodeStrength点作用力100
textureWidth点纹理宽度16
edgeTextureWidth边纹理宽度20
  • rotate
参数描述默认值
rad旋转弧度0

AntV Layout

How To Use

import GraphGL, { register } from '@graphgl/react';
import AntVLayout, { AntVLayoutOptions } from '@graphgl/react/layouts/adapter/antv';

AntVLayout.layouts.forEach((type) => {
  AntVLayout.workerScirptURL = [
    'https://unpkg.com/@antv/layout@1.2.13/dist/index.min.js',
    'https://unpkg.com/@antv/graphlib@2.0.4/dist/index.umd.min.js',
  ];
  register('layout', type, AntVLayout);
});

...

 <GraphGL<AntVLayoutOptions>
    options={{
        layout: {
            type: 'd3force'
        }
    }}
 />
  • force
  • forceAtlas2
  • fruchterman
  • d3force
  • circular
  • grid
  • random
  • mds
  • concentric
  • dagre
  • radial

Custom Layout Demo

import { Layouts, LayoutBaseProps, LayoutOptionsBase } from '@graphgl/react';
import forceAtlas2, {
  ForceAtlas2Settings,
} from 'graphology-layout-forceatlas2';
import FA2Layout from 'graphology-layout-forceatlas2/worker';
import Graphology from 'graphology';

const getNodesBuffer = (nodes: any) => {
  const length = nodes.length;
  const nodeBuffer = new Float32Array(length * 2);
  for (let index = 0; index < length; index++) {
    nodeBuffer[index * 2] = nodes[index].attributes?.x as number;
    nodeBuffer[index * 2 + 1] = nodes[index].attributes?.y as number;
  }
  return nodeBuffer;
};

export type GraphologyLayoutOptions = LayoutOptionsBase<
      {
        type: 'graphology-forceatlas2';
        isTick?: boolean;
      } & ForceAtlas2Settings
    >;

export type GraphologyLayoutProps = LayoutBaseProps<GraphologyLayoutOptions>;

export default class GraphologyLayout extends Layouts.BaseLayout {
  public options: GraphologyLayoutProps['options'] | null = null;
  public nodes: GraphologyLayoutProps['nodes'] = [];
  public edges: GraphologyLayoutProps['edges'] = [];
  public layout: FA2Layout | null = null;
  static layouts = ['graphology-forceatlas2'];
  static workerScirptURL: string | string[] = [];

  constructor(opts: GraphologyLayoutProps) {
    super();
    this.options = opts.options;
    this.nodes = opts.nodes;
    this.edges = opts.edges;
  }

  public destroy() {
    this.layout?.stop();
    this.layout?.kill();
  }

  public execute() {
    const { nodes, edges, options } = this;
    const { onTick, onLayoutEnd, isTick, type, ...restOptions } =
      options as GraphologyLayoutProps['options'];

    const graph = new Graphology();

    graph.import({
      nodes: nodes.map((item) => ({
        key: item.id,
        attributes: {
          x: item.x === undefined ? Math.random() * 2000 - 1000 : item.x,
          y: item.y === undefined ? Math.random() * 2000 - 1000 : item.y,
        },
      })),
      edges: edges,
    });

    const sensibleSettings = forceAtlas2.inferSettings(graph);

    const layout = new FA2Layout(graph, {
      settings: {
        ...sensibleSettings,
        ...restOptions,
      },
    });

    this.layout = layout;

    this.layout.start();

    const animate = () => {
      requestAnimationFrame(() => {
        const results = graph.export().nodes;
        const nodesBuffer = getNodesBuffer(results);

        if (this.layout?.isRunning()) {
          onTick?.(nodesBuffer);
          animate();
        } else {
          onLayoutEnd?.(nodesBuffer);
        }
      });
    };

    animate();
  }
}
0.6.7

5 months ago

0.6.6

5 months ago

0.6.9

5 months ago

0.6.8

5 months ago

0.5.19

10 months ago

0.7.2

5 months ago

0.7.1

5 months ago

0.7.3

5 months ago

0.7.0

5 months ago

0.5.21

7 months ago

0.5.22

7 months ago

0.5.20

10 months ago

0.5.23

7 months ago

0.6.3

7 months ago

0.6.2

7 months ago

0.6.5

5 months ago

0.6.4

5 months ago

0.6.1

7 months ago

0.6.0

7 months ago

0.5.18

1 year ago

0.5.17

1 year ago

0.5.16

2 years ago

0.5.14

2 years ago

0.5.15

2 years ago

0.5.13

2 years ago

0.5.10

2 years ago

0.5.11

2 years ago

0.5.12

2 years ago

0.5.8

2 years ago

0.5.9

2 years ago

0.5.7

2 years ago

0.5.6

2 years ago

0.5.5

2 years ago

0.5.4

2 years ago

0.5.3

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.6

2 years ago

0.4.5

2 years ago

0.4.4

2 years ago

0.4.1

2 years ago

0.4.3

2 years ago

0.4.2

2 years ago

0.3.0

2 years ago

0.3.6

2 years ago

0.2.7

2 years ago

0.3.5

2 years ago

0.2.6

2 years ago

0.3.8

2 years ago

0.3.7

2 years ago

0.3.2

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.4

2 years ago

0.2.5

2 years ago

0.3.3

2 years ago

0.2.4

2 years ago

0.2.3

3 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.2.2

4 years ago

0.1.8

4 years ago

0.1.9

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago