最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3中如何使用d3.js繪制流程圖(TS語法)

 更新時間:2023年10月17日 11:23:05   作者:是淺笑耶  
這篇文章主要給大家介紹了關(guān)于vue3中如何使用d3.js繪制流程圖的相關(guān)資料,D3.js是由javaScript語言編寫繪圖庫,其原理是通過調(diào)用一系列內(nèi)置函數(shù),生成SVG,并在網(wǎng)頁渲染,需要的朋友可以參考下

先放效果圖:

1.安裝dagre-d3和d3:

npm install d3
npm install dagre-d3

2.在組件中導(dǎo)入并使用d3和dagre-d3:

<script>
    import * as d3 from 'd3';
    import dagreD3 from 'dagre-d3';
</script>

3.在模板中創(chuàng)建節(jié)點(diǎn)元素:

<template>
    <div class="top_model">
        <div class="flow_chart" ref="flowchartContainer"></div>
    </div>
</template>

4.在setup中定義所需數(shù)據(jù):

setup()  {
    //創(chuàng)建一個ref引用對象,它用于引用以上綁定ref為flowchartContainer的DOM元素
    const flowchartContainer = ref(null);
    const taskLogData = reactive({
        dataSource: [] as any,
        //流程圖數(shù)據(jù)
        list: {
          nodeInfos: [] as any,
          edges: [] as any,
        },
    })
}

5.進(jìn)行繪制:

