Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
如何在 Vega 中实现节点悬停高亮链接效果_创想鸟

如何在 Vega 中实现节点悬停高亮链接效果

如何在 vega 中实现节点悬停高亮链接效果

本文旨在指导如何在 Vega 可视化库中实现节点悬停时高亮显示相关链接的功能。通过修改 Vega 的信号和 Marks 属性,我们可以动态改变链接的样式,使其在鼠标悬停于节点上时突出显示,从而增强交互性和数据可读性。本文将提供详细的配置步骤和示例代码,帮助您快速实现这一效果。

要在 Vega 中实现节点悬停时高亮显示链接的效果,主要涉及到以下几个步骤:定义信号(Signals)来追踪鼠标悬停状态,以及修改 Marks 的编码(Encode)属性来根据悬停状态改变链接的样式。

1. 定义信号(Signals)

首先,需要在 Vega 规范中定义一个信号,用于追踪当前鼠标悬停的节点。这个信号会在鼠标悬停在节点上时更新为节点的标识符,而在鼠标移开节点时重置为 null。

{  "name": "active",  "value": null,  "on": [    {"events": "symbol:mouseover", "update": "datum.name"},    {"events": "mouseover[!event.item]", "update": "null"}  ]}

这段代码定义了一个名为 active 的信号。

name: 信号的名称,这里是 “active”。value: 信号的初始值,设置为 null。on: 定义了信号更新的触发条件。{“events”: “symbol:mouseover”, “update”: “datum.name”}:当鼠标悬停在 symbol(即节点)上时,将 active 信号更新为当前节点的数据项的 name 属性。{“events”: “mouseover[!event.item]”, “update”: “null”}:当鼠标移开节点时,将 active 信号重置为 null。[!event.item] 表示鼠标移开的是当前节点。

2. 修改 Marks 编码(Encode)

接下来,需要修改链接(path)的 Marks 编码属性,使其根据 active 信号的值来改变链接的样式。

{  "type": "path",  "from": {"data": "link-data"},  "interactive": false,  "encode": {    "update": {      "stroke": [        {          "test": "datum.source.name === active || datum.target.name === active",          "value": "firebrick"        },        {"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"    }  ]}

这段代码修改了链接的 stroke 属性。

stroke: 定义了链接的颜色。{“test”: “datum.source.name === active || datum.target.name === active”, “value”: “firebrick”}:如果链接的源节点或目标节点的 name 属性与 active 信号的值相等(即鼠标悬停在与该链接相关的节点上),则将链接的颜色设置为 firebrick(红色)。{“value”: “#ccc”}:否则,将链接的颜色设置为 #ccc(浅灰色)。

3. 完整示例

下面是一个完整的 Vega 规范示例,展示了如何实现节点悬停高亮链接的效果。 该示例基于力导向图布局,并使用 miserables.json 数据集。

{  "$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": [            {              "test": "datum.source.name === active || datum.target.name === active",              "value": "firebrick"            },            {"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"        }      ]    }  ]}

注意事项:

确保 Vega 的版本在 5.0 或以上,因为某些功能可能在旧版本中不可用。根据实际数据结构,调整 datum.source.name 和 datum.target.name 中的属性名称,以正确匹配节点标识符。可以根据需要自定义高亮显示的颜色和样式。

总结:

通过定义信号来追踪鼠标悬停状态,并修改 Marks 的编码属性,可以轻松实现在 Vega 中节点悬停高亮链接的效果。这种交互方式能够显著提升数据可视化的可读性和用户体验。

以上就是如何在 Vega 中实现节点悬停高亮链接效果的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1525698.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
如何在点击按钮后重新执行脚本
上一篇 2025年12月20日 17:53:26
如何用Canvas API实现一个复杂的数据可视化图表?
下一篇 2025年12月20日 17:53:40

相关推荐

发表回复

登录后才能评论
关注微信