简体   繁体   English

Tableau JavaScript API-获取选定值

[英]Tableau JavaScript API - get Selected value

I am currently researching the possibility to grabbing data from the Tableau report(s) via the JavaScript API but the closet I can get to grabbing values from a graph after filtering is selecting the value via the selectSingleValue() method. 我目前正在研究通过JavaScript API从Tableau报告中获取数据的可能性,但是在过滤后,我可以从图形中获取值的壁橱是通过selectSingleValue()方法选择值。

For example: JavaScript API Tutorial 例如: JavaScript API教程

In the API tutorial tab called 'Select'. 在API教程标签中,称为“选择”。 One of the examples selects the row "Marcao Sao, China". 示例之一选择了“中国的马可骚”行。 Is it possible to extract that numerical value of $52.0k ? 是否可以提取$ 52,000k的数值?

I have tried looking into the Objects returned (via FireBug) but I cannot seem to locate the right object. 我尝试查看返回的对象(通过FireBug),但似乎找不到正确的对象。 My recent location was in getActiveSheets(). 我最近的位置在getActiveSheets()中。

Any help would be appreciated. 任何帮助,将不胜感激。

In the JavaScript API tutorial tab 'Events' it shows you how to add an event listener to return the selected marks. 在JavaScript API教程标签“事件”中,它显示了如何添加事件侦听器以返回所选标记。 You can then loop through the marks to get the values you want. 然后,您可以遍历标记以获取所需的值。

Copy the below code block into a file, save as html and open in your favourite web browser (tested on ie11). 将以下代码块复制到文件中,另存为html,然后在您喜欢的Web浏览器中打开(在ie11上测试)。

<html>
<head>
<meta charset="utf-8">
<title>Tableau 8 Javascrip API</title>
<script type="text/javascript" src="http://public.tableausoftware.com/javascripts/api/tableau_v8.js"></script>
<script type="text/javascript">
/////////////////////
// Global variables
var viz, workbook, activeSheet

// function called by viz on marks being selected in the workbook
function onMarksSelection(marksEvent) {
  return marksEvent.getMarksAsync().then(reportSelectedMarks);
}

function reportSelectedMarks(marks) {
    for (var markIndex = 0; markIndex < marks.length; markIndex++) {
        var pairs = marks[markIndex].getPairs();
        for (var pairIndex = 0; pairIndex < pairs.length; pairIndex++) {
            var pair = pairs[pairIndex];
            if (pair.fieldName == "AVG(F: GDP per capita (curr $))") {
                alert("You selected a country with an avg GPD per capita of " + pair.formattedValue);
            }
        }
    }
}


// Initialise the viz to hold the workbook
function initializeViz(){
     var placeholderDiv = document.getElementById("tableauViz"); 
     var url = "http://public.tableausoftware.com/views/WorldIndicators/GDPpercapita?Region="; 
     var options = {
          width: "800px", //width: placeholderDiv.offsetWidth,
          height: "400px", //height: placeholderDiv.offsetHeight,
          hideTabs: true,
          hideToolbar: true,
          onFirstInteractive: function () {
                workbook = viz.getWorkbook();
                activeSheet = workbook.getActiveSheet();
          }
     };
     viz = new tableauSoftware.Viz(placeholderDiv, url, options);    
    // Add event listener
    viz.addEventListener(tableauSoftware.TableauEventName.MARKS_SELECTION, onMarksSelection);
}
</script>
</head>
<body>
<!-- Tableau view goes here -->
     <div id="tableauViz" style="height:1200px; width:1200px"\></div>
     <script type='text/javascript'>
     //Initialize the viz after the div is created
     initializeViz();
     </script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM