如何在使用props时修复Chart.js环形图中缺少背景颜色和标签的问题?

huangapple go评论63阅读模式
英文:

How to fix Chart.js doughnut chart with missing background colors and missing labels when working with props?

问题

我正在使用Chart.js来显示一个圆环图(doughnut chart)。我想使用props来进行一些API调用,并显示不同的圆环图,展示各种数据。我的问题是我的代码没有完全工作。所有不同的部分都显示出来了,但没有背景颜色。有人可以帮助我解决这个问题吗?

以下是您提供的代码中的翻译部分:

<!-- DoughnutChart -->
<template>
    <div>
      <canvas id="myChart" width="80%" height="80%"></canvas>
      <p>这是我的图表 <h1>{{ topLabel }}</h1></p>
    </div>
  </template>
  
  <script>
  import { Chart, ArcElement, DoughnutController } from 'chart.js';
  Chart.register(DoughnutController, ArcElement);
  
  export default {
    name: "doughnut-chart",
    props: {
      ValuesLabel: {
        type: Array,
        required: true,
        default: ["value1", "value2", "value3", "value4", "value5"]
      },
      topLabel: {
        type: String,
        required: true,
        default: "这是我的图表"
      },
      dataChart: {
        type: Array,
        required: true,
        default: [10, 20, 30, 40, 50]
      }
    },
    mounted() {
      console.log("我的圆环图已挂载");
      console.log()
      const ctx = document.getElementById('myChart').getContext('2d');
      const myChart = new Chart(ctx, {
        type: 'doughnut',
        data: {
          labels: this.ValuesLabel,
          datasets: [
            {
              label: this.topLabel,
              data: this.dataChart,
            }
          ]
        }
      });
      return myChart;
    }
  };
  </script>

这是我的预算视图组件:

<!-- myBudgetView -->
<template>
    <div>
      <h1>我的预算</h1>
      <doughnut-chart
        :ValuesLabel="labels"
        :topLabel="topLabel"
        :dataChart="dataChart"
      />
    </div>
  </template>
  
  <script>
  import DoughnutChart from "../components/Charts/DoughnutChart.vue";
  
  export default {
    components: {
      DoughnutChart
    },
    name: "myBudgetView",
  
    // Data section
    data() {
      return {
        labels: ['水果', '蔬菜', '肉类', '奶制品', '零食'],
        topLabel: "我的开支",
        dataChart: [12, 20, 33, 40, 51]
      };
    }
  };
  </script>

这是我得到的结果:

查看图像描述

我期望得到与每个部分都有颜色的相同结果,如下所示:

查看图像描述

英文:

I am using Chart.js in order to display a doughnut chart. I want to use props so that I could do some API calls and have different doughnut charts with various data showing up. My issue is that my code is not fully working. I have all the different sections showing up but with no background color. Can someone help me fix this ?

