首页 > web前端 > js教程 > 正文

使用 Vega 实现节点悬停高亮链接效果

碧海醫心
发布: 2025-10-02 18:47:18
原创
335人浏览过

使用 vega 实现节点悬停高亮链接效果

本文档将指导你如何在 Vega 可视化库中实现节点悬停时高亮显示相关链接的功能。通过监听鼠标事件,动态更新节点的颜色和链接的样式,从而增强交互性和信息展示效果。本文将提供详细的代码示例和步骤说明,帮助你快速掌握该技巧。

实现步骤

要在 Vega 中实现节点悬停高亮链接的效果,主要需要以下几个步骤:

  1. 定义信号 (Signals): 创建用于存储当前激活节点信息的信号。
  2. 配置节点 (Nodes) 样式: 根据激活节点信号的值,动态改变节点的填充颜色。
  3. 配置链接 (Links) 样式: 根据激活节点信号的值,动态改变链接的颜色。

详细代码示例

以下是一个完整的 Vega 规范示例,展示了如何实现节点悬停高亮链接的效果。

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "description": "A node-link diagram with force-directed layout, depicting character co-occurrence in the novel Les Misérables.",
  "width": 700,
  "height": 500,
  "padding": 0,
  "autosize": "none",
  "signals": [
    {"name": "cx", "update": "width / 2"},
    {"name": "cy", "update": "height / 2"},
    {
      "name": "nodeRadius",
      "value": 8,
      "bind": {"input": "range", "min": 1, "max": 50, "step": 1}
    },
    {
      "name": "nodeCharge",
      "value": -30,
      "bind": {"input": "range", "min": -100, "max": 10, "step": 1}
    },
    {
      "name": "linkDistance",
      "value": 30,
      "bind": {"input": "range", "min": 5, "max": 100, "step": 1}
    },
    {"name": "static", "value": true, "bind": {"input": "checkbox"}},
    {
      "description": "State variable for active node fix status.",
      "name": "fix",
      "value": false,
      "on": [
        {
          "events": "symbol:mouseout[!event.buttons], window:mouseup",
          "update": "false"
        },
        {"events": "symbol:mouseover", "update": "fix || true"},
        {
          "events": "[symbol:mousedown, window:mouseup] > window:mousemove!",
          "update": "xy()",
          "force": true
        }
      ]
    },
    {
      "description": "Graph node most recently interacted with.",
      "name": "node",
      "value": null,
      "on": [
        {"events": "symbol:mouseover", "update": "fix === true ? item() : node"}
      ]
    },
    {
      "description": "Flag to restart Force simulation upon data changes.",
      "name": "restart",
      "value": false,
      "on": [{"events": {"signal": "fix"}, "update": "fix && fix.length"}]
    },
    {
      "name": "active",
      "value": null,
      "on": [
        {"events": "symbol:mouseover", "update": "datum.name"},
        {"events": "mouseover[!event.item]", "update": "null"}
      ]
    }
  ],
  "data": [
    {
      "name": "node-data",
      "url": "data/miserables.json",
      "format": {"type": "json", "property": "nodes"}
    },
    {
      "name": "link-data",
      "url": "data/miserables.json",
      "format": {"type": "json", "property": "links"}
    }
  ],
  "scales": [
    {
      "name": "color",
      "type": "ordinal",
      "domain": {"data": "node-data", "field": "group"},
      "range": {"scheme": "category20c"}
    }
  ],
  "marks": [
    {
      "name": "nodes",
      "type": "symbol",
      "zindex": 1,
      "from": {"data": "node-data"},
      "on": [
        {
          "trigger": "fix",
          "modify": "node",
          "values": "fix === true ? {fx: node.x, fy: node.y} : {fx: fix[0], fy: fix[1]}"
        },
        {"trigger": "!fix", "modify": "node", "values": "{fx: null, fy: null}"}
      ],
      "encode": {
        "enter": {"stroke": {"value": "white"}},
        "update": {
          "fill": {
            "signal": "datum.name==active?'yellow': scale('color', datum.group)"
          },
          "size": {"signal": "2 * nodeRadius * nodeRadius"},
          "cursor": {"value": "pointer"}
        }
      },
      "transform": [
        {
          "type": "force",
          "iterations": 300,
          "restart": {"signal": "restart"},
          "static": {"signal": "static"},
          "signal": "force",
          "forces": [
            {"force": "center", "x": {"signal": "cx"}, "y": {"signal": "cy"}},
            {"force": "collide", "radius": {"signal": "nodeRadius"}},
            {"force": "nbody", "strength": {"signal": "nodeCharge"}},
            {
              "force": "link",
              "links": "link-data",
              "distance": {"signal": "linkDistance"}
            }
          ]
        }
      ]
    },
    {
      "type": "path",
      "from": {"data": "link-data"},
      "interactive": false,
      "encode": {
        "update": {"stroke": {"value": "#ccc"}, "strokeWidth": {"value": 0.5}}
      },
      "transform": [
        {
          "type": "linkpath",
          "require": {"signal": "force"},
          "shape": "line",
          "sourceX": "datum.source.x",
          "sourceY": "datum.source.y",
          "targetX": "datum.target.x",
          "targetY": "datum.target.y"
        }
      ]
    }
  ]
}
登录后复制

