简体   繁体   English

ChartJS - 如何在空饼图上显示边框?

[英]ChartJS – How to show border on empty pie chart?

I have a couple of pie/doughnut charts displayed using ChartJS . 我使用ChartJS显示了几个饼图/圆环图。 Usually, they contain data like [200, 1200, 300] and [30, 500] to give an estimate, but at rare occasions, they will contain [0, 0, 0] and [0, 0] . 通常,它们包含[200, 1200, 300][30, 500]来进行估算,但在极少数情况下,它们将包含[0, 0, 0][0, 0] The problem then, is that the chart disappears, even though I have a border enabled. 问题是,即使我启用了边框,图表也会消失。 I have solved this by adding dummy values to the first element in the arrays, but I don't like how the code looks and want a better way. 我已经通过向数组中的第一个元素添加虚拟值来解决这个问题,但我不喜欢代码的外观和想要更好的方法。

Is it possible to make the border visible (a circle) when the array contains only zeroes? 当数组只包含零时,是否可以使边框可见(圆圈)?

Edit: I don't seem to get any answers to this question. 编辑:我似乎没有得到这个问题的任何答案。 Is it considered a bug when the border is not showing on empty data or is this intended behavior? 当边框没有显示在空数据上或者这是预期的行为时,它被认为是一个错误吗? I don't know if anyone else has had this problem. 我不知道是否有其他人有这个问题。 I still need to find an elegant way to deal with this issue, and I haven't found one yet. 我仍然需要找到一个优雅的方法来处理这个问题,我还没有找到一个。

This is not a bug, as the borders are rendered per data item. 这不是错误,因为边界是按数据项呈现的。 If all the data is 0s, then every slice has no width, so no border can show (the only other way to handle this scenario for ChartJS would be to give each item equal sizing, which isn't better). 如果所有数据都是0,那么每个切片都没有宽度,因此没有边框可以显示(处理ChartJS的这种情况的唯一方法是给每个项目大小相等,这不是更好)。

You can add a dummy value without a label and filter the legend and tooltips so that the data is treated like a blank space to the user. 您可以添加没有标签的虚拟值,并过滤图例和工具提示,以便将数据视为用户的空白区域。 In the example below, a check before rendering the chart ensures that if all data points are 0, a data point with a value of 1 is added to the chart with no label. 在下面的示例中,在呈现图表之前进行检查可确保如果所有数据点均为0,则将值为1的数据点添加到图表中,不带标签。 Two datasets are shown to highlight the difference. 显示两个数据集以突出显示差异。

 let ctx = document.getElementById('chartContainer').getContext('2d'); let data = [[0, 0, 0], [1,2,3]]; let labels = ["A", "B", "C"]; let bgColors = ['yellow', 'orange', 'aquamarine']; let options = { borderWidth: 1, borderColor: 'black', legend: { labels: { // Prevent items with undefined labels from appearing in the legend filter: (item) => item.text !== undefined } }, tooltips: { // Prevent items with undefined labels from showing tooltips filter: (item, chart) => chart.labels[item.index] !== undefined } } let chartConfig = { type: 'pie', data: { labels: labels, datasets: [{ data: data[0], backgroundColor: bgColors, label: "data", borderColor: 'black', borderWidth: 2 }, { data: data[1], backgroundColor: bgColors, label: "data", borderColor: 'black', borderWidth: 2 }] }, options: options } // Check if data is all 0s; if it is, add dummy data to end with empty label chartConfig.data.datasets.forEach(dataset => { if (dataset.data.every(el => el === 0)) { dataset.backgroundColor.push('rgba(255,255,255,0)'); dataset.data.push(1); } }) let pieChart = new Chart(ctx, chartConfig); 
 .chartContainer { height: 200px; width: 200px; } 
 <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script> <div class="chartContainer"> <canvas id="chartContainer" width="200" height="200"></canvas> </div> 

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

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