通过 javascript API 保存和恢复 Tableau 图形的当前视图状态

Saving and restoring current view state of a Tableau graph through javascript API

问题:

如何通过 javascript-API 存储并稍后检索 Tableau 视图的自定义状态?

描述:

我在一个网站上工作,我们目前允许任何用户将一组 Tableau 视图协作到类似 PowerPoint 的在线演示文稿中以供以后使用。在我们当前的实施中,Tableau 图表的状态未被存储,因此用户每次都必须在按住演示文稿的同时应用他或她所需的过滤器、select 工作表等。这是我们现在想要避免的。

最简单的解决方案是存储和检索通过底部栏界面访问的 "Share" 链接之一;这些链接包含当前视图的状态,但到目前为止,我们无法做到这一点:首先,由于域问题,我们不能简单地从嵌入代码 iframe 中获取共享链接;其次,API-方法workbook.getUrl()似乎不​​包括当前视图的状态。

我目前正在研究 workbook.rememberCustomViewAsync(name)workbook.showCustomViewAsync(name) 方法,这似乎是一个可能的解决方案。但是,我似乎无法从这两种方法中的任何一种中获得任何合理的结果,因为当 运行.

时,它们最终都会给出模糊的、无信息的 500 错误

示例文件和错误:

为了更好地说明这个问题,我创建了一个最小的 demo(下面的片段),它尝试使用上述第二种方法。在GoogleChrome中打开时,两个按钮('save state'和'retrieve state')都不适合我,在开发者工具(http响应消息和开发者控制台输出,分别为:

HTTP 响应:

<br>
2015-11-11 16&#x3a;14&#x3a;17.916
&#x28;VkNpWQrCQaIAACQo2YYAAAPi,0,0&#x29;

控制台错误:

POST http://public.tableau.com/vizql/w/Book6_426/v/YRKE/save_customized_view/sessions/208A699D34E14708A2268AA10A827C99-0:0 500 (Internal Server Error)

有谁知道我可以如何解决这个问题,是通过使提供的代码示例起作用(描述的第二种方法),还是通过任何其他方式?如有任何帮助,我们将不胜感激!

PS:这里的snippet模拟器会导致Access-Control-Allow-Origin错误。该文件也已发布 here.

<html>

<head>
  <title>A simple Tableau API demo</title>
  <!--script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script-->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
  <!--script type="text/javascript" src="https://online.tableau.com/javascripts/api/tableau_v8.js"></script-->
  <script type="text/javascript" src="https://online.tableau.com/javascripts/api/tableau-2.min.js "></script>
</head>

<body>


  <H2>Custom view storage demo</H2>

  <button id="remember-button">
    Remember state 'test'
  </button>
  <button id="retrieve-button">
    Retrieve state 'test'
  </button>

  <div id="viz-placeholder" style="width: 1000px; height: 1000px; display: block;"></div>


  <script>
    
    // Render tableau graph
    function initializeViz() {
      var placeholderDiv = document.getElementById("viz-placeholder");
      var url = "https://public.tableau.com/views/Book6_426/YRKE";
      var options = {
        width: placeholderDiv.offsetWidth,
        height: placeholderDiv.offsetHeight,
        hideTabs: true,
        hideToolbar: true,
        onFirstInteractive: function() {
          workbook = viz.getWorkbook();
          activeSheet = workbook.getActiveSheet();
        }
      };
      viz = new tableau.Viz(placeholderDiv, url, options);
    }

    $(initializeViz)

    
     // Assign and set up button actions for storing and retrieving the custom view
    var customViewName = "test";

    $('#remember-button').click(function() {
      console.log("Remembering: ", customViewName);
      
      // Try to save state, or print error
      viz.getWorkbook().rememberCustomViewAsync(customViewName).otherwise(function(err) {
        console.log("An error occured:");
        console.log(err);
      });
      
    });

    $('#retrieve-button').click(function() {
      console.log("Retrieving: ", customViewName);
      
      // Try to retrieve state, or print error
      viz.getWorkbook().showCustomViewAsync(customViewName).otherwise(function(err) {
        console.log("An error occured:");
        console.log(err);
      });
      
    });
  </script>


</body>

</html>

好的,所以我已经联系了 Tableau 客户支持,他们似乎已经发现了问题。

显然,javascript-API 的某些元素仅适用于 Tableau Online 和 Tableau Server - 不适用于 Tableau Public。

换句话说,Tableau Public 托管的图形不支持函数 workbook.rememberCustomViewAsync('customViewName') - 例如上例中使用的图形 (https://public.tableau.com/views/...)。