前端人物關(guān)系圖展示--狂飆1

1. 前端選型

本著凡事先問GPT的原則爱沟,先看看GPT給我哪些答案。


ChatGPT

1.1 vis:好像沒聽過,試一下看看

1.2. echarts:一般用來做圖表铣减,關(guān)系圖也沒用過,試一下

1.4. d3:聽說入門比較麻煩脚作,對菜鳥不友好葫哗,算了,先不嘗試了

1.3. G6:螞蟻出品的可視化的組件球涛,試試劣针。

2. 測試數(shù)據(jù)準(zhǔn)備

這里主要準(zhǔn)備了一些《狂飆》電視劇中的人物關(guān)系,只當(dāng)個Demo做演示用宾符,不要琢磨細(xì)致程度

3. 寫代碼測試效果

3.1. vis

<template>
  <div id="graphContainer"></div>
</template>

<script>
import {Network, DataSet} from "vis-network/standalone";
import "vis-network/styles/vis-network.css";
import {nodes, edges} from './data/data'

export default {
  name: "Index",
  data() {
    return {
      graph: undefined,
      nodes: [],
      edges: [],
      container: undefined,
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initGraph()
    })
  },
  methods: {

    // 初始化圖形配置
    initGraph() {
      edges.forEach((item) => {
        item.from = item.source
        item.to = item.target
      })
      this.nodes = new DataSet(nodes);
      this.edges = new DataSet(edges);
      const container = document.getElementById("graphContainer");
      const data = {
        nodes: this.nodes,
        edges: this.edges
      };
      const options = {};
      const network = new Network(container, data, options);
      console.log(network)
    }
  }
}
</script>

<style scoped>
#graphContainer {
  width: 100%;
  height: 800px;
  background-color: white;
}
</style>
vis.png

3.2. echarts

<template>
  <div id="graphContainer"></div>
</template>

<script>
import * as echarts from 'echarts'
import {nodes, edges} from './data/data'


export default {
  name: "Index",
  data() {
    return {
      graph: undefined,
      nodes: [],
      edges: [],
      option: {}
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initGraph()
    })
  },
  methods: {

    initData() {
      this.nodes = [...nodes]
      this.edges = [...edges]
      let iconList = []
      this.nodes.forEach(item => {
        item.name = item.label
        item.symbolSize = 50
        item.icon = item.icon || 'logo.png'
        iconList.push(this.getImgData(item.icon))
      })
      this.edges.forEach(item => {
        item.name = item.label
        item.value = item.label
      })
      // Promise.all(iconList).then(imgUrls => {
      //   this.nodes.forEach((item, i) => {
      //     item.symbol = "image://" + imgUrls[i]
      //   })
      //   this.renderGraph()
      // })
      this.renderGraph()
    },

    // 初始化圖形配置
    initGraph() {
      this.graph = echarts.init(document.getElementById("graphContainer"))
      this.initData()
      this.renderGraph()
    },

    // 用數(shù)據(jù)渲染圖形
    renderGraph() {
      this.option = {
        title: {
          text: '狂飆人物關(guān)系圖',
          subtext: 'Default layout',
          top: 'bottom',
          left: 'right'
        },
        tooltip: {
          show: false
        },
        series: [
          {
            name: '狂飆人物關(guān)系圖',
            type: 'graph',
            layout: 'force',
            data: this.nodes,
            links: this.edges,
            roam: true,
            itemStyle: {},
            label: {
              show: true,
              position: 'bottom',
              formatter: '酿秸'
            },
            edgeLabel: {
              show: true,
              formatter: '{c}'
            },
            force: {
              edgeLength: [100, 200],
              repulsion: 800
            },
            emphasis: {
              focus: 'adjacency',
              lineStyle: {
                width: 10
              },
              edgeLabel: {
                color: 'red'
              }
            }
          }
        ]
      }
      this.graph.setOption(this.option)
    },

    getImgData(imgSrc) {
      let fun = function (resolve) {
        const canvas = document.createElement('canvas');
        const contex = canvas.getContext('2d');
        const img = new Image();

        img.crossOrigin = '';
        img.onload = function () {
          // 設(shè)置圖形寬高比例
          let center = {
            x: img.width / 2,
            y: img.height / 2
          };
          let diameter = img.width;
          let radius = diameter / 2; // 半徑

          canvas.width = diameter;
          canvas.height = diameter;
          contex.clearRect(0, 0, diameter, diameter);
          contex.save();

          contex.beginPath();
          contex.arc(radius, radius, radius, 0, 2 * Math.PI); // 畫出圓
          contex.clip();

          contex.drawImage(
              img,
              center.x - radius,
              center.y - radius,
              diameter,
              diameter,
              0,
              0,
              diameter,
              diameter
          ); // 在剛剛裁剪的園上畫圖
          contex.restore(); // 還原狀態(tài)

          resolve(canvas.toDataURL('image/png', 1));
        };
        img.src = imgSrc;
      };
      let promise = new Promise(fun);
      return promise;
    }
  }
}
</script>

