Is there a best approach to handle "no data" with JqPlot?
Assuming that I'm consuming json data with an ajax call, and eventually no data is available, eg:
[['North'][0],['South'][0],['East'][0],['West'][0]]
I always have a condition that I check in my AJAX
function, which checks for no results. Then if no results happen I set a chart so that it looks empty. For this reason var data = [[null]]
should do, where data
is a parameter of jQuery.jqplot('chart', data, {})
. The value of data
might be dependant on a type of chart, thus I tested it for line, bar and pie chart and it works fine.
Optionally you could also hide legend and maybe other parts of the plot. For me just setting the data
and legend is always enough.
var data = [null];
will trigger errors in console and stop JS script execution further.
The better solution will be to use the following.
var data = [''];
jQuery.jqplot('chart', data, {});
This will print any subsequent graphs and/or continue JS scripts execution with no errors in FF/Chrome/IE consoles. :-)
In my case it was the opposite to Rahi's answer (maybe the missing double brackets around null was the issue), meaning I agree with Boro;
This works: var data = [[null]];
and this: var data = [''];
generates an error, one time no matter if I have multiple charts or just one with no data.
I'm running jqPlot 1.0.9
The error I get is: Uncaught Error: No data specified
from the condition in the jqPlot script
if (0 == this.noDataIndicator.show)
throw new Error("No data specified");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With