• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在vue使用3d-force-graph

武飞扬头像
爱吃土豆的兔子
帮助1

 一 下载

import ForceGraph3D from '3d-force-graph';

二、引入初始化

github地址:https://github.com/vasturiano/3d-force-graph 

1. 引入:

import ForceGraph3D from '3d-force-graph'

2.html元素:

<div ref="graph" id="3d-graph"></div>

3.初始化JSON数据格式

  1.  
    data () {
  2.  
    return {
  3.  
    myGraph: null, // 3D-graph对象
  4.  
    // 3D-graph加载的图数据
  5.  
    graphData: {
  6.  
    nodes: [
  7.  
    {
  8.  
    id: 'id1',
  9.  
    name: '小兰花',
  10.  
    val: 20,
  11.  
    colorkey: '#B7D2F0'
  12.  
    },
  13.  
    {
  14.  
    id: 'id2',
  15.  
    name: '东方青苍',
  16.  
    val: 20,
  17.  
    colorkey: '#ECB5C9'
  18.  
    },
  19.  
    {
  20.  
    id: 'id11',
  21.  
    name: '种花',
  22.  
    val: 10,
  23.  
    colorkey: '#D9C8AE'
  24.  
    },
  25.  
    {
  26.  
    id: 'id12',
  27.  
    name: '修命簿',
  28.  
    val: 15,
  29.  
    colorkey: '#D9C8AE'
  30.  
    },
  31.  
    {
  32.  
    id: 'id13',
  33.  
    name: '司命',
  34.  
    val: 20,
  35.  
    colorkey: '#D9C8AE'
  36.  
    },
  37.  
    {
  38.  
    id: 'id21',
  39.  
    name: '月族首领',
  40.  
    val: 10,
  41.  
    group: 1
  42.  
    },
  43.  
    {
  44.  
    id: 'id22',
  45.  
    name: '业火',
  46.  
    val: 10,
  47.  
    group: 2
  48.  
    },
  49.  
    {
  50.  
    id: 'id23',
  51.  
    name: '荡平水云天',
  52.  
    val: 20,
  53.  
    group: 2
  54.  
    }
  55.  
    ],
  56.  
    links: [
  57.  
    {
  58.  
    source: 'id1',
  59.  
    target: 'id2',
  60.  
    name: '情侣',
  61.  
    colorkey: '#B7D2F0',
  62.  
    value: 3
  63.  
    },
  64.  
    {
  65.  
    source: 'id1',
  66.  
    target: 'id11',
  67.  
    name: '爱好',
  68.  
    colorkey: '#D9C8AE',
  69.  
    value: 1
  70.  
    },
  71.  
    {
  72.  
    source: 'id1',
  73.  
    target: 'id12',
  74.  
    name: '职业',
  75.  
    colorkey: '#D9C8AE',
  76.  
    value: 1
  77.  
    },
  78.  
    {
  79.  
    source: 'id1',
  80.  
    target: 'id13',
  81.  
    name: '师傅',
  82.  
    colorkey: '#D9C8AE',
  83.  
    value: 1
  84.  
    },
  85.  
    {
  86.  
    source: 'id21',
  87.  
    target: 'id2',
  88.  
    name: '职业',
  89.  
    colorkey: '#D9C8AE',
  90.  
    value: 2
  91.  
    },
  92.  
    {
  93.  
    source: 'id2',
  94.  
    target: 'id22',
  95.  
    name: '技能',
  96.  
    colorkey: '#D9C8AE',
  97.  
    value: 3
  98.  
    },
  99.  
    {
  100.  
    source: 'id2',
  101.  
    target: 'id23',
  102.  
    name: '爱好',
  103.  
    colorkey: '#D9C8AE',
  104.  
    value: 1
  105.  
    },
  106.  
    {
  107.  
    source: 'id23',
  108.  
    target: 'id23',
  109.  
    name: '爱好',
  110.  
    colorkey: '#D9C8AE',
  111.  
    value: 1
  112.  
    }
  113.  
    ]
  114.  
    },
  115.  
    }
  116.  
    }
学新通
  1.  
    initGraph () {
  2.  
    this.myGraph = ForceGraph3D()
  3.  
    this.myGraph(document.getElementById('3d-graph')).graphData(this.graphData)
  4.  
    }

