5 Replies Latest reply on Apr 1, 2018 2:22 PM by Man Tsui

    JavaScript API to return unique values of a dimension

    John Gulino

      Hi all,

       

      Is it possible, using JavaScript and Tableau Server, to retrieve an array of unique values of a dimension? I haven't been able to figure this out via JavaScript API of the Extract API.

       

      Any nudges in the right direction would be greatly appreciated!

       

      Thanks,

       

      John

        • 1. Re: JavaScript API to return unique values of a dimension

          Hi John,

           

          I have moved this thread to the Javascript category, where our experts will be able to assist you regarding your query

          In the meantime - though I've started learning JS this month - what do you think of the following?

          var a = ['a','b','c','d','e','f','g'];
          var sliced
          = a.slice(0, 3); //will contain ['a', 'b', 'c']

           

          ----------

          Lénaïc RIÉDINGER, Global Community Engineer Tableau

          Tableau Community Forums | Knowledge Base

          If you see a Helpful or Correct response, please mark it thanks to the buttons below the targeted post!

          • 2. Re: JavaScript API to return unique values of a dimension
            John Gulino

            Hi Lénaïc,

             

            Thank you for moving my post to the appropriate forum, and thanks for your suggestion.

             

            What I would like to do is retrieve unique values from a given dimension in my workbook. Seems like it should be easy, but I’m haven’t figured it out yet.

            • 3. Re: JavaScript API to return unique values of a dimension

              That is why I proposed the JS code in my previous message.

              Have you tried it yet?

              Did it work for you?

               

              var a = ['a','b','c','d','e','f','g'];
              var sliced
              = a.slice(0, 3); //will contain ['a', 'b', 'c']
              • 4. Re: JavaScript API to return unique values of a dimension
                John Gulino

                I have not yet tried it because I don't understand how this will help.

                 

                Let me try to put it in context. I have this function that filters the worksheet "Map." Let's say the argument "filterName" is a dimension called "Region" and the "values" argument is an array of values contained in the Region dimension ["North", "South","East","West"]:

                 

                function myFilter(filterName, values) {

                    workbook = viz.getWorkbook();

                    activeSheet = workbook.getActiveSheet();

                    activeSheet.getWorksheets().get("Map").applyFilterAsync(filterName, values, 'REPLACE');

                }

                 

                I've got a loop that slowly cycles through the array, passing each value one at a time to the filter. An animation of sorts.

                 

                It all works great. However, having to enter the values in the array by hand is not ideal. What I would like to do, is write a function that looks at the dimension "Region" and returns an array of distinct values contained therein.

                 

                Does that make sense?

                 

                Thanks!

                • 5. Re: JavaScript API to return unique values of a dimension
                  Man Tsui

                  Unique value retrieval is possible within the javascript environment.

                  I've experimented on this in the past for trying to integrate Tableau with D3 (still in progress).

                   

                  This can be done through writing a javascript function. This is manipulating data within the javascript environment as opposed to in the native Tableau environment.

                   

                   

                  Prerequisite suggestions:

                  Step 1) You may want to write your Tableau javascript API codes in a separate .js file as opposed to .html file.

                   

                  Step 2) Brush up on the javascript course (udemy.com or lynda.com)

                  https://www.udemy.com/jquery-tutorial/learn/v4/overview

                   

                  Step 3) First experiment and get familar with Tableau Javascript API calls for addEventListener(), and getFilterAsync(), then you would be in better shape for removing duplicate values from fields or value.

                   

                  Step 4) Build you own javascript code to remove duplicate values.

                   

                  javascript - Remove duplicate values from JS array - Stack Overflow https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array

                   

                  ---------------------------------------------------------------------

                   

                  Sample javascript code below

                  function uniq_fast(a) {
                    
                  var seen = {};
                    
                  var out = [];
                    
                  var len = a.length;
                    
                  var j = 0;
                    
                  for(var i = 0; i < len; i++) {
                    
                  var item = a[i];
                    
                  if(seen[item] !== 1) {
                    seen
                  [item] = 1;
                    
                  out[j++] = item;
                    
                  }
                    
                  }
                    
                  return out;
                  }

                  ---------------------------------------------------------------------

                  My own experimental Tableau javascript API code below

                   

                  vizMedicareIP01.addEventListener('filterchange', function(filterEvent) {

                   

                  console.log('Event Listener Activated.'); //Debug code

                   

                  var arrayFilterList = [];

                  filterEvent.getFilterAsync().then( function(field){

                  var field_name = field.getFieldName();

                  var field_type = field.getFilterType();

                  if (field_name == "Provider State") {

                  var data_values = field.getAppliedValues();

                  for (i = 0; i < data_values.length; i++) {

                  var selectedFilterSingle = data_values[i].value;

                   

                  // Array manipulation: Concatenate multiple filter values into the array

                  arrayFilterList.push(selectedFilterSingle);

                  }

                  }

                  console.log(arrayFilterList);

                   

                   

                  // Cross-filter: Apply "Provider State" filter criteria to "Medicare Outpatient Charge Analysis 01"

                  // with single mark or multiple marks

                  setFilterTo(vizMedicareOP01, 'OP Map', 'Provider State', arrayFilterList);

                  });

                  });

                  ---------------------------------------------------------------------