//mounted生命周期鉤子函數(shù)是在組件實(shí)例掛載到 DOM 后調(diào)用的,
//在這個時候可以獲取到組件的根元素,并且可以執(zhí)行相應(yīng)的操作
//因此將繪制代碼放在這里執(zhí)行
onMounted(async () => {
    //調(diào)用了接口getListData,需要從其中取出數(shù)據(jù),因此需要執(zhí)行異步方法async/await
    await getListData();
    //nextTick函數(shù)確保了在DOM更新之后再執(zhí)行相應(yīng)的操作,
    //避免了由于異步更新導(dǎo)致的狀態(tài)不一致問題
    nextTick(() => {
        //使用dagreD3庫來創(chuàng)建一個有向無環(huán)圖的圖形
        var g = new dagreD3.graphlib.Graph().setGraph({
          rankdir: 'LR', // 指定布局方向?yàn)閺淖蟮接?
          nodesep: 200, // 設(shè)置節(jié)點(diǎn)間距
          ranksep: 250, //垂直間距
        });
        //添加節(jié)點(diǎn)
        taskLogData.list.nodeInfos.forEach(
          (
            item: {
              id: string;
              label: any;
              tooltip: any;
              tipone: any;
              tiptow: any;
              tipthree: any;
              color: any;
            },
            index: any,
          ) => {
            //設(shè)置圖形中指定節(jié)點(diǎn)的屬性
            g.setNode(item.id, {
              label: item.label,//節(jié)點(diǎn)內(nèi)容
              //自定義屬性,調(diào)整樣式使其成為節(jié)點(diǎn)備注
              tooltip: item.tooltip,//節(jié)點(diǎn)備注(對應(yīng)圖片中的節(jié)點(diǎn)下的名稱,開始-結(jié)束)
              tipone: item.tipone,//節(jié)點(diǎn)備注1(對應(yīng)圖片中的時間)
              tiptow: item.tiptow,//節(jié)點(diǎn)備注2(對應(yīng)圖片中的操作人員)
              tipthree: item.tipthree,//節(jié)點(diǎn)備注3(對應(yīng)圖片中的藍(lán)色備注)
              style: `fill: ${item.color}`,//節(jié)點(diǎn)填充顏色,item.color為變量
              shape: 'circle',//節(jié)點(diǎn)形狀設(shè)置為圓形
              class: 'node',//設(shè)置節(jié)點(diǎn)類名
              rank: 0, // 設(shè)置節(jié)點(diǎn)的rank屬性為0,表示在同一水平排列
            });
          },
        );
        //添加節(jié)點(diǎn)關(guān)系
        taskLogData.list.edges.forEach(
          (item: { source: string; target: string; edgeColor: string }) => {
            //創(chuàng)建并設(shè)置圖形中兩個節(jié)點(diǎn)之間的邊(Edge)
            g.setEdge(item.source, item.target, {
              // 設(shè)置邊的樣式
              style: 'stroke: ' + item.edgeColor + '; 
              fill: none; stroke-width: 2px;',
              arrowheadStyle: 'fill: none;', // 設(shè)置箭頭樣式為無箭頭
            });
          },
        );
        //創(chuàng)建一個SVG元素作為繪圖容器,
        //并將其添加到flowchartContainer.value所引用的DOM元素中
        const svg = d3
          .select(flowchartContainer.value)
          //在選定的DOM元素內(nèi)添加一個SVG元素
          .append('svg')
          //設(shè)置SVG元素的寬度與高度屬性
          .attr('width', '')
          .attr('height', 240);
        // 創(chuàng)建渲染器
        const render = new dagreD3.render();
        // 執(zhí)行渲染
        render(svg as any, g as any);
        // 添加節(jié)點(diǎn)備注
        //獲取并遍歷類名為node的元素
        svg.selectAll('.node').each((nodeId, index) => {
          // 獲取節(jié)點(diǎn)的備注信息
          const tooltipText = g.node(nodeId as any).tooltip;
          const tipone = g.node(nodeId as any).tipone;
          const tiptow = g.node(nodeId as any).tiptow;
          const tipthree = g.node(nodeId as any).tipthree;
          //獲取節(jié)點(diǎn)對象
          const node = d3.select(flowchartContainer.value);
          // 獲取元素的位置
          const bbox = g.node(nodeId as any);
          // 在節(jié)點(diǎn)下方添加備注文本
          const remarkText = (node as any)
            .append('text')
            .attr('class', 'node-remark')
            .text(tooltipText);
          const remarkTextone = (node as any)
            .append('text')
            .attr('class', 'node-remark')
            .text(tipone);
          const remarkTexttow = (node as any)
            .append('text')
            .attr('class', 'node-remark')
            .text(tiptow);
          const remarkTextthree = (node as any)
            .append('text')
            .attr('class', 'node-remark')
            .text(tipthree)
            .attr('class', 'remarkLast')
            .attr('id', 'remarkLast' + nodeId);
          //添加氣泡彈窗
          const remarkTextFour = (node as any)
            .append('div')
            .attr('class', 'remarkFlow')
            .attr('id', 'remarkFlow' + nodeId)
            .text(tipthree);
          // 調(diào)整備注位置
          remarkText
            .style('position', 'absolute')
            .style('top', `${bbox.y + 60}px`)
            .style('left', `${bbox.x + 30}px`);
          remarkTextone
            .style('position', 'absolute')
            .style('top', `${bbox.y + 80}px`)
            .style('left', `${bbox.x + 30}px`);
          remarkTexttow
            .style('position', 'absolute')
            .style('top', `${bbox.y + 100}px`)
            .style('max-width', '130px')
            .style('left', `${bbox.x + 30}px`);
          remarkTextthree
            .style('position', 'absolute')
            .style('top', `${bbox.y + 130}px`)
            .style('left', `${bbox.x + 30}px`);
          remarkTextFour
            .style('position', 'absolute')
            .style('top', `${bbox.y + 60}px`)
            .style('left', `${bbox.x + 30}px`);
          //鼠標(biāo)懸停效果
          (document.getElementById('remarkLast' + nodeId) as any).onmouseover
            = function () {
               (document.getElementById('remarkFlow' + nodeId) as any)
               .style.display = 'block';
           };
          (document.getElementById('remarkLast' + nodeId) as any).onmouseout
            = function () {
                (document.getElementById('remarkFlow' + nodeId) as any)
                .style.display = 'none';
           };
        });
      });
});

6.對接口數(shù)據(jù)進(jìn)行處理:

// 獲取流程圖數(shù)據(jù)
    const getListData = async () => {
      const res: any = await getTaskLogs();
      console.log('res', res);
      if (res.status_code == '0000') {
        const nodeList= res.data;
        // 打印數(shù)組中的節(jié)點(diǎn)
        console.log('節(jié)點(diǎn):', nodeList);
        for (var i = 0; i < nodeList.length; i++) {
          //默認(rèn)節(jié)點(diǎn)連線顏色為綠色
          let edgeColor = '#52c41a';
          //當(dāng)前節(jié)點(diǎn)之后的節(jié)點(diǎn)都設(shè)為灰色
          for (var j = i - 1; j > 0; j--) {
            if (nodeList[j].isCurNode == true) {
              taskLogData.list.nodeInfos[i].color = '#d9d9d9';
              edgeColor = '#d9d9d9';
            }
          }
          //當(dāng)前節(jié)點(diǎn)設(shè)為藍(lán)色
          if (nodeList[i].isCurNode == true) {
            taskLogData.list.nodeInfos[i].color = '#1890ff';
            edgeColor = '#1890ff';
          }
          //節(jié)點(diǎn)之間的連線
          if (i > 0) {
            taskLogData.list.edges.push({
              source: nodeList[i - 1].nodeId,
              target: nodeList[i].nodeId,
              edgeColor: edgeColor,
              style: 'stroke-solid',
            });
          }
        }
      } else {
        message.error(res.reason);
      }
    };