三、使用

     1. 基本渲染图谱

  1.  
    this.myGraph = ForceGraph3D()
  2.  
    this.myGraph(document.getElementById('3d-graph')).graphData(this.graphData)

2.画布容器样式修改(Container layout)

  1.  
    this.myGraph(document.getElementById('3d-graph')).graphData(this.graphData)
  2.  
    .backgroundColor('#ffffff') //画布的背景颜色
  3.  
    .height(this.graphHeight) //画布的背高度
  4.  
    .width(this.graphWidth) //画布的背宽度

3. 节点样式(Node styling)

  1.  
    this.myGraph = ForceGraph3D()
  2.  
    this.myGraph(document.getElementById('3d-graph'))
  3.  
    .graphData(this.graphData)
  4.  
    // .jsonUrl('./datasets/miserables.json') //找不到文件 目前无法使用,未解决此问题
  5.  
    // .nodeLabel('name') // 鼠标悬浮至mesh上显示标签,标签内容为id
  6.  
    .nodeLabel(node => {
  7.  
    return `<p style="color: #000000">${node.name}<p>` // `${node.id}: ${node.name}`
  8.  
    })
  9.  
    .nodeColor('colorkey') // 数据中对应节点颜色的key,不使用nodeColor默认会自己提取color的颜色,若没有color则会给默认颜色
  10.  
    .nodeAutoColorBy('group') // //按照group进行分类,仅影响没有颜色值的数据
  11.  
    .nodeResolution(12) // 数值越大 节点圆越圆滑
  12.  
    // 在此函数自定义节点显示状态,可使用3d也可使用2d,可以显示html、图片、以及使用three.js绘制的几何体等
  13.  
    .nodeThreeObject(node => {
  14.  
    const sprite = new SpriteText(node.name) //需要下载import SpriteText from 'three-spritetext'
  15.  
    // sprite.material.depthWrite = false; // make sprite background transparent
  16.  
    sprite.color = '#000000' // node.group
  17.  
    sprite.textHeight = 6
  18.  
    return sprite
  19.  
    })
  20.  
    .nodeThreeObjectExtend(true) // 节点对象访问器函数、属性或布尔值,用于确定在使用自定义nodeThreeObject(false)时是替换默认节点还是扩展它(true)
学新通

更多配置项见下方Node styling配置 

4.节点材质变化

  1.  
    // 文本作为节点
  2.  
    nodeThreeObject(node => {
  3.  
    const sprite = new SpriteText(node.id);
  4.  
    // sprite.material.depthWrite = false; // make sprite background transparent
  5.  
    sprite.color = node.color;
  6.  
    sprite.textHeight = 8;
  7.  
    return sprite;
  8.  
    });
  9.  
     
  10.  
    // 图片作为节点 img为node数据里面图片名 需要使用three.js
  11.  
    .nodeThreeObject(({ img }) => {
  12.  
    const imgTexture = new THREE.TextureLoader().load(`./imgs/${img}`); //创建纹理贴图
  13.  
    const material = new THREE.SpriteMaterial({ map: imgTexture });
  14.  
    const sprite = new THREE.Sprite(material);
  15.  
    sprite.scale.set(12, 12)
  16.  
    return sprite;
  17.  
    })
  18.  
    // html作为节点
  19.  
    .nodeThreeObject(node => {
  20.  
    const nodeEle = document.createElement('div');
  21.  
    nodeEle.textContent = node.name;
  22.  
    nodeEle.style.color = node.colorkey;
  23.  
    nodeEle.className = 'nodeLabel';
  24.  
    return new THREE.CSS2DObject(nodeEle);
  25.  
    })
  26.  
    // 几何体作为节点
  27.  
    .nodeThreeObject(({ id }) => {
  28.  
    const ids = id.slice(2)
  29.  
    const Mesh = new THREE.Mesh(
  30.  
    [
  31.  
    // 方块
  32.  
    new THREE.BoxGeometry(
  33.  
    Math.random() * 20,
  34.  
    Math.random() * 20,
  35.  
    Math.random() * 20
  36.  
    ),
  37.  
    // 锥体
  38.  
    new THREE.ConeGeometry(Math.random() * 10, Math.random() * 20),
  39.  
    // 圆柱
  40.  
    new THREE.CylinderGeometry(
  41.  
    Math.random() * 10,
  42.  
    Math.random() * 10,
  43.  
    Math.random() * 20
  44.  
    ),
  45.  
    // 十二面体
  46.  
    new THREE.DodecahedronGeometry(Math.random() * 10),
  47.  
    // 球体
  48.  
    new THREE.SphereGeometry(Math.random() * 10),
  49.  
    // 圆环
  50.  
    new THREE.TorusGeometry(Math.random() * 10, Math.random() * 2),
  51.  
    // 环面扭结
  52.  
    new THREE.TorusKnotGeometry(Math.random() * 10, Math.random() * 2)
  53.  
    ][ids % 7],
  54.  
    new THREE.MeshLambertMaterial({
  55.  
    color: Math.round(Math.random() * Math.pow(2, 24)),
  56.  
    transparent: true,
  57.  
    opacity: 0.75
  58.  
    })
  59.  
    )
  60.  
    return Mesh
  61.  
    })
