javascript - Highcharts 数据标签 'callout' 形状不适用于圆环图

标签 javascript highcharts angular2-highcharts

我一直在尝试使用 Highcharts javascript 库为圆环图实现动态“标注”形状数据标签,其中数据标签的凹口指向相应的弧。 像这样的:https://imgur.com/yKhWKOu

我已经使用 highcharts 渲染器方法创建了“标注”形状,但无法使其动态化。这就是我现在得到的:/image/VXtuN.jpg 我的代码是:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
  <div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>


  <script type="text/javascript" src="https://code.highcharts.com/highcharts.js"></script>

<script type="text/javascript">
  (function(Highcharts) {
    Highcharts.Renderer.prototype.symbols.callout = function(x, y, w, h) {
      var arrowLength = 6,
        halfDistance = 6,
        r = Math.min(0, w, h),
        safeDistance = r + halfDistance,
        <!-- anchorX = options && options.anchorX,
        //anchorY = options && options.anchorY, 
        path;
      path = [
        'M', x + r, y,      // 
        'L', x + w - r, y, // top side  
        'C', x + w, y, x + w, y, x + w, y + r, // top-right corner  
        'L', x + w, y + h - r, // right side 
        'C', x + w, y + h, x + w, y + h, x + w - r, y + h, // bottom-right corner 
        'L', x + r, y + h, // bottom side 
        'C', x, y + h, x, y + h, x, y + h - r, // bottom-left corner 
        'L', x, y + r, // left side 
        'C', x, y, x, y, x + r, y // top-right corner
      ];
      path.splice(23, 
        3,
        'L',
        w / 2 + halfDistance,
        y + h,
        w / 2, 
        y + h + arrowLength,
        w / 2 - halfDistance, 
        y + h,
        x + r, 
        y + h
      );
      return path;
    };
  }(Highcharts));

  Highcharts.chart('container', {
    plotOptions: {
      pie: {
        dataLabels: {
          enabled: true,
          style: {
            fontWeight: 'bold',
            color: 'white'
          },
          connectorWidth: 0,
          distance: 10, 
          shape: 'callout',
          backgroundColor: 'red',
          <!-- backgroundColor: 'rgba(0, 0, 0, 0.75)', -->
          style: {
            color: '#FFFFFF',
            textOutline: 'none'
          }
        },
        startAngle: 0,
        endAngle: 360,
        center: ['50%', '50%']
      }
    },
    series: [{
      type: 'pie',
      innerSize: '80%',
      data: [
        ['Firefox', 10.38],
        ['IE', 56.33],
        ['Chrome', 24.03],
        ['Opera', 31.44]
      ]
    }]
  }, function(chart) {

  });
  </script>
  </body>

  </html>

“提前致谢”

最佳答案

我检查了这个主题,似乎在 Highcharts 中做起来相当棘手。

该库使用 SVGRenderer.label ( https://api.highcharts.com/class-reference/Highcharts.SVGRenderer#label ) 函数来创建数据标签。

仅当从 Highcharts 定义了 xAnchoryAnchor 且满足必要条件时,callout 形状标签中才会出现 V 形(小箭头) .Renderer.prototype.symbols.callout 功能得到满足( https://github.com/highcharts/highcharts/blob/master/js/parts/SvgRenderer.js )。当为饼图系列生成数据标签时,这些值未定义 - V 形符号不会出现。

您已覆盖此函数并对标签路径进行硬编码 - 当您希望它具有响应能力时,这不是解决方案。

这是我发现的解决方法:

我将 dataLabels.format 设置为空字符串 - 生成了标签,但它们不可见,因为它们没有内容。我使用它们在 chart.events.render 中的位置来生成新标签:

  render: function() {
    var chart = this,
      series = chart.series[0],
      renderer = chart.renderer;

    series.points.forEach(function(p) {
      var dl = p.dataLabel,
        x = dl.x + chart.plotLeft,
        y = dl.y + chart.plotTop,
        chartCenterX = chart.chartWidth / 2,
        chartCenterY = chart.chartHeight / 2,
        anchorX,
        anchorY;

      // destroy the old label
      if (dl.customLabel) {
        dl.customLabel.destroy();
      }

      // definitions for all the directions
      if (x < chartCenterX && y < chartCenterY) { // right bottom corner chevron
        anchorX = x + 30;
        anchorY = y + 50;
      } // more should be added here

      // create custom label
      dl.customLabel = renderer.label(p.name, x, y, 'callout', anchorX, anchorY).attr({
        fill: 'blue'
      }).css({
        color: 'white'
      }).add();

    });
  }

现场演示: http://jsfiddle.net/kkulig/8s968m7f/

这里最大的挑战是找到合适的 anchor 坐标。我只对绘图区域左上角的标签执行了此操作(仅用于示例目的 - 需要找到更好的公式)。我使用图表的尺寸来计算这些值。

这不是非常优雅的解决方案,但如果您找到合适的公式并进行一些编码,它就会起作用。

关于javascript - Highcharts 数据标签 'callout' 形状不适用于圆环图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48213120/

相关文章:

java - 如何将 javascript 生成的报告导出为 pdf?

jquery - 图表不可见

javascript - 无法读取未定义的 highcharts highstock 的属性 'type'

javascript - OWL Carousel 图片一开始大小不合适

javascript - 如何向下滚动单击按钮 1 次

javascript - 为什么很难根据固定柱形图(highcharts)中的数据动态设置点填充和放置?

javascript - 如何向连续更新 Angular Highcharts 添加标志

angular - 从我的浏览器/服务器获取时区以注入(inject) angular2-highcharts

angular - 如何在 typescript 的 Angular2 Highcharts 中添加鼠标滚轮代码

javascript - 在 JSON 中编码日期的最佳实践是什么?