Sometimes we may need to move an item (or items) in legend to a specific position, for example moving 'N\A', 'Unknown', 'Not available' etc to the end of list. We can achieve this using below script.
Supported widget types : Bar chart, Line chart, Column Chart, Area Chart, Pie Chart
widget.on('processresult', function(se, args){
var itemList = [{"name":"Unknown", "position":-1, "color":"grey"}]
$.each(itemList, function(index, item){
var old_index
$.each(args.result.series, function(index, value){
if(value.name.toUpperCase() == item.name.toUpperCase())
{
value.color = ((item.color == '' || !item.color) ? value.color : item.color)
old_index = index
}
})
moveItem(args.result.series, old_index, item.position)
})
//Function to move an item at old_index to new_index
function moveItem(array, old_index, new_index)
{
while (old_index < 0) {
old_index += array.length;
}
while (new_index < 0) {
new_index += array.length;
}
array.splice(new_index, 0, array.splice(old_index, 1)[0]);
}
})
Here itemList is the variable where we specify item, position and its color.
name: Name of item in legend
position: Index where we need to move the item (new position).
0 - Move item to first position
1 - Move item to 2nd position
-1 - Move item to last position
color: Color of legend. This is an optional parameter.
We can add multiple items in itemList
Example:
var itemList = [{"name":"South", "position":0, "color":"Green"},
{"name":"West", "position":1, "color":"Blue"},
{"name":"Unknown", "position":-1, "color":"grey"}]
Result:
Commenti