学新通

学新通

5.连接线配置(Link styling)

  1.  
    this.myGraph = ForceGraph3D()
  2.  
    this.myGraph(document.getElementById('3d-graph'))
  3.  
    .linkColor('colorkey') // 可以选择根据colorkey值显示颜色,如果不设置默认显示color字段
  4.  
    .linkOpacity(0.7) // 链接透明度
  5.  
    .linkWidth(1)
  6.  
    .linkDirectionalArrowLength(3.5) // 箭头长度
  7.  
    .linkDirectionalArrowRelPos(1) // 箭头位置偏移 source指向target
  8.  
    .linkCurvature(0.25) // 曲度

6.连接线上显示文字(text in links)

  1.  
    this.myGraph = ForceGraph3D()
  2.  
    this.myGraph(document.getElementById('3d-graph'))
  3.  
    // 显示的文字
  4.  
    .linkThreeObject(link => {
  5.  
    // extend link with text sprite
  6.  
    const sprite = new SpriteText(`${link.name}`)
  7.  
    sprite.color = '#000'
  8.  
    sprite.textHeight = 6
  9.  
    return sprite
  10.  
    })
  11.  
    .linkThreeObjectExtend(true) //不替换原来的样式只扩展 true
  12.  
    //连接的位置更新
  13.  
    .linkPositionUpdate((sprite, { start, end }) => {
  14.  
    const middlePos = Object.assign(
  15.  
    ...['x', 'y', 'z'].map(c => ({
  16.  
    [c]: start[c] (end[c] - start[c]) / 2 // calc middle point
  17.  
    }))
  18.  
    )
  19.  
    console.log(middlePos)
  20.  
    // Position sprite
  21.  
    Object.assign(sprite.position, middlePos)
  22.  
    })
学新通

7.自连接

  1.  
    this.myGraph = ForceGraph3D()
  2.  
    this.myGraph(document.getElementById('3d-graph'))
  3.  
    .linkCurvature('curvature') // 曲率
  4.  
    .linkCurveRotation(180) // 连接线旋转方向

学新通

8.定向移动例子

  1.  
    this.myGraph = ForceGraph3D()
  2.  
    this.myGraph(document.getElementById('3d-graph'))
  3.  
    // 定向移动例子
  4.  
    .linkDirectionalParticles('value') // 粒子个数
  5.  
    .linkDirectionalParticleSpeed(d => d.value * 0.001) // 粒子运动速度

9.节点和连接线监听事件(Interaction)

  1.  
    this.myGraph = ForceGraph3D()
  2.  
    this.myGraph(document.getElementById('3d-graph'))
  3.  
    // 点击事件 放大当前选中节点
  4.  
    .onNodeClick((node, event) => {
  5.  
    console.log('点击事件')
  6.  
    const distance = 40
  7.  
    const distRatio = 1 distance / Math.hypot(node.x, node.y, node.z)
  8.  
    _this.myGraph.cameraPosition(
  9.  
    { x: node.x * distRatio, y: node.y * distRatio, z: node.z * distRatio }, // new position
  10.  
    node, // lookAt ({ x, y, z })
  11.  
    3000 // ms transition duration
  12.  
    )
  13.  
    })
  14.  
    // 右击事件,拖拽事件、拖拽完成, 鼠标悬停事件等使用方式相同 ,详细API见下文Interaction
学新通

常见使用效果 :

