【问题标题】:Highcharts - stacked column - order series index dynamically for each categoryHighcharts - 堆积列 - 为每个类别动态排序系列索引
【发布时间】:2019-07-05 09:38:33
【问题描述】:

我有一个场景,我正在绘制一个堆积柱形图,目前卡在一个点,我需要以特定顺序对系列进行排序,以防同一天有多个数据点。问题是,每天的系列顺序可能不同 - 我有下面的代码和示例 -

Highcharts.chart('container', {
chart: {
    type: 'column'
},
title: {
    text: 'Stacked column chart'
},
xAxis: {
    categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
},
yAxis: {
    min: 0,
    title: {
        text: 'Total fruit consumption'
    },
    stackLabels: {
        enabled: true,
        style: {
            fontWeight: 'bold',
            color: (Highcharts.theme && Highcharts.theme.textColor) || 
    'gray'
        }
    }
},
legend: {
    align: 'right',
    x: -30,
    verticalAlign: 'top',
    y: 25,
    floating: true,
    backgroundColor: (Highcharts.theme && Highcharts.theme.background2) 
      || 'white',
    borderColor: '#CCC',
    borderWidth: 1,
    shadow: false
 },
tooltip: {
    headerFormat: '<b>{point.x}</b><br/>',
    pointFormat: '{series.name}: {point.y}<br/>Total: 
                 {point.stackTotal}'
},
plotOptions: {
    column: {
        stacking: 'normal',
        dataLabels: {
            enabled: true,
            color: (Highcharts.theme && 
                   Highcharts.theme.dataLabelsColor) || 'white'
        }
    }
},
series: [{
    name: 'John',
    data: [5, 3, 4, 7, 2]
}, {
    name: 'Mary',
    data: [2, 2, 3, 2, 1]
}, {
    name: 'Kevin',
    data: [3, 4, 4, 2, 5]
}]
});

JSFiddle : http://jsfiddle.net/arj_ary/4kz7rdpn/2/

要求:在上面的 JSFiddle 中,对于每个类别,我希望最低的数据点显示在顶部。所以对于苹果,我希望 Mary 在顶部,因为它的值为 2。对于橘子和梨也是如此。

这可以实现吗?任何帮助表示赞赏。

【问题讨论】:

  • 我能做的最好的事情是得到每个人的总数,然后对数据进行排序,但我认为这不是你所要求的。我认为您希望每列从大到小,底部最大。
  • JSFiddle 示例:jsfiddle.net/jeffld/kseqow0m/3
  • @jeffld 很好的尝试,但是对我不起作用。我需要对系列本身进行排序,该类别的数据最少,始终位于顶部。 :-)

标签: javascript jquery highcharts react-highcharts


【解决方案1】:

可以使用这种方法来完成:

  1. 创建一个对列进行排序的函数:
  • 遍历点并按类别(苹果、橙子等)对它们进行分组
  • 循环遍历每组点并按 y 值对组进行排序
  • 循环遍历每组中的点并使用SVGElement.attr 方法设置新计算的y 位置(point.graphic 是一个SVGElement 实例)。对point.dataLabel 执行相同操作,并将新位置设置为point.tooltipPos 数组(否则工具提示将被错误放置)。
function sortColumns() {
  var chart = this,
    pointsByCat = {},
    zeroPixels,
    bottomYPositive,
    bottomYNegative,
    shapeArgs;

  chart.series.forEach(function(serie) {
    serie.points.forEach(function(point, index) {

      if (pointsByCat[point.category] === undefined) {
        pointsByCat[point.category] = [];
      }

      pointsByCat[point.category].push(point);
    });
  });

  Highcharts.objectEach(pointsByCat, function(points, key) {
    zeroPixels = chart.yAxis[0].toPixels(0) - chart.plotTop;
    bottomYPositive = bottomYNegative = zeroPixels;

    points.sort(function(a, b) {
      return b.y - a.y;
    });

    points.forEach(function(point) {
      if (point.series.visible) {
        if (point.shapeArgs.y < zeroPixels) {

          // Positive values
          point.graphic.attr({
            y: bottomYPositive - point.shapeArgs.height
          });

          point.dataLabel.attr({
            y: bottomYPositive - point.shapeArgs.height / 2 - point.dataLabel.height / 2
          });

          point.tooltipPos[1] = bottomYPositive - point.shapeArgs.height;
          bottomYPositive = bottomYPositive - point.shapeArgs.height;
        } else {

          // Negative values
          point.graphic.attr({
            y: bottomYNegative
          });

          point.dataLabel.attr({
            y: bottomYNegative + point.shapeArgs.height / 2 - point.dataLabel.height / 2
          });

          point.tooltipPos[1] = bottomYNegative;
          bottomYNegative = bottomYNegative + point.shapeArgs.height;
        }
      }
    });
  });
}



2) 将上述函数设置为chart.events.loadchart.events.redraw 事件的回调:

  chart: {
    type: 'column',
    events: {
      load: sortColumns,
      redraw: sortColumns
    }
  }

演示:

API 参考:

【讨论】:

  • 在上面的pointsByCat中选择最后一个点的shapeArgs背后的逻辑是什么?这一行: - shapeArgs = points[points.length - 1].shapeArgs;上面的逻辑有一个缺陷,列开始浮动,因为设置新的“y”和“bottomY”的逻辑不清楚。
  • 选择最后一个点是因为点被排序并呈现在从堆栈顶部的最后一个开始的新位置。你能重现这个问题吗?
  • 如果列需要按另一个属性而不是“y”值排序,我应该在上述逻辑中进行哪些更改?比如说他们需要按日历日期排序?在这种情况下,我可以将排序函数更改为按 calendarDate 排序,但 shapeArgs 是从数组中的最后一个元素中获取的?
  • 是的,它是从最后一个元素中提取的,因为点是从最后一个点开始渲染的。整个点堆栈的高度相同,无论列如何排序,顺序都会改变。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-04
相关资源
最近更新 更多