【前端学习】AntV G6-02 图元素及样式配置、状态样式和事件监听

课程链接:

AntV G6 系列课程第 3 集:图元素及样式配置、状态样式和事件监听_哔哩哔哩_bilibili

课程大纲

Node节点

图形样式属性 Shape Attr | G6 (antgroup.com)

Edge 边 03:18

Cubic | G6 (antgroup.com)

Combo 节点分组 03:51

所有图表 | G6 (antgroup.com)

StateStyles 状态样式 04:04

G6 状态管理的最佳实践 | G6 (antgroup.com)

事件监听 08:13

基础事件 Event | G6 (antgroup.com)

【扩展】

(x/y,clientX/clientY,canvasX/canvasY 三套坐标系详解见 G6 坐标系深度解析 | G6 (antgroup.com)

完整代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>03 图元素及样式配置、状态样式和事件监听</title>
        <!-- 引入 G6 -->
        <script src="https://gw.alipayobjects.com/os/lib/antv/g6/4.3.11/dist/g6.min.js"></script>
    </head>
    <body>
        <div id="container"></div>
        <script>
            const data = {
                nodes: [{
                        id: '1',
                        label: 'node-1',
                        type: 'rect',
                        x: 100,
                        y: 200,
                        size: [50, 25],
                        style: {
                            fill: '#91d5ff',
                            stroke: '#0050b3',
                        },
                        stateStyles: {
                            stateName1: {
                                fill: '#1890ff'
                            },
                            stateName2: {
                                fill: '#1890ff',
                                stroke: '#ff7875',
                                lineWidth: 3,
                                'text-shape': {
                                    fontSize: 20,
                                    fontWeight: 500
                                }
                            }
                        }
                    },
                    {
                        id: '2',
                        label: '节点2',
                        labelCfg: {
                            position: 'bottom',
                            style: {
                                fill: '#531dab',
                                fontWeight: 500,
                            }
                        },
                        stateStyles: {
                            stateName1: {
                                stroke: '#9254de',
                                lineWidth: 5
                            },
                            stateName2: {
                                fill: '#9254de',
                                'text-shape': {
                                    fontSize: 20,
                                    fill: '#f00'
                                }
                            }
                        }
                    },
                    {
                        id: '3',
                        label: 'node-3'
                    }
                ],
                edges: [{
                        source: '1',
                        target: '2',
                        style: {
                            endArrow: {
                                path: 'M 0,0 L 20,10 L 20,-10 Z',
                                fill: '#ccc',
                                stroke: '#0f0'
                            },
                            startArrow: true
                        }
                    },
                    {
                        source: '2',
                        target: '3'
                    },
                    {
                        source: '3',
                        target: '1'
                    }
                ]
            }

            const width = document.getElementById('container').scrollWidth
            const height = document.getElementById('container').scrollHeight || 450

            const graph = new G6.Graph({
                container: 'container',
                width,
                height,
                defaultNode: {
                    type: 'ellipse',
                    style: {
                        fill: '#efdbff',
                        stroke: '#531dab'
                    }
                },
                defaultEdge: {
                    // ...
                },
                nodeStateStyles:{
                    stateName3: {
                        fill: '#d9f7be',
                        stroke: '#135200'
                    },    
                    stateName4: {
                        fill: '#d9f7be',
                        stroke: '#135200',
                        'text-shape': {
                            fontWeight: 800
                        }
                    },
                },
                fitView: true,
                fitViewPadding: 100,
                // 模式
                modes: {
                    default: [{
                        type: 'drag-canvas', // 可以按这个格式写成对象的形式
                        direction: 'x'
                    }, 'drag-node', 'zoom-canvas'],
                    edit: ['drag-canvas', 'zoom-canvas', 'click-select'],
                    edit2: ['drag-node'],
                },
            })
            
            // afterrender 需要在 read 之前进行监听
            // graph.on('afterrender', e=> {
            //     alert('render done!')
            // })            

            graph.read(data)

            // mouseenter与mouseleave
            graph.on('node:mouseenter', e => {
                // 【注】在添加2种状态(stateName1与stateName3)后
                // node1、node2直接hover无反应,在click后,hover才有样式 
                // 在调换stateName1与stateName3顺序后, 直接hover node3 无反应
                graph.setItemState(e.item, 'stateName1', true)    
                graph.setItemState(e.item, 'stateName3', true)            
            })
            graph.on('node:mouseleave', e => {
                graph.setItemState(e.item, 'stateName1', false)    
                graph.setItemState(e.item, 'stateName3', false)            
            })
            
            // node click
            graph.on('node:click', e => {
                graph.setItemState(e.item, 'stateName4', true)    
                graph.setItemState(e.item, 'stateName2', true)            
            })
            
            // canvas click
            graph.on('canvas:click', e => {
                graph.getNodes().forEach(node=>{
                    graph.setItemState(node, 'stateName4', false)
                    graph.setItemState(node, 'stateName2', false)
                })
            })
            
            graph.on('text-shape:click', e => {
                alert('clicked text-shape')
            })

            graph.data(data)
            graph.render()
        </script>
    </body>
</html>



    
        
        
        
        

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于antV G6引入本地片的问题,您可以使用G6提供的image节点来插入本地片。例如: ``` import G6 from '@antv/g6'; const data = { /* 节点数据 */ nodes: [ { id: 'node', x: 100, y: 100, label: '片节点', labelCfg: { position: 'bottom' }, /* 自定义节点 */ type: 'image-node', /* 自定义节点属性 */ subtype: 'normal', /* 自定义节点样式 */ style: { width: 80, height: 80, img: 'https://gw.alipayobjects.com/mdn/rms/afts/img/A*KfYTQIobizcAAAAAAAAAAABjAQAAAQ/original', cursor: 'move', stroke: '#eee', shadowOffsetX: 0, shadowOffsetY: 0, shadowColor: 'rgba(0, 0, 0, .2)', shadowBlur: 10 } } ], /* 边数据 */ edges: [] }; G6.registerNode('image-node', { /* 绘制节点 */ draw(cfg, group) { const { style: { img, ...style } } = cfg; const keyShape = group.addShape('image', { attrs: { x: 0, y: 0, width: 80, height: 80, img } }); /* 添加锚点 */ keyShape.addAnchor([ [0.5, 0], [1, 0.5], [0.5, 1], [0, 0.5] ]); return keyShape; } }); const graph = new G6.Graph({ container: 'mountNode', width: 800, height: 600, /* 支持片节点 */ supportCSSTransform: true, /* 拓扑配置 */ layout: { type: 'dagre', rankdir: 'LR', align: 'DL', nodesep: 40, ranksep: 40 }, defaultNode: { type: 'image-node', subtype: 'normal' }, modes: { default: [ { type: 'drag-canvas', enableOptimize: true, optimizeZoom: 3 }, { type: 'zoom-canvas', enableOptimize: true, optimizeZoom: 3 }, 'click-select', { type: 'tooltip', formatText: function formatText(model) { return "<div class=\"tooltip-title\">".concat(model.label, "</div>"); }, offset: 10, shouldUpdate: function shouldUpdate(e) { return e.item.getType() !== 'edge'; } }, { type: 'brush-select' } ] }, /* 数据 */ data }); graph.render(); ``` 在节点的style中,可以通过img属性引入本地片,例如img: require('./images/icon.png')。 希望我的回答对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值