简体   繁体   English

让chartsjs显示数据键label而不是值

[英]getting chartsjs to show key of data label instead of value

I am having trouble with my chartsjs labels.我的chartsjs标签有问题。 my graph generates fine, I am trying to set the chart so that the data labels are for the text part of the data and not the numbers part.我的图表生成良好,我正在尝试设置图表,以便数据标签用于数据的文本部分而不是数字部分。 my code shows the data labels as 4, 6 and 2 where as i need them to show the text: me, you and them.我的代码将数据标签显示为 4、6 和 2,因为我需要它们来显示文本:我、你和他们。

<script>
                        var ctx = document.getElementById('myChart4').getContext('2d');
                        var myChart4 = new Chart(ctx, {
                            plugins: [ChartDataLabels],
                            type: 'doughnut',
                            data: {
                                labels: ['me','you','them'
             
                                ],
                                datasets: [{
                                    label: false,
                                    data: [4,6,2
                                 
                                    ],

                                  

                                }]
                            },
                            options: {
                            responsive: false,
                                plugins: {
                                    datalabels: {
                                        color: 'black',
                                        anchor: 'end',
                                        align: 'end',
                                        offset: 15
                                    },


                                    legend: {
                                    display: false
                                    },
                                    title: {
                                        display: true,
                                        text:'Reason'

                                        }
                                    },
                                    responsive: false,
                                    onClick: (evt, activeEls) => {
                                        show();
                                        
                                    },

                            }
});

You can use the formatter function for this:您可以为此使用formatter function:

 Chart.register(ChartDataLabels) const options = { type: 'bar', data: { labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"], datasets: [{ label: '# of Votes', data: [12, 19, 3, 5, 2, 3], backgroundColor: 'orange' }] }, options: { plugins: { datalabels: { color: 'black', anchor: 'end', align: 'end', offset: 15, formatter: (val, ctx) => (ctx.chart.data.labels[ctx.dataIndex]) }, } } } const ctx = document.getElementById('chartJSContainer').getContext('2d'); new Chart(ctx, options);
 <body> <canvas id="chartJSContainer" width="600" height="400"></canvas> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.0/chart.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-datalabels/2.0.0/chartjs-plugin-datalabels.js"></script> </body>

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

相关问题 ChartsJS获取工具提示的“数据/数据集标签”的值 - ChartsJS get value of Data/Dataset Label for Tooltip 从数组而不是值获取 label 时遇到问题 - Trouble getting label from array instead of value 如何在单元格下拉列表中显示 label 值而不是存储值? - How to show label value in cell dropdown instead of stored value? 创建了一个 onclick function 以从 ChartsJs 中的折线图中删除数据,但出现“无法读取未定义的属性‘数据’”错误 - Created an onclick function to remove data from a line chart in ChartsJs, but getting “Cannot read property 'data' of undefined” error JQPlot-在pointLabels标签中显示实际输入值,而不是点位置 - JQPlot - Show real input value in pointLabels label instead of point location 当数据值为空时将标签显示为 N/A - Show label as N/A when the data value is null React chartsJS 流式传输实时数据 - React chartsJS streaming live data 尝试在 ChartsJS 中为饼图插入另一个标签 - Trying to insert another label for a piechart in ChartsJS 给定一定范围的数字,显示标签而不是轴值? - Given a range of numbers, show label instead of axis value? 角度数据绑定返回键而不是值 - angular data binding returning key instead of value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM