Adam Cooke

I currently work as a consultant for Qlik, a software company doing some interesting stuff in business intelligence. Check it out Qlik.com. I write this blog to showcase some technical bits that I hope will be useful to others.
adamcooke@webofwork.com

Tags

Getting data from a Qlik Sense object using the API

Webofwork > Qlik Sense for beginners  > Getting data from a Qlik Sense object using the API

Getting data from a Qlik Sense object using the API

Qlik Sense objects each have a table of data behind them that is used by the visualisation. This is been called a ‘hypercube’ by Qlik. If you have lots of rows in your data table, then Qlik will split the ‘hypercube’ into separate pages so the data doesn’t crash the browser when the page is first loaded.Qlik will only bring back the first page of data in the initial paint of the extension, each page can have up to 10,000 cell values (columns*rows). This means you need to either make sure the extension can function with either just the first page or you will need to loop through each page and extract the data.

 

Get the first page of hypercube data in Qlik Sense

This is pretty easy, just make sure you use qInitialDataFetch in the initialProperties definition:

        return {
            // new object properties
            initialProperties: {
                version: 1.02,
                qHyperCubeDef: {
                    qDimensions: [],
                    qMeasures: [],
                    qDebugMode: true,
                    qInitialDataFetch: [{
                        qWidth: 7,
                        qHeight: 200
                    }]
                }
            },
            definition: Props,
            paint: function($element, layout) {


                    console.log(layout);
                    var qData = layout.qHyperCube.qDataPages[0].qMatrix;

                    console.log(qData);


                } //end return
                ////////////////////////////////////////////////////////
        } //close function
    }
); //close define


 

 

You can then start working with the data using layout.qHyperCube.qDataPages[0].qMatrix;

 

Paginating through Qlik Sense Hypercube data

If you want the complete data set, you can use the engine api to loop through each page and build up a big table. Here is an example:

      $scope.getAllData = function(layout, handle, lastRow, callbackFn){
        if(!layout.qHyperCube){
          callbackFn.call();
        }
        if(lastRow==0){
          layout.qHyperCube.qDataPages = [];
        }
        var pages = [{
          qTop: lastRow,
          qLeft: 0,
          qHeight: 100,
          qWidth: 10
        }];
        $scope.session.rpc({handle: handle, method: "GetHyperCubeData", params: ["/qHyperCubeDef", pages]}).then(function(response){
          var data = response.result.qDataPages[0];
          lastRow+=data.qArea.qHeight;
          layout.qHyperCube.qDataPages.push(data);
          if(lastRow < layout.qHyperCube.qSize.qcy){
            $scope.getAllData(layout, handle, lastRow, callbackFn);
          }
          else{
            callbackFn.call(null, layout);
          }
        }).catch(function(err){
          console.log(err);
        });
      };

Useful links:

https://help.qlik.com/en-US/sense-developer/2.2/Subsystems/Extensions/Content/Overview/qHyperCubeDef.htm#NxPage

 

user-gravatar
Adam Cooke

I currently work as a consultant for Qlik, a software company doing some interesting stuff in business intelligence. Check it out Qlik.com. I write this blog to showcase some technical bits that I hope will be useful to others.

No Comments

Post a Comment

Comment
Name
Email
Website