@acdh/network-visualization 中文文档教程

发布于 3年前 浏览 18 项目主页 更新于 3年前

network-visualization

这是一个 React 组件,用于可视化 2D 和 3D 图形数据。 它是基于 force-graph3d-force-graph,它使用 d3-forced3-force-3d 用于模拟。

How to install

从 NPM 将其安装为一个包:

npm i @acdh/network-visualization

您还可以将 UMD 构建包含在 script 标记中,并访问组件 在 NetworkVisualization 全局:

<script src="network-visualization.umd.js"></script>

UMD 构建也可通过 CDN 获得:

<script src="https://unpkg.com/@acdh/network-visualization@0/lib/network-visualization.umd.js"></script>

使用 UMD 构建时,请确保还包括 react,如果您打算 使用 3D 组件 three(注意 three 不遵循语义 版本控制,最后一个已知的工作版本是 0.126.1):

<script
  crossorigin
  src="https://unpkg.com/react@16/umd/react.production.min.js"
></script>
<script
  crossorigin
  src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"
></script>
<script
  crossorigin
  src="https://unpkg.com/three@0.126.1/build/three.min.js"
></script>

请参阅 examples folder 了解如何使用 UMD 构建。

How to use

该包导出三个组件:基本的 组件,以及一个 组件 包装可视化组件并处理选择/取消选择节点 图。

对于 2D 可视化:

import React from 'react'
import { Visualization } from '@acdh/network-visualization'

const MyComponent = props => (
  <div style={{ position: 'relative', width: '600px', height: '600px' }}>
    <Visualization
      graph={{
        nodes: [
          { id: 'n1', label: 'Node 1', type: 't1' },
          { id: 'n2', label: 'Node 2', type: 't2' },
          { id: 'n3', label: 'Node 3', type: 't2' },
        ],
        edges: [
          { id: 'e1', label: 'Edge 1', source: 'n1', target: 'n2', type: 'r1' },
          { id: 'e2', label: 'Edge 2', source: 'n1', target: 'n3', type: 'r1' },
        ],
        types: {
          nodes: [
            { id: 't1', label: 'Category 1', color: '#006699' },
            { id: 't2', label: 'Category 2', color: '#669900' },
          ],
          edges: [{ id: 'r1', label: 'Relation 1', color: '#990066' }],
        },
      }}
    />
  </div>
)

graph 属性是唯一必需的属性。 它必须包括图的节点 和边,以及描述节点和边类型的 types 对象。 全部 图中的实体可以有一个可选的 label 和一个 color,其中标签 在单个节点/边上定义的颜色优先于标签和 为节点类型/边缘类型定义的颜色。

请注意,nodesedgestypes.nodestypes.edges 可以提供为 数组,或者作为id映射的对象,eg:

{
  nodes: {
    n1: {
      id: 'n1'
    },
    n2: {
      id: 'n2',
    },
  },
}

当图数据在组件的生命周期发生变化时,默认new 节点和边将被添加到之前提供的图表中。 如果你想 replace 图形,您可以将 replace 属性添加到图形对象:

 {
   nodes: [],
   edges: [],
   types: {
     edges: [],
     nodes: []
   },
+  replace: true,
 }

Selection controls

允许选择/取消选择节点的单击交互可以添加 组件:

import React from 'react'
import { SelectionControls as Visualization } from '@acdh/network-visualization'

const MyComponent = props => (
  <div style={{ position: 'relative', width: '600px', height: '600px' }}>
    <Visualization dimensions={3} graph={props.graph} />
  </div>
)

默认情况下,SelectionControls是一个不受控制的组件,即它持有 内部选定节点的状态。 可以将其切换为受控 通过使用 selectedNodeIds 属性提供一组 Set 组件。

Live example

您可以通过本地查看故事书中组件的实时示例 运行 npm run storybook,或者 此处

Props

Visualization and Visualization3D

proptypedefaultdescription
backgroundColorstringCanvas color
dagModestring | nullnullLayout mode for directed acyclical graphs
graphobjectGraph data, see above for the expected format
heightnumbercontainer heightCanvas height
highlightedNodeIdsSet\<string>Ids of highlighted nodes
onNodeClickfunctionEvent callback fired when clicking on a node. Receives an object with { node, graph, forceGraph } as argument
onSimulationEndfunctionEvent callback fired when simulation ends
onSimulationTickfunctionEvent callback fired every simulation tick
onZoomfunctionEvent callback fired on every zoom/pan interaction
selectedNodeIdsSet\<string>Ids of selected nodes
showNeighborsOnlyboolfalseOnly how neighbors of selected nodes
widthnumbercontainer widthCanvas width

SelectionControls

具有 中的所有属性,并添加:

proptypedefaultdescription
dimensionsnumber22D or 3D layout
onNodeDeselectfunctionEvent callback fired when node is deselected. Receives the node object as argument (and the set of currently selected node ids when used as an uncontrolled component)
onNodeSelectfunctionEvent callback fired when node is selected. Receives the node object as argument (and the set of currently selected node ids when used as an uncontrolled component)