<style scoped>
#graphContainer {
  width: 100%;
  height: 800px;
  background-color: white;
}
</style>

echarts.png

3.3. G6

<template>
  <div id="graphContainer"></div>
</template>

<script>
import G6 from '@antv/g6'
import {nodes, edges} from './data/data'

export default {
  name: "Index",
  data() {
    return {
      graph: undefined,
      nodes: [],
      edges: [],
      container: undefined,
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initGraph()
    })
  },
  methods: {

    // 初始化圖形配置
    initGraph() {
      this.container = document.getElementById("graphContainer")
      const width = this.container.scrollWidth
      const height = this.container.scrollHeight
      this.graph = new G6.Graph({
        container: 'graphContainer',
        width,
        height,
        layout: {
          type: 'force',
          preventOverlap: true,
          linkDistance: 120,         // 可選,邊長
          nodeStrength: 30,         // 可選
          edgeStrength: 0.6,        // 可選
          collideStrength: 0.2,     // 可選
          nodeSize: 120
        },
        defaultNode: {
          color: '#5B8FF9',
          size: 40,
          labelCfg: {
            position: 'bottom'
          }
        },
        modes: {
          default: ['drag-canvas'],
        },
      });
      this.graph.on('node:dragstart', e => {
        this.graph.layout();
        console.log(e)
        this.refreshDragedNodePosition(e);
      });
      this.graph.on('node:drag', e => {
        console.log(e)
        this.refreshDragedNodePosition(e);
      });
      this.graph.on('node:dragend', e => {
        e.item.get('model').fx = null;
        e.item.get('model').fy = null;
      });
      this.renderGraph()
    },

    // 用數(shù)據(jù)渲染圖形
    renderGraph() {
      this.nodes = nodes || []
      this.edges = nodes || []
      this.graph.data({
        nodes: nodes,
        edges: edges.map(function (edge, i) {
          edge.id = 'edge' + i;
          return Object.assign({}, edge);
        }),
      });
      this.graph.render();
    },

    refreshDragedNodePosition(e) {
      const model = e.item.get('model');
      model.fx = e.x;
      model.fy = e.y;
    }
  }
}
</script>

<style scoped>
#graphContainer {
  width: 100%;
  height: 800px;
  background-color: white;
}
</style>

G6.png

3. 加上人物頭像

三個組件其實展示效果都差不多魏烫,需要更好的效果還是需要后期再進(jìn)行一些配置與編碼辣苏,這里我用了echarts進(jìn)行了進(jìn)一步的配置,增加了頭像哄褒,這里獲取人物頭像這個參考了網(wǎng)絡(luò)上的稀蟋,具體的地址忘記了,如有需要聯(lián)系加上哈呐赡。


image.png

echarts中有個突出關(guān)聯(lián)節(jié)點的配置還比較實用退客,vis其實也有,g6的我是沒找到链嘀,可能是我還沒看到萌狂,看下echarts中的效果。

emphasis: {
  focus: 'adjacency',
  lineStyle: {
    width: 10
  },
  edgeLabel: {
    color: 'red'
   }
 }
emphasis.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末怀泊,一起剝皮案震驚了整個濱河市茫藏,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌霹琼,老刑警劉巖务傲,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異枣申,居然都是意外死亡售葡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進(jìn)店門忠藤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來挟伙,“玉大人,你說我怎么就攤上這事模孩∠窈” “怎么了烘豹?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長诺祸。 經(jīng)常有香客問我携悯,道長,這世上最難降的妖魔是什么筷笨? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任憔鬼,我火速辦了婚禮,結(jié)果婚禮上胃夏,老公的妹妹穿的比我還像新娘轴或。我一直安慰自己,他們只是感情好仰禀,可當(dāng)我...
    茶點故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布照雁。 她就那樣靜靜地躺著,像睡著了一般答恶。 火紅的嫁衣襯著肌膚如雪饺蚊。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天悬嗓,我揣著相機(jī)與錄音污呼,去河邊找鬼。 笑死包竹,一個胖子當(dāng)著我的面吹牛燕酷,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播周瞎,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼苗缩,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了声诸?” 一聲冷哼從身側(cè)響起挤渐,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎双絮,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體得问,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡囤攀,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了宫纬。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片焚挠。...
    茶點故事閱讀 40,498評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖漓骚,靈堂內(nèi)的尸體忽然破棺而出蝌衔,到底是詐尸還是另有隱情榛泛,我是刑警寧澤,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布噩斟,位于F島的核電站曹锨,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏剃允。R本人自食惡果不足惜沛简,卻給世界環(huán)境...
    茶點故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望斥废。 院中可真熱鬧椒楣,春花似錦、人聲如沸牡肉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽统锤。三九已至毛俏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間跪另,已是汗流浹背拧抖。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留免绿,地道東北人唧席。 一個月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像嘲驾,于是被迫代替她去往敵國和親淌哟。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,507評論 2 359

推薦閱讀更多精彩內(nèi)容