highcharts responsive datalabels display

半世苍凉 提交于 2019-12-13 02:38:27

问题


Is there any way to set the dataLabels of the highcharts to not overlap for smaller sizes?

I have a chart which is similar to this one but what I'd like to achieve is:

  • alternate the Y values (data Labels: 15, 40, and so on.)
  • avoid text overlapping, instead of number I have: "Long text" which overlaps at iPhone size

Is there any other option without specifying the x and y position of the dataLabels?

{
  "x": 1400457600000,
  "y": 15,
  "dataLabels": {
      "x": 25,
      "y": -5,
      "style": {
         "color": "#000000"
      }
   }
}

回答1:


You can use this example which detect a collision.

 events: {
                load: function() {
                    StaggerDataLabels(this.series);
                },
                redraw: function() {
                    var series = this.series;
                    setTimeout(function() {
                        StaggerDataLabels(series);
                    }, 1000);
                }
            },



回答2:


You can alternate them with a customer formatter. This function tracks how many times it's been called and only returns the label for the even calls:

plotOptions: {
        series: {
            dataLabels: {
                enabled: true,
                formatter: function(){
                    if (this.series.chart.dlFCalls == null){
                        this.series.chart.dlFCalls = -1;
                    }
                    this.series.chart.dlFCalls++;
                    if (this.series.chart.dlFCalls % 2 == 0){
                        return this.y;   
                    }
                }
            }
        }
    }

As far as fitting long text, I'd use a combination of rotation and offset the y value up a bit.

Here's a fiddle example.



来源:https://stackoverflow.com/questions/23739935/highcharts-responsive-datalabels-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!