You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Vis.js Network节点自定义:卡片式节点实现技术咨询

实现卡片式结构化节点网络的方案(Vis.js及替代库)

嘿,你完全可以用Vis.js Network实现这种带文本区+控制按钮的卡片式节点,不用急着换库!下面给你详细讲实现方法,以及备选方案:

一、用Vis.js实现的具体步骤

Vis.js其实支持通过HTML自定义节点内容,只是需要开启对应的配置项,具体操作如下:

1. 开启HTML支持与基础配置

首先在网络初始化时,开启节点的HTML允许权限,并设置合适的节点形状(box最适合卡片布局):

// 假设container是页面上的DOM元素
const network = new vis.Network(container, data, {
  nodes: {
    shape: 'box',
    allowHTML: true, // 核心:允许节点标签使用HTML结构
    shapeProperties: {
      useImageSize: false, // 让节点尺寸自动适配HTML内容
      borderRadius: 8 // 可选:给卡片加圆角,提升美观度
    },
    widthConstraint: { minimum: 200 }, // 可选:设置节点最小宽度
    heightConstraint: { minimum: 120 }, // 可选:设置节点最小高度
    color: { background: '#ffffff', border: '#cccccc' } // 卡片基础样式
  },
  edges: {
    color: '#999999' // 连接线样式,可按需调整
  }
});

2. 编写结构化的HTML节点标签

在定义节点数据时,把label字段写成包含文本区控制区的HTML结构,按钮可以用内联SVG或者字体图标(示例用内联SVG避免依赖外部资源):

const nodes = new vis.DataSet([
  {
    id: 1,
    label: `
      <div style="padding: 12px; font-family: Arial, sans-serif;">
        <!-- 多行文本展示区 -->
        <div style="margin-bottom: 16px; line-height: 1.7;">
          <div><strong>姓名:</strong> 张三</div>
          <div><strong>电话:</strong> 138xxxx1234</div>
          <div><strong>地址:</strong> 北京市朝阳区xxx街道xxx号</div>
        </div>
        <!-- 控制按钮区 -->
        <div style="display: flex; gap: 10px; justify-content: flex-end;">
          <button onclick="handleMaximize(${1})" 
                  style="border: none; background: #f5f5f5; padding: 6px 10px; border-radius: 4px; cursor: pointer;">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#666" stroke-width="2">
              <path d="M18 3v4M6 3v4M3 6h4M3 18h4M18 21v-4M6 21v-4M21 18h-4M21 6h-4"/>
            </svg>
          </button>
          <button onclick="setAsMain(${1})" 
                  style="border: none; background: #2196F3; color: white; padding: 6px 10px; border-radius: 4px; cursor: pointer;">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2">
              <path d="M12 2L15.09 8.26L22 9.27L17 14.14L18.18 21.02L12 17.77L5.82 21.02L7 14.14L2 9.27L8.91 8.26L12 2Z"/>
            </svg>
          </button>
        </div>
      </div>
    `
  },
  // 可以添加更多节点...
]);

// edges数据按需定义
const edges = new vis.DataSet([
  { from: 1, to: 2 }
]);

const data = { nodes, edges };

3. 实现按钮交互逻辑

接下来写控制按钮对应的功能,比如最大化节点、设为主节点:

// 最大化/还原节点尺寸
function handleMaximize(nodeId) {
  const targetNode = nodes.get(nodeId);
  // 切换节点的最小尺寸,实现放大/缩小效果
  if (targetNode.widthConstraint?.minimum === 200) {
    nodes.update({ 
      id: nodeId, 
      widthConstraint: { minimum: 320 }, 
      heightConstraint: { minimum: 180 } 
    });
  } else {
    nodes.update({ 
      id: nodeId, 
      widthConstraint: { minimum: 200 }, 
      heightConstraint: { minimum: 120 } 
    });
  }
}

// 设置为主节点(通过背景色标记)
function setAsMain(nodeId) {
  // 先清除所有节点的主标记样式
  nodes.forEach(node => {
    if (node.color?.background === '#E3F2FD') {
      nodes.update({ id: node.id, color: { background: '#ffffff' } });
    }
  });
  // 给当前节点设置主节点样式
  nodes.update({ id: nodeId, color: { background: '#E3F2FD' } });
}

二、如果Vis.js不够满足需求,可考虑这些替代库

如果需要更极致的自定义能力,或者处理大规模数据的性能需求,推荐这几个库:

  • D3.js:完全自由的可视化工具,能从零构建任何样式的节点和网络布局,适合高度定制化场景,但学习成本相对较高。
  • Cytoscape.js:专注于图形网络的专业库,支持用CSS和HTML自定义节点外观,内置多种成熟的布局算法,API设计更贴合图形操作场景。
  • Sigma.js:轻量高效的网络可视化库,性能优异,适合处理大规模节点数据,同时支持自定义节点渲染逻辑。

内容的提问来源于stack exchange,提问作者EdKa

火山引擎 最新活动