简体   繁体   English

切换仪表板过滤器后如何更新 tableau 扩展 API 图表?

[英]How to update tableau extensions API graph after toggling dashboard filter?

I am using Tableau to create a dashboard.我正在使用 Tableau 创建仪表板。 I needed the power of D3 in my dashboard, and so I created a Tableau Extension that creates the viz.我的仪表板需要 D3 的强大功能,因此我创建了一个 Tableau 扩展来创建可视化。 Here's the skeleton of the JS that uses the Tableau Extensions API functionality:以下是使用 Tableau Extensions API 功能的 JS 框架:

$(document).ready(function () {
  console.log('were in again'); 
  tableau.extensions.initializeAsync().then(function () {

    // To get dataSource info, first get the dashboard.
    const dashboard = tableau.extensions.dashboardContent.dashboard;
    const worksheets = dashboard.worksheets;

    let underlyingDataFetchPromises = [];
    let dataObjects = {};
    let worksheetNames = [];

    // Save Promises to Array + Add Event Listeners
    dashboard.worksheets.forEach(worksheet => {
      worksheetNames.push(worksheet.name);
      underlyingDataFetchPromises.push(worksheet.getUnderlyingDataAsync());

      // add event listener too...
      worksheet.addEventListener(tableau.TableauEventType.FilterChanged, (filterEvent) => {
        clearChart();
        drawChart(dataObjects.dataA, dataObjects.dataB);
      });
    });

    // Maps dataSource id to dataSource so we can keep track of unique dataSources.
    Promise.all(underlyingDataFetchPromises).then(fetchedData => {

      // loop over the 2 fetchedData arrays [(1) dataA, (2) dataB]
      fetchedData.forEach((dataTable, i) => {
        let data = dataTable.data;
        let columns = dataTable.columns;
        let formattedData = formatData(data, columns);
        dataObjects[worksheetNames[i]] = formattedData;
      });

      // currently requires worksheets named "dataA" and "dataB"... not flexible atm
      drawChart(dataObjects.dataA, dataObjects.dataB);
    });

  }, function (err) {
    // Something went wrong in initialization.
    console.log('Error while Initializing: ' + err.toString());
  }); 
});

cleanData() and drawChart() are functions I wrote myself that draw the D3 visualizations, these are working. cleanData()drawChart()是我自己编写的用于绘制 D3 可视化的函数,它们正在运行。 My event handler "works" a bit, in the sense that it is properly triggered when a fitler in the dashboard is changed.我的事件处理程序“有效”了一点,因为当仪表板中的 fitler 更改时它会被正确触发。

However , my problem is that, when drawChart() is called from within the eventListener, my chart is drawn with the same data , not the new data resulting from the filter being toggled.但是,我的问题是,当从drawChart()中调用drawChart()时,我的图表使用相同的数据绘制,而不是由切换过滤器产生的新数据。 I understand inherently why this is the case - the eventListener is not refetching data, only redrawing the chart.我从本质上理解为什么会这样 - eventListener 没有重新获取数据,只是重新绘制图表。

My question is then, how do I re-fetch new data to re-draw my viz correctly after the filters are toggled?我的问题是,如何在切换过滤器后重新获取新数据以正确重新绘制我的可视化? And also, is there a function that will return to me the filter names + values, that I can use to improve the functionality of my extension?而且,是否有一个函数可以将过滤器名称 + 值返回给我,我可以用它来改进我的扩展程序的功能? It would be good to have all of the dashboard filters + values in JS to use in this code.在此代码中使用 JS 中的所有仪表板过滤器 + 值会很好。

Thanks in advance for any help with this!在此先感谢您的帮助!

The following code refreshes the data source "ABC" and reloads the workbook-以下代码刷新数据源“ABC”并重新加载工作簿-

//The following code is used to refresh the data source 
// Wrap everything in an anonymous function to avoid polluting the global namespace
function refreshMySql() {
  $(document).ready(function() {
      tableau.extensions.initializeAsync().then(function() {
          // Since dataSource info is attached to the worksheet, we will perform
          // one async call per worksheet to get every dataSource used in this
          // dashboard.  This demonstrates the use of Promise.all to combine
          // promises together and wait for each of them to resolve.
          let dataSourceFetchPromises = [];

          // Maps dataSource id to dataSource so we can keep track of unique dataSources.
          let dashboardDataSources = {};

          // To get dataSource info, first get the dashboard.
          const dashboard = tableau.extensions.dashboardContent.dashboard;

          // Then loop through each worksheet and get its dataSources, save promise for later.
          dashboard.worksheets.forEach(function(worksheet) {
              dataSourceFetchPromises.push(worksheet.getDataSourcesAsync());
          });
          Promise.all(dataSourceFetchPromises).then(function(fetchResults) {
              fetchResults.forEach(function(dataSourcesForWorksheet) {
                  dataSourcesForWorksheet.forEach(function(dataSource) {
                      if (!dashboardDataSources[dataSource.id]) { // We've already seen it, skip it.
                          dashboardDataSources[dataSource.id] = dataSource;
                          var datasourceName = dashboardDataSources[dataSource.id].name;
                          if (dashboardDataSources[dataSource.id].name == "ABC") {

                              refreshDataSource(dashboardDataSources[dataSource.id]);
                              console.log("refreshSql() was called for Datasource Name: 'ABC'");
                          }


                      }
                  });
              });
          });
      }, function(err) {
          // Something went wrong in initialization.
          console.log('Error while Initializing: ' + err.toString());
      });
  });
  // Refreshes the given dataSource.
  function refreshDataSource(dataSource) {
      dataSource.refreshAsync().then(function() {
          //alert(dataSource.name + ': Refreshed Successfully');
          console.log(dataSource.name + ': Refreshed Successfully');
      });
  }

}

GitHub Source GitHub 源码

Tableau Writeback Source Tableau 写回源

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

相关问题 如何使用 Docker 在本地部署 Tableau Dashboard Extensions(Web 应用程序) - How to deploy Tableau Dashboard Extensions (web app) locally with Docker 我如何使用Tableau Javascript API获取除活动对象以外的Tableau Dashboard对象? - How can I get Tableau Dashboard objects other than the active one using Tableau Javascript API? Tableau JS过滤器:使用javascript API,将过滤器从一个仪表板移动到另一个仪表板 - Tableau JS filter: Using javascript API, move filters from one dashboard to another 在通过aspx加载的Tableau仪表板上使用Tableau Javascript API - Using Tableau Javascript API on Tableau Dashboard Loaded through aspx Tableau API JavaScript过滤器示例 - Tableau API JavaScript Filter Example Tableau筛选器不适用于使用Tableau Extension API创建的SET - Tableau filter is not working for SETs that are created using Tableau Extension API Tableau JS Api筛选器不接受变量 - Tableau JS Api Filter does not accept variable Shiny R中的Tableau JavaScript API过滤器功能 - Tableau JavaScript API filter function in Shiny R 通过javascript API保存和还原Tableau图形的当前视图状态 - Saving and restoring current view state of a Tableau graph through javascript API 如何使用Tableau JavaScript API将Tableau嵌入到网页中? - How to embed Tableau into a web page using the tableau JavaScript API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM