Vuejs 如何为 vue-chartjs 甜甜圈添加颜色?

Vuejs How can I add colors to vue-chartjs Doughnut?

我正在尝试实现 vue-chartjs Donut,但我无法获得正确的颜色。

这是我使用 Doughnut 组件的地方

我的App.vue

<template>
  <div id="app">
    <label for="Doughnut">Doughnut</label>
    <div id="ChartDoughnut">
      <ChartDoughnut :chartData="chartDataDoughnut" />
    </div>
  </div>
</template>

<script>
import ChartDoughnut from "./components/ChartDoughnut.vue";
export default {
  name: "App",
  components: {
    ChartDoughnut
  },
  data() {
    return {
      chartDataDoughnut: {
        datasets: [
          {
            data: [10, 20, 30]
          }
        ],

        // These labels appear in the legend and in the tooltips when hovering different arcs
        labels: ["Red", "Yellow", "Blue"]
      }
      // optionsDoughnut: {}
    };
  }
};
</script>

这是 Doughnut 的组件

chartDoughnut.vue

<script>
import { Doughnut } from "vue-chartjs";

export default {
  extends: Doughnut,
  props: {
    chartData: {
      type: Object,
      default: null
    },
    options: {
      type: Object,
      default: null
    }
  },
  mounted() {
    this.renderChart(this.chartData);
  }
};
</script>

我在标签上添加了颜色,但它不起作用。

尝试将带有颜色数组的 ba​​ckgroundColor 属性 添加到您的数据集 属性。

backgroundColor: ['red', 'yellow', 'blue']

// The important part: the Chart component
Vue.component('chart', {
   extends: VueChartJs.Doughnut,
  props: {
    chartData: {
      type: Object,
      default: null
    },
    options: {
      type: Object,
      default: null
    }
  },
  mounted() {
    this.renderChart(this.chartData);
  }
})

//App setup
new Vue({
 el:'#vue',
  data(){
   return {
        chartDataDoughnut: {
          datasets: [
            {
              data: [10, 20, 30],
              backgroundColor: ['red', 'yellow', 'blue']
            }
          ],
          labels: ["Red", "Yellow", "Blue"]
        }
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
<script src="https://unpkg.com/vue-chartjs/dist/vue-chartjs.min.js"></script>

<div id="vue">
  <chart :chart-data="chartDataDoughnut"></chart>
</div>

颜色是每个数据集的一部分。

chartDataDoughnut: {
  datasets: [
    {
      data: [10, 20, 30],
      backgroundColor: [
        'rgba(255, 99, 132, 0.5)',
        'rgba(54, 162, 235, 0.2)',
        'rgba(255, 206, 86, 0.2)',
      ]
    }
  ],
  labels: ["Foo", "Bar", "Lorem"]
}