Tabitha - Voice Recognition and Voice Response for Tableau

Getting started with Tableau Javascript API

First thing is to understand the basics of the Tableau Javascript API. If you are not at all familiar with it, there is a Tableau Javascript Tutorial by Tableau Software to help get you started. This will walk you through the code step by step.

Requirements for Tabitha

  • A Tableau Public Visualization that you would like to interact with
  • Tableau Javascript API
  • annyang.js
  • responsivevoice.js
  • Chrome browser
  • Hosting capability, preferably secure hosting for microphone permission
  • Some basic HTML and Javascript knowledge to put it together (listed step by step below)
  • Some sort of text editor, for example Notepad++ (download here for free)

Step 1 - Build your Viz

Create your visualization and load it up to Tableau Public. In this demonstration we will show how to control the viz using both Highlight and Filter Actions. To make it easy, start with a single worksheet or a simple dashboard and pick a single dimension, for example a category or state, that you wish to highlight or filer.

In this tutorial I will be using the "Star Words" viz created by Matt Francis located here.

Step 2 - Create a basic HTML page

Start with some basic tags to create a generic HTML page.

Standard Tableau Embed code could be inserted after the "Hello Word" line and you would have an embedded viz on your page. Instead of doing this, we are going to load the viz a different way. This is the starting point for using the Tableau Javascript API.

In the example below we add:

  • A reference to the Tableau Javascript in the "head" tag.
  • An onload event in the "body" tag calling the "initializeViz" function.
  • A div id referencing the viz (we use "center" to align the viz to the center)
  • In the script section:
      • create some variables for the viz, workbook and worksheet
      • create the function for "initializeViz"
      • enter the URL for the viz you want to call
        var url = "https://public.tableau.com/views/my-workbook/my-view";
        TIP - Use the Twitter share button on any Tableau Public Viz to get the URL
      • set width and height to match the viz
        TIP - Use the Embed code from the Tableau Public viz to get width and height

Step 3

Next, you will need to download 3 Javascript files and place in the directory with the webpage (or whatever directory you wish to call them from in the HTML code).

Right click on these files and "Save As" to save them to your computer. Then upload to your hosting server in the appropriate directory.

Voice Response requires responsivevoice.js

  • responsivevoice.js

Voice Recognition requires two files: annyang.js and annyang.min.js

  • annyang.js
  • annyang.min.js

Step 4

Next we add code for the various viz functions. In this Star Words viz, we will create 4 selection functions and a function to start over for this tutorial. There is a demo at the bottom of this page that includes all of the functions for Matt's viz.

Place this code in the script tag, right before the closing "/script" tag section in the above code. In this example, the field name is "Character" and the values in the field are the names of the characters, for example "DARTH VADER" and "YODA". The function uses activeSheet and the selectMarks based on those selections. Think of it as dragging the blue pill named "Character" over to the filter shelf and then selecting the name of the character you are filtering.

Step 5

In the step we create voice commands, add them to annyang and start the listening. Each voice command line is structured with 3 parts:

   1.) the words that Tabitha is listening for
   2.) the function to control the viz
   3.) the voice response back from Tabitha

'Select Darth Vader': function() { selectVader(); responsiveVoice.speak('selecting Darth Vader', 'UK English Male'); },

'Select Darth Vader' - these are the words Tabitha is listening for
selectVader() - the function we defined to control the viz to select Character = "DARTH VADOR"
responsiveVoice.speak('selecting Darth Vader', "UK English Female"); - this is the text that Tabitha speaks back and the voice type.

The default is a female voice, but notice in the code below we have the response for Darth Vader switched to a "UK English Male" voice. There are 160+ voice types available in the latest responsivevoice.js. See the Javascript documentation on their website for more information (here)

Putting it all together

Here's what the final HTML looks like for this tutorial. Feel free to copy and paste from the box below and play with it. In addition, I completed all of the functions for "Star Words" and included a link below.

A completed version of this tutorial with all of the commands added is available here.

Using Filters instead of Selecting Marks/Highlight

When using Filtering instead of Highlighting, use "applyFilterAsync" instead of "selectMarksAsync"

For Example, this code filters for the State="AL" instead of highlighting.

    function filterAlabama()
    activeSheet.getWorksheets()[1].applyFilterAsync('State', 'AL', tableauSoftware.FilterUpdateType.REPLACE);

This is the technique I used in my blog post from March of last year when I added voice to Kelly Martin's Birdstrike viz here.

I hope you have fun adding Tabitha to your visualizations. If you have any questions feel free to email me at Jeff@DataPlusScience.com

Jeffrey A. Shaffer

Follow on Twitter @HighVizAbility