Google图表sankey-“在行中找到循环”错误

问题描述

我正在使用Google图表-Sankey Diagram来显示一段时间内同一类别A,B,C,D中的流量。 Photo attached.

我想删除月份和年份,例如我的节点标签中的“(2019年12月)”,“(2020年4月)”,因此标签仅读取“ A”,“ B”,“ C”,“ D”,并且月/年打印在图形下方对于Sankey的每个级别。我无法执行此操作,因为当我从标签中删除月份/年份时,出现“在行中找到循环”错误。

  1. 如何制作具有相同节点标签但没有出现此错误的图?
  2. 如何为Sankey的每个级别(即月/年)添加图例?

非常感谢。

代码:

<html>
<body>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="sankey_multiple" style="width: 900px; height: 300px;"></div>
<script type="text/javascript">
  google.charts.load('current',{'packages':['sankey']});
  google.charts.setOnLoadCallback(drawChart);
   
   function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('string','From');
    data.addColumn('string','To');
    data.addColumn('number','Weight');
    data.addRows([
       [ 'A (Aug 2019)','A (Dec 2019)',83.94 ],[ 'A (Aug 2019)','C (Dec 2019)',6.94 ],'B (Dec 2019)',6.11  ],'D (Dec 2019)',3.01 ],[ 'C (Aug 2019)',1.29  ],83.30 ],9.87  ],5.54  ],[ 'B (Aug 2019)',1.23 ],13.55 ],78.79 ],6.42 ],[ 'D (Aug 2019)',0.36 ],2.08 ],1.90 ],95.67 ],[ 'A (Dec 2019)','A (Apr 2020)',38.3 ],'C (Apr 2020)',21.19 ],'B (Apr 2020)',9.82 ],'D (Apr 2020)',30.69 ],[ 'C (Dec 2019)',4 ],55.9 ],6.23 ],33.87 ],[ 'B (Dec 2019)',3.53 ],18.82 ],20.31 ],57.34 ],[ 'D (Dec 2019)',0.73 ],3 ],1.65 ],94.61 ],[ 'A (Apr 2020)','A (Aug 2020)',37.38 ],'C (Aug 2020)',28.90 ],'B (Aug 2020)',22.01 ],'D (Aug 2020)',11.71 ],[ 'C (Apr 2020)',4.26 ],68.97 ],14.35 ],12.41 ],[ 'B (Apr 2020)',3.78 ],27.26 ],49.72 ],18.74 ],[ 'D (Apr 2020)',0.98 ],10.15 ],10.87 ],78 ]
    ]);
    
    var colors = ['#355C7D','#355C7D','#6C5B7B','#C06C84','#F8B195','#F8B195'];

    var options = {
      height: 400,sankey: {
        node: {
          colors: colors,label: { bold: true}
        },link: {
          colors: colors,colorMode: 'gradient',iterations: 0
        }
      }
    };

    // Instantiate and draw our chart,passing in some options.
    var chart = new google.visualization.Sankey(document.getElementById('sankey_multiple'));
    chart.draw(data,options);
   }
   
</script>
</body>
</html>

解决方法

那是因为去掉月份标签后两边都有重复的分类,即自链接。

例如在删除月份标签后,您不希望 A 链接到您的案例中的 A

Here's a line from the docs

注意:避免数据中的循环:如果 A 链接到自身,或者链接到 B 链接到 C 链接到 A,您的图表将不会呈现。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...