Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

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

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
        }]
    }]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...