diff --git a/changelog b/changelog index 6e0d531..e587f6f 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,8 @@ +From version 1.7.0 to 1.8.0 +--------------------------- +- Ensure nbClass param is an integer | issue #45 + + From version 1.6.0 to 1.7.0 --------------------------- - Creating temporary variables instead of calling functions several times | issue #40 diff --git a/lib/geostats.js b/lib/geostats.js index 943bb2d..6da0e41 100755 --- a/lib/geostats.js +++ b/lib/geostats.js @@ -465,6 +465,19 @@ var geostats = function(a) { }; + /** ensure nbClass is an integer */ + this._nbClassInt = function(nbClass) { + + var nbclassTmp = parseInt(nbClass, 10); + if (isNaN(nbclassTmp)) { + if(this.silent) this.log("[silent mode] '" + nbclassTmp + "' is not a valid integer. Enable to set class number.", true); + else throw new TypeError("'" + nbclassTmp + "' is not a valid integer. Enable to set class number."); + } else { + return nbclassTmp; + } + + }; + /** check if the serie contains negative value */ this._hasNegativeValue = function() { @@ -559,6 +572,8 @@ var geostats = function(a) { */ this.getClassEqInterval = function(nbClass, forceMin, forceMax) { + nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer + if (this._nodata()) return; @@ -588,6 +603,9 @@ var geostats = function(a) { this.getQuantiles = function(nbClass) { + + nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer + var tmp = this.sorted(); var quantiles = []; @@ -606,6 +624,8 @@ var geostats = function(a) { */ this.getClassQuantile = function(nbClass) { + nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer + if (this._nodata()) return; @@ -633,6 +653,8 @@ var geostats = function(a) { */ this.getClassStdDeviation = function(nbClass, matchBounds) { + nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer + if (this._nodata()) return; @@ -715,6 +737,8 @@ var geostats = function(a) { */ this.getClassGeometricProgression = function(nbClass) { + nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer + if (this._nodata()) return; @@ -765,6 +789,8 @@ var geostats = function(a) { */ this.getClassArithmeticProgression = function(nbClass) { + nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer + if (this._nodata()) return; @@ -805,6 +831,8 @@ var geostats = function(a) { */ this.getClassJenks = function(nbClass) { + nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer + if (this._nodata()) return; diff --git a/lib/geostats.min.js b/lib/geostats.min.js index c099306..8d3f5d6 100644 --- a/lib/geostats.min.js +++ b/lib/geostats.min.js @@ -7,19 +7,20 @@ this.serie[0];for(i=0;ithis.serie[i])return!0;return!1};this._hasZeroValue=function(){for(i=0;id?1:0}));return this.stat_sorted};this.info=function(){if(!this._nodata()){var a="Population : "+this.pop()+" - [Min : "+this.min()+" | Max : "+this.max()+"]\n";a+="Mean : "+this.mean()+" - Median : "+this.median()+"\n";return a+="Variance : "+this.variance()+" - Standard deviation : "+ -this.stddev()+" - Coefficient of variation : "+this.cov()+"\n"}};this.setClassManually=function(a){if(!this._nodata())if(a[0]!==this.min()||a[a.length-1]!==this.max())if(this.silent)this.log("[silent mode] "+t("Given bounds may not be correct! please check your input.\nMin value : "+this.min()+" / Max value : "+this.max()),!0);else throw new TypeError("Given bounds may not be correct! please check your input.\nMin value : "+this.min()+" / Max value : "+this.max());else return this.setBounds(a),this.setRanges(), -this.method="manual classification ("+(a.length-1)+" classes)",this.bounds};this.getClassEqInterval=function(a,b,c){if(!this._nodata()){b="undefined"===typeof b?this.min():b;c="undefined"===typeof c?this.max():c;var d=[],e=b;b=(c-b)/a;for(i=0;i<=a;i++)d[i]=e,e+=b;d[a]=c;this.setBounds(d);this.setRanges();this.method="eq. intervals ("+a+" classes)";return this.bounds}};this.getQuantiles=function(a){for(var b=this.sorted(),c=[],d=this.pop()/a,e=1;e=h+c[q][n-1]&&(b[d][n]=p,c[d][n]=h+c[q][n-1])}b[d][1]=1;c[d][1]=h}h=dataList.length;c=[];for(d=0;d<=a;d++)c.push(0);c[a]=parseFloat(dataList[dataList.length-1]);c[0]=parseFloat(dataList[0]);for(d=a;2<=d;)e=parseInt(b[h][d]-2),c[d-1]=dataList[e],h=parseInt(b[h][d]-1),--d;c[0]==c[1]&&(c[0]=0);this.setBounds(c);this.setRanges();this.method="Jenks ("+a+" classes)";return this.bounds}}; -this.getClassUniqueValues=function(){if(!this._nodata()){this.is_uniqueValues=!0;var a=this.sorted(),b=[];for(i=0;iparseFloat(this.bounds[c])&&(a[c-1]=""+d+this.separator+b[i-1],d=b[i],c++);if(c==this.bounds.length-1)return a[c-1]=""+d+this.separator+b[b.length-1],this.inner_ranges=a}};this.getSortedlist=function(){return this.sorted().join(", ")}; -this.getHtmlLegend=function(a,b,c,d,e,g){var f="",k=[];this.doCount();ccolors=null!=a?a:this.colors;lg=null!=b?b:"Legend";getcounter=null!=c?!0:!1;fn=null!=d?d:function(a){return a};null==e&&(e="default");if("discontinuous"==e&&(this.getInnerRanges(),-1!==this.counter.indexOf(0))){if(this.silent)this.log("[silent mode] Geostats cannot apply 'discontinuous' mode to the getHtmlLegend() method because some classes are not populated.\nPlease switch to 'default' or 'distinct' modes. Exit!",!0);else throw new TypeError("Geostats cannot apply 'discontinuous' mode to the getHtmlLegend() method because some classes are not populated.\nPlease switch to 'default' or 'distinct' modes. Exit!"); +this.stat_median=this.stat_mean=this.stat_sorted=null};this._nodata=function(){if(0==this.serie.length){if(this.silent)this.log("[silent mode] Error. You should first enter a serie!",!0);else throw new TypeError("Error. You should first enter a serie!");return 1}return 0};this._nbClassInt=function(a){a=parseInt(a,10);if(isNaN(a))if(this.silent)this.log("[silent mode] '"+a+"' is not a valid integer. Enable to set class number.",!0);else throw new TypeError("'"+a+"' is not a valid integer. Enable to set class number."); +else return a};this._hasNegativeValue=function(){for(i=0;ithis.serie[i])return!0;return!1};this._hasZeroValue=function(){for(i=0;id?1:0}));return this.stat_sorted}; +this.info=function(){if(!this._nodata()){var a="Population : "+this.pop()+" - [Min : "+this.min()+" | Max : "+this.max()+"]\n";a+="Mean : "+this.mean()+" - Median : "+this.median()+"\n";return a+="Variance : "+this.variance()+" - Standard deviation : "+this.stddev()+" - Coefficient of variation : "+this.cov()+"\n"}};this.setClassManually=function(a){if(!this._nodata())if(a[0]!==this.min()||a[a.length-1]!==this.max())if(this.silent)this.log("[silent mode] "+t("Given bounds may not be correct! please check your input.\nMin value : "+ +this.min()+" / Max value : "+this.max()),!0);else throw new TypeError("Given bounds may not be correct! please check your input.\nMin value : "+this.min()+" / Max value : "+this.max());else return this.setBounds(a),this.setRanges(),this.method="manual classification ("+(a.length-1)+" classes)",this.bounds};this.getClassEqInterval=function(a,b,c){a=this._nbClassInt(a);if(!this._nodata()){b="undefined"===typeof b?this.min():b;c="undefined"===typeof c?this.max():c;var d=[],e=b;b=(c-b)/a;for(i=0;i<=a;i++)d[i]= +e,e+=b;d[a]=c;this.setBounds(d);this.setRanges();this.method="eq. intervals ("+a+" classes)";return this.bounds}};this.getQuantiles=function(a){a=this._nbClassInt(a);for(var b=this.sorted(),c=[],d=this.pop()/a,e=1;e=h+c[q][n-1]&&(b[d][n]=p,c[d][n]=h+c[q][n-1])}b[d][1]=1;c[d][1]=h}h=dataList.length;c=[];for(d=0;d<=a;d++)c.push(0);c[a]=parseFloat(dataList[dataList.length-1]);c[0]=parseFloat(dataList[0]);for(d=a;2<=d;)e=parseInt(b[h][d]-2),c[d-1]=dataList[e],h=parseInt(b[h][d]-1),--d;c[0]==c[1]&&(c[0]=0);this.setBounds(c);this.setRanges();this.method="Jenks ("+a+" classes)";return this.bounds}};this.getClassUniqueValues=function(){if(!this._nodata()){this.is_uniqueValues= +!0;var a=this.sorted(),b=[];for(i=0;iparseFloat(this.bounds[c])&&(a[c-1]=""+d+this.separator+b[i-1],d=b[i],c++);if(c==this.bounds.length-1)return a[c-1]=""+d+this.separator+b[b.length-1],this.inner_ranges=a}};this.getSortedlist=function(){return this.sorted().join(", ")};this.getHtmlLegend=function(a,b,c,d,e,g){var f="",k=[];this.doCount();ccolors=null!=a?a:this.colors; +lg=null!=b?b:"Legend";getcounter=null!=c?!0:!1;fn=null!=d?d:function(a){return a};null==e&&(e="default");if("discontinuous"==e&&(this.getInnerRanges(),-1!==this.counter.indexOf(0))){if(this.silent)this.log("[silent mode] Geostats cannot apply 'discontinuous' mode to the getHtmlLegend() method because some classes are not populated.\nPlease switch to 'default' or 'distinct' modes. Exit!",!0);else throw new TypeError("Geostats cannot apply 'discontinuous' mode to the getHtmlLegend() method because some classes are not populated.\nPlease switch to 'default' or 'distinct' modes. Exit!"); return}"DESC"!==g&&(g="ASC");if(ccolors.length('+this.counter[i]+")"),b=this.ranges[i].split(this.separator),a=parseFloat(b[0]).toFixed(this.precision),b=parseFloat(b[1]).toFixed(this.precision), "distinct"==e&&0!=i&&(m(a)?(a=parseInt(a)+1,"manual"==this.precisionflag&&0!=this.precision&&(a=parseFloat(a).toFixed(this.precision))):(a=parseFloat(a)+1/Math.pow(10,this.precision),a=parseFloat(a).toFixed(this.precision))),"discontinuous"==e&&(b=this.inner_ranges[i].split(this.separator),a=parseFloat(b[0]).toFixed(this.precision),b=parseFloat(b[1]).toFixed(this.precision)),a=fn(a)+this.legendSeparator+fn(b),a='
'+ a+f+"
",k.push(a);else for(i=0;i('+this.counter[i]+")"),a=fn(this.bounds[i]),a='
'+a+f+"
",k.push(a);"DESC"===g&&k.reverse();e='
'+lg+"
";for(i=0;i"}};this.objectID=(new Date).getUTCMilliseconds();this.log("Creating new geostats object"); -"undefined"!==typeof k&&0