javascript - 圆环图,在饼图内和向下也有文本
问题描述
我已经这样创建了
此处添加的文本是明确的,顶部和左侧是固定的。我希望它是响应式的。即当屏幕尺寸改变时移动。有办法吗?在 highchart 文档中,只有 name 属性没有给出 title 属性。我的代码在这里给出 jsfiddle
$(function() {
$('#percentile ').highcharts({
title: {
text: 'Section Scores'
},
xAxis: {
categories: ['Apples', 'Oranges', 'Pears', 'Bananas', 'Plums']
},
labels: {
items: [{
html: 'Total fruit consumption',
style: {
left: '50px',
top: '12px',
color: (Highcharts.theme && Highcharts.theme.textColor) || 'black'
}
}]
},
series: [{
type: 'pie',
name: 'Total ',
data: [{
name: 'Jane',
y: 13,
color: Highcharts.getOptions().colors[0] // Jane's color
}, {
name: 'John',
y: 23,
color: Highcharts.getOptions().colors[1] // John's color
}],
center: [100, 80],
size: 150,
innerSize: '70%',
showInLegend: false,
dataLabels: {
enabled: false
}
}, {
type: 'pie',
name: ' consumption',
data: [{
name: 'Jane',
y: 13,
color: Highcharts.getOptions().colors[0] // Jane's color
}, {
name: 'John',
y: 23,
color: Highcharts.getOptions().colors[1] // John's color
}],
center: [400, 80],
size: 150,
innerSize: '70%',
showInLegend: false,
dataLabels: {
enabled: false
}
}]
});
});
解决方案
您可以使用Highcharts.SVGRenderer
该类并在具有计算位置的图表上呈现自定义 svg 元素,例如:
chart: {
events: {
render: function() {
var chart = this,
series1BBox = chart.series[0].group.getBBox(),
series2BBox = chart.series[1].group.getBBox(),
x1 = chart.plotLeft + series1BBox.x + series1BBox.width / 2,
x2 = chart.plotLeft + series2BBox.x + series2BBox.width / 2,
y1 = chart.plotTop + series1BBox.y + series1BBox.height / 2,
y2 = chart.plotTop + series1BBox.y + series1BBox.height;
if (!this.customLabels) { // render custom labels
chart.customLabels = [];
this.customLabels[0] = chart.renderer.text('Text 1')
.css({
color: '#4572A7',
fontSize: '16px',
'vertical-align': 'middle'
})
.attr({
align: 'center'
})
.add();
...
}
// define position
chart.customLabels[0].attr({
x: x1,
y: y1 + chart.customLabels[0].getBBox().height / 2
});
...
}
}
}
现场演示: https ://jsfiddle.net/BlackLabel/5sro1tj9/
API 参考: https ://api.highcharts.com/class-reference/Highcharts.SVGRenderer#text
推荐阅读
- python - Cython:如何加速递归函数?
- salesforce - 如何在 Salesforce 社区中创建包含用户列表的页面?
- json - Flutter 异常错误处理 Google Apps 脚本生成的 json
- java - 如何在 Ubuntu 20.04 上安装 JavaFX
- javascript - 使用 FirebaseAuthentication 创建用户后,Firebase 不会将用户数据保存到 Firestore - Angular 9
- java - PeriodicRequest 不起作用。我尝试每 15 分钟发送一次通知,但它不起作用
- reactjs - 在“节点”上执行“removeChild”失败:要删除的节点不是该节点的子节点。在反应
- angular - 我正在尝试在自定义组件中使用超级选项卡,但内容不可见
- google-sheets - 如何获取行列表元素比较两行谷歌表格
- python - 计算 A * relu(A * X * W0) * W1 wrt A 的梯度