使用antv/g6 實現(xiàn)企業(yè)圖譜

ANTV/G6

使用D3搞出了企業(yè)圖譜蝌以,那就趁熱打鐵 再研究研究@antv/g6

類企業(yè)圖譜

1. 安裝 g6

yarn add @antv/g6
cnpm i @antv/g6 -S

2. 引用 d3.js

import G6 from '@antv/g6'

3. 代碼

// 折疊圖標
const COLLAPSE_ICON = function COLLAPSE_ICON(x, y, r) {
    return [
        ['M', x - r, y - r],
        ['a', r, r, 0, 1, 0, r * 2, 0],
        ['a', r, r, 0, 1, 0, -r * 2, 0],
        ['M', x + 2 - r, y - r],
        ['L', x + r - 2, y - r],
    ]
}
// 展開圖標
const EXPAND_ICON = function EXPAND_ICON(x, y, r) {
    return [
        ['M', x - r, y - r],
        ['a', r, r, 0, 1, 0, r * 2, 0],
        ['a', r, r, 0, 1, 0, -r * 2, 0],
        ['M', x + 2 - r, y - r],
        ['L', x + r - 2, y - r],
        ['M', x, y - 2 * r + 2],
        ['L', x, y - 2],
    ]
}
// 自定義連線
G6.registerEdge(
    'line-arrow',
    {
        draw(cfg, group) {
            const { startPoint, endPoint } = cfg
            const keyShape = group.addShape('path', {
                attrs: {
                    path: [
                        ['M', startPoint.x, startPoint.y],
                        ['L', endPoint.x / 3 + (2 / 3) * startPoint.x, startPoint.y], // 三分之一處
                        ['L', endPoint.x / 3 + (2 / 3) * startPoint.x, endPoint.y], // 三分之二處
                        ['L', endPoint.x, endPoint.y],
                    ],
                    stroke: cfg.target._cfg.model.fill || cfg.target._cfg.model.stroke,
                    opacity: 0.3,
                },
                name: 'path-shape',
            })
            return keyShape
        },
    },
    'cubic-horizontal'
)
// 自定義節(jié)點
G6.registerNode(
    'tree-node',
    {
        drawShape: function drawShape(cfg, group) {
            // 邊框樣式設(shè)置
            let rect = group.addShape('rect', {
                attrs: {
                    fill: cfg.fill || '#fff', // 背景色
                    stroke: cfg.fill ? '#fff' : '#40a9ff', // 邊框色
                    opacity: cfg.fill && cfg.depth ? 0.15 : 1, // 透明度
                },
                name: 'rect-shape',
            })
            if (cfg.depth == 2) {
                rect = group.addShape('rect', {
                    attrs: {
                        fill: '#fff', // 背景色
                        stroke: cfg.stroke, // 邊框色
                        opacity: 0.5, // 透明度
                    },
                    name: 'rect-shape',
                })
            }
            if (cfg.depth == 1) {
                console.log(group)
            }
            const content = cfg.name.replace(/(.{19})/g, '$1\n')
            // 字體樣式設(shè)置
            const text = group.addShape('text', {
                attrs: {
                    text: content,
                    x: 0,
                    y: 0,
                    textAlign: 'left',
                    textBaseline: 'middle',
                    fill: cfg.depth ? '#000' : '#fff', // 字體顏色
                },
                name: 'rect-shape',
            })
            const bbox = text.getBBox()
            const hasChildren = cfg.children && cfg.children.length > 0
            // 第一級元素 增加數(shù)量顯示
            if (cfg.depth == 1) {
                // 目前寫死距離
                const leftData = {
                    '2': 4,
                    '4': 16,
                    '7': 34,
                }
                group.addShape('text', {
                    attrs: {
                        text: 20,
                        x: leftData[content.length],
                        y: 20,
                        textAlign: 'left',
                        textBaseline: 'middle',
                        fill: '#000', // 字體顏色
                    },
                    name: 'rect-shape',
                })
                if (hasChildren) {
                    group.addShape('marker', {
                        attrs: {
                            x: cfg.x > 0 ? bbox.maxX + 14 : -16,
                            y: bbox.minX + bbox.height + 2,
                            r: 6,
                            symbol: cfg.collapsed ? EXPAND_ICON : COLLAPSE_ICON,
                            stroke: cfg.fill,
                            lineWidth: 1,
                        },
                        name: 'collapse-icon',
                    })
                }
                rect.attr({
                    x: hasChildren && cfg.x < 0 ? bbox.minX - 32 : bbox.minX - 16,
                    y: bbox.minY - 16,
                    width: hasChildren ? bbox.width + 48 : bbox.width + 32,
                    height: bbox.height + 48,
                    radius: 4,
                })
            } else {
                // 無子數(shù)據(jù) 返回樣式
                if (!hasChildren) {
                    rect.attr({
                        x: bbox.minX - 8,
                        y: bbox.minY - 6,
                        width: bbox.width + 16,
                        height: bbox.height + 12,
                        radius: 4,
                    })
                } else {
                    // 有子數(shù)據(jù) 非根元素
                    if (cfg.depth) {
                        rect.attr({
                            x: cfg.x > 0 ? bbox.minX - 8 : bbox.minX - 24,
                            y: bbox.minY - 6,
                            width: cfg.x > 0 ? bbox.width + 32 : bbox.width + 30,
                            height: bbox.height + 12,
                            radius: 4,
                        })
                    } else {
                        rect.attr({
                            x: bbox.minX - 16,
                            y: bbox.minY - 16,
                            width: bbox.width + 32,
                            height: bbox.height + 32,
                            radius: 4,
                        })
                    }
                }
            }
            return rect
        },
    },
    'single-node'
)
// 繪制大小
const width = document.getElementById('mountNode').scrollWidth
const height = document.body.clientHeight
// 工具欄
const toolbar = new G6.ToolBar()
// mini圖
// const minimap = new G6.Minimap()
// 基礎(chǔ)配置
const graph = new G6.TreeGraph({
    container: 'mountNode',
    width,
    height,
    modes: {
        default: [
            {
                type: 'collapse-expand',
                onChange: function onChange(item, collapsed) {
                    const data = item.get('model')
                    const icon = item.get('group').find(element => element.get('name') === 'collapse-icon')
                    if (collapsed) {
                        icon.attr('symbol', EXPAND_ICON)
                    } else {
                        icon.attr('symbol', COLLAPSE_ICON)
                    }
                    data.collapsed = collapsed
                    return true
                },
            },
            'drag-canvas',
            'zoom-canvas',
        ],
    },
    defaultNode: {
        type: 'tree-node',
    },
    defaultEdge: {
        // cubic-horizontal 曲線 polyline 直線 line 默認線
        type: 'line-arrow',
        // style: {
        //     stroke: '#40a9ff',
        // },
    },
    nodeStateStyles: {
        hover: {
            lineWidth: 1,
        },
    },
    layout: {
        type: 'compactBox',
        direction: 'H',
        getId: function getId(d) {
            return d.id
        },
        getHeight: function getHeight() {
            return 30
        },
        getWidth: function getWidth() {
            return 30
        },
        getVGap: function getVGap() {
            return 30
        },
        getHGap: function getHGap() {
            return 80
        },
    },
    plugins: [toolbar],
})
// 渲染
graph.data(Data)
graph.render()
graph.fitView()
// 事件監(jiān)聽
graph.on('node:mouseenter', evt => {
    const { item } = evt
    if (item._cfg.model.depth == 1) {
        item._cfg.keyShape.attrs.opacity = 0.3
    }
    if (item._cfg.model.depth == 2) {
        item._cfg.keyShape.attrs.opacity = 1
    }
    graph.setItemState(item, 'hover', true)
})
graph.on('node:mouseleave', evt => {
    const { item } = evt
    if (item._cfg.model.depth == 1) {
        item._cfg.keyShape.attrs.opacity = 0.15
    }
    if (item._cfg.model.depth == 2) {
        item._cfg.keyShape.attrs.opacity = 0.3
    }
    graph.setItemState(item, 'hover', false)
})

