One of the really powerful features of Qlik Sense API’s is the possibility to dynamically modify visualizations. You can build solutions that allows the user not only to make selections and have them reflected in all charts displayed (that’s the default, you will get that automatically if you don’t turn it off) but also to dynamically change chart properties and do things like
- change dimension from for example Product to Product Lin (or to Division)
- change measure from Sales Amount to Quantity or Margin or Cost
- add an additional dimension and make a barchart into a stacked bar chart
- modify ordering
- limit the chart to only show Top 10, or bottom 5
- change the chart type from Barchart to Linechart or to an extension
And all of this can be done for both built-in visualizations and extensions in the same way. The key to all of this is the applyPatches method and soft patches.
Soft patches
A soft patch is a temporary change to properties for a visualization. The fact that it is temporary means:
- it will not be persisted either in Desktop or Server
- it will not affect other users using the same app
- once the Qix engine session is closed it will be gone, so when the user reconnects, visualizations will be back to their original state
- the user can make soft patches to visualizations that me does not have the right to modify
You make sure that its a soft patch by setting the second parameter in the applyPatches call to true. The first parameter is the list of patches you want to apply. Since you might want to apply several patches, it is an array, where each entry has three values:
- qPath: the path to the property you want to change, with slash (/) where javascrip/json usually has a dot
- qOp: the operation you want to perform, one of “add”, “replace”, “remove”
- qValue: the value you want to set. This should be a JSON value inside a string, so a boolean value would be “true” or “false”, a numeric value something like “-1” and a string (this is the tricky part) “\”new string\”” (that is a string in a string). You could also set a javascript object, use javascript standard JSON.stringify method in that case.
Perhaps this is clearer if we look at an example:
What we do in this example is that we show a list of dimensions and measures to the user. When the user clicks on one of them, we move it to be the first one in the sort order, and use applyPatches to update the array and the chart will be resorted. Since it is all done with soft patches, it will not affect other users.
You find a full working example in the Github repository.
This post is based on a session I originally held at Qonnections 2015, under the title ‘5 things you did not know you can do with the Mashup API’.