气泡图中的文字省略号

时间:2017-04-06 09:37:01

标签: text highcharts overlap ellipsis bubble-chart

我正在使用Highcharts的气泡图,气泡内的标签文字是动态的,有时可能比气泡本身更大,

我想知道是否有办法根据包含它的气泡的大小来制作文本省略号?

  containerOptions = {
        chart: {
            type: 'bubble',
            renderTo: $(container)[0],
            events: {
                drilldown: function (e) {
                    if (!e.seriesOptions) {

                        var chart = this,
                            drilldowns = {
                                'Animals': {
                                    name: 'Animals',
                                    data: [
                                        {name: 'Dogs', y:2, x:10, z: 7, drilldown: true},
                                        {name: 'Cats', y:4, x:12, z: 7}
                                    ]
                                },
                                'Dogs': {
                                    name:"Dogs",
                                    data: [
                                        {name: 'Pitbull', y:3.7, x:7.6, z: 5, drilldown: false},
                                        {name: 'German shepherd', y:6.7, x:6.9, z: 5, drilldown: false}
                                    ]
                                }
                            },
                            series = drilldowns[e.point.name];
                        chart.showLoading('Loading..');
                        setTimeout(function () {
                            chart.hideLoading();
                            chart.addSeriesAsDrilldown(e.point, series);
                        }, 1000);

                    }
                }
            }
        },
        plotOptions: {
            series: {
                borderWidth: 0,
                dataLabels: {
                    enabled: true,
                    style: { color: 'red' },
                    format: '{point.name}'

                }
            }
        },
        series: [{
            name: 'Things',
            colorByPoint: true,
            data: [{
                name: 'Animals',
                y: 5,
                x: 1,
                z: 9,
                drilldown: true
            }, {
                name: 'Fruits',
                y: 2,
                x: 9,
                z: 9,
                drilldown: false
            }
            ]
        }],

        drilldown: {
            series: [],
            drillUpButton: {
                relativeTo: 'spacingBox',
                position: {
                    y: 0,
                    x: 0
                }
            }
        }

    }
}

1 个答案:

答案 0 :(得分:1)

您可以遍历加载/重绘事件中的数据标签,并根据气泡的宽度和文本宽度添加/删除省略号。

function applyEllipsis() {
  var series = this.series[0];
  var options = series.options.dataLabels;

  series.points.forEach(p => {
    var r = p.marker.radius;
    var label = p.dataLabel;
    var text = label.text.textStr;
    var bbox = label.getBBox(true);

    while (bbox.width > 2 * r && text.length !== 1) {
      text = text.slice(0, -1);
      p.dataLabel.attr({
        text: text + '\u2026'
      });
      bbox = label.getBBox(true);
    }

    p.dataLabel.align({
      width: bbox.width,
      height: bbox.height,
      align: options.align,
      verticalAlign: options.verticalAlign
    }, null, p.dlBox);

  });
}

在加载/重绘

上附加功能
Highcharts.chart('container', {
  chart: {
    type: 'bubble',
    events: {
      load: applyEllipsis,
      redraw: applyEllipsis
    }
  },

示例:http://jsfiddle.net/12d997o4/