cancel
Showing results for 
Search instead for 
Did you mean: 

manual sorting categories in Scatter Chart

zohebakber
9 - Travel Pro
9 - Travel Pro

Hi Team,

I want to manually sort the categories for the x axis in scatter chart.
Please help.

Thanks

1 ACCEPTED SOLUTION

Hi Sijo,

Thanks for the reply..
I tried the below script and it worked..

 

var categories= ['10+% lower','5-10% lower','2-5% lower','0-2% lower','0%','0-2% Higher','2-5% Higher','5-10% Higher','10+% Higher'];

widget.on('queryend',function(se,ev){
ev.rawResult.values.sort(function(a, b){
var aIndex = categories.indexOf(a[0].data.trim());
var bIndex = categories.indexOf(b[0].data.trim());

if (aIndex < bIndex)
return -1;
if (aIndex > bIndex)
return 1;

return 0;
})
})

View solution in original post

3 REPLIES 3

Sijo1995
9 - Travel Pro
9 - Travel Pro

Hi @zohebakber ,

Please try this script

var xOrder = 'asc' //also, this value can be desc
var yOrder = 'asc'

widget.on('beforeviewloaded', function(scope, args) {
    var shouldBeExecuted = (order === 'desc' || order === 'asc' )
    if (args.widget.type !== 'chart/scatter' || !shouldBeExecuted) {
        return
    }

    var daysOrder = args.options.xAxis.categories.sort();
    if (xOrder === 'desc') {
        daysOrder.reverse()   
    }


    if (daysOrder.length === args.options.xAxis.categories.length) {
        args.options.xAxis.categories = daysOrder
        for (i=0; i<daysOrder.length; i++) {
            for (k=0; k<args.options.series.length; k++) {
                for (j=0; j<args.options.series[k].data.length; j++) {                    
                    if (args.options.series[k].data[j].selectionData[0] === daysOrder[i]) {
                        args.options.series[k].data[j].x = i;
                    }
                }
            }
        }
    }
})


widget.on('beforequery', function(se, ev) {
    ev.query.metadata.forEach(function(m) {
        if (m.wpanel && m.wpanel === 'y-axis') {
            m.jaql.sort = yOrder
        }
    })
})

 

Thanks

Sijo

Hi Sijo,

Thanks for the reply..
I tried the below script and it worked..

 

var categories= ['10+% lower','5-10% lower','2-5% lower','0-2% lower','0%','0-2% Higher','2-5% Higher','5-10% Higher','10+% Higher'];

widget.on('queryend',function(se,ev){
ev.rawResult.values.sort(function(a, b){
var aIndex = categories.indexOf(a[0].data.trim());
var bIndex = categories.indexOf(b[0].data.trim());

if (aIndex < bIndex)
return -1;
if (aIndex > bIndex)
return 1;

return 0;
})
})

Anonymous
Not applicable

@zohebakber - Thanks for sharing the solution in the community. Very helpful. 

One small note: If the same sorting has to be performed on the Categories on the y-axis, just change the a[0] -> a[1] and b[0] to b[1]. Refer to the updated script below: 

var categories= ['New','Refurbished','Used','Unspecified'];

widget.on('queryend',function(se,ev){
	console.log(ev.rawResult.values); 
	ev.rawResult.values.sort(function(a, b){
		//Change a[1] b[1] to a[0] b[0] to apply the sort on the X-Axis Categories
		var aIndex = categories.indexOf(a[1].data);
		var bIndex = categories.indexOf(b[1].data);

		if (aIndex < bIndex)
			return -1;
		if (aIndex > bIndex)
			return 1;

		return 0;
	})
})