如何在Doughnut图表中使用chart.js React添加标题

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

How to add a title in Doughnut chart chart.js React

问题

我想在我的React应用中为圆环图添加一个标题,但出现了问题。我有以下代码:

const chartData = {
  labels: [],
  datasets: [{
    data: [],
    backgroundColor: [
      Colors.primary,
      Colors.secondary,
      Colors.danger,
      Colors.warning
    ],
    hoverBackgroundColor: [
      'rgb(143, 0, 180, 0.3)',
      'rgb(0, 196, 204, 0.3)',
      'rgb(206, 0, 0, 0.3)',
      'rgb(255, 179, 0, 0.3)'
    ],
    hoverOffset: 10
  }]
};

const options = {
  responsive: true,
  legend: {
    display: false,
    position: 'right',
  },
  title: {
    display: true,
    fontSize: 20,
    text: 'Tickets'
  }
}
<Doughnut 
  data={chartData} 
  options={options}
/>

这里可能出了什么问题?我还想将标签放在图表的右侧,但这也不起作用。以下是输出。

如何在Doughnut图表中使用chart.js React添加标题

英文:

I want to add a Title for the Doughnut chart in my React app but for some reason it doesn't work. I have this code:

const chartData = {
      labels: [],
      datasets: [{
        data: [],
        backgroundColor: [
          Colors.primary,
          Colors.secondary,
          Colors.danger,
          Colors.warning
        ],
        hoverBackgroundColor: [
          &#39;rgb(143, 0, 180, 0.3)&#39;,
          &#39;rgb(0, 196, 204, 0.3)&#39;,
          &#39;rgb(206, 0, 0, 0.3)&#39;,
          &#39;rgb(255, 179, 0, 0.3)&#39;
        ],
        hoverOffset: 10
      }]
    };

    const options = {
      responsive: true,
      legend: {
        display: false,
        position: &#39;right&#39;,
      },
      title: {
        display: true,
        fontSize: 20,
        text: &#39;Tickets&#39;
      }
    }

        &lt;Doughnut 
          data = { chartData } 
          options = { options }
        /&gt;

What could have gone wrong here? I also want to place the labels on the right side of the chart, but it doesn't work as well. Below is the output.

如何在Doughnut图表中使用chart.js React添加标题

答案1

得分: 1

自 Chart.js 版本 3 起,titlesubtitlelegendtooltip 都是插件,它们的选项必须在插件节点中定义。
据我所见,react-chartjs-2 适用于 Chart.js 版本 >= 3。

const options = {
  responsive: true,
  plugins: {
    legend: {
      display: false,
      position: 'right',
    },
    title: {
      display: true,
      fontSize: 20, // &lt;--- 自 CHART.JS 3 起不再是托管选项
      text: 'Tickets'
    }
  }
}
英文:

Since Chart.js version 3 title, subtitle, legend and tooltip are plugins and their options must be defined in plugins node.
As far as I have seen, react-chartjs-2 is working with Chartjs version >= 3.

const options = {
  responsive: true,
  plugins: {
    legend: {
      display: false,
      position: &#39;right&#39;,
    },
    title: {
      display: true,
      fontSize: 20, // &lt;--- this is not a managed option since CHART.JS 3
      text: &#39;Tickets&#39;
    }
  }
}

huangapple
  • 本文由 发表于 2023年2月18日 20:41:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493409.html
匿名

发表评论

匿名网友

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

确定