Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@antv/graphin

antvis72.2kMIT3.0.4TypeScript support: included

A React toolkit for graph analysis based on g6

readme

English | 简体中文

Graphin

A lightweight React toolkit for graph analysis based on G6.

Version NPM downloads Latest commit

✨ 功能特性

  • 🎨 轻量级:不做过度封装,尽量保持 G6 能力同步,尽量不新增概念,整体核心代码 <200 行。
  • 🎗️ React 风格:舒心的开发体验,符合 React 用户心智,基于 React 扩展组件更容易。
  • 🚀 丰富组件:丰富的组件,源于业务沉淀,让用户定制自己的图应用更快更容易。

component

🔨 快速使用

graphin 当作一个普通的 React 组件来使用即可,通过 NPM 或 Yarn 等包管理器来安装。

$ npm install @antv/graphin
$ yarn add @antv/graphin

成功安装之后,可以通过 import 导入 Graphin 组件。

import React from 'react';
import { Graphin } from '@antv/graphin';

export function Demo() {
  return (
    <Graphin
      id="my-graphin-demo"
      className="my-graphin-container"
      style={{ width: '100%', height: '100%' }}
      options={{
        data,
        node: {
          style: {
            labelText: (d) => d.id,
          },
          palette: {
            type: 'group',
            field: 'cluster',
          },
        },
        layout: {
          type: 'd3force',
          collide: {
            strength: 0.5,
          },
        },
        behaviors: ['zoom-canvas', 'drag-canvas'],
        animation: true,
      }}
    >
    </Graphin>
    />
  );
}

📖 API Reference

Property Description Type Default
id 设置图画布容器的 id 属性。 string -
className 设置图画布容器的 class 属性。 string -
style 设置图画布容器的 style 样式属性。 CSSProperties -
options 设置图画布的配置项,参考 G6 配置 文档,在 graph.setOptions(options) 中调用 GraphOptions | null -
onInit 当图实例初始化之后调用,在 new Graph() 之后。 (graph: Graph) => void -
onReady 当图实例渲染完成之后调用,在 graph.render() 之后。 (graph: Graph) => void -
onDestroy 当图实例被销毁的时候调用,在 graph.destroy() 之后。 () => {} -

🗂 Examples

Creating Graph

更多创建图表的示例,请参见 G6 示例

import React from 'react';
import { Graphin } from '@antv/graphin';

export function Demo() {
  return (
    <Graphin
      options={{
        autoResize: true,
        data: {
          nodes: [
            { id: 'node-1', style: { x: 50, y: 100 } },
            { id: 'node-2', style: { x: 150, y: 100 } },
          ],
          edges: [{ id: 'edge-1', source: 'node-1', target: 'node-2' }],
        },
        behaviors: ['drag-canvas', 'zoom-canvas', 'drag-element'],
      }}
    />
  );
}

Fetching Data

获取远程数据并在数据更新时重新渲染图表。

import React, { useEffect, useMemo, useState } from 'react';
import { Graphin } from '@antv/graphin';

export function Demo() {
  const [data, setData] = useState(undefined);

  useEffect(() => {
    fetch('https://assets.antv.antgroup.com/g6/graph.json')
      .then((res) => res.json())
      .then((data) => setData(data));
  }, []);

  // The options will update when the data changes
  const options = useMemo(
    () => ({
      autoResize: true,
      data,
      layout: { type: 'd3-force' },
      behaviors: ['drag-canvas', 'zoom-canvas', 'drag-element'],
    }),
    [data],
  );

  if (!data) return <p>Loading...</p>;

  return <Graphin options={options} />;
}

Handling Events

<Graphin /> 暴露了 ref 用于获取图实例,以便处理事件或获取实例属性。

import React, { useEffect, useRef } from 'react';
import { Graphin } from '@antv/graphin';
import { GraphEvent, NodeEvent } from '@antv/g6';

export function Demo() {
  const graphRef = useRef();

  const onInit = () => {
    const graph = graphRef.current;

    // Listen input events.
    graph.on(NodeEvent.CLICK, (event) => {});

    // Listen to lifecycle events.
    graph.on(GraphEvent.AFTER_RENDER, (event) => {
      // Simulate a click event on a node.
      graph.emit(NodeEvent.CLICK, { target: { id: 'node-1' }, targetType: 'node' });
    });
  };

  return <Graphin ref={graphRef} onInit={onInit} />;
}

