score:6
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).
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. 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>
Source: stackoverflow.com
Related Query
- ChartJS – How to show border on empty pie chart?
- ChartJS Pie Chart How default just show 2 legend datas
- ChartJs - Pie Chart - how to remove labels that are on the pie chart
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- How to show percentage (%) using chartjs-plugin-labels ( Pie chart ) in angular 2/8
- ChartJS version 3 how to add percentage to pie chart tooltip
- How to show slice value inside of slice in pie chart using chart.js
- How to style a pie chart in chart js? I want to change the border color, border width and give them shadow
- How to get labels on ChartJS Pie chart segments
- How can I change the cursor on pie chart segment hover in ChartJS 3?
- How to make chartjs pie chart responsive
- How to group smaller Pie Chart slices together to Improve Readability in chartjs
- How to show the chartjs bar chart data values labels as text?
- how to display name on multi series pie chart in chartjs
- Char.js - How to show labels by default in pie chart
- How to start the chart from specific time and offest hour and then show the data on chart from target datetime in chartjs
- How can I remove the white border from Chart.js pie chart when all legends are hidden?
- How to set up a simple pie chart using React in ChartJS on codesandbox
- ChartJS - How to add Text between Pie Chart and Legend
- How can I show "No Data" message in Pie Chart when there is no data using VueJS?
- How Can customize chartjs doughnut chart border and tooltip
- How to show text likt this in Chartjs Dougnet chart
- Laravel - How to Display both count and percentage (%) in chartjs pie chart
- How to show labels above pie chart in chart.js
- I am Creating pie chart using Chartjs 2.6.0. I want to show label on Slices
- Chart.js Show labels on Pie chart
- Chartjs random colors for each part of pie chart with data dynamically from database
- chart.js: Show labels outside pie chart
- How set color family to pie chart in chart.js
- Chart.js how to show cursor pointer for labels & legends in line chart
More Query from same tag
- How to generate dynamic bar chart in laravel using chart.js
- Chart.js ignore options
- Chartjs2 set data 0 to type doughnut
- Chart.js - Tooltips overlapped by bar values
- ChartJs: Is there a way to control the font options per line for a multiline axis label
- ChartJs line chart repaint glitch while hovering over
- Cannot read property 'labels' of undefined
- How to add font color in vue-chartjs
- Updating Chartjs Data with Response from POST Call?
- How can I fasten up the Chart.js graph generation from JQuery over PHP and mySQL?
- Gantt Chart Variation with Chart JS or other libraries
- Charts.js graph not displaying
- chat,js stacked bar chart (make one dataset into stacked)
- Papaparse with chart.js not displaying csv value on the x axis
- Change point size and color on hover in chartjs
- Chartjs real time graph x axis movement
- Vue.js component for Chart.js - strange issue on AJAX reload
- Chart.js Example Isn't working
- How can I change color of only one column in Barchart using Chart.js
- Chart.js doughnut text colors
- Importing data from Model into a View gives me an error I cannot solve
- Dropdown component not having effect in Chromium browser
- Update of options for PrimeNG Chart needs two calls instead of one
- Chart.js drag points on linear chart
- Chart.js dataset label issue
- Angular .subscribe executes at the end after the ngOnInit
- How to show dynamic dates with month name of current month and previous month in momentjs?
- Chart.js: Thousand Separator and Tittle ind the Middle of the Doughnut
- Y axis set custom value using chart.js
- Chart.JS changing the animations