summaryrefslogtreecommitdiff
path: root/chart2
diff options
context:
space:
mode:
authorSamuel Mehrbrodt <samuel.mehrbrodt@allotropia.de>2021-03-09 09:44:24 +0100
committerSamuel Mehrbrodt <samuel.mehrbrodt@allotropia.de>2021-03-10 08:37:59 +0100
commita9b71098845edcb4f6b4629795168d174f28ef70 (patch)
tree5c65be57ca1b8b55794e617460ee7e06bb9cf50d /chart2
parent96388e5e809a48573970df9b6b2649517a08447f (diff)
tdf#133423 Fix 'central' formula for moving average
Change-Id: I3685c310fdbe4809cb7863628be96645364c6a7a Reviewed-on: https://gerrit.libreoffice.org/c/core/+/112191 Tested-by: Jenkins Reviewed-by: Samuel Mehrbrodt <samuel.mehrbrodt@allotropia.de>
Diffstat (limited to 'chart2')
-rw-r--r--chart2/source/inc/MovingAverageRegressionCurveCalculator.hxx5
-rw-r--r--chart2/source/tools/MovingAverageRegressionCurveCalculator.cxx44
2 files changed, 37 insertions, 12 deletions
diff --git a/chart2/source/inc/MovingAverageRegressionCurveCalculator.hxx b/chart2/source/inc/MovingAverageRegressionCurveCalculator.hxx
index 8ba5456c2939..8bc76a33762e 100644
--- a/chart2/source/inc/MovingAverageRegressionCurveCalculator.hxx
+++ b/chart2/source/inc/MovingAverageRegressionCurveCalculator.hxx
@@ -52,9 +52,8 @@ private:
const css::uno::Reference<css::chart2::XScaling>& xScalingY,
sal_Bool bMaySkipPointsInCalculation ) override;
- void calculateValues(RegressionCalculationHelper::tDoubleVectorPair aValues, sal_Int32 nPeriod,
- bool bUseXAvg);
-
+ void calculateValues(RegressionCalculationHelper::tDoubleVectorPair aValues, bool bUseXAvg);
+ void calculateValuesCentral(RegressionCalculationHelper::tDoubleVectorPair aValues);
std::vector<double> aYList;
std::vector<double> aXList;
};
diff --git a/chart2/source/tools/MovingAverageRegressionCurveCalculator.cxx b/chart2/source/tools/MovingAverageRegressionCurveCalculator.cxx
index bca154047935..5496f78577e6 100644
--- a/chart2/source/tools/MovingAverageRegressionCurveCalculator.cxx
+++ b/chart2/source/tools/MovingAverageRegressionCurveCalculator.cxx
@@ -53,45 +53,71 @@ void SAL_CALL MovingAverageRegressionCurveCalculator::recalculateRegression(
aYList.clear();
aXList.clear();
+ // For formulas, see
+ // https://docs.oasis-open.org/office/OpenDocument/v1.3/cs02/part3-schema/OpenDocument-v1.3-cs02-part3-schema.html#property-chart_regression-moving-type
+
switch (mnMovingType)
{
case MovingAverageType::Central:
{
- sal_Int32 nCentralPeriod = (mPeriod % 2 == 0) ? (mPeriod / 2) : ((mPeriod - 1) / 2);
- calculateValues(aValues, nCentralPeriod, false);
+
+ calculateValuesCentral(aValues);
break;
}
case MovingAverageType::AveragedAbscissa:
{
- calculateValues(aValues, mPeriod, true);
+ calculateValues(aValues, true);
break;
}
case MovingAverageType::Prior:
default:
{
- calculateValues(aValues, mPeriod, false);
+ calculateValues(aValues, false);
break;
}
}
}
+void MovingAverageRegressionCurveCalculator::calculateValuesCentral(
+ RegressionCalculationHelper::tDoubleVectorPair aValues)
+{
+ const size_t aSize = aValues.first.size();
+ for (size_t i = mPeriod - 1; i < aSize; ++i)
+ {
+ double yAvg = 0.0;
+
+ for (sal_Int32 j = 0; j < mPeriod; j++)
+ {
+ yAvg += aValues.second[i - j];
+ }
+ yAvg /= mPeriod;
+ aYList.push_back(yAvg);
+ }
+ sal_Int32 nPeriodLocal = (mPeriod % 2 == 0) ? (mPeriod / 2) : ((mPeriod - 1) / 2);
+ for (size_t i = nPeriodLocal; i < aSize - 1; ++i)
+ {
+ double x = aValues.first[i];
+ aXList.push_back(x);
+ }
+}
+
void MovingAverageRegressionCurveCalculator::calculateValues(
- RegressionCalculationHelper::tDoubleVectorPair aValues, sal_Int32 nPeriod, bool bUseXAvg)
+ RegressionCalculationHelper::tDoubleVectorPair aValues, bool bUseXAvg)
{
const size_t aSize = aValues.first.size();
- for (size_t i = nPeriod - 1; i < aSize; ++i)
+ for (size_t i = mPeriod - 1; i < aSize; ++i)
{
double xAvg = 0.0;
double yAvg = 0.0;
- for (sal_Int32 j = 0; j < nPeriod; j++)
+ for (sal_Int32 j = 0; j < mPeriod; j++)
{
xAvg += aValues.first[i - j];
yAvg += aValues.second[i - j];
}
- yAvg /= nPeriod;
- xAvg /= nPeriod;
+ yAvg /= mPeriod;
+ xAvg /= mPeriod;
aYList.push_back(yAvg);
if (bUseXAvg)