如何在圆环图中间添加图像?(Chart.js)

ddrv8njm  于 2023-10-18  发布在  Chart.js
关注(0)|答案(3)|浏览(236)

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

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

如何在圆环图中间添加照片?下面是我的JavaScript代码:

  1. $.ajax({
  2. type: "POST",
  3. url: "/Plant/SunChart",
  4. contentType: "application/json; charset=utf-8",
  5. dataType: "json",
  6. success: function (jsonData) {
  7. var items = jsonData[0];
  8. var countOfItems = jsonData[1];
  9. var ctx = document.getElementById("sunPieChart");
  10. var myPieChart = new Chart(ctx, {
  11. type: 'doughnut',
  12. data: {
  13. labels: items,
  14. datasets: [{
  15. data: countOfItems,
  16. backgroundColor: ['#ffff00', '#ffffa4', '#ced5df'],
  17. hoverBackgroundColor: ['#ffff00', '#ffffa4', '#ced5df'],
  18. hoverBorderColor: "rgba(234, 236, 244, 1)",
  19. }],
  20. },
  21. options: {
  22. maintainAspectRatio: false,
  23. tooltips: {
  24. backgroundColor: "rgb(255,255,255)",
  25. bodyFontColor: "#858796",
  26. borderColor: '#dddfeb',
  27. borderWidth: 1,
  28. xPadding: 15,
  29. yPadding: 15,
  30. displayColors: false,
  31. caretPadding: 10,
  32. },
  33. legend: {
  34. display: false
  35. },
  36. cutoutPercentage: 80,
  37. },
  38. });
  39. }
  40. });
wz1wpwve

wz1wpwve1#

插件核心API提供了一系列可用于执行自定义代码的钩子。您可以使用afterDraw钩子直接在画布上使用CanvasRenderingContext2D.drawImage()绘制图像。
请看看下面的修改代码。

  1. new Chart('myChart', {
  2. type: 'doughnut',
  3. plugins: [{
  4. afterDraw: chart => {
  5. var ctx = chart.chart.ctx;
  6. ctx.save();
  7. var image = new Image();
  8. image.src = 'https://i.stack.imgur.com/S7tJH.png';
  9. imageSize = 40;
  10. ctx.drawImage(image, chart.chart.width / 2 - imageSize / 2, chart.chart.height / 2 - imageSize / 2, imageSize, imageSize);
  11. ctx.restore();
  12. }
  13. }],
  14. data: {
  15. labels: ['A', 'B', 'C'],
  16. datasets: [{
  17. data: [50, 25, 25],
  18. backgroundColor: ['#ffff00', '#ffffa4', '#ced5df'],
  19. hoverBackgroundColor: ['#ffff00', '#ffffa4', '#ced5df'],
  20. hoverBorderColor: "rgba(234, 236, 244, 1)",
  21. }],
  22. },
  23. options: {
  24. maintainAspectRatio: false,
  25. tooltips: {
  26. backgroundColor: "rgb(255,255,255)",
  27. bodyFontColor: "#858796",
  28. borderColor: '#dddfeb',
  29. borderWidth: 1,
  30. xPadding: 15,
  31. yPadding: 15,
  32. displayColors: false,
  33. caretPadding: 10,
  34. },
  35. legend: {
  36. display: false
  37. },
  38. cutoutPercentage: 80,
  39. },
  40. });
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
  2. <canvas id="myChart" height="180"></canvas>
展开查看全部
vsmadaxz

vsmadaxz2#

Uminder's answer非常有用。添加到将图像放在中心更新下面的代码

  1. afterDraw: chart => {
  2. var ctx = chart.chart.ctx;
  3. ctx.save();
  4. var image = new Image();
  5. image.src = 'https://i.stack.imgur.com/S7tJH.png';
  6. imageSize = 40;
  7. const {top, left, width, height} = chart.chartArea;
  8. const x = left + width / 2 - imageSize / 2;
  9. const y = top + height / 2 - imageSize / 2;
  10. ctx.drawImage(image, x , y, imageSize, imageSize);
  11. ctx.restore();
  12. }

官方网站。我希望有人会发现它有帮助。

q8l4jmvw

q8l4jmvw3#

https://dotnetfiddle.net/jJKo4I

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

归功于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/

相关问题