我使用sankey图表来表示一些数据,并在今天偶然发现了一个问题。
下面的代码示例包含两个数据集:未正确显示的dataJSONfull
,以及有效的子集dataJSON
(dataJSONfull
的前两个元素)。
所有代码也是available on JSFiddle。
案例一(dataJSON
)没问题:
let dataJSONfull = JSON.parse('[["10.10.90.3","10.10.81.36",5],["10.35.12.8","10.35.24.33",5],["10.40.1.254","10.40.8.151",4],["10.89.7.117","10.89.3.109",4],["10.40.0.126","10.40.8.151",4],["10.6.94.77","10.10.86.5",4],["10.81.102.133","10.10.77.21",3],["10.81.102.133","10.10.86.32",3],["10.81.102.133","10.20.3.91",3],["10.81.102.133","10.85.75.250",3],["10.81.102.133","10.91.114.78",2],["10.10.66.1","10.10.82.0",3],["10.40.15.254","10.40.8.151",2],["10.40.8.175","10.120.0.150",2],["10.40.8.175","10.40.1.15",2],["10.40.8.175","10.40.8.151",2],["10.24.137.61","10.10.77.21",2],["10.24.137.61","10.10.85.1",2],["10.10.68.56","10.10.68.56",2],["10.10.84.3","10.10.86.5",2],["10.10.84.3","10.10.85.1",1],["10.10.86.5","10.10.86.5",2],["10.20.3.91","10.20.3.91",2],["172.16.15.150","172.16.15.150",2],["10.120.0.254","10.40.8.151",1],["10.2.0.1","10.2.0.71",1],["10.40.8.151","10.40.8.151",1],["10.81.99.19","10.91.114.78",1]]')
let dataJSON = JSON.parse('[["10.10.90.3","10.10.81.36",5],["10.35.12.8","10.35.24.33",5]]')
//console.log(dataJSONfull, dataJSON)
Highcharts.chart('container', {
title: {
text: 'Highcharts Sankey Diagram'
},
series: [{
keys: ['from', 'to', 'weight'],
data: dataJSON,
type: 'sankey',
name: 'Sankey demo series'
}]
});
#container {
min-width: 300px;
max-width: 800px;
height: 400px;
margin: 1em auto;
border: 1px solid silver;
}
#csv {
display: none;
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/sankey.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container"></div>
案例2(dataJSONfull
)因控制台错误而中断:
VM141 sankey.js:22 Uncaught TypeError: Cannot read property 'options' of undefined
at q.<anonymous> (VM141 sankey.js:22)
at Array.forEach (<anonymous>)
at q.createNodeColumns (VM141 sankey.js:21)
at q.translate (VM141 sankey.js:28)
at VM140 highcharts.js:289
at Array.forEach (<anonymous>)
at a.Chart.renderSeries (VM140 highcharts.js:289)
at a.Chart.render (VM140 highcharts.js:291)
at a.Chart.firstRender (VM140 highcharts.js:294)
at a.Chart.<anonymous> (VM140 highcharts.js:268)
let dataJSONfull = JSON.parse('[["10.10.90.3","10.10.81.36",5],["10.35.12.8","10.35.24.33",5],["10.40.1.254","10.40.8.151",4],["10.89.7.117","10.89.3.109",4],["10.40.0.126","10.40.8.151",4],["10.6.94.77","10.10.86.5",4],["10.81.102.133","10.10.77.21",3],["10.81.102.133","10.10.86.32",3],["10.81.102.133","10.20.3.91",3],["10.81.102.133","10.85.75.250",3],["10.81.102.133","10.91.114.78",2],["10.10.66.1","10.10.82.0",3],["10.40.15.254","10.40.8.151",2],["10.40.8.175","10.120.0.150",2],["10.40.8.175","10.40.1.15",2],["10.40.8.175","10.40.8.151",2],["10.24.137.61","10.10.77.21",2],["10.24.137.61","10.10.85.1",2],["10.10.68.56","10.10.68.56",2],["10.10.84.3","10.10.86.5",2],["10.10.84.3","10.10.85.1",1],["10.10.86.5","10.10.86.5",2],["10.20.3.91","10.20.3.91",2],["172.16.15.150","172.16.15.150",2],["10.120.0.254","10.40.8.151",1],["10.2.0.1","10.2.0.71",1],["10.40.8.151","10.40.8.151",1],["10.81.99.19","10.91.114.78",1]]')
let dataJSON = JSON.parse('[["10.10.90.3","10.10.81.36",5],["10.35.12.8","10.35.24.33",5]]')
//console.log(dataJSONfull, dataJSON)
Highcharts.chart('container', {
title: {
text: 'Highcharts Sankey Diagram'
},
series: [{
keys: ['from', 'to', 'weight'],
data: dataJSONfull,
type: 'sankey',
name: 'Sankey demo series'
}]
});
#container {
min-width: 300px;
max-width: 800px;
height: 400px;
margin: 1em auto;
border: 1px solid silver;
}
#csv {
display: none;
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/sankey.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container"></div>
代码中唯一的区别是设置大小,但我不相信这可能是错误(更不用说代码昨天使用更大的集合)。
dataJSON
和dataJSONfull
都被正确解析为一个Object(所以JSON很好)
您的大型数据集失败,因为两行是自引用的。特别
["10.10.68.56", "10.10.68.56", 2],
["172.16.15.150", "172.16.15.150", 2],
点from
和to
自己。如果你删除它们,你将得到一个工作代码:
let dataJSONfull = JSON.parse('[["10.10.90.3","10.10.81.36",5],["10.35.12.8","10.35.24.33",5],["10.40.1.254","10.40.8.151",4],["10.89.7.117","10.89.3.109",4],["10.40.0.126","10.40.8.151",4],["10.6.94.77","10.10.86.5",4],["10.81.102.133","10.10.77.21",3],["10.81.102.133","10.10.86.32",3],["10.81.102.133","10.20.3.91",3],["10.81.102.133","10.85.75.250",3],["10.81.102.133","10.91.114.78",2],["10.10.66.1","10.10.82.0",3],["10.40.15.254","10.40.8.151",2],["10.40.8.175","10.120.0.150",2],["10.40.8.175","10.40.1.15",2],["10.40.8.175","10.40.8.151",2],["10.24.137.61","10.10.77.21",2],["10.24.137.61","10.10.85.1",2],["10.10.84.3","10.10.86.5",2],["10.10.84.3","10.10.85.1",1],["10.10.86.5","10.10.86.5",2],["10.20.3.91","10.20.3.91",2],["10.120.0.254","10.40.8.151",1],["10.2.0.1","10.2.0.71",1],["10.40.8.151","10.40.8.151",1],["10.81.99.19","10.91.114.78",1]]')
let dataJSON = JSON.parse('[["10.10.90.3","10.10.81.36",5],["10.35.12.8","10.35.24.33",5]]')
//console.log(dataJSONfull, dataJSON)
Highcharts.chart('container', {
title: {
text: 'Highcharts Sankey Diagram'
},
series: [{
keys: ['from', 'to', 'weight'],
data: dataJSONfull,
type: 'sankey',
name: 'Sankey demo series'
}]
});
#container {
min-width: 300px;
max-width: 800px;
height: 400px;
margin: 1em auto;
border: 1px solid silver;
}
#csv {
display: none;
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/sankey.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container"></div>
在github中,highcharts团队表示目前不支持循环引用:https://github.com/highcharts/highcharts/issues/8218