Styling Container

给图画布容器添加 css 样式:

import React from 'react';
import { Graphin } from '@antv/graphin';

export function Demo() {
  // ...
  return (
    <Graphin
      className="my-graphin-container"
      style={{
        width: 600,
        height: 600,
        background: '#eee',
        padding: '1em',
        borderRadius: '0.5em',
      }}
    />
  );
}

Using hooks

使用 useGraphin() 来方便地访问图实例和其状态。

import React from 'react';
import { Graphin, useGraphin } from '@antv/g6'';

const CustomComponent = () => {
  const { graph, isReady } = useGraphin();

  return <>{!isReady ? <p>Loading...</p> : <div className="graphin-component"></div>}</>;
};

export function Demo() {
  // ...
  return (
    <Graphin>
      <CustomComponent />
    </Graphin>
  );
}

changelog

2021-03-08

Chores
Documentation Changes
New Features
Other Changes

2021-02-10

Chores
Documentation Changes
New Features
Bug Fixes
Other Changes
  • add layout switching (4096a667)
  • //github.com/antvis/Graphin (d4e18a57)
  • fix eslint error and skip test (63162f23)
  • graphin:
Refactors
Code Style Changes

2021-02-07

Chores
Documentation Changes
New Features
Bug Fixes
Other Changes
  • //github.com/antvis/Graphin (d4e18a57)
  • fix eslint error and skip test (63162f23)
  • graphin:
Refactors
Code Style Changes

2020-07-17

Graphin v1.4.3

Other Changes
  • common:

2020-07-08

Graphin v1.4.2

New Features
  • common: export G6 from @antv/g6 (fc6885e4)

Graphin v1.4.1

New Features
  • site: add node combo static layout demo (54faf117)
  • components: update version to 1.4.0 (7cf8daf9)
Other Changes
  • common:
    • fix poly edge display error when poly edge's count > 2 (160ec2e5)
    • fix label size issue in default cfg (06cc99e2)
  • graphin: fix ci (eb56c951)
Code Style Changes
  • graphin: remove console.log (25c51ac6)

Graphin v1.4.0

New Features
Bug Fixes
Other Changes
  • v1.4.0 upgrade g6 version to 3.5 (0d33bec2)
  • add cursor pointer (931e9dee)
  • upgrading dependencies (d844962c)
  • common:
    • v1.3.0 (dc167f4e)
    • fix ts typing error after upgrade 3.5 (2af719cc)
    • remove useless layout option for g6 degre layout (2931dc2b)
    • clear selected state brefore new node is selected (77a0cc1a)
    • fix line arrow direction after upgrade g6 (b31d598b)
    • close g6 local refresh to avoid clip ghost issue (c7d470e5)
    • upgrade inner shape to adopt g6 3.4 (fe3893e1)
    • fix pointer center issue in layout after update g6 3.4 (08c315c1)
    • replace shape by type in Node/Edge (fbad8a89)
    • upgrade clearItemStates api (5f0d640d)
    • upgrade @antv/g6 to 3.4 (6bf439e6)
    • fix ts error (390fb4ec)
    • fix testing case (17cf0f77)
    • fix testing case which is broken after upgrading g6 to 3.3 (b8b2102b)
    • clean eslint warning (9a51056a)
    • fix G Object is unknown issue (e31f81c7)
    • fix typing error (faf32663)
  • graphin: change didupdate logic (a1975c7b)
  • upgrade:
    • upgrade g6 type definition (87645c5e)
    • update g6 to 3.4 (bb8331c9)
    • upgrade g6 to 3.3.6 and upgrade ts definitions (03eb7d18)
    • upgrade @antv/g6 to ^3.3.5 (a9d043fd)

Graphin v1.3.0

Other Changes

Graphin v1.2.2

Chores
  • 🔀 add GitHub Action to sync Gitee (79d9d937)
New Features
Bug Fixes
  • update npmClient (e38808fd)
  • replace tnpm with yarn to avoid ci error (24682a8b)
  • support user defined property in date props (49d0076f)
  • fetch data url (988b580d)
  • Support multiple behavior modes closes #62 (f7f88968)
  • test: graphin first render fragment should have only one child (d3cfa49a)
  • common: update jest snapshot (c78380e5)
  • site: resolve style conflicts #44 (3425139e)
