Commit 91b7ef47 authored by Alexander.Trofimov's avatar Alexander.Trofimov

fix percentile calculate

parent 008d0461
......@@ -243,19 +243,19 @@
res.aColors.push(this.aColors[i].clone());
return res;
};
CColorScale.prototype.getMin = function(min, max, count) {
CColorScale.prototype.getMin = function(min, max, values) {
var oCFVO = (0 < this.aCFVOs.length) ? this.aCFVOs[0] : null;
return this.getValue(min, max, count, oCFVO);
return this.getValue(min, max, values, oCFVO);
};
CColorScale.prototype.getMid = function(min, max, count) {
CColorScale.prototype.getMid = function(min, max, values) {
var oCFVO = (2 < this.aCFVOs.length ? this.aCFVOs[1] : null);
return this.getValue(min, max, count, oCFVO);
return this.getValue(min, max, values, oCFVO);
};
CColorScale.prototype.getMax = function(min, max, count) {
CColorScale.prototype.getMax = function(min, max, values) {
var oCFVO = (2 === this.aCFVOs.length) ? this.aCFVOs[1] : (2 < this.aCFVOs.length ? this.aCFVOs[2] : null);
return this.getValue(min, max, count, oCFVO);
return this.getValue(min, max, values, oCFVO);
};
CColorScale.prototype.getValue = function(min, max, count, oCFVO) {
CColorScale.prototype.getValue = function(min, max, values, oCFVO) {
var res = min;
if (oCFVO) {
// ToDo Formula
......@@ -273,7 +273,12 @@
res = min + Math.floor((max - min) * parseFloat(oCFVO.Val) / 100);
break;
case AscCommonExcel.ECfvoType.Percentile:
res = min + Math.floor(count * parseFloat(oCFVO.Val) / 100);
res = AscCommonExcel.getPercentile(values, parseFloat(oCFVO.Val) / 100.0);
if (AscCommonExcel.cElementType.number === res.type) {
res = res.getValue();
} else {
res = min;
}
break;
}
}
......
......@@ -372,6 +372,27 @@ function (window, undefined) {
return getLogGammaHelper(fZ + 2) - Math.log(fZ + 1) - Math.log(fZ);
}
function getPercentile(values, alpha) {
values.sort(fSortAscending);
var nSize = values.length;
if (nSize === 0) {
return new cError(cErrorType.not_available);
} else {
if (nSize === 1) {
return new cNumber(values[0]);
} else {
var nIndex = Math.floor(alpha * (nSize - 1));
var fDiff = alpha * (nSize - 1) - Math.floor(alpha * (nSize - 1));
if (fDiff == 0.0) {
return new cNumber(values[nIndex]);
} else {
return new cNumber(values[nIndex] + fDiff * (values[nIndex + 1] - values[nIndex]));
}
}
}
}
function cAVEDEV() {
// cBaseFunction.call( this, "AVEDEV" );
// this.setArgumentsMin( 1 );
......@@ -391,7 +412,7 @@ function cAVEDEV() {
}
cAVEDEV.prototype = Object.create( cBaseFunction.prototype )
cAVEDEV.prototype = Object.create( cBaseFunction.prototype );
cAVEDEV.prototype.Calculate = function ( arg ) {
var count = 0, sum = new cNumber( 0 ), arrX = [];
for ( var i = 0; i < arg.length; i++ ) {
......@@ -446,7 +467,7 @@ cAVEDEV.prototype.getInfo = function () {
name:this.name,
args:"( argument-list )"
};
}
};
function cAVERAGE() {
// cBaseFunction.call( this, "AVERAGE" );
......@@ -3664,26 +3685,7 @@ cPERCENTILE.prototype.Calculate = function ( arg ) {
}
}
tA.sort(fSortAscending);
var nSize = tA.length;
if ( tA.length < 1 || nSize == 0 )
return new cError( cErrorType.not_available );
else {
if ( nSize == 1 )
return new cNumber( tA[0] );
else {
var nIndex = Math.floor( alpha * (nSize - 1) );
var fDiff = alpha * (nSize - 1) - Math.floor( alpha * (nSize - 1) );
if ( fDiff == 0.0 )
return new cNumber( tA[nIndex] );
else {
return new cNumber( tA[nIndex] +
fDiff * (tA[nIndex + 1] - tA[nIndex]) );
}
}
}
return getPercentile(tA, alpha);
}
var arg0 = arg[0], arg1 = arg[1];
......@@ -5665,6 +5667,7 @@ cZTEST.prototype = Object.create( cBaseFunction.prototype );
window['AscCommonExcel'].phi = phi;
window['AscCommonExcel'].gauss = gauss;
window['AscCommonExcel'].gaussinv = gaussinv;
window['AscCommonExcel'].getPercentile = getPercentile;
window['AscCommonExcel'].cAVERAGE = cAVERAGE;
window['AscCommonExcel'].cCOUNT = cCOUNT;
......
......@@ -3650,12 +3650,12 @@ Woorksheet.prototype._updateConditionalFormatting = function(range) {
l = oRuleElement.aColors.length;
if (0 < values.length && 2 <= l) {
oGradient1 = new AscCommonExcel.CGradient(oRuleElement.aColors[0], oRuleElement.aColors[1]);
min = oRuleElement.getMin(min, max, nc);
max = oRuleElement.getMax(min, max, nc);
min = oRuleElement.getMin(min, max, values);
max = oRuleElement.getMax(min, max, values);
oGradient2 = null;
if (2 < l) {
oGradient2 = new AscCommonExcel.CGradient(oRuleElement.aColors[1], oRuleElement.aColors[2]);
mid = oRuleElement.getMid(min, max, nc);
mid = oRuleElement.getMid(min, max, values);
oGradient1.init(min, mid);
oGradient2.init(mid, max);
......@@ -6306,6 +6306,14 @@ Cell.prototype.setFormulaCA = function(ca){
};
//-------------------------------------------------------------------------------------------------
function CellAndValue(c, v) {
this.c = c;
this.v = v;
}
CellAndValue.prototype.valueOf = function() {
return this.v;
};
/**
* @constructor
*/
......@@ -6594,7 +6602,7 @@ Range.prototype._getRangeType=function(oBBox){
Range.prototype._getValues = function (withEmpty) {
var res = [];
var fAction = function(c) {
res.push({c: c, v: c.getValueWithoutFormat()});
res.push(new CellAndValue(c, c.getValueWithoutFormat()));
};
if (withEmpty) {
this._setProperty(null, null, fAction);
......@@ -6607,7 +6615,7 @@ Range.prototype._getValuesAndMap = function (withEmpty) {
var v, arrRes = [], mapRes = {};
var fAction = function(c) {
v = c.getValueWithoutFormat();
arrRes.push({c: c, v: v});
arrRes.push(new CellAndValue(c, v));
mapRes[v.toLowerCase()] = true;
};
if (withEmpty) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment