score:7
Here an example working with chart.js 2.8.0
<canvas id="pieChartExample01" width="25" height="25"></canvas>
<div id="no-data">Nothing to display</div>
...
options: {
title: {
display: false,
text: 'Overall Activity'
},
animation: {
onComplete: function(animation) {
var firstSet = animation.chart.config.data.datasets[0].data,
dataSum = firstSet.reduce((accumulator, currentValue) => accumulator + currentValue);
if (typeof firstSet !== "object" || dataSum === 0) {
document.getElementById('no-data').style.display = 'block';
document.getElementById('pieChartExample01').style.display = 'none'
}
}
}
}
score:-4
You need to set some data in data: [0, 0, 0, 0] It's imposible to display pie graph without any data i try to change only data and all it's good.
...
data: [2, 3, 4, 5]
...
score:1
For those using version 3.x of the library, here's how I did it
const plugin = {
id: 'emptyChart',
afterDraw(chart, args, options) {
const { datasets } = chart.data;
let hasData = false;
for (let dataset of datasets) {
//set this condition according to your needs
if (dataset.data.length > 0 && dataset.data.some(item => item !== 0)) {
hasData = true;
break;
}
}
if (!hasData) {
//type of ctx is CanvasRenderingContext2D
//https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
//modify it to your liking
const { chartArea: { left, top, right, bottom }, ctx } = chart;
const centerX = (left + right) / 2;
const centerY = (top + bottom) / 2;
chart.clear();
ctx.save();
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText('No data to display', centerX, centerY);
ctx.restore();
}
}
};
And just use the above like so:
const chart1 = new Chart(ctx, {
plugins: [plugin]
});
score:2
For React
(react-chartjs-2)
Use this plugin
const plugins = [
{
afterDraw: function (chart) {
console.log(chart);
if (chart.data.datasets[0].data.length < 1) {
let ctx = chart.ctx;
let width = chart.width;
let height = chart.height;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "30px Arial";
ctx.fillText("No data to display", width / 2, height / 2);
ctx.restore();
}
},
},
];
Use this plugin
<Line height={120} data={props.data} options={options} plugins={plugins} />
<Pie height={320} width={500} data={props.data} options={options} plugins={plugins} />
score:15
Use this plugin I slightly modified which checks if each dataset item is zero:
<script>
Chart.plugins.register({
afterDraw: function(chart) {
if (chart.data.datasets[0].data.every(item => item === 0)) {
let ctx = chart.chart.ctx;
let width = chart.chart.width;
let height = chart.chart.height;
chart.clear();
ctx.save();
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText('No data to display', width / 2, height / 2);
ctx.restore();
}
}
});
</script>
With this, if all dataset items are 0, it will display No data to display
in place of the chart.
Source: stackoverflow.com
Related Query
- Pie chart.js - display a no data held message
- How to display data labels outside in pie chart with lines in ionic
- Show "No Data" message for Pie chart with no data
- Display data labels on a pie chart in angular-chart.js
- How can I show "No Data" message in Pie Chart when there is no data using VueJS?
- Chart.js - Display data label leader lines on a pie chart
- chart.js - Pie Chart doesn't display all data
- Chartjs random colors for each part of pie chart with data dynamically from database
- Display values outside of pie chart in chartjs
- Display Doughnut Pie Chart As Circle Progress Chart.js
- ChartJs line chart - display permanent icon above some data points with text on hover
- How can I display the xAxes and yAxes data in the tooltip, Chart JS?
- Chart JS display Data Value on the top of the Bar
- Custom data in label on ChartJS pie chart
- Beginner using chart.js: having trouble display state full of data into a column chart using variables
- how to display chart data as html table chartjs
- How to display chart using Chartjs with JSON data in Laravel
- How do I get a chart.js chart to display data in a Node/React web application?
- How do I display chart on my HTML page based on JSON data sent by Node JS
- Chart JS how to display an array of data objects as bar chart
- How to get Data from API to display chart using chartjs in Vuejs
- Updating a Pie Chart in React as Data is Entered
- Make chartjs pie chart wiyh dynamic data
- Display legend of grouped data (different colors) in chart
- how to display name on multi series pie chart in chartjs
- ng2-Chart: can we show the tooltip data of pie chart on load?
- The chart doesn't display the data from my call to the API with Axios
- Display chart data based on API call
- How can i display my data in a chart using chart js
- Chart.js How to sum the values in a pie chart and display them in the header
More Query from same tag
- Array data not rendering in data table using this.props - ReactJS
- Using Chart.js on Angular 4
- How to display variables from Laravel controller to view javascript with specific index
- Cannot make my chart to work with data from mongoDB
- Is it possible to make 3D pie chart using chart.js?
- How to have onclick/hover display associated value in ChartJS
- How to customize horizontal bar in chart.js?
- Html chart does not fit a small Android WebView
- ChartJS changing graphs based upon Drop Down selection
- can't dispal 2 garphs on chart.js
- Change time format in Chart.js date
- Chart.js: load new data on click
- Bar Chart cannot read data and label in laravel
- Drawing a mixed stacked horizontal bar/line in chartjs
- Dotted lines using Chart.js
- How can I move a label left, paint it black, or remove it (Chart.JS)?
- Getting x,y values from chart click event
- Canvas JS using ajax call
- Chart.js legend alignment
- label too long in yAxis with chartJs
- Change ChartJs axis title dynamically
- Chart.js always visible labels
- Define new position for tooltip in ng2-charts
- Update Chart.js plugin
- Single Point Curve Chart.js
- Charts.js Formatting Y Axis with both Currency and Thousands Separator
- Vuejs - Chartjs doesn't render my own options with api request
- How to wait for data in useEffect hook before displaying it?
- How can I change color of only one column in Barchart using Chart.js
- pie chart not rendered properly in flask app