總結(jié) 

到此這篇關(guān)于vue3中如何使用d3.js繪制流程圖的文章就介紹到這了,更多相關(guān)vue3用d3.js繪制流程圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue3.0組件通信mitt源碼ts實(shí)例解析

    Vue3.0組件通信mitt源碼ts實(shí)例解析

    這篇文章主要為大家介紹了Vue3.0組件通信mitt源碼ts實(shí)例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • 關(guān)于vue2使用swiper4的踩坑記錄

    關(guān)于vue2使用swiper4的踩坑記錄

    最近寫vue的一個練手項(xiàng)目需要在里面實(shí)現(xiàn)一個輪播圖,想到去用第三方插件,百度了一輪,發(fā)現(xiàn)大部分都是swiper這個插件,這篇文章主要給大家介紹了關(guān)于vue2使用swiper4踩坑的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Vue.js 使用AntV X6的示例步驟

    Vue.js 使用AntV X6的示例步驟

    X6 是 AntV 旗下的圖編輯引擎,提供了一系列開箱即用的交互組件和簡單易用的節(jié)點(diǎn)定制能力,方便我們快速搭建流程圖、DAG 圖、ER 圖等圖應(yīng)用。接下來就看看vue如何使用它
    2021-05-05
  • Ant design vue table 單擊行選中 勾選checkbox教程

    Ant design vue table 單擊行選中 勾選checkbox教程

    這篇文章主要介紹了Ant design vue table 單擊行選中 勾選checkbox教程,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • element中el-table中的el-input校驗(yàn)的實(shí)現(xiàn)

    element中el-table中的el-input校驗(yàn)的實(shí)現(xiàn)

    本文主要介紹了element中el-table中的el-input校驗(yàn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 少女風(fēng)vue組件庫的制作全過程

    少女風(fēng)vue組件庫的制作全過程

    這篇文章主要給大家介紹了關(guān)于少女風(fēng)vue組件庫的制作全過程,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Vue transition實(shí)現(xiàn)點(diǎn)贊動畫效果的示例

    Vue transition實(shí)現(xiàn)點(diǎn)贊動畫效果的示例

    點(diǎn)贊動畫是網(wǎng)頁評論中常見的功能,本文將介紹如何用vue實(shí)現(xiàn)這一效果。點(diǎn)贊時愛心縮小變大,變大時略微大一點(diǎn)再變正常,取消點(diǎn)贊時愛心無動畫,同時數(shù)字滾動,+1 時向上滾動,-1 時向下滾動
    2021-05-05
  • 搭建element-ui的Vue前端工程操作實(shí)例

    搭建element-ui的Vue前端工程操作實(shí)例

    下面小編就為大家分享一篇搭建element-ui的Vue前端工程操作實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • vue表單中遍歷表單操作按鈕的顯示隱藏示例

    vue表單中遍歷表單操作按鈕的顯示隱藏示例

    今天小編就為大家分享一篇vue表單中遍歷表單操作按鈕的顯示隱藏示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • vue3解決各場景l(fā)oading過度的五種方法

    vue3解決各場景l(fā)oading過度的五種方法

    這篇文章主要為大家詳細(xì)介紹了vue3中解決各場景l(fā)oading過度的五種方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以學(xué)習(xí)一下
    2023-11-11

最新評論

兖州市| 罗江县| 平和县| 英德市| 陇西县| 新巴尔虎左旗| 涿鹿县| 图木舒克市| 玛纳斯县| 大关县| 卫辉市| 汾阳市| 梁山县| 泰兴市| 察哈| 襄城县| 历史| 宝鸡市| 雅安市| 墨江| 金乡县| 江阴市| 九龙县| 手游| 汨罗市| 亚东县| 柏乡县| 方山县| 龙岩市| 桦甸市| 睢宁县| 仙居县| 会昌县| 武陟县| 固原市| 班玛县| 东丰县| 淮南市| 尤溪县| 安吉县| 丹东市|