1.拉近相机距离

  1.  
    /**
  2.  
    * 拉近相机距离
  3.  
    * node 选中的节点
  4.  
    * 修改distance大小即修改相机拉近距离
  5.  
    * */
  6.  
    handleCamera (node) {
  7.  
    const distance = 600
  8.  
    const distRatio = 1 distance / Math.hypot(node.x, node.y, node.z)
  9.  
    this.myGraph.cameraPosition(
  10.  
    { x: node.x * distRatio, y: node.y * distRatio, z: node.z * distRatio }, // new position
  11.  
    node, // lookAt ({ x, y, z })
  12.  
    3000 // ms transition duration
  13.  
    )
  14.  
    }

2.播放/暂停组件周期 

  1.  
    /**
  2.  
    * 播放/暂停
  3.  
    * 根据run 判断当前出发的是播放 还是暂停 暂停 会禁止图上的所有方法
  4.  
    * **/
  5.  
    runCanvas () {
  6.  
    this.run = !this.run
  7.  
    !this.run ? this.myGraph.pauseAnimation() : this.myGraph.resumeAnimation()
  8.  
    }

3.文字显示

  1.  
    /**
  2.  
    * 添加文字,用three-spritetext(精灵字体)会更好,也更简单,有近大远小的空间感
  3.  
    * CSS2DObject 2D字体没有空间感 但是性能好
  4.  
    * 精灵字体会比2d字体性能更差,可酌情选择(3d字体需要下载字体文件,占用空间和加载性能最好不要用)
  5.  
    * */
  6.  
    // 精灵字体 需要添加
  7.  
    // import SpriteText from 'three-spritetext'
  8.  
    addSpriteText (node) {
  9.  
    const sprite = new SpriteText(node.name)
  10.  
    // sprite.material.depthWrite = false // make sprite background transparent
  11.  
    sprite.color = '#000000' // node.group
  12.  
    sprite.textHeight = 6
  13.  
    // sprite.position.set(0, 12, 0) // 改变文字再节点的位置
  14.  
    return sprite
  15.  
    },
  16.  
    // 2D字体 需要添加
  17.  
    //import {CSS2DRenderer,CSS2DObject} from 'three/examples/jsm/renderers/CSS2DRenderer.js'
  18.  
    // this.myGraph = ForceGraph3D({ extraRenderers: [new CSS2DRenderer()] })
  19.  
    createAttackLabel (node) {
  20.  
    // 成功后需要在对应的位置下面去找创建的2drender的dom下面就看到了。由于可能出现被canvas覆盖的情况,
  21.  
    // 所以有些时候会以为自己没有添加成功,需要通过z-index设置让文字显现出来且不影响图谱交互
  22.  
    const labelDiv = document.createElement('div')
  23.  
    labelDiv.className = 'attackLabel'
  24.  
    labelDiv.id = node.id
  25.  
    labelDiv.textContent = node.name
  26.  
    labelDiv.style.color = '#000000'
  27.  
    const label = new CSS2DObject(labelDiv)
  28.  
    label.position.set(0, 0, 0)
  29.  
    return label
  30.  
    }
学新通

 4.增加、删除数据

  1.  
    //更新数据然后执行以下方法
  2.  
    this.myGraph.graphData(jsondata)
  1.  
    // 删除
  2.  
    removeNode (node) {
  3.  
    let { nodes, links } = this.myGraph.graphData()
  4.  
    links = links.filter(l => l.source !== node && l.target !== node) // Remove links attached to node
  5.  
    nodes.splice(node.index, 1) // 删除 node
  6.  
    // nodes.forEach((n, idx) => { n.index = idx }) // 重置 node ids to array index index自动排序
  7.  
    this.myGraph.graphData({ nodes, links })
  8.  
    }

5. 外部对象添加进场景中 

  1.  
    //将外部对象添加到3d-force-graph的场景中
  2.  
    addScene () {
  3.  
    const planeGeometry = new THREE.PlaneGeometry(1000, 1000, 1, 1)
  4.  
    const planeMaterial = new THREE.MeshLambertMaterial({ color: 0xFF0000, side: THREE.DoubleSide })
  5.  
    const mesh = new THREE.Mesh(planeGeometry, planeMaterial)
  6.  
    mesh.position.set(-100, -200, -100)
  7.  
    mesh.rotation.set(0.5 * Math.PI, 0, 0)
  8.  
     
  9.  
    this.myGraph.scene().add(mesh)
  10.  
    },