Other Changes
  • common:
    • Jest does not support ES module (4beabd56)
    • uniform inner shape name (f34af717)
    • downgrade ts target to es2015 (4967aba7)
    • replace degre with badge and explore it to user (df4c426b)
    • fix inner shape label position and badge color (f5f2f6f7)
    • new inner shape demo (7abb7d11)
    • update history change list (bb908daa)
    • add change log generator tool (3abd8e3f)
    • add change log generator tool (57df91f7)
    • update edge label default color (a1ded89e)
    • adjust leabel position and label color (365ab77b)
    • update dark style of graph studio shape (faa9e7d2)
    • support user-defined layout option declaration (40e9179b)
    • fix behavior document (9c863783)
    • opt tween animation of force layout in worker mode to make it more smoothly (a07335b6)
    • invalid data path (cc617d0b)
    • new stub and point shape (8cdc46a5)
    • new hexagon shape (901e9d9b)
    • new node/edge shape (6b0cb8bd)
    • upgrade prettier to 2.0.2 to support ts optional chain (c85e620e)
    • update copyright (f4cc64f6)
    • replace graphin studio with graph studio (0a5182aa)
  • antvis/Graphin into doc/graphin-shape-demo (c52c7dab)
  • update to version 1.1.0-beta.1 (4c0519d0)
  • format code (509a3ef9)
  • update graphin-studio prettier config (5e5ec40a)
  • find-connections and node-expand (4a62c187)
  • update layout apis docs (bc0af2f0)
  • add webworker for force layout (53903091)
  • fix full screen bugs (50ca0ccc)
  • demand loading antd for components (5098991e)

Graphin v1.0.5

New Features
Other Changes

Graphin v1.0.3

Documentation Changes
  • 🔍 Add algolia docsearchOptions (c8b96a1e)
  • fix example throw error (f5222a0d)
New Features
  • using shallow equal for should update for deps (025ee503)
Bug Fixes
Other Changes
  • add restartForceOnDrag flag in options (fb706729)
  • add umd bundle for graphin components and add umd install doc in readme (23d3e46d)
  • disable isZoomOptimize by default (50fc0798)
  • add id, source, target on data when check data (591a705a)
  • remove useless file (ec1b63d6)
  • format jsx file (bf3bb100)
  • add umd build for graphin (2726fa63)
  • fix doc typo (29b58d87)
  • format code with 2 spaces (90996517)
  • allow options.modes to be merge into g6 options (1a8f1512)
  • 将 forceSimulation 的处理内置在 graphin,业务端不需要感知 (cfb06a8e)
  • remove forceSimulation before layout change (466e67fe)
  • subgraph layout demo on site. (4310001f)
  • learn link graphin-site (e160b0fa)
  • provide layout api's return type (bc620591)
  • eslint is broken after upgrade to 2.19.1 (ed4c867e)
  • update deps version in graphin studio (5dfd7c6a)
  • fix graphin studio bundle config (31ca2227)
  • move graphin to devDependencies (0220fb35)
  • modify gatsby.config.js (af9b099f)
  • graphin examples (94bca824)
  • modify the wrong word (fd9f79cf)
  • update readme.md (23415f89)
  • add 红楼梦 case (a8d0b10b)
  • update docs for getting started (ad34f9bb)
  • add examples (859a081e)
  • add label for Node type (ba444e1e)
  • fix examples throw error (a1b547a3)
  • add layout demo (5615c67b)
  • add graphin-site/examples (d3dfc810)

Graphin 1.0.2

Other Changes

Graphin v1.0.0

Other Changes
  • release graphin/v1.0.0 and graphin-components/v1.0.0 (b72480d6)
  • allow empty icon for circle shape (18a42cb9)
  • fix extend.icon issue (ac2bb18f)
  • update readme (dd9daff8)

Graphin v1.0.0-beta.11

Bug Fixes

Graphin v1.0.0-beta.9

New Features
Bug Fixes
Other Changes
Code Style Changes

Graphin v1.0.0-beta.8

New Features
Other Changes