gpt4 book ai didi

chart.js - 对于没有数据的饼图显示 "No Data"消息

转载 作者:行者123 更新时间:2023-12-02 08:03:44 25 4
gpt4 key购买 nike

我使用chartjs制作了下面的饼图。当没有任何内容可显示时,我需要在其上显示“无数据”消息。

但是我在chartjs教程中找不到这样的方法。有人可以帮忙吗?

饼图 HTML

<div id="canvas-holder-CT" style="width:46%;float:right;position: absolute; left: 0px; top: 700px;">
<canvas id="chart-area-CT" width="350" height="450" style="display: block; margin-left:2em">
</canvas>
<center> <b><details>
<summary>Distribution by Call Types</summary>
</details> </b></center>
</div>

饼图脚本

var configCT = {
type : 'pie',
data : {
datasets : [{
data : valuesCT,
backgroundColor : coloringCT,
label : 'Distribution by Call Types'
}
],
labels : labelsCT
},
options : {
segmentShowStroke : false,
legend : false,
animateScale : true,
responsive : true,
showAllTooltips : false,
tooltips : {
custom : function (tooltip) {
if (!tooltip)
return;
tooltip.displayColors = false;
}
}
}
};

var ctxCT = document.getElementById("chart-area-CT").getContext("2d");
if (myPieCT != null) {
myPieCT.destroy();
}
myPieCT = new Chart(ctxCT, configCT);

最佳答案

你检查过这个吗open issue

以及etimberg提出的相对解决方案用户:

Chart.plugins.register({
afterDraw: function(chart) {
if (chart.data.datasets.length === 0) {
// No data is present
var ctx = chart.chart.ctx;
var width = chart.chart.width;
var height = chart.chart.height
chart.clear();

ctx.save();
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.font = "16px normal 'Helvetica Nueue'";
ctx.fillText('No data to display', width / 2, height / 2);
ctx.restore();
}
}
});

参见:https://jsfiddle.net/x04ptfuu/

关于chart.js - 对于没有数据的饼图显示 "No Data"消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48136597/

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