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 的配置,添加交互信号和条件样式,使得当鼠标悬停在节点上时,与其相连的链接能够突出显示,从而增强数据的可探索性和可视化效果。

实现节点悬停高亮链接

以下步骤将指导你如何在现有的 Vega 力导向图配置中添加节点悬停高亮链接的功能。

1. 定义交互信号 (Signals)

首先,我们需要定义一个信号来跟踪当前鼠标悬停的节点。该信号将存储悬停节点的 name 属性,并在鼠标移出节点时重置为 null。

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

这段代码定义了一个名为 active 的信号。它监听 symbol:mouseover 事件,当鼠标悬停在节点上时,将节点的 name 属性更新到 active 信号中。同时,它还监听 mouseover[!event.item] 事件,该事件在鼠标移出节点时触发,将 active 信号重置为 null。

2. 修改节点样式 (Marks – nodes)

为了在视觉上突出显示悬停的节点,我们可以修改节点的填充颜色。当节点的 name 属性与 active 信号的值相等时,将节点填充为黄色,否则使用默认的颜色。

"encode": {  "enter": {"stroke": {"value": "white"}},  "update": {    "fill": {      "signal": "datum.name==active?'yellow': scale('color', datum.group)"    },    "size": {"signal": "2 * nodeRadius * nodeRadius"},    "cursor": {"value": "pointer"}  }}

这段代码修改了节点 (nodes) 的 update 编码。fill 属性现在使用一个条件表达式。如果节点的 name 属性等于 active 信号的值,则填充颜色为黄色 (‘yellow’);否则,使用 scale(‘color’, datum.group) 根据节点所属的组设置颜色。

3. 修改链接样式 (Marks – path)

接下来,我们需要修改链接的样式,以便在节点悬停时突出显示与其相连的链接。我们可以通过检查链接的 source 或 target 是否与 active 信号的值相等来实现。

{  "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"    }  ]}

这段代码定义了链接 (path) 的样式。目前,链接的颜色 (stroke) 设置为 #ccc,宽度 (strokeWidth) 设置为 0.5。要实现高亮效果,我们需要修改 update 编码,添加条件判断。由于原始问题和答案中未提供高亮链接的具体实现,以下提供一种修改思路,但需要根据实际数据结构调整:

{  "type": "path",  "from": {"data": "link-data"},  "interactive": false,  "encode": {    "update": {      "stroke": [        {          "test": "active && (datum.source.name === active || datum.target.name === active)",          "value": "red"        },        {"value": "#ccc"}      ],      "strokeWidth": [        {          "test": "active && (datum.source.name === active || datum.target.name === active)",          "value": 2        },        {"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 和 strokeWidth 的条件判断。如果 active 信号有值(即鼠标悬停在节点上),并且链接的 source.name 或 target.name 等于 active 信号的值,则将链接的颜色设置为红色 (“red”),宽度设置为 2。否则,使用默认的颜色 #ccc 和宽度 0.5。 注意:请根据你的数据结构调整 datum.source.name 和 datum.target.name 的访问方式。

4. 完整示例

将以上代码片段添加到你的 Vega 配置中,即可实现节点悬停高亮链接的效果。完整的 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": [            {              "test": "active && (datum.source.name === active || datum.target.name === active)",              "value": "red"            },            {"value": "#ccc"}          ],          "strokeWidth": [            {              "test": "active && (datum.source.name === active || datum.target.name === active)",              "value": 2            },            {"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"        }      ]    }  ]}

注意事项

数据结构: 请务必根据你的实际数据结构调整 datum.source.name 和 datum.target.name 的访问方式。颜色和宽度: 可以根据需要自定义高亮链接的颜色和宽度。性能: 对于大型图,高亮链接可能会影响性能。可以考虑使用更高效的条件判断或限制高亮链接的数量。

总结

通过定义交互信号和修改节点及链接的样式,我们成功实现了 Vega 力导向图中节点悬停高亮链接的功能。这可以帮助用户更好地理解和探索图数据,提高可视化的交互性和可读性。记住根据你的数据结构和需求调整代码,并注意潜在的性能问题。

以上就是使用 Vega 实现节点悬停高亮特定链接的详细内容,更多请关注创想鸟其它相关文章!

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

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
在JavaScript中,如何实现基于角色的访问控制(RBAC)?
上一篇 2025年12月20日 17:51:02
如何构建一个实时数据仪表盘(Dashboard)?
下一篇 2025年12月20日 17:51:10

相关推荐

发表回复

登录后才能评论
关注微信