如何在桑基图中添加百分比?

问题描述 投票:0回答:1

我想创建一个基于谷歌表格条目的桑基图。 在谷歌表格中,您会发现一列包含源,另一列包含目标,第三列显示值。

这是电子表格的屏幕截图: enter image description here

我编写了一个脚本来创建桑基图,显示从源到目的地的依赖关系。

在桑基图中,有几个节点(彩色条)。桑基图的脚本如下所示:

代码.gs

function onOpen() {
    SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
        .createMenu('Zusatzfunktionen')
        .addItem('SankeyDiagram', 'openDialog')
        .addToUi();
}

function getSpreadsheetData() {
    // ID of your Document, take from URL
    var ssID = "",
        // which Sheet? [0] is the first and so on...
        sheet = SpreadsheetApp.openById(ssID).getSheets()[0],
        data = sheet.getDataRange().getValues();
    return data;
}

function openDialog() {
    var html = HtmlService.createHtmlOutputFromFile('index')
        .setHeight(300)
        .setWidth(1000);
    SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
        .showModalDialog(html, 'Sankey Diagram');
}

index.html:

<!DOCTYPE html>
<html>
   <head>
      <base target="_top">
      <script src="https://www.google.com/jsapi"></script>
   </head>
   <body>
      <div id="main"></div>
      <script type="text/javascript">
         google.load('visualization', '1', {
           packages: ['corechart', 'sankey']
         });      google.setOnLoadCallback(initialize);
           
           function initialize() {
            google.script.run.withSuccessHandler(drawChart).getSpreadsheetData();
           }
           
           function drawChart(rows) {
             console.log(rows);
             var data = google.visualization.arrayToDataTable(rows);
             var chart = new google.visualization.Sankey(document.getElementById('main'));
             chart.draw(data, {width: 900, sankey: {iterations: 64}});
           }
      </script>
   </body>
</html>

在这里您可以找到由上面显示的脚本创建的桑基图的屏幕截图。

enter image description here

在第二步中,我想在桑基图中的每个节点添加附加信息。我的目标是添加每个节点的百分比。图片显示了我最喜欢的解决方案。

enter image description here

如何修改我的脚本来计算与电子表格中第 3 列的总值相关的每个节点的百分比份额?

google-apps-script google-visualization sankey-diagram
1个回答
0
投票

这就是工作表中的输入数据。 input data

© www.soinside.com 2019 - 2024. All rights reserved.