network-visualization

This is a React component to visualize graph data in 2D and 3D. It is based on force-graph and 3d-force-graph, which use d3-force and d3-force-3d for the simulation.

How to install

Install it as a package from NPM:

npm i @acdh/network-visualization

You can also include the UMD build in a script tag, and access the components on the NetworkVisualization global:

<script src="network-visualization.umd.js"></script>

The UMD build is also available via CDN:

<script src="https://unpkg.com/@acdh/network-visualization@0/lib/network-visualization.umd.js"></script>

When using the UMD build, make sure to also include react and, if you plan to use the 3D component, three (note that three does not follow semantic versioning, last known working version is 0.126.1):

<script
  crossorigin
  src="https://unpkg.com/react@16/umd/react.production.min.js"
></script>
<script
  crossorigin
  src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"
></script>
<script
  crossorigin
  src="https://unpkg.com/three@0.126.1/build/three.min.js"
></script>

See the examples folder for how to use the UMD build.

How to use

The package exports three components: the basic <Visualization /> and <Visualization3D /> components, and a <SelectionControls /> component that wraps the visualization components and handles selecting/deselecting nodes in the graph.

For a 2D visualization:

import React from 'react'
import { Visualization } from '@acdh/network-visualization'

const MyComponent = props => (
  <div style={{ position: 'relative', width: '600px', height: '600px' }}>
    <Visualization
      graph={{
        nodes: [
          { id: 'n1', label: 'Node 1', type: 't1' },
          { id: 'n2', label: 'Node 2', type: 't2' },
          { id: 'n3', label: 'Node 3', type: 't2' },
        ],
        edges: [
          { id: 'e1', label: 'Edge 1', source: 'n1', target: 'n2', type: 'r1' },
          { id: 'e2', label: 'Edge 2', source: 'n1', target: 'n3', type: 'r1' },
        ],
        types: {
          nodes: [
            { id: 't1', label: 'Category 1', color: '#006699' },
            { id: 't2', label: 'Category 2', color: '#669900' },
          ],
          edges: [{ id: 'r1', label: 'Relation 1', color: '#990066' }],
        },
      }}
    />
  </div>
)

The graph prop is the only required prop. It must include the graph's nodes and edges, as well as a types object describing the node and edge types. All entities in the graph can have an optional label and a color, where labels and colors defined on individual nodes/edges take precedence over labels and colors defined for node types/edge types.

Note that nodes, edges, types.nodes and types.edges can be provided as arrays, or as objects mapped by id, e.g.:

{
  nodes: {
    n1: {
      id: 'n1'
    },
    n2: {
      id: 'n2',
    },
  },
}

When the graph data changes during the lifetime of the component, by default new nodes and edges will be added to the previously provided graph. If you want to replace a graph, you can add the replace property to the graph object:

 {
   nodes: [],
   edges: [],
   types: {
     edges: [],
     nodes: []
   },
+  replace: true,
 }

Selection controls

Click interactions which allow selecting/deselecting nodes can be added with the <SelectionControls /> component:

import React from 'react'
import { SelectionControls as Visualization } from '@acdh/network-visualization'

const MyComponent = props => (
  <div style={{ position: 'relative', width: '600px', height: '600px' }}>
    <Visualization dimensions={3} graph={props.graph} />
  </div>
)

By default, SelectionControls is an uncontrolled component, i.e. it holds the state of selected nodes internally. It is possible to switch it to a controlled component by providing a Set of ids with the selectedNodeIds prop.

Live example

You can view a live example of the components in storybook, by either locally running npm run storybook, or here.

Props

Visualization and Visualization3D

proptypedefaultdescription
backgroundColorstringCanvas color
dagModestring | nullnullLayout mode for directed acyclical graphs
graphobjectGraph data, see above for the expected format
heightnumbercontainer heightCanvas height
highlightedNodeIdsSet\<string>Ids of highlighted nodes
onNodeClickfunctionEvent callback fired when clicking on a node. Receives an object with { node, graph, forceGraph } as argument
onSimulationEndfunctionEvent callback fired when simulation ends
onSimulationTickfunctionEvent callback fired every simulation tick
onZoomfunctionEvent callback fired on every zoom/pan interaction
selectedNodeIdsSet\<string>Ids of selected nodes
showNeighborsOnlyboolfalseOnly how neighbors of selected nodes
widthnumbercontainer widthCanvas width

SelectionControls

Has all props from <Visualization />, and adds:

proptypedefaultdescription
dimensionsnumber22D or 3D layout
onNodeDeselectfunctionEvent callback fired when node is deselected. Receives the node object as argument (and the set of currently selected node ids when used as an uncontrolled component)
onNodeSelectfunctionEvent callback fired when node is selected. Receives the node object as argument (and the set of currently selected node ids when used as an uncontrolled component)
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文