Doughnut Empty State

Doughnut Empty State - 图1

Randomize

config plugin data

  1. const config = {
  2. type: 'doughnut',
  3. data: data,
  4. options: {
  5. plugins: {
  6. emptyDoughnut: {
  7. color: 'rgba(255, 128, 0, 0.5)',
  8. width: 2,
  9. radiusDecrease: 20
  10. }
  11. }
  12. },
  13. plugins: [plugin]
  14. };
  1. const plugin = {
  2. id: 'emptyDoughnut',
  3. afterDraw(chart, args, options) {
  4. const {datasets} = chart.data;
  5. const {color, width, radiusDecrease} = options;
  6. let hasData = false;
  7. for (let i = 0; i < datasets.length; i += 1) {
  8. const dataset = datasets[i];
  9. hasData |= dataset.data.length > 0;
  10. }
  11. if (!hasData) {
  12. const {chartArea: {left, top, right, bottom}, ctx} = chart;
  13. const centerX = (left + right) / 2;
  14. const centerY = (top + bottom) / 2;
  15. const r = Math.min(right - left, bottom - top) / 2;
  16. ctx.beginPath();
  17. ctx.lineWidth = width || 2;
  18. ctx.strokeStyle = color || 'rgba(255, 128, 0, 0.5)';
  19. ctx.arc(centerX, centerY, (r - radiusDecrease || 0), 0, 2 * Math.PI);
  20. ctx.stroke();
  21. }
  22. }
  23. };
  1. const data = {
  2. labels: [],
  3. datasets: [
  4. {
  5. label: 'Dataset 1',
  6. data: []
  7. }
  8. ]
  9. };

Docs