gpt4 book ai didi

How do I add an image in the middle of Donut Chart?(Chart.js)(如何在甜甜圈图表中间添加图像?(Chart.js))

转载 作者:bug小助手 更新时间:2023-10-25 14:38:20 30 4
gpt4 key购买 nike



I'm using Chart.js to visualize my datas like below image.

我正在使用Chart.js来可视化我的数据,如下图所示。


current version


I want to add a photo in the middle of chart.

我想在图表中间加一张照片。


chart with photo


How can I add a photo in the middle of Donut Chart?
Here my javascript code:

如何在甜甜圈图表的中间添加照片?下面是我的javascript代码:


$.ajax({
type: "POST",
url: "/Plant/SunChart",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (jsonData) {
var items = jsonData[0];
var countOfItems = jsonData[1];
var ctx = document.getElementById("sunPieChart");
var myPieChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: items,
datasets: [{
data: countOfItems,
backgroundColor: ['#ffff00', '#ffffa4', '#ced5df'],
hoverBackgroundColor: ['#ffff00', '#ffffa4', '#ced5df'],
hoverBorderColor: "rgba(234, 236, 244, 1)",
}],
},
options: {
maintainAspectRatio: false,
tooltips: {
backgroundColor: "rgb(255,255,255)",
bodyFontColor: "#858796",
borderColor: '#dddfeb',
borderWidth: 1,
xPadding: 15,
yPadding: 15,
displayColors: false,
caretPadding: 10,
},
legend: {
display: false
},
cutoutPercentage: 80,
},
});
}
});


更多回答
优秀答案推荐

The Plugin Core API offers a range of hooks that may be used for performing custom code. You can use the afterDraw hook to draw the images directly on the canvas using CanvasRenderingContext2D.drawImage().

插件核心API提供了一系列可用于执行定制代码的挂钩。您可以使用After Draw挂钩,使用Canvas RenderingContext2D.draImage()在画布上直接绘制图像。


Please have a look at your amended code below.

请看一下您修改后的代码。




new Chart('myChart', {
type: 'doughnut',
plugins: [{
afterDraw: chart => {
var ctx = chart.chart.ctx;
ctx.save();
var image = new Image();
image.src = 'https://i.stack.imgur.com/S7tJH.png';
imageSize = 40;
ctx.drawImage(image, chart.chart.width / 2 - imageSize / 2, chart.chart.height / 2 - imageSize / 2, imageSize, imageSize);
ctx.restore();
}
}],
data: {
labels: ['A', 'B', 'C'],
datasets: [{
data: [50, 25, 25],
backgroundColor: ['#ffff00', '#ffffa4', '#ced5df'],
hoverBackgroundColor: ['#ffff00', '#ffffa4', '#ced5df'],
hoverBorderColor: "rgba(234, 236, 244, 1)",
}],
},
options: {
maintainAspectRatio: false,
tooltips: {
backgroundColor: "rgb(255,255,255)",
bodyFontColor: "#858796",
borderColor: '#dddfeb',
borderWidth: 1,
xPadding: 15,
yPadding: 15,
displayColors: false,
caretPadding: 10,
},
legend: {
display: false
},
cutoutPercentage: 80,
},
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="180"></canvas>





Uminder's answer was quite helpful. Adding to that to place image at the center Update below code

乌明德的回答很有帮助。此外,还可以将图像放在中心位置更新下面的代码


afterDraw: chart => {
var ctx = chart.chart.ctx;
ctx.save();
var image = new Image();
image.src = 'https://i.stack.imgur.com/S7tJH.png';
imageSize = 40;
const {top, left, width, height} = chart.chartArea;
const x = left + width / 2 - imageSize / 2;
const y = top + height / 2 - imageSize / 2;
ctx.drawImage(image, x , y, imageSize, imageSize);
ctx.restore();
}

Official Docs. I hope somebody will find it helpful.

官方文件。我希望有人会觉得它有帮助。



https://dotnetfiddle.net/jJKo4I

Https://dotnetfiddle.net/jJKo4I


<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js" type="text/javascript"></script>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>

credit to
How to add image inside the doughnut chart using chart.js?
and
https://www.c-sharpcorner.com/article/charts-in-asp-net-mvc-using-chart-js/
and
http://jsfiddle.net/jimrhoskins/Sv87G/

如何使用chart.js在甜甜圈图表中添加图像?还有https://www.c-sharpcorner.com/article/charts-in-asp-net-mvc-using-chart-js/和http://jsfiddle.net/jimrhoskins/Sv87G/


更多回答

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