反正先保存下耻陕,在慢慢琢磨吧。今天不想搞了 彪标。。掷豺。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末捞烟,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子当船,更是在濱河造成了極大的恐慌题画,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件德频,死亡現(xiàn)場離奇詭異苍息,居然都是意外死亡,警方通過查閱死者的電腦和手機壹置,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門竞思,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人钞护,你說我怎么就攤上這事盖喷。” “怎么了难咕?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵课梳,是天一觀的道長。 經(jīng)常有香客問我步藕,道長惦界,這世上最難降的妖魔是什么挑格? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任咙冗,我火速辦了婚禮,結(jié)果婚禮上漂彤,老公的妹妹穿的比我還像新娘雾消。我一直安慰自己灾搏,他們只是感情好,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布立润。 她就那樣靜靜地躺著狂窑,像睡著了一般。 火紅的嫁衣襯著肌膚如雪桑腮。 梳的紋絲不亂的頭發(fā)上泉哈,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天,我揣著相機與錄音破讨,去河邊找鬼丛晦。 笑死,一個胖子當著我的面吹牛提陶,可吹牛的內(nèi)容都是我干的烫沙。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼隙笆,長吁一口氣:“原來是場噩夢啊……” “哼锌蓄!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起撑柔,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤瘸爽,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后铅忿,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蝶糯,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年辆沦,在試婚紗的時候發(fā)現(xiàn)自己被綠了昼捍。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡肢扯,死狀恐怖妒茬,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蔚晨,我是刑警寧澤乍钻,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站铭腕,受9級特大地震影響银择,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜累舷,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一浩考、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧被盈,春花似錦析孽、人聲如沸搭伤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽怜俐。三九已至,卻和暖如春邓尤,著一層夾襖步出監(jiān)牢的瞬間拍鲤,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工汞扎, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留殿漠,地道東北人。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓佩捞,卻偏偏與公主長得像绞幌,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子一忱,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354