&lt;!-- DoughnutChart --&gt;
&lt;template&gt;
    &lt;div&gt;
      &lt;canvas id=&quot;myChart&quot; width=&quot;80%&quot; height=&quot;80%&quot;&gt;&lt;/canvas&gt;
      &lt;p&gt;Here is my chart &lt;h1&gt;{{ topLabel }}&lt;/h1&gt;&lt;/p&gt;
    &lt;/div&gt;
  &lt;/template&gt;
  
  &lt;script&gt;
  import { Chart, ArcElement, DoughnutController } from &#39;chart.js&#39;;
  Chart.register(DoughnutController, ArcElement);
  
  export default {
    name: &quot;doughnut-chart&quot;,
    props: {
      ValuesLabel: {
        type: Array,
        required: true,
        default: [&quot;value1&quot;, &quot;value2&quot;, &quot;value3&quot;, &quot;value4&quot;, &quot;value5&quot;]
      },
      topLabel: {
        type: String,
        required: true,
        default: &quot;Here is my chart&quot;
      },
      dataChart: {
        type: Array,
        required: true,
        default: [10, 20, 30, 40, 50]
      }
    },
    mounted() {
      console.log(&quot;My doughnut chart is mounted&quot;);
      console.log()
      const ctx = document.getElementById(&#39;myChart&#39;).getContext(&#39;2d&#39;);
      const myChart = new Chart(ctx, {
        type: &#39;doughnut&#39;,
        data: {
          labels: this.ValuesLabel,
          datasets: [
            {
              label: this.topLabel,
              data: this.dataChart,
            }
          ]
        }
      });
      return myChart;
    }
  };
  &lt;/script&gt;
  

here is my budget view component

&lt;-- myBudgetView --&gt;
&lt;template&gt;
    &lt;div&gt;
      &lt;h1&gt;My Budget&lt;/h1&gt;
      &lt;doughnut-chart
        :ValuesLabel=&quot;labels&quot;
        :topLabel=&quot;topLabel&quot;
        :dataChart=&quot;dataChart&quot;
      /&gt;
    &lt;/div&gt;
  &lt;/template&gt;
  
  &lt;script&gt;
  import DoughnutChart from &quot;../components/Charts/DoughnutChart.vue&quot;;
  
  export default {
    components: {
      DoughnutChart
    },
    name: &quot;myBudgetView&quot;,
  
    // Data section
    data() {
      return {
        labels: [&#39;fruits&#39;, &#39;veggies&#39;, &#39;meat&#39;, &#39;dairy&#39;, &#39;snacks&#39;],
        topLabel: &quot;My expenses&quot;,
        dataChart: [12, 20, 33, 40, 51]
      };
    }
  };
  &lt;/script&gt;
  

here is what I get
enter image description here

I expect the same thing with some color in each section like so :
enter image description here

答案1

得分: 1

在我的代码中存在两个问题:

  • 未导入Colors
  • 插件 > 启用:false
英文:

there were two issues in my code :

  • not importing Colors
  • plugin > enabled : false

答案2

得分: 0

我将以更好的方式展示我是如何解决这个问题的。

我定义了一个名为option的对象,如下所示:

const options = {
  plugins: {
    colors: {
      enabled: true
    }
  }
};

然后,我获取了canvas的上下文对象:

const ctx = document.getElementById('myChart').getContext('2d');

接着,我创建了一个Chart实例:

const myChart = new Chart(ctx, {
  type: 'doughnut',
  data: {
    labels: this.ValuesLabel,
    datasets: [
      {
        label: this.topLabel,
        data: this.dataChart,
      }
    ]
  },
  options: options
});

最后,我返回了myChart。

使用这种方法,您可以忘记导入'Colors',因为它不再作为库存在,而是作为一个插件存在。

但是,如果您需要在同一个Vue应用程序中创建多个图表,似乎是不可能的,您必须使用一个使用SVG标签而不是Canvas的库。我尝试过使用Chart.js和SVG标签,但是这是不可能的。

英文:

I will show in a better way how I did solve the issue.

I defined an object called option like so :

     const options = {
  plugins: {
    colors: {
      enabled: true
    }
  }
};
 const ctx = document.getElementById(&#39;myChart&#39;).getContext(&#39;2d&#39;);
  const myChart = new Chart(ctx, {
    type: &#39;doughnut&#39;,
    data: {
      labels: this.ValuesLabel,
      datasets: [
        {
          label: this.topLabel,
          data: this.dataChart,
        }
      ]
    },
    options : options
  });
  return myChart;

with this method you can forget about importing 'Colors' because it doesn't exist anymore as a lib. It is now a plug-in.

However, if you need to createt multiple charts in the same vue-app, it seems to be
impossible and you have to use a lib that uses svg tags instead of canvas. I tried
to use svg tags with chart.js but it is impossible.

huangapple
  • 本文由 发表于 2023年5月28日 23:59:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定