Highcharts - 如何强制dataLabels在重叠内容时显示标签

时间:2018-03-05 23:23:09

标签: javascript charts highcharts overlap

我需要显示所有数据(即使一个数据与另一个重叠),但是当这些数据非常接近时,会自动消失:

http://jsfiddle.net/vdgphk3L/

enter image description here

documentation推荐allowOverlap,但在我使用时会出现两个值Min和Max,但我只需要Min:

plotOptions: {
    series: {
        dataLabels: {
            enabled: true,
            allowOverlap: true
        }
    }
}

完整代码:

$(function() {

  $('#container').highcharts({

    chart: {
      type: 'columnrange',
      inverted: false,
    },

    xAxis: {
      categories: ['Title 1', 'Title 2', 'Title 3', 'Title 4', 'Title 5'],
      alternateGridColor: '#F6F9FC',
    },

    tooltip: {
      shared: true,
      width: 20,
      formatter: function() {
        var retorno = '<strong>Prices</strong>';

        $.each(this.points, function(i, point) {
          if (point.series.name == 'Min' || point.series.name == 'Med' || point.series.name == 'Max') {
            retorno += '<br /><span style="color:' + point.color + '">\u25CF</span> ' + point.series.name + ': ' + point.y;
          }
        });

        return retorno;
      }
    },

    plotOptions: {
      columnrange: {
        grouping: false,
        pointWidth: 70
      },
      series: {
        lineWidth: 0.5
      }
    },

    series: [

      // Min
      {
        name: 'Min',
        color: '#C30000',
        minPointLength: 25,
        data: [
          [2000, 2001],
          [5000, 5001],
          [1000, 1001],
          [8000, 8001],
          [3500, 3501]
        ],
        dataLabels: {
          inside: true,
          enabled: true,
          align: 'right',
          format: '{point.y}',
        }
      }, {
        name: 'Min (Line)',
        color: '#C30000',
        type: 'spline',
        dashStyle: 'shortdot',
        data: [
          [2000],
          [5000],
          [1000],
          [8000],
          [3500]
        ]
      },

      // Med
      {
        name: 'Med',
        color: '#215994',
        minPointLength: 25,
        data: [
          [3000, 3001],
          [6001, 6002],
          [3001, 3002],
          [9001, 9002],
          [4001, 4160]
        ],
        dataLabels: {
          enabled: true,
          inside: true,
          align: 'center',
          format: '{point.y}'

        }
      }, {
        name: 'Med (Line)',
        color: '#215994',
        type: 'spline',
        dashStyle: 'shortdot',
        data: [
          [3000],
          [6001],
          [3001],
          [9001],
          [4001]
        ]
      },

      // Max
      {
        name: 'Max',
        color: '#ECEC09',
        minPointLength: 25,
        data: [
          [4000, 4001],
          [7001, 7002],
          [5001, 5002],
          [9501, 9502],
          [4501, 4502]
        ],
        dataLabels: {
          enabled: true,
          inside: true,
          align: 'left',
          format: '{point.y}'
        }
      }, {
        name: 'Max (Line)',
        color: '#ECEC09',
        type: 'spline',
        dashStyle: 'shortdot',
        data: [
          [4000],
          [7001],
          [5001],
          [9501],
          [4501]
        ]
      }

    ]

  });

});

1 个答案:

答案 0 :(得分:1)

通过删除您添加的dataLabel格式并添加新格式,我可以使用allowOverlap实现您的目标。

plotOptions: {
  columnrange: {
    dataLabels: {
      allowOverlap: true,
      formatter: function() {
        if (this.y == this.point.low) { 
        //this is needed because highcharts draws 2 boxes for columnrange
        //1 for high series and 1 for low series
          return this.point.low;
        }
      }
    }
  },
}

在小提琴中,我还将每个系列中的一致情节选项移至plotOptions

&#13;
&#13;
function numberToReal(numero) {
  var numero = numero.toFixed(2).split('.');
  numero[0] = "R$ " + numero[0].split(/(?=(?:...)*$)/).join('.');
  return numero.join(',');
}

$(function() {

  $('#container').highcharts({

    chart: {
      type: 'columnrange',
      inverted: false,
    },

    xAxis: {
      categories: ['Title 1', 'Title 2', 'Title 3', 'Title 4', 'Title 5'],
      alternateGridColor: '#F6F9FC',
    },

    tooltip: {
      shared: true,
      width: 20,
      formatter: function() {
        var retorno = '<strong>Prices</strong>';

        $.each(this.points, function(i, point) {
          if (point.series.name == 'Min' || point.series.name == 'Med' || point.series.name == 'Max') {
            retorno += '<br /><span style="color:' + point.color + '">\u25CF</span> ' + point.series.name + ': ' + numberToReal(point.y);
          }
        });

        return retorno;
      }
    },

    plotOptions: {
      columnrange: {
        grouping: false,
        pointWidth: 70,
        minPointLength: 25,
        dataLabels: {
          inside: true,
          enabled: true,
          allowOverlap: true,
          formatter: function() {
            if (this.y == this.point.low) {
        	  return this.point.low;
            }
          }
        }
      },
      series: {
        lineWidth: 0.5
      }
    },

    series: [

      // Min
      {
        name: 'Min',
        color: '#C30000',
        data: [
          [2000, 2001],
          [5000, 5001],
          [1000, 1001],
          [8000, 8001],
          [3500, 3501]
        ],
        dataLabels: {
          align: 'right',
        }
      }, {
        name: 'Min (Line)',
        color: '#C30000',
        type: 'spline',
        dashStyle: 'shortdot',
        data: [
          [2000],
          [5000],
          [1000],
          [8000],
          [3500]
        ]
      },

      // Med
      {
        name: 'Med',
        color: '#215994',
        data: [
          [3000, 3001],
          [6001, 6002],
          [3001, 3002],
          [9001, 9002],
          [4001, 4160]
        ],
        dataLabels: {
          align: 'center',
        }
      }, {
        name: 'Med (Line)',
        color: '#215994',
        type: 'spline',
        dashStyle: 'shortdot',
        data: [
          [3000],
          [6001],
          [3001],
          [9001],
          [4001]
        ]
      },

      // Max
      {
        name: 'Max',
        color: '#ECEC09',
        data: [
          [4000, 4001],
          [7001, 7002],
          [5001, 5002],
          [9501, 9502],
          [4501, 4502]
        ],
        dataLabels: {
          align: 'left',
        }
      }, {
        name: 'Max (Line)',
        color: '#ECEC09',
        type: 'spline',
        dashStyle: 'shortdot',
        data: [
          [4000],
          [7001],
          [5001],
          [9501],
          [4501]
        ]
      }

    ]

  });

});
&#13;
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
&#13;
&#13;
&#13;

修改:感谢this answer我理解为什么要绘制2个标签。我已经更新了这篇文章,现在它只会绘制1个标签。

工作示例: http://jsfiddle.net/ewolden/vdgphk3L/80/