Chart.js设置甜甜圈背景色?

jlm*_*mns 4 javascript canvas chart.js

我正在使用Chart.js(文档),但我似乎无法为甜甜圈路径设置背景颜色.文档中甚至没有提到它.

我想要实现的目标:

在此输入图像描述

当前代码:

var meterInvoicesData = [
    {
        value: 75,
        color: '#22d319'
    },
    {
        value: 25,     // rest
        color: 'transparent'  // invisible (setting this as background color will animate it too)
    }
];

var meterOptions =
{
    percentageInnerCutout : 80,
    animationEasing : 'easeInQuint'
};

var meterInvoices = new Chart(document.getElementById('meterInvoices').getContext('2d')).Doughnut(meterInvoicesData,meterOptions);
Run Code Online (Sandbox Code Playgroud)

更新:我目前通过使用值为100 的复制甜甜圈(第二个画布),没有动画和我想要的(背景)颜色来解决它,并将其定位在第一个下面.

然而,这是一个讨厌的伎俩,效率非常低,所以我仍然希望得到正确答案.

jon*_*ord 11

以为我发布了一个最新的解决方案,对我有用,使用引入插件的 v2.1.0 .

没有值的图表显示背景与图表,其值覆盖背景,只有主图表会动画,背景只是一个简单的弧:

没有显示背景值的图表 图表的价值涵盖背景


我首先根据他们的文档注册了一个插件:

var radiusBackground = function() {
  var self = this;

  self.draw = function(chartInstance) {
    if(chartInstance.options.radiusBackground) {
      var x = chartInstance.chart.canvas.clientWidth / 2,
          y = chartInstance.chart.canvas.clientHeight / 2,
          ctx = chartInstance.chart.ctx;

      ctx.beginPath();
      ctx.arc(x, y, chartInstance.outerRadius - (chartInstance.radiusLength / 2), 0, 2 * Math.PI);
      ctx.lineWidth = chartInstance.radiusLength;
      ctx.strokeStyle = chartInstance.options.radiusBackground.color || '#d1d1d1';
      ctx.stroke();
    }
  };

  // see http://www.chartjs.org/docs/#advanced-usage-creating-plugins for plugin interface
  return {
    beforeDatasetsDraw: self.draw,
    onResize: self.draw
  }
};

// Register with Chart JS
Chart.plugins.register(new radiusBackground());
Run Code Online (Sandbox Code Playgroud)

单例语法只是为了能够减少重复并对draw多个插件事件使用相同的方法.


然后我使用了我的新注册插件,如下所示:

var chartElement = document.getElementById('doughnut-chart');

var chart = new Chart(chartElement, {
  type: 'doughnut',
  options: {
    // Here is where we enable the 'radiusBackground'
    radiusBackground: {
      color: '#d1d1d1' // Set your color per instance if you like
    },
    cutoutPercentage: 90,
    title: {
      display: false,
    },
    legend: {
      display: false,
    },
  },
  data: {
    labels: ["Type 1", "Type 2", "Type 3"],
    datasets: [{
      data: [2, 5, 1],
      backgroundColor: ["#a3c7c9","#889d9e","#647678"],
      borderWidth: 0,
      hoverBackgroundColor: ["#96b7b9","#718283","#5c6b6d"]
    }]
  }
});
Run Code Online (Sandbox Code Playgroud)

JS小提琴在这里


Don*_*Kim 5

我使用了 @Jonlunsford 的代码,但是当我将 ChartJS 升级到 3.x 时它不起作用。

根据迁移指南,它说

Chart.innerRadius now lives on doughnut, pie, and polarArea controllers

所以我将代码更改为:

import { Chart, DoughnutController } from 'chart.js'

type DoughnutChartBackgroundPluginOptions = {
  enabled: boolean
  color: string
}

function handler(chart: Chart<'doughnut'>, args, options: DoughnutChartBackgroundPluginOptions) {
  const { ctx, width, height } = chart

  const { innerRadius } = chart.getDatasetMeta(chart.data.datasets.length - 1).controller as DoughnutController
  const { outerRadius } = chart.getDatasetMeta(0).controller as DoughnutController
  const radiusLength = outerRadius - innerRadius

  if (options.enabled) {
    const x = width / 2,
      y = height / 2

    ctx.beginPath()
    ctx.arc(x, y, outerRadius - radiusLength / 2, 0, 2 * Math.PI)
    ctx.lineWidth = radiusLength
    ctx.strokeStyle = options.color
    ctx.stroke()
  }
}

export default {
  id: 'doughnutChartBackground',
  beforeDatasetsDraw: handler,
}

Run Code Online (Sandbox Code Playgroud)

然后,在创建图表时,您可以使用以下选项:

  ...
  plugins: {
    legend: {
      display: false,
    },
    doughnutBackground: {
      enabled: true,
      color: '#E4E6E6',
    },
    ...
  },
Run Code Online (Sandbox Code Playgroud)


jlm*_*mns 1

我通过使用值为 100 的重复甜甜圈(第二个画布)、没有动画和我想要的背景颜色来解决这个问题,并将其绝对定位在第一个画布的下方。

然而,这是一个令人讨厌的技巧,而且效率很低,所以我仍然希望得到一个正确的答案。