6.节点、连接线高亮显示

  1.  
    initGraph(){
  2.  
    this.myGraph = ForceGraph3D()
  3.  
    this.myGraph(document.getElementById('3d-graph'))
  4.  
    .nodeColor(node => this.highlightNodes.has(node) ? node === this.hoverNode ? 'rgb(255,0,0,1)' : 'rgba(255,160,0,0.8)' : node.colorkey)
  5.  
     
  6.  
    .nodeAutoColorBy('group') // 按照group进行分类,仅影响没有颜色值的数据
  7.  
    .linkThreeObjectExtend(true) //不替换原来的样式只扩展 true
  8.  
    .linkWidth(link => this.highlightLinks.has(link) ? 4 : 1)
  9.  
    .linkDirectionalParticles('value') // 粒子个数
  10.  
    .linkDirectionalParticleSpeed(d => d.value * 0.001) // 粒子运动速度
  11.  
    .linkDirectionalParticleWidth(link => this.highlightLinks.has(link) ? 4 : 1)
  12.  
    .onNodeHover((node, prevNode) => {
  13.  
    console.log('悬停事件')
  14.  
    this.highLightNode(node)
  15.  
    })
  16.  
    .onLinkHover((link, event) => {
  17.  
    console.log('连接线移入')
  18.  
    this.highLightLink(link)
  19.  
    })
  20.  
    },
  21.  
    /**
  22.  
    * 高亮显示节点
  23.  
    * */
  24.  
    // 处理数据,在初始化之前执行
  25.  
    highLightProcessData () {
  26.  
    this.graphData.links.forEach(link => {
  27.  
    const a = this.graphData.nodes.find(object => object.id === link.source)
  28.  
    // const a = this.graphData.nodes[link.source]
  29.  
    const b = this.graphData.nodes.find(object => object.id === link.target)
  30.  
    // const b = this.graphData.nodes[link.target]
  31.  
    !a.neighbors && (a.neighbors = [])
  32.  
    !b.neighbors && (b.neighbors = [])
  33.  
    a.neighbors.push(b)
  34.  
    b.neighbors.push(a)
  35.  
     
  36.  
    !a.links && (a.links = [])
  37.  
    !b.links && (b.links = [])
  38.  
    a.links.push(link)
  39.  
    b.links.push(link)
  40.  
    })
  41.  
    },
  42.  
    // 鼠标移入节点,突出显示节点/链接(源)
  43.  
    highLightNode (node) {
  44.  
    // no state change
  45.  
    if ((!node && !this.highlightNodes.size) || (node && this.hoverNode === node)) return
  46.  
     
  47.  
    this.highlightNodes.clear()
  48.  
    this.highlightLinks.clear()
  49.  
    if (node) {
  50.  
    this.highlightNodes.add(node)
  51.  
    node.neighbors.forEach(neighbor => this.highlightNodes.add(neighbor))
  52.  
    node.links.forEach(link => this.highlightLinks.add(link))
  53.  
    }
  54.  
     
  55.  
    this.hoverNode = node || null
  56.  
    this.updateHighlight()
  57.  
    },
  58.  
    // 鼠标移入连接线,突出显示节点/链接(源)
  59.  
    highLightLink (link) {
  60.  
    this.highlightNodes.clear()
  61.  
    this.highlightLinks.clear()
  62.  
     
  63.  
    if (link) {
  64.  
    this.highlightLinks.add(link)
  65.  
    this.highlightNodes.add(link.source)
  66.  
    this.highlightNodes.add(link.target)
  67.  
    }
  68.  
     
  69.  
    this.updateHighlight()
  70.  
    },
  71.  
    // 更新效果
  72.  
    updateHighlight () {
  73.  
    // trigger update of highlighted objects in scene
  74.  
    this.myGraph
  75.  
    .nodeColor(this.myGraph.nodeColor())
  76.  
    .linkWidth(this.myGraph.linkWidth())
  77.  
    .linkDirectionalParticles(this.myGraph.linkDirectionalParticles())
  78.  
    },
学新通

四、API

Initialisation

ForceGraph3d({ configOptions })(<domElement>)
配置选项 描述 默认
controlType:str字符 使用哪种类型的控件来控制相机。trackballorbitfly之间进行选择。 trackball
rendererConfig: object对象 要传递给ThreeJS WebGLRenderer构造函数的配置参数 { antialias: true, alpha: true }
extraRenderers : 数组 如果您希望包含需要专用渲染器的自定义对象WebGL,例如CSS3DRenderer,请在此数组中包含那些额外的渲染器实例。 [ ]

