如何各个积累列中的项目进行排序?(How to sort the items within each

2019-10-18 16:56发布

如何各个积累列中的项目进行排序? ASC或DESC。

Answer 1:

每个系列添加到图表绘制在它被接收的顺序在图表上。 要更改图表系列,您将需要更改系列的顺序是在你的系列项目列表中的第一个。

话虽这么说 - 我想你想要做的是,独立系列顺序,排序上的每个堆栈的值。 我不认为这是可能的HighCharts。



Answer 2:

您只能设置意甲全球指数,但你不能定位每个单一的“栈”。

http://api.highcharts.com/highcharts#series.index



Answer 3:

您可以使用下面的脚本来按类别名称堆积图栏排序。

var sortData = function(chartSource) {
    var series = chartSource.series;
    var axis = chartSource.xAxis[0];
    var categories = [];

    if($.isArray(series)) {
        var ser = 
          $.grep(series, function(ser, seriesIndex) 
          {
              return ser.visible;
          })[0];

        $.each(ser.data, 
            function(dataIndex, datum) 
          {
            console.log(datum.category + ':' + datum.stackTotal);
            var obj = {
                name: datum.category,
                index: dataIndex,
                stackTotal: datum.stackTotal
            }
            categories.push(obj);
            }
          );
    }

    categories.sort(function(a, b) {
        var aName = a.name.toLowerCase();
        var bName = b.name.toLowerCase();
        var aTotal = a.stackTotal;
        var bTotal = b.stackTotal;
        //if(aTotal === bTotal) {
            return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
        //} else {
        //    return ((aTotal > bTotal) ? -1 : ((aTotal < bTotal) ? 1 : 0));
        //}
    });

    var mappedIndex = $.map(categories, function(category, index) {
        return category.index;
    });

    categories = $.map(categories, function(category, index) {
        return category.name;
    });

    console.log(categories);
    console.log(mappedIndex);
    axis.setCategories(categories);

    var newDataArray = [];

    $.each(series, function(seriesIndex, ser) {
        newDataArray = [];
        var data = $.map(mappedIndex, function(mappedIndex2, origIndex) {
            var ydata = ser.data[mappedIndex2];
            if(ydata.y!=null){
                var y = ydata.y
                newDataArray.push(y);
                return y;
            }
            else
            { 
                newDataArray.push(null);
                return null;
            }

        });
        ser.setData(newDataArray);
    });


};


文章来源: How to sort the items within each stacking column?
标签: highcharts