javascript - Sort the series data for every X-Axis in Highcharts - Stack Overflow

admin2025-04-20  0

I need to sort the data of series from largest to smallest for every series.

Sample fiddle

    series: [{
        name: 'John',
        data: [{
            y: 1}, {y: 2}, {y: 3}, {y: 4}, {y: 5
        }]
    }, {
        name: 'Jane',
        data: [{
            y: 5}, {y: 4}, {y: 3}, {y: 2}, {y: 1
        }]
    }, {
        name: 'Joe',
        data: [{
            y: 5}, {y: 2}, {y: 3}, {y: 4}, {y: 1
        }]
    }]

I need to sort the data of series from largest to smallest for every series.

Sample fiddle

    series: [{
        name: 'John',
        data: [{
            y: 1}, {y: 2}, {y: 3}, {y: 4}, {y: 5
        }]
    }, {
        name: 'Jane',
        data: [{
            y: 5}, {y: 4}, {y: 3}, {y: 2}, {y: 1
        }]
    }, {
        name: 'Joe',
        data: [{
            y: 5}, {y: 2}, {y: 3}, {y: 4}, {y: 1
        }]
    }]
Share Improve this question edited Jan 4, 2016 at 12:43 HaveNoDisplayName 8,527106 gold badges40 silver badges50 bronze badges asked Jan 2, 2016 at 11:26 BabuBabu 1653 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You can use the sort function. It can be applied like that:

series.forEach(function(name){
  name.data.sort(function (a,b) {
    if(a.y < b.y) {
      return 1;
    } else if (a.y > b.y) {
      return -1;
    }
    return 0;
  });
});

To make the code more understandable you can create a series variable and then sort it before calling the highcharts function. This is demonstrated here.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745123608a286295.html

最新回复(0)