Data input

Method Description Default
graphData([data]) 图形数据结构的Getter/setter(语法详细信息请参阅下面的部分)。也可以用于应用增量更新。 { nodes: [], links: [] }
jsonUrl([url]) 直接从中加载图形数据的JSON文件的URL,作为直接指定graphData的替代方法。  
nodeId([str]) 唯一节点id的节点对象访问器属性(用于链接对象源/目标)。 id
linkSource([str]) 引用源节点id的链接对象访问器属性。 source
linkTarget([str]) 引用目标节点id的链接对象访问器属性。 target

Container layout

Method Description Default
width([px]) 画布宽度的Getter/setter。 <window width>
height([px]) 画布高度的Getter/setter。 <window height>
backgroundColor([str]) 图表背景色的Getter/setter。 #000011
showNavInfo([boolean]) 是否显示导航控件页脚信息的Getter/setter。 true

Node styling

Method Description Default
nodeRelSize([num]) 每个值单位的节点球体体积(立方像素)比率的Getter/setter。 4
nodeVal([num,str或fn]) 节点对象访问器函数、属性或节点数值的数值常量(影响球体体积)。 val
nodeLabel([str或fn]) 名称的节点对象访问器函数或属性(显示在标签中)。支持纯文本或HTML内容。请注意,此方法在内部使用innerHTML,因此请确保pre-sanitize任何user-input内容以防止XSS漏洞。 name
nodeVisibility([boolean,str或fn]) 节点对象访问器函数、属性或用于是否显示节点的布尔常量。 true
nodeColor([str或fn]) 节点对象访问器函数或节点颜色属性(影响球体颜色)。 color
nodeAutoColorBy([str或fn]) 节点对象访问器函数(fn(node))或属性(例如'type')自动对颜色分组。仅影响没有颜色属性的节点。  
nodeOpacity([num]) 节点球体不透明度的Getter/setter,介于[0,1]之间。 0.75
nodeResolution([num]) 每个节点的几何分辨率的Getter/setter,用划分圆周的切片段数表示。值越大,球体越平滑。 8
nodeThreeObject([Object3d,str或fn]) 节点对象访问器用于生成自定义三维对象以渲染为图形节点的函数或属性。应返回ThreeJS Object3d的实例。如果返回错误的值,则该节点将改用默认的3d对象类型。 默认的node对象是一个球体,大小根据val确定,样式根据color而定。
nodeThreeObjectExtend([bool、str或fn]) 节点对象访问器函数、属性或布尔值,用于确定在使用自定义nodeThreeObjectfalse)时是替换默认节点还是扩展它(true)。 false

Link styling

