gpt4 book ai didi

javascript - Chart.js v2 : How to make tooltips always appear on pie chart?

转载 作者:行者123 更新时间:2023-12-03 21:32:24 24 4
gpt4 key购买 nike

我找到了similar questions在 Stack Overflow 上,但所有这些问题都在一两年前得到了解决。现在 Chart.js 已经出现在版本 2 中,并且许多文档发生了变化。有人可以帮我展示带有标签的饼图示例 - 或所有部分的工具提示都可见的饼图吗?

更新

感谢@potatopeelings,他的答案非常适合 Chart.js v2.1。

虽然我最初询问如何在饼图上永久显示工具提示,但我找到了更好的解决方案:将值显示为百分比标签!现在在 Chart.js v2.1 中启用了饼图。在图表选项中:

animation: {
duration: 0,
onComplete: function () {
var self = this,
chartInstance = this.chart,
ctx = chartInstance.ctx;

ctx.font = '18px Arial';
ctx.textAlign = "center";
ctx.fillStyle = "#ffffff";

Chart.helpers.each(self.data.datasets.forEach(function (dataset, datasetIndex) {
var meta = self.getDatasetMeta(datasetIndex),
total = 0, //total values to compute fraction
labelxy = [],
offset = Math.PI / 2, //start sector from top
radius,
centerx,
centery,
lastend = 0; //prev arc's end line: starting with 0

for (var val of dataset.data) { total += val; }

Chart.helpers.each(meta.data.forEach( function (element, index) {
radius = 0.9 * element._model.outerRadius - element._model.innerRadius;
centerx = element._model.x;
centery = element._model.y;
var thispart = dataset.data[index],
arcsector = Math.PI * (2 * thispart / total);
if (element.hasValue() && dataset.data[index] > 0) {
labelxy.push(lastend + arcsector / 2 + Math.PI + offset);
}
else {
labelxy.push(-1);
}
lastend += arcsector;
}), self)

var lradius = radius * 3 / 4;
for (var idx in labelxy) {
if (labelxy[idx] === -1) continue;
var langle = labelxy[idx],
dx = centerx + lradius * Math.cos(langle),
dy = centery + lradius * Math.sin(langle),
val = Math.round(dataset.data[idx] / total * 100);
ctx.fillText(val + '%', dx, dy);
}

}), self);
}
},

最佳答案

ChartJs 版本 > 2.1.5 的解决方案:

Chart.pluginService.register({
beforeRender: function (chart) {
if (chart.config.options.showAllTooltips) {
// create an array of tooltips
// we can't use the chart tooltip because there is only one tooltip per chart
chart.pluginTooltips = [];
chart.config.data.datasets.forEach(function (dataset, i) {
chart.getDatasetMeta(i).data.forEach(function (sector, j) {
chart.pluginTooltips.push(new Chart.Tooltip({
_chart: chart.chart,
_chartInstance: chart,
_data: chart.data,
_options: chart.options.tooltips,
_active: [sector]
}, chart));
});
});

// turn off normal tooltips
chart.options.tooltips.enabled = false;
}
},
afterDraw: function (chart, easing) {
if (chart.config.options.showAllTooltips) {
// we don't want the permanent tooltips to animate, so don't do anything till the animation runs atleast once
if (!chart.allTooltipsOnce) {
if (easing !== 1)
return;
chart.allTooltipsOnce = true;
}

// turn on tooltips
chart.options.tooltips.enabled = true;
Chart.helpers.each(chart.pluginTooltips, function (tooltip) {
tooltip.initialize();
tooltip.update();
// we don't actually need this since we are not animating tooltips
tooltip.pivot();
tooltip.transition(easing).draw();
});
chart.options.tooltips.enabled = false;
}
}
});

关于javascript - Chart.js v2 : How to make tooltips always appear on pie chart?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36992922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com