Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle no data in JqPlot

Tags:

jquery

jqplot

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]]
like image 673
Custodio Avatar asked Jun 28 '12 13:06

Custodio


3 Answers

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.

like image 95
Boro Avatar answered Nov 15 '22 17:11

Boro


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. :-)

like image 25
Rahi Avatar answered Nov 15 '22 18:11

Rahi


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");
like image 2
orjtor Avatar answered Nov 15 '22 18:11

orjtor