Method Description Default
linkLabel([str或fn]) 名称的链接对象访问器函数或属性(显示在标签中)。支持纯文本或HTML内容。请注意,此方法在内部使用innerHTML,因此请确保pre-sanitize任何user-input内容以防止XSS漏洞。 name
linkVisibility ([boolean、str或fn]) 链接对象访问器函数、属性或用于是否显示链接线的布尔常量。值false维护链接力而不呈现它。 true
linkColor([str或fn]) 线条颜色的链接对象访问器函数或属性。 color
linkAutoColorBy([str或fn]) 链接对象访问器函数(fn(link))或属性(例如'type')以自动将颜色分组。只影响没有颜色属性的链接。  
linkOpacity([num]) 链接的行不透明度的Getter/setter,介于[0,1]。 0.2
linkWidth([num,str或fn]) 链接对象访问器函数、属性或链接线宽的数字常量。值为零将呈现宽度为常量(1px)的ThreeJS Line,而与距离无关。为了编制索引,值四舍五入到最接近的十进制数。 0
linkResolution([num]) Getter/setter用于每个链接的几何分辨率,表示为要划分圆柱体的径向分段数。值越大,生成的圆柱体越平滑。仅适用于正宽度的链接。 6
linkCurvature([num,str或fn]) 链接对象访问器函数、属性或链接线曲率半径的数值常量。曲线表示为三维贝塞尔曲线,并接受任何数值。值0呈现一条直线。1表示半径等于直线长度的一半,使曲线近似于semi-circle。对于self-referencing链接(source等于target),曲线表示为围绕节点的一个环,长度与曲率值成正比。对于正值,线是顺时针弯曲的;对于负值,则是counter-clockwise。请注意,渲染曲线纯粹是一种视觉效果,不会影响基础力的行为。 0
linkCurveRotation([num,str或fn]) 链接对象访问器函数、属性或用于沿直线轴旋转的数值常量,以应用于曲线。对直线没有影响。在0旋转时,曲线朝向与XY平面相交的方向。旋转角度(以弧度表示)将围绕"start-to-end“轴从该参考方向顺时针旋转曲线。 0
linkMaterial([Material,str或fn]) 链接对象访问器函数或属性,用于指定自定义材质以设置图形链接的样式。应该返回一个ThreeJS Material的实例。如果返回错误的值,则该链接将使用默认材质。 默认链接材质是根据coloropacity设计的MeshLambertMaterial。
linkThreeObject([Object3d,str or fn]) 链接对象访问器函数或属性,用于生成自定义三维对象以渲染为图形链接。应返回ThreeJS Object3d的实例。如果返回错误的值,则该链接将使用默认的3d对象类型。默认链接对象是一条线或圆柱体,大小width根据material. 默认的link对象是一条线或一个圆柱体,根据width调整大小,并根据material设置样式。
linkThreeObjectExtend([bool、str或fn]) 链接对象访问器函数、属性或布尔值,用于在使用自定义linkThreeObjectfalse)时替换默认链接还是扩展该链接(true)。 false
inkPositionUpdate([fn(linkObject,{start,end},link)]) 自定义函数的Getter/setter,以在每次呈现迭代时调用更新链接的位置。它接收相应的链接ThreeJS Object3d,链接的startend坐标({x,y,z}),以及链接的data。如果函数返回一个真实值,则不会为该链接运行常规位置更新函数。  
linkDirectionalArrowLength([num,str或fn]) 链接对象访问器函数、属性或指示链接方向的箭头长度的数字常量。箭头直接显示在连接线上方,并指向source>target的方向。值0隐藏箭头。 0
linkDirectionalArrowColor([str或fn]) 箭头颜色的链接对象访问器函数或属性。 color
linkDirectionalArrowRelPos([num,str或fn]) 链接对象访问器函数、属性或数值常数,用于箭头沿链接线的纵向位置,表示为01之间的比率,其中0紧邻source节点,1紧邻target节点,0.5正好在中间。 0.5
linkDirectionalArrowResolution([num]) Getter/setter用于箭头的几何分辨率,表示为将锥基周长除以多少个切片段。值越大,箭头越平滑。 8
linkDirectionalParticles([num,str或fn]) 链接对象访问器函数、属性或要在链接线上显示的粒子数(小球)的数值常量。粒子沿equi-spaced分布,沿source>target方向运动,可用来指示链路的方向性。 0
linkDirectionalParticleSpeed([num,str或fn]) 链接对象访问器函数、属性或定向粒子速度的数字常量,表示为链接长度与每帧行程的比率。不鼓励使用0.5以上的值。 0.01
linkDirectionalParticleWidth([num,str或fn]) 链接对象访问器函数、属性或定向粒子宽度的数字常量。为了编制索引,值四舍五入到最接近的十进制数。 0.5
linkDirectionalParticleColor([str或fn]) 定向粒子颜色的链接对象访问器函数或属性。 color
linkDirectionalParticleResolution([num]) 每个定向粒子的几何分辨率的Getter/setter,用划分圆周的切片段数表示。值越大,粒子越平滑。 4
emitParticle(link) 另一种生成粒子的机制是,该方法在特定链接中发射non-cyclical单个粒子。发射的粒子共享常规粒子道具的样式(速度、宽度、颜色)。包含在graphData中的有效link对象应作为单个参数传递。

Interaction

