Google Visualization.Sankey — GoogleScript 删除节点标签

huangapple go评论67阅读模式
英文:

Google Visualization.Sankey -- GoogleScript Dropping Node Label

问题

以下是您要翻译的内容:

"So I am able to make a Sankey plot from my Google Sheet data using Google Chart. The issue is that the plot seems to drop off the label for one of the nodes.

My data looks as follow:

Source	  Type	Count
External	A	12
External	B	7
External	C	1
External	D	0
Internal	A	26
Internal	B	23
Internal	C	15
Internal	D	0
Other	    A	0
Other	    B	1
Other	    C	24
Other	    D	0

The plot looks like
Google Visualization.Sankey — GoogleScript 删除节点标签

As you can see the last Node name "C" (in green is dropped)

My GoogleScript code was as follow:
code.gs

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

function getSpreadsheetData() {
    // ID of your Document, take from URL
    var ssID = "sheetID",
        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');
}

And the corresponding HTML was:
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('sankey_basic'));
             chart.draw(data, {'title':'Detection Method by Severity',width: 900,height: 250, sankey: {}});
             google.script.run.withSuccessHandler().newChart(chart);
           }
      </script>
   </body>
  </head>
  <body>
    <div id="sankey_basic" style="width: 900px; height: 300px;"></div>
  </body>
</html>

What am I overlooking that is causing the "C" category not to appear?

Separate but not critical question -- is there a way to embed the image as an image/chart on the active sheet

Also for the code reference... special thanks to
njoerd114 for creating the framework for drawing Sankey plots... https://gist.github.com/njoerd114/839b9a5298843ea4cf9fd241e39ebbf6"

英文:

So I am able to make a Sankey plot from my Google Sheet data using Google Chart. The issue is that the plot seems to drop off the label for one of the nodes.

My data looks as follow:

Source	  Type	Count
External	A	12
External	B	7
External	C	1
External	D	0
Internal	A	26
Internal	B	23
Internal	C	15
Internal	D	0
Other	    A	0
Other	    B	1
Other	    C	24
Other	    D	0

The plot looks like
Google Visualization.Sankey — GoogleScript 删除节点标签

As you can see the last Node name "C" (in green is dropped)

My GoogleScript code was as follow:
code.gs

function onOpen() {
    SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
        .createMenu(&#39;CustomScripts&#39;)
        .addItem(&#39;SankeyDiagram&#39;, &#39;openDialog&#39;)
        .addToUi();
}

function getSpreadsheetData() {
    // ID of your Document, take from URL
    var ssID = &quot;sheetID&quot;,
        sheet = SpreadsheetApp.openById(ssID).getSheets()[0],
        data = sheet.getDataRange().getValues();
    return data;
}

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

And the corresponding HTML was:
index.html

&lt;!DOCTYPE html&gt;
&lt;html&gt;
   &lt;head&gt;
      &lt;base target=&quot;_top&quot;&gt;
      &lt;script src=&quot;https://www.google.com/jsapi&quot;&gt;&lt;/script&gt;
   &lt;/head&gt;
   &lt;body&gt;
      &lt;div id=&quot;main&quot;&gt;&lt;/div&gt;
      &lt;script type=&quot;text/javascript&quot;&gt;
         google.load(&#39;visualization&#39;, &#39;1&#39;, {
           packages: [&#39;corechart&#39;, &#39;sankey&#39;]
         });      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(&#39;sankey_basic&#39;));
             chart.draw(data, {&#39;title&#39;:&#39;Detection Method by Severity&#39;,width: 900,height: 250, sankey: {}});
             google.script.run.withSuccessHandler().newChart(chart);
           }
      &lt;/script&gt;
   &lt;/body&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;sankey_basic&quot; style=&quot;width: 900px; height: 300px;&quot;&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

What am I overlooking that is causing the "C" category not to appear?

Separate but not critical question -- is there a way to embed the image as an image/chart on the active sheet

Also for the code reference... special thanks to
njoerd114 for creating the framework for drawing Sankey plots... https://gist.github.com/njoerd114/839b9a5298843ea4cf9fd241e39ebbf6

答案1

得分: 1

您之所以获取桑基图的切割图像是因为在“D”中存在“零”值。我在GitHub上找到了一个类似的问题:桑基图中的链接值为零时节点位置不起作用

我建议您首先通过过滤数组来删除“零”值:

修改后的代码:

function onOpen() {
    SpreadsheetApp.getUi() // 或者 DocumentApp 或 FormApp.
        .createMenu('CustomScripts')
        .addItem('SankeyDiagram', 'openDialog')
        .addToUi();
}

function getSpreadsheetData() {
    // 您的文档ID,从URL中获取
    var ssID = "1KtvAFFQV13zSwktXKtUXZLQF9hmb9VaxbC4OM3hGEVw",
        // 选择哪个工作表?[0] 是第一个工作表,依此类推...
        sheet = SpreadsheetApp.openById(ssID).getSheets()[0],
        data = sheet.getDataRange().getValues(),
        // 通过移除零值来筛选数据
        header = sheet.getRange(1, 1, 1, 3).getValues()[0],
        filteredData = data.filter(r => r[2] > 0);
    filteredData.unshift(header);
    return filteredData;
}

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

结果:
Google Visualization.Sankey — GoogleScript 删除节点标签

英文:

Suggestion:

The reason why you are getting the cut image of the sankey graph is because of the zero values you have in &quot;D&quot;.
I have found a similar concern like this in git hub: Node positions do not work with link value zero in Sankey

I suggest that you remove zeros first by filtering the array:

Modified code:

function onOpen() {
    SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
        .createMenu(&#39;CustomScripts&#39;)
        .addItem(&#39;SankeyDiagram&#39;, &#39;openDialog&#39;)
        .addToUi();
}

function getSpreadsheetData() {
    // ID of your Document, take from URL
    var ssID = &quot;1KtvAFFQV13zSwktXKtUXZLQF9hmb9VaxbC4OM3hGEVw&quot;,
        // which Sheet? [0] is the first and so on...
        sheet = SpreadsheetApp.openById(ssID).getSheets()[0],
        data = sheet.getDataRange().getValues(),
      ///Filtered the data by removing zeros
        header = sheet.getRange(1,1,1,3).getValues()[0],
        filteredData = data.filter(r =&gt; r[2] &gt; 0);
        filteredData.unshift(header)
    return filteredData
}

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

Result:
Google Visualization.Sankey — GoogleScript 删除节点标签

huangapple
  • 本文由 发表于 2023年2月10日 07:15:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定