英文:
How to hover over a node to highlight specific links in Vega
问题
我是VEGA的新手,正在使用开发人员提供的这个示例来绘制网络。然而,我尝试在悬停在节点上时突出显示链接。
我尝试按照边捆绑示例来实现这一点。然后,在信号和标记中:
信号:
"signals": [ { "name": "colorIn", "value": "firebrick" },
{ "name": "colorOut", "value": "forestgreen" },
{ "name": "originX", "update": "width / 2" },
{ "name": "originY", "update": "height / 2" },
{
"name": "active", "value": null,
"on": [
{ "events": "symbol:mouseover", "update": "datum.id" },
{ "events": "mouseover[!event.item]", "update": "null" }
]
}
标记->"path"->"encode"->"update"
"stroke": [
{"test": "datum.source === active", "signal": "colorOut"},
{"test": "datum.target === active", "signal": "colorIn"},
{"value": "steelblue"}
],
英文:
I'm new in VEGA and I am using this example provided by developers to plot a network. However, I'm trying to highlight the links when I hover into nodes.
I have tried to follow the edge bundling example to achieve that. Then, into the signals and Marks:
Signals:
"signals": [ { "name": "colorIn", "value": "firebrick" },
{ "name": "colorOut", "value": "forestgreen" },
{ "name": "originX", "update": "width / 2" },
{ "name": "originY", "update": "height / 2" },
{
"name": "active", "value": null,
"on": [
{ "events": "symbol:mouseover", "update": "datum.id" },
{ "events": "mouseover[!event.item]", "update": "null" }
]
}
Marks->"path"->"encode"->"update"
"stroke": [
{"test": "datum.source === active", "signal": "colorOut"},
{"test": "datum.target === active", "signal": "colorIn"},
{"value": "steelblue"}
],
答案1
得分: 1
这将活动节点突出显示为黄色。
{
"$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"
}
]
}
]
}
英文:
This highlights the active node as yellow.
{
"$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"
}
]
}
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论