Method Description Default
onNodeClick(fn) 用于节点(left-button)单击的回调函数。node对象和event对象作为参数onNodeClick(node, event)包含。 -
onNodeRightClick(fn) 节点right-clicks的回调函数。node对象和event对象作为参数onNodeRightClick(node, event)包含。 -
onNodeHover(fn) 节点鼠标悬停事件的回调函数。node对象(或者null,如果鼠标视线下没有节点)作为第一个参数,前一个节点对象(或null)作为第二个参数:onNodeHover(node, prevNode) -
onNodeDrag(fn) 用于节点拖动交互的回调函数。每次更新节点位置时,都会在拖动节点时重复调用此函数。node对象作为第一个参数被包括在内,并且自这个函数的最后一次迭代以来坐标的变化作为第二个参数包含在{x,y,z}:onNodeDrag(node, translate)格式中。 -
onNodeDragEnd(fn) 用于节点拖动交互结束的回调函数。此函数在释放节点时调用。node对象作为第一个参数包括在内,从初始位置到坐标的整个变化以{x,y,z}:onNodeDragEnd(node, translate)的格式作为第二个参数包含。 -
onLinkClick(fn) 用于链接(left-button)单击的回调函数。link对象和event对象作为参数onLinkClick(link, event)包含。 -
onLinkRightClick(fn) 链接right-clicks的回调函数。link对象和event对象作为参数onLinkRightClick(link, event)包含。 -
onLinkHover(fn) 用于链接鼠标悬停事件的回调函数。link对象(或者null,如果在鼠标的视线下没有链接)作为第一个参数包含,前一个链接对象(或null)作为第二个参数:onLinkHover(link, prevLink) -
onBackgroundClick(fn) 用于在节点和链接之间的空白处单击事件的回调函数。事件对象作为单个参数onBackgroundClick(event)包含。 -
onBackgroundRightClick(fn) 在节点和链接之间的空白空间上的right-click事件的回调函数。事件对象作为单个参数onBackgroundRightClick(event)包含。 -
linkHoverPrecision([int]) 当密切注视链接(低值)或远离链接(高值)时,是否显示链接标签。 1
enablePointerInteraction([boolean]) 是否启用鼠标跟踪事件的Getter/setter。这将激活画布鼠标位置的内部跟踪器,并启用对象悬停/单击和工具提示标签的功能,但以性能为代价。如果要在图形性能中寻找最大增益,建议关闭此属性。 true
enableNodeDrag([boolean]) 是否允许用户交互通过click-dragging拖动节点的Getter/setter。仅在d3强制引擎上受支持。如果启用,则每次拖动一个节点时,模拟都是re-heated,因此其他节点react都要进行更改。仅当enablePointerInteraction为true并且使用d3强制引擎时才适用。 true
enableNavigationControls([boolean]) 是否启用轨迹球导航控件的Getter/setter,用于使用鼠标交互(rotate/zoom/pan)移动摄影机。

Render control

Method Description Default
pauseAnimation() 暂停组件的呈现周期,有效地冻结当前视图并取消所有用户交互。此方法可用于在静态图像足够的情况下节省性能。  
resumeAnimation() 恢复组件的呈现周期,并re-enables用户交互。此方法可与pauseAnimation一起用于性能优化。  
cameraPosition([{x,y,z}], [lookAt], [ms]) 照相机位置的Getter/setter,以xyz坐标表示。每个坐标都是可选的,只允许在某些维度上运动。可选的第二个参数可用于根据3D空间中的{x,y,z}点来定义相机应该瞄准的方向。第三个可选参数定义转换的持续时间(以毫秒为单位),以设置摄影机运动的动画。值为0(默认值)会立即将摄影机移动到最终位置。 默认情况下,相机将以z的距离面向图形的中心,距离与系统中节点的数量成比例。
zoomToFit([ms],[px],[nodeFilterFn]) 自动移动摄影机,使所有节点在其视野内可见,对准图形中心(0,0,0)。如果找不到节点,则不执行任何操作。它接受三个可选参数:第一个参数定义转换的持续时间(以毫秒为单位)来设置摄影机运动的动画(默认值:0ms)。第二个参数是画布边缘和最外层节点位置之间的填充量(px)(默认值:10px)。第三个参数指定了一个自定义的节点过滤器:node => <boolean>,如果要包含该节点,则该过滤器应返回一个truthy值。这对于关注图形的一部分很有用。 (0, 10, node => true)
postProcessingComposer() 访问post-processing作曲器。使用此选项可将post-processing渲染效果添加到场景中。默认情况下,编写器有一个直接渲染场景的过程(RenderPass),没有任何效果。  
scene() 进入内部的ThreeJS场景。可用于使用与3d-force-graph无关的其他对象扩展当前场景。  
camera() 进入内部的ThreeJS摄像头。  
renderer() 访问内部的3eJS WebGL渲染器。  
controls() 访问内部的ThreeJS控件对象。  
refresh() 重新绘制所有节点/链接。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhficjfk
系列文章
更多 icon
同类精品
更多 icon
继续加载