简体   繁体   English

X轴上使用不同Y轴的Highcharts列类别

[英]Highcharts column category in X-axis using different Y-axis

I'm using a column chart to display average result. 我正在使用柱形图显示平均结果。 On the X-axis, I want to show the average vital of female users, male users and also both genders. 在X轴上,我想显示女性用户,男性用户以及男女的平均生命。 However, the Y-axis range is quite different as the smallest reading is < 30 and the largest reading is > 1000 (BMR). 但是,Y轴范围差别很大,因为最小读数小于30,最大读数大于1000(BMR)。 This make the chart became harder to read as the columns of < 30 readings are almost invisible. 这使得图表变得更难以阅读,因为<30个读数的列几乎是不可见的。 How can I make the reading of > 1000(BMR) to use another Y-axis? 如何读取> 1000(BMR)以使用另一个Y轴?

在此处输入图片说明

I've tried to use linkedTo in series 我试图在系列中使用linkedTo

            {
                name: 'Male',
                linkedTo: ':previous',
                stack: 10,
                yAxis: 1,
                tooltip: { valueSuffix: ` of total {point.numOfUser} users` },
                dataLabels: { enabled: true },
                data: [this.state.maleAvrBmr],
            }, {
                name: 'Female',
                linkedTo: ':previous',
                stack: 11,
                yAxis: 1,
                tooltip: { valueSuffix: ` of total {point.numOfUser} users` },
                dataLabels: { enabled: true },
                data: [this.state.femaleAvrBmr],
            }, {
                name: 'Male & Female',
                linkedTo: ':previous',
                stack: 12,
                yAxis: 1,
                tooltip: { valueSuffix: ` of total {point.numOfUser} users` },
                dataLabels: { enabled: true },
                data: [this.state.allAvrBmr],
            }

but the X-axis categories is messed up as shown. 但X轴类别却被弄乱了,如图所示。 在此处输入图片说明

Any help is appreciated. 任何帮助表示赞赏。 Thanks in advance and sorry for my english. 在此先感谢您,我的英语很抱歉。

This issue is quite difficult because each series can be bind to separate yAxis but there is no way to bind particular points in one series. 这个问题非常困难,因为每个系列都可以绑定到单独的yAxis,但是无法绑定一个系列中的特定点。 The workaround is to add an additional series with null points so that one group of columns can be bind to the second yAxis: 解决方法是添加一个带有空点的附加序列,以便可以将一组列绑定到第二个yAxis:

  series: [{
    name: 'Tokyo',
    id: 'Tokyo',
    color: '#7BB5EC',
    data: [49.9, 71.5, null, 129.2]
  }, {
    name: 'New York',
    id: 'New York',
    color: '#000',
    data: [83.6, 78.8, null, 93.4]
  }, {
    name: 'London',
    id: 'London',
    color: '#90ED7D',
    data: [48.9, 38.8, null, 41.4]
  }, {
    name: 'Berlin',
    id: 'Berlin',
    color: '#F7A35B',
    data: [42.4, 33.2, null, 39.7]
  }, {
    linkedTo: 'Tokyo',
    yAxis: 1,
    color: '#7BB5EC',
    data: [null, null, 1006.4, null]
  }, {
    linkedTo: 'New York',
    yAxis: 1,
    color: '#000',
    data: [null, null, 906.2, null]
  }, {
    linkedTo: 'London',
    yAxis: 1,
    color: '#90ED7D',
    data: [null, null, 553.1, null]
  }, {
    linkedTo: 'Berlin',
    yAxis: 1,
    color: '#F7A35B',
    data: [null, null, 696.8, null]
  }]

Demo: 演示:

However, it causes another problem. 但是,这引起了另一个问题。 The groups of columns are not properly centered, because null points take space. 列组未正确居中,因为空点占用空间。 This issue can be resolved by adding a custom logic that will center the group without computing space for null points. 可以通过添加自定义逻辑来解决此问题,该逻辑将使组居中,而无需为空点计算空间。 Check demo and code posted below. 检查下面的演示和代码。

Code: 码:

function centerColumns(chart) {
  var xAxis = chart.xAxis[0],
    categories = xAxis.categories,
    catLen = categories.length,
    catOffset = xAxis.width / catLen,
    catStart = catOffset / 2,
    sortedPoints = {},
    tempPoint1 = chart.series[0].points[0],
    tempPoint2 = chart.series[1].points[0],
    pointWidth = tempPoint1.pointWidth,
    spaceWidth = tempPoint2.barX - tempPoint1.barX - pointWidth,
    category,
    pointsLen,
    groupWidth,
    point,
    newX,
    cat,
    key,
    i;

  for (i = 0; i < catLen; i++) {
    sortedPoints[categories[i]] = {
      plotLeft: catStart + i * catOffset,
      points: []
    }
  }

  chart.series.forEach(function(series) {
    series.points.forEach(function(p) {
      cat = p.category;

      if (!p.isNull) {
        sortedPoints[cat].points.push(p);
      }
    });
  });

  for (key in sortedPoints) {
    category = sortedPoints[key];
    pointsLen = category.points.length;
    groupWidth =
      pointsLen * pointWidth + (pointsLen - 1) * spaceWidth;

    for (i = 0; i < pointsLen; i++) {
      point = category.points[i];
      newX = category.plotLeft - groupWidth / 2 +
        i * (pointWidth + spaceWidth);

      point.graphic.attr({
        x: newX
      });
    }
  }
}

...

chart: {
  type: 'column',
  height: 300,
  events: {
    render: function() {
      centerColumns(this);
    }
  }
}

Demo: 演示:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM