简体   繁体   English

使用 Chart.js 在圆环图上显示标签

[英]Displaying labels on a Doughnut Chart using Chart.js

I am really stuck at the moment.我现在真的被困住了。

Using Chart.js v3.2.1 to display some charts, which were working great.使用 Chart.js v3.2.1 显示一些图表,效果很好。

Then when I attempted use the chartjs-plugin-datalabels plugin to display labels on a Doughnut chart, that chart no longer displays.然后,当我尝试使用 chartjs-plugin-datalabels 插件在甜甜圈图表上显示标签时,该图表不再显示。

I can't see what I've done wrong.我看不出我做错了什么。 I'm in need of help!我需要帮助!

Note: There are a lot of questions similar to this on Google and Stackoverflow but most of them are about previous versions, but my work has only approved for me to be working with the lastst version of Chart.JS.注意:在 Google 和 Stackoverflow 上有很多类似的问题,但大多数都是关于以前的版本,但我的工作只批准我使用 Chart.JS 的最新版本。

 //DOUGHNUT GRAPH var doughnutChartData = { labels: [ 'Dr @ Fault', 'TP @ Fault', 'Wthr Evt', 'Other' ], datasets: [ { label: "slices", borderWidth: 3, data: [2,3,2,1], backgroundColor: [ '#D6001C', '#00A3E0', '#52A886', '#2E3138' ], borderColor: [ '#fff', '#fff', '#fff', '#fff' ] } ] }; //DOUGHNUT CHART OPTIONS var doughnutChartOptions = { responsive: true, plugins: { datalabels: { formatter: function (value, context) { return context.chart.data.labels[ context.dataIndex ]; }, }, title: { display: true, text: "Reported Fault Allocation", color: "#D6001C", font: { family: "AvenirNextLTW01-Regular", size: 16, style: 'normal' } }, legend: { display: false } }, scales: { x: { grid: { display: false, drawBorder: true } }, y: { grid: { display: true, drawBorder: true, }, }, }, elements: { point: { radius: 0 } }, } //DISPLAY DOUGHNUT GRAPH var ctx = document.getElementById("canvas3-detailed").getContext("2d"); window.myDoughnut = new Chart(ctx, { plugins: [ChartDataLabels], type: "doughnut", data: doughnutChartData, options: doughnutChartOptions });
 <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/chart.js"></script> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2"></script> <canvas id="canvas3-detailed"></canvas>

The link for your Plugin is broken.您的插件的链接已损坏。 You need to remove @2 from the end:您需要从末尾删除@2

https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels

There is also the not defined error because you reference ChartDataLabels which has not been declared.还有未定义的错误,因为您引用了尚未声明的ChartDataLabels You need to put it in a string:你需要把它放在一个字符串中:

 //DOUGHNUT GRAPH var doughnutChartData = { labels: [ 'Dr @ Fault', 'TP @ Fault', 'Wthr Evt', 'Other' ], datasets: [ { label: "slices", borderWidth: 3, data: [2,3,2,1], backgroundColor: [ '#D6001C', '#00A3E0', '#52A886', '#2E3138' ], borderColor: [ '#fff', '#fff', '#fff', '#fff' ] } ] }; //DOUGHNUT CHART OPTIONS var doughnutChartOptions = { responsive: true, plugins: { datalabels: { formatter: function (value, context) { return context.chart.data.labels[ context.dataIndex ]; }, }, title: { display: true, text: "Reported Fault Allocation", color: "#D6001C", font: { family: "AvenirNextLTW01-Regular", size: 16, style: 'normal' } }, legend: { display: false } }, scales: { x: { grid: { display: false, drawBorder: true } }, y: { grid: { display: true, drawBorder: true, }, }, }, elements: { point: { radius: 0 } }, } //DISPLAY DOUGHNUT GRAPH var ctx = document.getElementById("canvas3-detailed").getContext("2d"); window.myDoughnut = new Chart(ctx, { plugins: ["ChartDataLabels"], type: "doughnut", data: doughnutChartData, options: doughnutChartOptions });
 <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels"></script> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <canvas id="canvas3-detailed"></canvas>

I found out that the version of the plugin I used was incorrect.我发现我使用的插件版本不正确。 I have now updated and it is displaying with labels!我现在已经更新,它正在显示标签!

 //DOUGHNUT GRAPH var doughnutChartData = { labels: [ 'Dr @ Fault', 'TP @ Fault', 'Wthr Evt', 'Other' ], datasets: [{ label: "slices", borderWidth: 3, data: [2, 3, 2, 1], backgroundColor: [ '#D6001C', '#00A3E0', '#52A886', '#2E3138' ], borderColor: [ '#fff', '#fff', '#fff', '#fff' ] }] }; //DOUGHNUT CHART OPTIONS var doughnutChartOptions = { responsive: true, plugins: { datalabels: { color: 'white', formatter: function (value, context) { return context.chart.data.labels[ context.dataIndex ]; }, }, title: { display: true, text: "Reported Fault Allocation", color: "#D6001C", font: { family: "AvenirNextLTW01-Regular", size: 16, style: 'normal' } }, legend: { display: false } }, scales: { x: { grid: { display: false, drawBorder: true } }, y: { grid: { display: true, drawBorder: true, }, }, }, elements: { point: { radius: 0 } }, } //DISPLAY DOUGHNUT GRAPH var ctx = document.getElementById("canvas3-detailed").getContext("2d"); window.myDoughnut = new Chart(ctx, { plugins: [ChartDataLabels], type: "doughnut", data: doughnutChartData, options: doughnutChartOptions });
 <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/chart.js"></script> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2.0.0-rc"></script> <canvas id="canvas3-detailed"></canvas>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM