Commits

Anonymous committed f0fc025

Added custom error handling to display error in plot target with options for background, border, font. Cleaned up some lint.

Comments (0)

Files changed (3)

     $.jqplot = function(target, data, options) {
         var _data, _options;
         
-        // check to see if only 2 arguments were specified, what is what.
-        if (data == null) {
-            throw "No data specified";
-        }
-        if (data.constructor == Array && data.length == 0 || data[0].constructor != Array) {
-            throw "Improper Data Array";
-        }
         if (options == null) {
             if (data instanceof Array) {
                 _data = data;
             _options = options;
         }
         var plot = new jqPlot();
-        plot.init(target, _data, _options);
-        plot.draw();
-        plot.themeEngine.init.call(plot);
-        return plot;
+        try {
+            plot.init(target, _data, _options);
+            plot.draw();
+            plot.themeEngine.init.call(plot);
+            return plot;
+        }
+        catch(e) {
+            var msg = $.jqplot.config.ErrorMessage || e.message;
+            $('#'+target).append('<div style="text-align:center;position:relative;top:50%;">'+msg+'</div>');
+            $('#'+target).css({background: $.jqplot.config.ErrorBackground, border: $.jqplot.config.ErrorBorder, font: $.jqplot.config.ErrorFont});
+        }
     };
         
     $.jqplot.debug = 1;
         defaultHeight:300,
         defaultWidth:400,
         UTCAdjust:false,
-        timezoneOffset: new Date(new Date().getTimezoneOffset() * 60000)
+        timezoneOffset: new Date(new Date().getTimezoneOffset() * 60000),
+        ErrorMessage: '',
+        ErrorBackground: '#fffbf9',
+        ErrorBorder: '1px solid #f6dccb',
+        ErrorFont: ''
     };
     
     $.jqplot.enablePlugins = $.jqplot.config.enablePlugins;
                 throw "Canvas dimension not set";
             }
             
+            if (data == null) {
+                throw{
+                    name: "DataError",
+                    message: "No data to plot."
+                };
+            }
+            if (data.constructor == Array && data.length == 0 || data[0].constructor != Array) {
+                throw{
+                    name: "DataError",
+                    message: "No data to plot."
+                };
+            }
+            
             this.data = data;
             
             this.parseOptions(options);

src/jqplot.themeEngine.js

      * true on success, false on failure.
      */
     $.jqplot.ThemeEngine.prototype.remove = function(name) {
-        if (name == 'Default') return false;
+        if (name == 'Default') {
+            return false;
+        }
         return delete this.themes[name];
     };
 
     	}
 
     	// Handle case when target is a string or something (possible in deep copy)
-    	if ( typeof target !== "object" && !toString.call(target) === "[object Function]" )
-    		target = {};
+    	if ( typeof target !== "object" && !toString.call(target) === "[object Function]" ) {
+    	    target = {};
+    	}
 
-    	for ( ; i < length; i++ )
+    	for ( ; i < length; i++ ){
     		// Only deal with non-null/undefined values
-    		if ( (options = arguments[ i ]) != null )
+    		if ( (options = arguments[ i ]) != null ) {
     			// Extend the base object
     			for ( var name in options ) {
     				var src = target[ name ], copy = options[ name ];
 
     				// Prevent never-ending loop
-    				if ( target === copy )
+    				if ( target === copy ) {
     					continue;
+    				}
 
     				// Recurse if we're merging object values
-    				if ( deep && copy && typeof copy === "object" && !copy.nodeType )
+    				if ( deep && copy && typeof copy === "object" && !copy.nodeType ) {
     					target[ name ] = $.jqplot.extend( deep, 
     						// Never move original objects, clone them
     						src || ( copy.length != null ? [ ] : { } )
     					, copy );
-
+                    }
     				// Don't bring in undefined values
-    				else if ( copy !== undefined )
+    				else if ( copy !== undefined ) {
     					target[ name ] = copy;
+    				}
     			}
-
+    		}
+        }
     	// Return the modified object
     	return target;
     };
         this.borderWidth = null;
         this.ticks = new AxisTicks();
         this.label = new AxisLabel();
-    }
+    };
     
     var AxisTicks = function() {
         this.show = null;
         this.whiteSpace = null;
         this.fontSize = null;
         this.fontFamily = null;
-    }
+    };
     
     var AxisLabel = function() {
         this.textColor = null;
         this.fontSize = null;
         this.fontFamily = null;
         this.fontWeight = null;
-    }
+    };
     
     var LineSeriesProperties = function() {
         this.color=null;

src/plugins/jqplot.pointLabels.js

                     label = '';
                 }
                 
-                if (label != null) label = p.formatter(p.formatString, label);
+                if (label != null) {
+                    label = p.formatter(p.formatString, label);
+                } 
                 var elem = $('<div class="jqplot-point-label jqplot-series-'+this.index+' jqplot-point-'+i+'" style="position:absolute"></div>');
                 elem.insertAfter(sctx.canvas);
                 p._elems.push(elem);