Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
ExtensionMaster
Contributor II
Contributor II

API visualization.create : barchart : sort by measure

Hello,

I am creating visualizations on the fly with the Capability APIs / Visualization API 

https://help.qlik.com/en-US/sense-developer/August2023/Subsystems/APIs/Content/Sense_ClientAPIs/Capa...

It seems there is no way to sort a barchart by measure value.

All the options are documented here:

https://help.qlik.com/en-US/sense-developer/August2023/Subsystems/APIs/Content/Sense_ClientAPIs/Capa...

When I create the graph manually in a Qlik app , and then inspect the hypercube Qlik has generated, I can see there is a parameter "qInterColumnSortOrder" to specify the sort order of the different dimensions and measures.

By ex, in a barchart with one dimension and one measure, if the measure is first in sorting order, qInterColumnSortOrder  is set to [1,0]

But this parameter has no impact when using the API visualization.create()

Could you please advice if this is possible and how to do it ?

 

 

 

 

 

 

Labels (2)
1 Solution

Accepted Solutions
alex_colombo
Employee
Employee

Hi @ExtensionMaster , for changing the sort order you have to use the qHyperCubeDef option, and this means that you have to pass the entire barchart HyperCube definition, in which you can find qInterColumnSortOrder property.

Below you can find an example where I'm creating a viz on the fly passing the qHyperCubeDef option. For keeping things simple, I'm reusing an HypercubeDef from an existing barchart, very similar to the one I'm creating on the fly. In your scenario, you can save somewhere the HyperCubeDef for a barchart, and then use it for you visualizations on the fly.

let hyperCubeDef;
app.visualization.get('QssGsxc').then(function(viz) {
	//Get HyperCubeDef from existing viz
	const vizProps = viz.model.getFullPropertyTree().then(function(props) {
		//Save current hyperCubeDef
		hyperCubeDef = props.qProperty.qHyperCubeDef
		
		//Change sort order
		hyperCubeDef.qInterColumnSortOrder = [1,0,2];
		
		//Create viz on the fly based on hypercube of another viz
		app.visualization.create('barchart',["Dim1","=Sum(Expression1)","=Sum(Expression2)"],{qHyperCubeDef: hyperCubeDef}).then(function(bar){
			bar.show('QV02');
		});
	})
});

 

View solution in original post

3 Replies
alex_colombo
Employee
Employee

Hi @ExtensionMaster , for changing the sort order you have to use the qHyperCubeDef option, and this means that you have to pass the entire barchart HyperCube definition, in which you can find qInterColumnSortOrder property.

Below you can find an example where I'm creating a viz on the fly passing the qHyperCubeDef option. For keeping things simple, I'm reusing an HypercubeDef from an existing barchart, very similar to the one I'm creating on the fly. In your scenario, you can save somewhere the HyperCubeDef for a barchart, and then use it for you visualizations on the fly.

let hyperCubeDef;
app.visualization.get('QssGsxc').then(function(viz) {
	//Get HyperCubeDef from existing viz
	const vizProps = viz.model.getFullPropertyTree().then(function(props) {
		//Save current hyperCubeDef
		hyperCubeDef = props.qProperty.qHyperCubeDef
		
		//Change sort order
		hyperCubeDef.qInterColumnSortOrder = [1,0,2];
		
		//Create viz on the fly based on hypercube of another viz
		app.visualization.create('barchart',["Dim1","=Sum(Expression1)","=Sum(Expression2)"],{qHyperCubeDef: hyperCubeDef}).then(function(bar){
			bar.show('QV02');
		});
	})
});

 

ExtensionMaster
Contributor II
Contributor II
Author

Hi Alex,

Very good, this is working, thank you !

My mistake was to set the qInterColumnSortOrder at the top level of the options, but it needs to be inside the qHyperCubeDef property.

In fact I don't need to copy the hypercube of another object, just set the property like this : 

const dimAndMeasures = []; // fill the list

const options = {};
options.qHyperCubeDef = {};
options.qHyperCubeDef.qInterColumnSortOrder = [1, 0];
app.visualization.create('barchart', dimAndMeasures, options)

 

Thanks

alex_colombo
Employee
Employee

Good to know this! Basically in this way is just overriding the single property (qInterColumnSortOrder) instead of the entire HyperCubeDef.