如何覆盖 Hihgchart.js Network Graph 默认节点悬停效果?
How to override Hihgchart.js Network Graph default node hover effect?
我需要覆盖 Highchart.js 中网络图上的默认节点悬停效果。
默认行为是,当您将鼠标悬停在节点上时,linkedTo 和 linkedFrom 节点会突出显示,所需的行为是当我将鼠标悬停在节点上时,只有 linkedFrom 节点会突出显示,基本上就像广度优先搜索可视化,我已成功编写算法,但突出显示了一些额外的节点。
这是我用来突出显示所有节点的算法,但这不会覆盖默认功能
point: {
events: {
mouseOver: function () {
var point = this,
chart = this.series.chart,
nodes = chart.series[0].nodes;
bfs(this.id);
function bfs(start) {
const queue = [findNodeById(start)];
// Store visited nodes in a set
const visited = new Set();
// Loop until we have something in the queue
while (queue.length > 0) {
// Pop out first element from queue
const topNode = queue.shift();
// Edges TO first element
const prevEdges = topNode.linksTo;
for (const edge of prevEdges) {
// For each edge find their corresponding nodes and set their state to 'hover'
let prevNode = findNodeById(edge.from);
prevNode.setState("hover");
// If edge is not visited yet, push to Set and add to queue
if (!visited.has(prevNode)) {
visited.add(prevNode);
queue.push(prevNode);
}
// nextNode.setState('inactive')
}
}
}
function findNodeById(id) {
return nodes.filter((node) => node.id == id)[0];
}
},
},
},
我试过 disable/enable 悬停状态,但没成功。我的方法在这里可能是完全错误的,欢迎任何建议!
最简单的解决方案是覆盖默认的 setState
函数,示例:
(function(H) {
H.seriesTypes.networkgraph.prototype.pointClass.prototype.setState = function(state) {
var args = arguments,
Point = H.Point,
others = this.isNode ? this.linksTo.concat(this.linksFrom) : [this.fromNode,
this.toNode
];
if (state !== 'select') {
others.forEach(function(linkOrNode) {
if (linkOrNode && linkOrNode.series) {
Point.prototype.setState.apply(linkOrNode, args);
if (!linkOrNode.isNode) {
if (linkOrNode.fromNode.graphic) {
Point.prototype.setState.apply(linkOrNode.fromNode, args);
}
/* Modification - prevent hover effect on toNode
if (linkOrNode.toNode && linkOrNode.toNode.graphic) {
Point.prototype.setState.apply(linkOrNode.toNode, args);
}
*/
}
}
});
}
Point.prototype.setState.apply(this, args);
}
}(Highcharts));
现场演示: https://jsfiddle.net/BlackLabel/1039zwbt/1/
文档: https://www.highcharts.com/docs/extending-highcharts/extending-highcharts
我需要覆盖 Highchart.js 中网络图上的默认节点悬停效果。 默认行为是,当您将鼠标悬停在节点上时,linkedTo 和 linkedFrom 节点会突出显示,所需的行为是当我将鼠标悬停在节点上时,只有 linkedFrom 节点会突出显示,基本上就像广度优先搜索可视化,我已成功编写算法,但突出显示了一些额外的节点。
这是我用来突出显示所有节点的算法,但这不会覆盖默认功能
point: {
events: {
mouseOver: function () {
var point = this,
chart = this.series.chart,
nodes = chart.series[0].nodes;
bfs(this.id);
function bfs(start) {
const queue = [findNodeById(start)];
// Store visited nodes in a set
const visited = new Set();
// Loop until we have something in the queue
while (queue.length > 0) {
// Pop out first element from queue
const topNode = queue.shift();
// Edges TO first element
const prevEdges = topNode.linksTo;
for (const edge of prevEdges) {
// For each edge find their corresponding nodes and set their state to 'hover'
let prevNode = findNodeById(edge.from);
prevNode.setState("hover");
// If edge is not visited yet, push to Set and add to queue
if (!visited.has(prevNode)) {
visited.add(prevNode);
queue.push(prevNode);
}
// nextNode.setState('inactive')
}
}
}
function findNodeById(id) {
return nodes.filter((node) => node.id == id)[0];
}
},
},
},
我试过 disable/enable 悬停状态,但没成功。我的方法在这里可能是完全错误的,欢迎任何建议!
最简单的解决方案是覆盖默认的 setState
函数,示例:
(function(H) {
H.seriesTypes.networkgraph.prototype.pointClass.prototype.setState = function(state) {
var args = arguments,
Point = H.Point,
others = this.isNode ? this.linksTo.concat(this.linksFrom) : [this.fromNode,
this.toNode
];
if (state !== 'select') {
others.forEach(function(linkOrNode) {
if (linkOrNode && linkOrNode.series) {
Point.prototype.setState.apply(linkOrNode, args);
if (!linkOrNode.isNode) {
if (linkOrNode.fromNode.graphic) {
Point.prototype.setState.apply(linkOrNode.fromNode, args);
}
/* Modification - prevent hover effect on toNode
if (linkOrNode.toNode && linkOrNode.toNode.graphic) {
Point.prototype.setState.apply(linkOrNode.toNode, args);
}
*/
}
}
});
}
Point.prototype.setState.apply(this, args);
}
}(Highcharts));
现场演示: https://jsfiddle.net/BlackLabel/1039zwbt/1/
文档: https://www.highcharts.com/docs/extending-highcharts/extending-highcharts