• react中使用cytoscape


    1. 安装 cytoscape

    npm install cytoscape

    2. 使用

    import React, { useEffect, useRef, useState } from "react";
    import cytoscape from "cytoscape";
    
    const peopleList = [
      {
        "data": {
          "id": "1",
          "label": "我"
        },
        "position": {
          "x": 100,
          "y": 100
        },
        "style": {
          "shape": "rectangle",
          "backgroundColor": "#dfacde",
          "color": "#ff0abc",
          "borderColor": "red",
          "borderWidth": 2,
          "fontSize": "25px"
        }
      },
      {
        "data": {
          "id": "2",
          "label": "父亲"
        },
        "position": {
          "x": 100,
          "y": 200
        },
        "style": {
          "shape": "rectangle"
        }
      },
      {
        "data": {
          "id": "3",
          "label": "母亲"
        },
        "position": {
          "x": 200,
          "y": 100
        },
        "style": {
          "shape": "ellipse",
          "borderColor": "#000",
          "borderWidth": 5
        }
      },
      {
        "data": {
          "id": "4",
          "label": "姐姐"
        },
        "position": {
          "x": 130,
          "y": 130
        },
        "style": {
          "shape": "ellipse",
          "color": "#ffa500",
          "backgroundImage": "url(http://localhost:8088/11.png)"
        }
      }
    ]
    const relations = [
      {
        "data": {
          "id": "1-2",
          "source": "1",
          "target": "2",
          "label": "父子关系"
        },
        "style": {
          "width": 5,
          "color": null
        }
      },
      {
        "data": {
          "id": "1-3",
          "source": "1",
          "target": "3",
          "label": "母子关系",
          "lineType": "dashed"
        },
        "style": {
          "width": 2,
          "color": null,
          "lineStyle": "dashed"
        }
      },
      {
        "data": {
          "id": "1-4",
          "source": "1",
          "target": "4",
          "label": "姐弟关系",
          "lineType": "zigzag"
        },
        "style": {
          "width": 2,
          "color": null,
          "lineStyle": "zigzag"
        }
      },
      {
        "data": {
          "id": "2-3",
          "source": "2",
          "target": "3",
          "label": "夫妻关系",
          "lineType": "dotted"
        },
        "style": {
          "width": 2,
          "color": null,
          "lineStyle": "dotted",
          "lineColor": "#ffabbb"
        }
      },
      {
        "data": {
          "id": "2-4",
          "source": "2",
          "target": "4",
          "label": "父女关系",
          "lineType": "sinewave"
        },
        "style": {
          "width": 2,
          "color": "#fafbcd",
          "lineStyle": "sinewave"
        }
      },
      {
        "data": {
          "id": "3-4",
          "source": "3",
          "target": "4",
          "label": "母女关系",
          "lineType": "segment"
        },
        "style": {
          "width": 2,
          "color": "#ff0000",
          "lineStyle": "segment"
        }
      }
    ]
    
    interface ElementItem {
      nodes: any[];
      edges: any[];
    }
    
    export default function Graph() {
      const cyRef = useRef<HTMLDivElement>(null);
    
      const [elements, setElements] = useState<ElementItem>();
    
      useEffect(() => {
        getGraphData();
      }, []);
    
      useEffect(() => {
        if (elements) {
          drawGraph();
        }
      }, [elements])
    
      async function getGraphData() {
        setElements({
          nodes: peopleList,
          edges: relations,
        });
      }
    
      function drawGraph() {
        if (cyRef.current) {
          const cy = cytoscape({
            container: cyRef.current,
            elements,
            style: [
              {
                selector: "node",
                style: {
                  "background-fit": "cover cover",
                  label: "data(label)",
                  'text-halign': 'center',
                  'text-valign': 'center',
                  width: 'label' + 10,
                  height: 'label' + 10,
                  'font-size': '10px',
                  "text-wrap": "wrap",
                  "text-max-width": 100,
                },
              },
              {
                selector: "edge",
                style: {
                  'line-color': '#333',
                  'target-arrow-color': '#333',
                  label: 'data(label)',
                  "control-point-step-size": 20,
                  'control-point-distances': [-3, 3, -3, 3, -3, 3, -3, 3, -3, 3, -3, 3, -3, 3, -3, 3, -3, 3, -3],
                  'control-point-weights': [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55,
                    0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95],
                  'curve-style': (edge: any) => {
                    switch (edge.data('lineType')) {
                      case 'sinewave':
                        return 'unbundled-bezier';
                      case 'zigzag':
                        return 'segments'
                      default:
                        return 'bezier';
                    }
                  },
                  'segment-distances': [-3, 3, -3, 3, -3, 3, -3, 3, -3, 3, -3, 3, -3, 3, -3, 3, -3, 3, -3],
                  'segment-weights': [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55,
                    0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95],
                  'font-size': '8px',
                },
              },
    
            ],
            layout: {
              name: "preset",
            },
          });
    
          cy.fit();
        }
      }
    
      return <div ref={cyRef} style={{ height: '100%', width: '100%' }} />
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238

    3. 运行效果图

    在这里插入图片描述

  • 相关阅读:
    JavaScript endsWith() 方法
    循环执行某段代码,待某种条件满足后停止循环 java原始Timer实现
    第3周学习:ResNet+ResNeXt
    java-单列集合List详解
    基于Html5的个性化学习系统的设计与实现
    监督学习(无、半)&分类与回归
    Flutter快学快用20 原生通信:应用原生平台交互扩充 Flutter 基础能力
    car包recode函数多分类变量的重新编码
    Redis加Lua脚本实现分布式锁
    Linux 网络协议栈收消息过程-Ring Buffer
  • 原文地址:https://blog.csdn.net/M_Eve/article/details/132625789