highcharts - 为缺失的列添加标签

时间:2017-06-14 11:28:25

标签: highcharts

在我的图表中,我有一个值为-1的列。这表示此系列的值不可用。

在图表中,我想添加一个旋转90度的标签,上面写着"没有数据"酒吧应该在哪里。有人能为我提供一个如何实现这一目标的例子吗?

1 个答案:

答案 0 :(得分:2)

如果你有一个-1的列,它仍然会被渲染。您需要将-1值映射为null。之后,您可以使用renderer.text函数并为空列渲染旋转的文本。

渲染/重绘标签的功能:

  function renderLabels() {
    this.series[0].points.forEach((point, i) => {
      if (point.isNull) {
        point.noDataLabel = this.renderer.text('No data', 0, -9e9).attr({
          rotation: 90,
          zIndex: 99
        }).add()
      }
    })

    redrawLabels.call(this)
}

function redrawLabels() {
  this.series[0].points.forEach(point => {
    if (point.noDataLabel) {
      point.noDataLabel.attr({
        x: point.series.group.translateX + point.plotX,
        y: this.plotTop + this.plotHeight - 80
      })
    }
  })
}

在加载和重绘事件上附加函数,并映射数据:

Highcharts.chart('container', {
    chart: {
        type: 'column',
        events: {
          load: renderLabels,
          redraw: redrawLabels
        }
    },
    series: [{
        data: data.map(v => v === -1 ? null : v)
    }]
});

示例和输出

http://jsfiddle.net/n3nuhvL7/

no data label