javascript - highcharts donut 饼图中的多行文本

标签 javascript highcharts pie-chart

我正在尝试在我的 highcharts 圆环饼图中实现居中的多行文本。我希望第一行的字体大小为 50px,第二行的字体大小为 25px。

目前,我无法在每行上获得不同的字体大小,也无法使文本居中。

JSFiddle:https://jsfiddle.net/r7tp9wp6/

这是我渲染标题的方式

 var r = this.renderer,
        x = this.series[0].center[0] + this.plotLeft,
        y = this.series[0].center[1] + this.plotTop;
      this.title = r.text('Top<br>Bottom', 0, 0)
        .css({
          fontSize: '25px',
          textAlign: "center",
        }).hide()
        .add();

最佳答案

使用 textAnchor:'middle' 更新 renderer

 this.title = r.text('1,234 <br> <span style="font-size:25px">Calls Today</span>', 0, 0)
    .css({
      fontSize: '40px',
      textAnchor:'middle'
    }).hide()
    .add();

$(function() {

  Highcharts.setOptions({
    colors: ['#79CEB8', '#66041A', '#0099CC', '#1D3461']
  });

  var chart = new Highcharts.Chart({
      chart: {
        renderTo: 'container',
        type: 'pie'
      },
      title: {
        align: 'center',
        verticalAlign: 'middle'
      },
      yAxis: {
        title: {
          text: ''
        }
      },
      plotOptions: {
        pie: {
          shadow: false
        }
      },
      tooltip: {
        formatter: function() {
          return '<b>' + this.point.name + '</b>: ' + this.y + ' %';
        }
      },
      series: [{
        name: 'Browsers',
        data: [
          ["Bypass", 10],
          ["No IVR", 20],
          ["Handled", 7],
          ["Required Operator", 7]
        ],
        size: '100%',
        innerSize: '80%',
        showInLegend: false,
        dataLabels: {
          enabled: true
        }
      }]
    },
    function addTitle() {

      if (this.title) {
        this.title.destroy();
      }

      var r = this.renderer,
        x = this.series[0].center[0] + this.plotLeft,
        y = this.series[0].center[1] + this.plotTop;
      this.title = r.text('1,234 <br> <span style="font-size:25px">Calls Today</span>', 0, 0)
        .css({
          fontSize: '40px',
          textAnchor: 'middle'
        }).hide()
        .add();

      var bbox = this.title.getBBox();
      this.title.attr({
        x: x - (bbox.width / 100),
        y: y
      }).show();
    }
  );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>

<div id="container" style="min-width:c310px; height: 400px; max-width: 600px; margin: 0 auto"></div>

关于javascript - highcharts donut 饼图中的多行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49601548/

相关文章:

Highcharts 气泡图数据标签重叠

javascript - 使用饼图时出错

css - CSS 饼图中的切片偏移

mysql - 在 highstock 上绘制多个系列数据时遇到问题

javascript - 无法确定 req.body.name 的值 - 表示 Node js

javascript - 在 SVG 定义的视口(viewport)内绘制多边形

javascript - Leafletjs - GeoJSON 层未显示

javascript - Firebase 数据未在 HighCharts 中绘制

javascript - 使饼图变小 Chart JS

javascript - Angular Google Charts - 自动高度