我使用Chart.js来可视化我的数据,如下图所示。
我想在图表中间加一张照片。
如何在圆环图中间添加照片?下面是我的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,
},
});
}
});
3条答案
按热度按时间wz1wpwve1#
插件核心API提供了一系列可用于执行自定义代码的钩子。您可以使用
afterDraw
钩子直接在画布上使用CanvasRenderingContext2D.drawImage()
绘制图像。请看看下面的修改代码。
vsmadaxz2#
Uminder's answer非常有用。添加到将图像放在中心更新下面的代码
官方网站。我希望有人会发现它有帮助。
q8l4jmvw3#
https://dotnetfiddle.net/jJKo4I
归功于How to add image inside the doughnut chart using chart.js?和https://www.c-sharpcorner.com/article/charts-in-asp-net-mvc-using-chart-js/和http://jsfiddle.net/jimrhoskins/Sv87G/