JavaScript API Tutorial

This tutorial is a sample web application, created with Tableau's JavaScript API. It's designed to help you explore and learn about the API using a series of steps that guide you through the basics. To get started, click the button below.

Note This tutorial uses version 2 of the JavaScript API (tableau-2.min.js).

The view will load here after you click Run this code, below.

Show .js file

Create the Viz

As you build your web application, the first step is to create, or instantiate the view. To do this, you create a new Viz object, passing the required parentElement (document.getElementByID) and url parameters, along with any options, such as hideTabs and hideToolbar. Here's the code:

function initializeViz() {
  var placeholderDiv = document.getElementById("tableauViz");
  var url = "https://public.tableau.com/views/WorldIndicators/GDPpercapita";
  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);
}      

You should now see a view with which you can interact, just like you can with views on Tableau Server. If you don't see a view above, it may need a few more moments to load, or you may need to use a different web browser.

In the code above, the constructor for the Viz object handles loading the view. Specifying a function in the onFirstInteractive option allows you to perform actions once the view has finished loading. In this case, the function caches the workbook and activeSheet variables so they can be used later on. These two variables were declared as global variables in the actual script. Typically you'll want to create the view when the page has finished loading and the browser is ready. If you're using jQuery, this can be done using jQuery's ready handler:

$(initializeViz);