我使用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
都正确地解析为对象(因此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