结合Bubble和Sankey Diagram Google Charts

时间:2014-03-16 14:37:14

标签: javascript google-visualization bubble-chart sankey-diagram

我正在尝试将Google Chart中的Bubble Chart和Sankey Diagram元素组合在一起,但我很难让它们在页面上显示。 (这是来自Google的示例代码)。当我为气泡图或仅仅是Sankey图分离代码时,它们可以工作 - 但是将它们放在一起,没有任何反应。我确保设置唯一的变量名称,不确定我还缺少什么。此外,我确实没有编码经验,只是熟悉Python,所以我可能完全过分简化了这一点。谢谢你的帮助!

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.load("visualization", "1.1", {packages:["sankey"]});

      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.arrayToDataTable([
          ['ID', 'Life Expectancy', 'Fertility Rate', 'Region',     'Population'],
          ['CAN',    80.66,              1.67,      'North America',  33739900],
          ['DEU',    79.84,              1.36,      'Europe',         81902307],
          ['DNK',    78.6,               1.84,      'Europe',         5523095],
          ['EGY',    72.73,              2.78,      'Middle East',    79716203],
          ['GBR',    80.05,              2,         'Europe',         61801570],
          ['IRN',    72.49,              1.7,       'Middle East',    73137148],
          ['IRQ',    68.09,              4.77,      'Middle East',    31090763],
          ['ISR',    81.55,              2.96,      'Middle East',    7485600],
          ['RUS',    68.6,               1.54,      'Europe',         141850000],
          ['USA',    78.09,              2.05,      'North America',  307007000]
        ]);

        var data2 = new google.visualization.DataTable();
          data2.addColumn('string', 'From');
          data2.addColumn('string', 'To');
          data2.addColumn('number', 'Weight');
          data2.addRows([
           [ 'A', 'X', 5 ],
           [ 'A', 'Y', 7 ],
           [ 'A', 'Z', 6 ],
           [ 'B', 'X', 2 ],
           [ 'B', 'Y', 9 ],
           [ 'B', 'Z', 4 ],
        ]); 

        var options = {
          title: 'Correlation between life expectancy, fertility rate and population of some world countries (2010)',
          hAxis: {title: 'Life Expectancy'},
          vAxis: {title: 'Fertility Rate'},
          bubble: {textStyle: {fontSize: 11}}
        };

        var options2 = {
          width: 600,
        };

        var chart = new google.visualization.BubbleChart(document.getElementById('chart_div'));
        chart.draw(data, options);

        var chart2 = new google.visualization.Sankey(document.getElementById('sankey_basic'));
        chart2.draw(data2, options2);
      }
    </script>
  </head>
  <body>
    <div id="sankey_basic" style="width: 900px; height: 300px;"></div>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

使用此代码错误

Uncaught TypeError: undefined is not a function 

报告行:

var chart2 = new google.visualization.Sankey(document.getElementById('sankey_basic'));

错误与未加载包sankey的错误相同。

更改行:

  google.load("visualization", "1.1", {packages:["sankey"]});

  google.load("visualization", "1", {packages:["sankey"]});

加载两个图表。

Google sankey docs显示版本1,一些示例使用版本1.1,这令人困惑。