【发布时间】:2021-02-07 23:21:19
【问题描述】:
我进行了很多搜索,并在工具提示中包含了一千个分隔符。但我想让它在任何有文本的地方都能工作。在我住的地方,我们使用“。”将千位和“,”分隔为小数。
我没有找到将标题放在甜甜圈中间的简单方法。
这就是我所拥有的:
Chart.defaults.global.defaultFontColor = '#7792b1';
var ctx = document.getElementById('myChart').getContext('2d');
var dataset = [{
label: 'Volume de Operações',
data: [254000.87, 355000.57],
backgroundColor: ['#4bb8df', '#6290df']
}]
var chart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ['CALL', 'PUT'],
datasets: dataset
},
options: {
rotation: 1 * Math.PI,
circumference: 1 * Math.PI,
legend: {
display: false
},
cutoutPercentage: 60,
plugins: {
labels: [{
render: 'label',
arc: true,
fontStyle: 'bold',
position: 'outside'
}, {
render: 'percentage',
fontColor: '#ffffff',
precision: 1
}],
},
title: {
display: true,
fontSize: 15,
text: [
dataset.reduce((t, d) => t + d.data.reduce((a, b) => a + b), 0),
'Volume Total'
],
position: 'bottom'
},
tooltips: {
callbacks: {
label: function(tooltipItem, data) {
var dataLabel = data.labels[tooltipItem.index];
var value = ': ' + data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index].toLocaleString();
if (Chart.helpers.isArray(dataLabel)) {
dataLabel = dataLabel.slice();
dataLabel[0] += value;
} else {
dataLabel += value;
}
return dataLabel;
}
}
}
}
});
<canvas id="myChart" style="max-width: 450px"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
<script src="https://cdn.jsdelivr.net/gh/emn178/chartjs-plugin-labels/src/chartjs-plugin-labels.js"></script>
简而言之:
- 全局千位分隔符:(.)
- 甜甜圈中间的标题
- 没有标签的工具提示:只有值
【问题讨论】:
-
能否请您改进第一段?你的意思是“我想让它全球化:也有标题”。同时这里有一些how to use dots as thousand separators 和showing only values on tooltips 的资源。关于标题there is no such functionality
-
@adelriosantiago,完成了!我改进了第一段。谢谢你的帮助。我要检查这些。
标签: chart.js