node.js - 对齐高图甜甜圈中的标签
问题描述
我正在尝试制作一个侧面带有图例的高图甜甜圈,我真的很难让数据标签更加居中。此刻他们每个人都在不同的地方,
预期结果的图像:
https://codepen.io/mattdavidson/pen/qgqZyV
Highcharts.chart('container', {
credits: { enabled: false },
chart: { height: 300, width: 500, animation: false },
title: {
align: 'center',
verticalAlign: 'middle',
text: 10,
y: 25,
x: 55,
style: { color: '#333333', fontSize: '72px', fontWeight:'bold'},
},
plotOptions: {
pie: {
dataLabels: {
enabled: true,
format: '{point.y}',
distance: -25,
style: { fontSize: '32px', textOutline: 0 },
},
animation: false,
showInLegend: true,
},
},
legend: {
layout: 'vertical',
verticalAlign: 'middle',
align: 'left',
symbolHeight: 25,
symbolRadius: 5,
itemMarginTop: 10,
itemMarginBottom: 10,
},
series: [
{
type: 'pie',
innerSize: '55%',
colors: ['rgb(212,33,71)', 'rgb(250,189,43)', 'rgb(60,168,74)'],
data: [['Category 1', 4], ['Category 2', 5], ['Category 3', 1]],
},
],
});
解决方案
Highcharts github 上报告了一个关于甜甜圈标签位置的问题。在这里查看:https ://github.com/highcharts/highcharts/issues/9005 。
我更改了一些选项,并在甜甜圈中心使用Highcharts.SVGRenderer#text(所有值的动态总和)添加了自定义文本。也许它会帮助你:https ://jsfiddle.net/BlackLabel/2jmqext9/ 。
代码:
Highcharts.chart('container', {
credits: {
enabled: false
},
chart: {
height: 300,
width: 500,
animation: false,
events: {
load: function() {
var chart = this,
offsetLeft = -20,
offsetTop = 10,
x = chart.plotLeft + chart.plotWidth / 2 + offsetLeft,
y = chart.plotTop + chart.plotHeight / 2 + offsetTop,
value = 0;
chart.series[0].yData.forEach(function(point) {
value += point;
});
chart.renderer.text(value, x, y).add().css({
fontSize: '30px'
}).toFront();
}
}
},
plotOptions: {
pie: {
dataLabels: {
enabled: true,
color: '#fff',
format: '{point.y}',
distance: -25,
style: {
fontSize: '20px',
textOutline: 0
},
},
animation: false,
showInLegend: true,
},
},
legend: {
layout: 'vertical',
verticalAlign: 'middle',
align: 'left',
symbolHeight: 15,
symbolRadius: 5,
symbolPadding: 10,
itemMarginTop: 10,
itemMarginBottom: 10,
itemStyle: {
fontSize: '15px'
}
},
series: [{
type: 'pie',
innerSize: '55%',
colors: ['rgb(212,33,71)', 'rgb(250,189,43)', 'rgb(60,168,74)'],
data: [
['Category 1', 4],
['Category 2', 2],
['Category 3', 12]
],
}, ],
});
推荐阅读
- azure - azure api部署在多个区域问题
- javascript - Angular 中的数据管理 - 在具有大量重复信息的类中引用其他对象
- python - 添加行*在*列名Pandas Dataframe 的顶部作为标题信息?
- c - 从 txt 文件读入数组
- office-js - 尝试获取非常大的邮件正文时,mailbox.item.body.getAsync 调用失败
- html - 可滚动的 html顶部有溢出
- c# - 我是否应该记录异常,即使它们已经记录在他们的界面中?
- javascript - 循环遍历 jQuery 数组/将值输出到 div
- ethereum - 合同可以假装吗?
- php - Laravel 5.6 - 在日志/laravel.log ubuntu xenial 16.04 上的权限被拒绝