如何与JavaScript切换HighCharts Sankey链接
如何切换可见的Sankey链接(以及节点和标签)? 我希望用户通过单击节点来逐步扩展节点。完全扩展时,该图将具有Ca 5级别和CA 10列。第一个负载上的默认视图显示约6列和4个级别。
- 我可以操纵数据并将重量设置为0,以隐藏所有3个(节点,链接,标签)。
- 3个工作?
- 我可以在节点上添加一个className并使用“显示:无”(对所有 -Chart-toggle-visibily of节点“>有关组织图表的问题。我可以适应Sankey的解决方案吗?
我尝试了第一个选项。它运行良好 - 隐藏所有3个(节点,链接,标签)。但是,为了使其正常工作,我需要每次查询数据库,然后重新绘制(重新加载)图表。我正在做API电话并操纵JSON。不是太多的API呼叫还是有更好的方法可以这样做?
与第三次相比,第二个选项看起来更清楚,因为我比HighCharts.js了解更多的CSS。 (尽管我倾向于避免级联样式:)
第三选项似乎很乏味,因为我需要找出这些库的工作方式。但是也许这是一个很好的解决方案,那么我对投资时间毫无疑问。不过,给定的例子不是很好。
避免开发和自定义,功能,导出。,响应式设计等问题的最佳解决方案是什么?
这是所需视图的小提琴: link
var nodes = [{
id: 'Col_1-Row_1',
column: 1
}, ],
data0 = [
// default - data is set to 0 - link is hidden
['Col_0-Row_0', 'Col_1-Row_0', 0],
['Col_0-Row_1', 'Col_1-Row_0', 0],
// data to display
['Col_1-Row_0', 'Col_2-Row_0', 153],
['Col_1-Row_1', 'Col_2-Row_0', 91],
['Col_1-Row_1', 'Col_2-Row_0', 221],
['Col_1-Row_1', 'Col_2-Row_0', 200],
// circulra link
['Col_2-Row_0', 'Col_1-Row_1', 10],
],
data1 = [
['Col_0-Row_0', 'Col_1-Row_0', 0.1],
['Col_0-Row_1', 'Col_1-Row_0', 152.9],
['Col_1-Row_0', 'Col_2-Row_0', 153],
['Col_1-Row_1', 'Col_2-Row_0', 91],
['Col_1-Row_1', 'Col_2-Row_0', 221],
['Col_1-Row_1', 'Col_2-Row_0', 200],
['Col_2-Row_0', 'Col_1-Row_1', 10],
]
const chart = Highcharts.chart('container', {
chart: {
height: (9 / 16 * 80) + "%",
marginBottom: 60, // display circulra link
marginRight: 60 // display circulra link
},
title: {
text: 'Sankey toggle link visibilty'
},
plotOptions: {
series: {
animation: false,
minLinkWidth: 1,
nodePadding: 50,
colors: ['#0dcaf0'],
clip: false, // display circulra link
dataLabels: {
enabled: true,
// set data label position
align: "left",
verticalAlign: "top",
y: -20,
x: -5,
// show data labels taht overlap
allowOverlap: true,
padding: 0,
// handle data labels that flow outside the plot area
overflow: "allow",
crop: false,
style: {
fontSize: "12px",
fontFamily: "Arial, sans-serif",
color: "black",
},
},
},
},
series: [{
data: data0,
nodes: nodes,
type: 'sankey',
}, {
data: data1,
nodes: nodes,
visible: false,
type: 'sankey',
}]
});
#container {
height: 90hw;
margin: 1em auto;
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/sankey.src.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<button id="btn_show" class="autocompare">expand</button>
<button id="btn_hide" class="autocompare">hide</button>
<div id="container"></div>
<script>
document.getElementById('btn_show').addEventListener('click', e => {
seriesHide();
seriesShow(1);
});
document.getElementById('btn_hide').addEventListener('click', e => {
seriesHide();
seriesShow(0);
});
function seriesHide() {
chart.series.forEach(ser => ser.hide())
}
function seriesShow(i) {
var series = chart.series[i];
series.show();
}
</script>
How can I toggle visible the Sankey link (together with the node and label)?
I want users to expand nodes step by step by clicking on the node. The diagram would have ca 5 levels and ca 10 columns when fully expanded. Default view on the first load is showing about 6 columns and 4 levels.
- I could manipulate the data and set the weight to 0 to hide all 3 of them (node,link,label).
- I could add a className to the node and use 'display:none' (Would it work for all 3 of them?)
- Here is a similar question about organization chart. Can I adapt the solution to Sankey?
I've tried out the 1st option. It works well - hiding all 3 of them(node,link,label). Though, to make it to work, I need to query the database every time and redraw (reload) the chart. I'm doing API call and manipulating the json. Isn't it too many API calls or there is a better way to do it?
The 2nd option looks more clear compared to the 3rd, because I know more CSS than highcharts.js. (Though I tend to avoid the cascading styles:)
The 3rd option seems tedious, because I need to find out how these libraries are working. But maybe it's a good solution, then I have no doubts about investing time. Though, the given example was not very well working.
What would be the best solution to avoid problems with dev and customisation, functionalities, export.js, responsive design, etc?
Here is the fiddle of the desired view : link
var nodes = [{
id: 'Col_1-Row_1',
column: 1
}, ],
data0 = [
// default - data is set to 0 - link is hidden
['Col_0-Row_0', 'Col_1-Row_0', 0],
['Col_0-Row_1', 'Col_1-Row_0', 0],
// data to display
['Col_1-Row_0', 'Col_2-Row_0', 153],
['Col_1-Row_1', 'Col_2-Row_0', 91],
['Col_1-Row_1', 'Col_2-Row_0', 221],
['Col_1-Row_1', 'Col_2-Row_0', 200],
// circulra link
['Col_2-Row_0', 'Col_1-Row_1', 10],
],
data1 = [
['Col_0-Row_0', 'Col_1-Row_0', 0.1],
['Col_0-Row_1', 'Col_1-Row_0', 152.9],
['Col_1-Row_0', 'Col_2-Row_0', 153],
['Col_1-Row_1', 'Col_2-Row_0', 91],
['Col_1-Row_1', 'Col_2-Row_0', 221],
['Col_1-Row_1', 'Col_2-Row_0', 200],
['Col_2-Row_0', 'Col_1-Row_1', 10],
]
const chart = Highcharts.chart('container', {
chart: {
height: (9 / 16 * 80) + "%",
marginBottom: 60, // display circulra link
marginRight: 60 // display circulra link
},
title: {
text: 'Sankey toggle link visibilty'
},
plotOptions: {
series: {
animation: false,
minLinkWidth: 1,
nodePadding: 50,
colors: ['#0dcaf0'],
clip: false, // display circulra link
dataLabels: {
enabled: true,
// set data label position
align: "left",
verticalAlign: "top",
y: -20,
x: -5,
// show data labels taht overlap
allowOverlap: true,
padding: 0,
// handle data labels that flow outside the plot area
overflow: "allow",
crop: false,
style: {
fontSize: "12px",
fontFamily: "Arial, sans-serif",
color: "black",
},
},
},
},
series: [{
data: data0,
nodes: nodes,
type: 'sankey',
}, {
data: data1,
nodes: nodes,
visible: false,
type: 'sankey',
}]
});
#container {
height: 90hw;
margin: 1em auto;
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/sankey.src.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<button id="btn_show" class="autocompare">expand</button>
<button id="btn_hide" class="autocompare">hide</button>
<div id="container"></div>
<script>
document.getElementById('btn_show').addEventListener('click', e => {
seriesHide();
seriesShow(1);
});
document.getElementById('btn_hide').addEventListener('click', e => {
seriesHide();
seriesShow(0);
});
function seriesHide() {
chart.series.forEach(ser => ser.hide())
}
function seriesShow(i) {
var series = chart.series[i];
series.show();
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我根据So的帖子准备了一个演示,隐藏的分支似乎是一个很好的方法。
: httpps://jsfiddle.net.net/blacklabel/blacklabel/blacklabel/blacklabel/sm01jtof/1/
演示
使用 update 方法。
演示:
I prepared a demo based on a post from SO, hiding branches seems to be a good approach.
Demo: https://jsfiddle.net/BlackLabel/sm01jtof/1/
EDIT
Updated demo to show/hide nodes in sankey graph using update method.
Demo: https://jsfiddle.net/BlackLabel/80ruyxdq/4/