【问题标题】:How to position dataLabels at the midpoint of an arearange chart in highcharts?如何将dataLabels定位在highcharts中arearange图表的中点?
【发布时间】:2020-10-16 10:11:49
【问题描述】:
Highcharts.chart('container', {
  chart: {
    type: 'arearange',
    zoomType: 'x',
    scrollablePlotArea: {
      minWidth: 600,
      scrollPositionX: 1
    }
  },

  title: {
    text: 'Temperature variation by day'
  },

  xAxis: {
    //type: 'datetime',
    accessibility: {
      rangeDescription: 'Range: Jan 1st 2017 to Dec 31 2017.'
    }
  },

  yAxis: {
    title: {
      text: null
    },
    labels: {
      enabled:true
    }
  },
  plotOptions: {
    arearange: {
      dataLabels: {
        enabled: true,
        yLow: 5,
        verticalAlign: 'bottom',
        inside: true,
        color: 'black',
        formatter: function(e) { 
          if((this.x==0) && (this.point.low==this.y))
            return this.series.name
        }
      }
    }
  },

  legend: {
    enabled: false
  },

  series: [{
    name: "AREA 1",
    data: [
      [0, 10],
      [0, 10],
    ],
    borderColor: 'black',
    borderWidth: 0.2,
  },
    {
    name: "AREA 2",
    data: [
     [20, 40],
     [20, 40],
    ]
  }]
});

以上是我使用 arearange 图表绘制的示例代码。正如我想要的那样,该图已成功绘制。但是好像有问题。我希望系列名称“AREA1”和“AREA2”显示在各自 y 轴值的中点。即“区域 1”应显示在 5(0 和 10 的中点),“区域 2”应显示在 30(20 和 40 的中点)。有没有办法做到这一点?我尝试在 plotOptions 中指定 yLow 值。但它对我不起作用,因为系列数据是动态的。

我使用的图表是 highcharts,版本是 4.1.5

请帮忙!!提前致谢

【问题讨论】:

    标签: javascript php jquery charts highcharts


    【解决方案1】:

    您可以添加带有禁用标记和启用数据标签的模拟分散系列,以在适当的位置显示系列名称:

        series: [..., {
                linkedTo: 0,
                dataLabels: {
                    format: 'AREA 1'
                },
                type: 'scatter',
                data: [(area1Data[0][0] + area1Data[0][1]) / 2]
            }, {
                linkedTo: 1,
                dataLabels: {
                    format: 'AREA 2'
                },
                type: 'scatter',
                data: [(area2Data[0][0] + area2Data[0][1]) / 2]
            }
        ]
    

    现场演示: http://jsfiddle.net/BlackLabel/d01e2ymx/

    API 参考: https://api.highcharts.com/highcharts/series.scatter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多