代码解释:

火龙果写作
火龙果写作

用火龙果,轻松写作,通过校对、改写、扩展等功能实现高质量内容生产。

火龙果写作 106
查看详情 火龙果写作
  • Signals: active 信号用于存储当前鼠标悬停的节点名称。当鼠标悬停在节点上时,active 信号更新为节点的 datum.name;当鼠标移开节点时,active 信号更新为 null。
  • Nodes - encode - update - fill: 节点的填充颜色根据 active 信号的值动态变化。如果节点的 datum.name 等于 active 信号的值,则填充颜色为 yellow,否则使用颜色比例尺 color 计算出的颜色。
  • Links - encode - update - stroke: 链接的颜色始终保持为 "#ccc",没有实现高亮效果。如果需要高亮链接,需要根据 active 信号的值,动态改变链接的颜色。例如,可以添加一个条件判断,如果链接的源节点或目标节点等于 active 信号的值,则将链接的颜色设置为其他颜色。

高亮链接的修改示例

要实现链接高亮,需要修改 path mark 的 encode 部分。以下是一个修改后的示例:

{
  "type": "path",
  "from": {"data": "link-data"},
  "interactive": false,
  "encode": {
    "update": {
      "stroke": [
        {
          "test": "datum.source.name === active || datum.target.name === active",
          "value": "red"
        },
        {"value": "#ccc"}
      ],
      "strokeWidth": {"value": 0.5}
    }
  },
  "transform": [
    {
      "type": "linkpath",
      "require": {"signal": "force"},
      "shape": "line",
      "sourceX": "datum.source.x",
      "sourceY": "datum.source.y",
      "targetX": "datum.target.x",
      "targetY": "datum.target.y"
    }
  ]
}
登录后复制

修改解释:

  • Links - encode - update - stroke: 链接的颜色现在根据 active 信号的值动态变化。如果链接的源节点 (datum.source.name) 或目标节点 (datum.target.name) 等于 active 信号的值,则链接的颜色设置为 red,否则保持为 #ccc。

注意事项

  • 确保你的数据包含 nodes 和 links 两个数据集,并且 links 数据集中的 source 和 target 字段指向 nodes 数据集中的相应节点。
  • 根据你的实际需求调整颜色、线条粗细等样式属性。
  • 可以根据需要添加更多的交互效果,例如改变节点的大小、显示节点的详细信息等。

总结

通过以上步骤,你可以在 Vega 中实现节点悬停高亮链接的效果。这种交互方式可以帮助用户更好地理解节点之间的关系,提高数据可视化的交互性和可读性。 记得根据你的具体数据和需求调整代码,以达到最佳的可视化效果。

以上就是使用 Vega 实现节点悬停高亮链接效果的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号