使用vis-network时,在同一节点中设置多种样式的办法

复制代码
var svg1 =
        '<svg xmlns="http://www.w3.org/2000/svg" width="390px" height="200px">' +
        '' +
        '<foreignObject x="0" y="0" width="100%" height="100%">' +
        '<div xmlns="http://www.w3.org/1999/xhtml" style="font-size:40px;border-radius: 25px;border: 2px solid #0066ff;  background: #D2E5FF;box-sizing: border-box;  padding: 20px; margin:5px; width:380px; height:190px" >' +

        '<span>' + item.name2.substring(0, 3) + '</span><span align="right" style="color:grey;float: right;">'+item.amount+"元"+'</span><br/>' +
        '<div style="font-size:20px;height:50%; padding: 70px 0px 10px 0px;text-align: left;">' +
        " " + item.account2 + "</div>" +
        "</div>" +
        "</foreignObject>" +
        "</svg>";

var url1 = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(svg1);
复制代码

指定一个svg1,之后添加node节点时只需要

this.node.push({
        id: item.account2,
image: {
          unselected: url1,
          selected: url2,
        }, 
     shape:
"image" })

设置image属性,同时指定shape属性就可以改变节点的样式。

我这里用了unselected属性和selected属性,可以指派选中时的样式和非选中时节点的样式,这样操作需要再另外设定一个svg2

复制代码
var svg2 =
        '<svg xmlns="http://www.w3.org/2000/svg" width="390px" height="200px">' +
        '' +
        '<foreignObject x="0" y="0" width="100%" height="100%">' +
        '<div xmlns="http://www.w3.org/1999/xhtml" style="font-size:40px;border-radius: 25px;border: 2px solid #0066ff;  background: #0066ff;box-sizing: border-box;  padding: 20px; margin:5px; width:380px; height:190px" >' +

        '<span style="color:white">' + item.name2.substring(0, 3) + '</span><span align="right" style="color:white;float: right;">' + item.amount + "元" + '</span><br/>' +
        '<div style="font-size:20px;height:50%; padding: 70px 0px 10px 0px;text-align: left;color:black">' +
        " " + item.account2 + "</div>" +
        "</div>" +
        "</foreignObject>" +
        "</svg>";


var url2 = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(svg2);
复制代码

如果需要修改节点的值和样式,可以修改svg里面的代码,写法类似于html,可以通过内联样式修改样式,插入html元素设置新元素,改的时候要注意引号,最后需要注意生成的svg为一张图片,在节点中插入的是一张图片,设置了img之后不需要再设置label属性,否则label属性中展示的值会被顶到节点外面。

最后差不多能酱紫

 

posted @   北火廿口  阅读(873)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示