javascript - 将文本添加到饼图高位图的中心

标签 javascript css highcharts

我在将文本添加到饼图的中心后具有特定的字体、字体大小和字体颜色。如何使用 highcharts 对下面的代码执行此操作?

我已经尝试了很多方法来使用其他帖子来解决这个问题,但似乎没有任何方法可以完全回答这个问题并且确实有效。

这是我的代码:

$(function() {
    // Create the chart
    Highcharts.setOptions({
    colors: ['#26d248', '#323232']
});

    chart = new Highcharts.Chart({
      chart: {
            renderTo: 'summoner-pie-container',
            type: 'pie',
             backgroundColor:'transparent'

        }, plotOptions: {

        series: {
            marker: {
                states: {
                    hover: {
                        enabled: false
                    }
                }
            }
        },

        pie: {
            borderWidth: 0
        } 
    },


      title: {
text: '',
style: {
    display: 'none'
}
}, credits: {
  enabled: false
}, exporting: {
    buttons: {
        contextButton: {
            enabled: false
        }    
    }
   },
  tooltip: {
            formatter: function() {
                return '<b>'+ this.point.name +'</b>: '+ this.y;
            }
        },
        series: [{
            data: [["Damage Dealt",34000],["Team Damage Dealt",86423]],
            size: '60px',
            innerSize: '70%',
            dataLabels: {
                enabled: false
            }
        }]
    });


});

最佳答案

您可以使用渲染器在图表顶部绘制自定义元素: http://api.highcharts.com/highcharts/Renderer

您可以在 svg 元素上使用 css 方法来更改其样式: http://api.highcharts.com/highcharts/Element.css

例子:

// Render chart
var chart = Highcharts.chart(options);

// Create custom text element
var text = chart.renderer.text('100%').add(),
    textBBox = text.getBBox(),
    x = chart.plotLeft + (chart.plotWidth * 0.5) - (textBBox.width * 0.5),
    y = chart.plotTop + (chart.plotHeight * 0.5) + (textBBox.height * 0.25);
// Set position and styles
text.attr({ x: x, y: y }).css({ fontSize: '13px', color: '#666666' }).add();

实例:https://jsfiddle.net/zLhwqnq2/

关于javascript - 将文本添加到饼图高位图的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40322070/

相关文章:

html - 在 CSS Grid 1 行多列的行元素中

html - 如何解决 float 问题

html - 使用纯 CSS 绘制 3D 立方体

jquery - Highcharts 无法重新加载多个系列图表

javascript - Highcharts - 重叠的散点图标签不可读

javascript - Angular 6 多场滤镜

Javascript,如何匹配并获取包含在两个数组中的相似对象?

javascript - Meteor:通过 Chart-Div 重用模板

javascript - 将对象转换为 JSON

javascript - 一键切换两张图片