1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <MovingAverageRegressionCurveCalculator.hxx>
21 #include <RegressionCalculationHelper.hxx>
23 #include <strings.hrc>
28 #include <com/sun/star/chart2/MovingAverageType.hpp>
30 using namespace ::com::sun::star
;
31 using namespace ::com::sun::star::chart2
;
36 MovingAverageRegressionCurveCalculator::MovingAverageRegressionCurveCalculator()
39 MovingAverageRegressionCurveCalculator::~MovingAverageRegressionCurveCalculator()
42 // ____ XRegressionCurveCalculator ____
43 void SAL_CALL
MovingAverageRegressionCurveCalculator::recalculateRegression(
44 const uno::Sequence
< double >& aXValues
,
45 const uno::Sequence
< double >& aYValues
)
47 m_fCorrelationCoefficient
= std::numeric_limits
<double>::quiet_NaN();
49 RegressionCalculationHelper::tDoubleVectorPair
aValues(
50 RegressionCalculationHelper::cleanup(
52 RegressionCalculationHelper::isValid()));
58 // https://docs.oasis-open.org/office/OpenDocument/v1.3/cs02/part3-schema/OpenDocument-v1.3-cs02-part3-schema.html#property-chart_regression-moving-type
62 case MovingAverageType::Central
:
65 calculateValuesCentral(std::move(aValues
));
69 case MovingAverageType::AveragedAbscissa
:
71 calculateValues(std::move(aValues
), true);
74 case MovingAverageType::Prior
:
77 calculateValues(std::move(aValues
), false);
83 void MovingAverageRegressionCurveCalculator::calculateValuesCentral(
84 const RegressionCalculationHelper::tDoubleVectorPair
& aValues
)
86 const size_t aSize
= aValues
.first
.size();
89 for (size_t i
= mPeriod
- 1; i
< aSize
; ++i
)
93 for (sal_Int32 j
= 0; j
< mPeriod
; j
++)
95 yAvg
+= aValues
.second
[i
- j
];
98 aYList
.push_back(yAvg
);
100 sal_Int32 nPeriodLocal
= (mPeriod
% 2 == 0) ? (mPeriod
/ 2) : ((mPeriod
- 1) / 2);
101 for (size_t i
= nPeriodLocal
; i
< aSize
- 1; ++i
)
103 double x
= aValues
.first
[i
];
108 void MovingAverageRegressionCurveCalculator::calculateValues(
109 const RegressionCalculationHelper::tDoubleVectorPair
& aValues
, bool bUseXAvg
)
111 const size_t aSize
= aValues
.first
.size();
112 for (size_t i
= mPeriod
- 1; i
< aSize
; ++i
)
117 for (sal_Int32 j
= 0; j
< mPeriod
; j
++)
119 xAvg
+= aValues
.first
[i
- j
];
120 yAvg
+= aValues
.second
[i
- j
];
125 aYList
.push_back(yAvg
);
128 aXList
.push_back(xAvg
);
132 double x
= aValues
.first
[i
];
138 double SAL_CALL
MovingAverageRegressionCurveCalculator::getCurveValue( double /*x*/ )
140 return std::numeric_limits
<double>::quiet_NaN();
143 uno::Sequence
< geometry::RealPoint2D
> SAL_CALL
MovingAverageRegressionCurveCalculator::getCurveValues(
144 double /*min*/, double /*max*/, sal_Int32
/*nPointCount*/,
145 const uno::Reference
< chart2::XScaling
>& /*xScalingX*/,
146 const uno::Reference
< chart2::XScaling
>& /*xScalingY*/,
147 sal_Bool
/*bMaySkipPointsInCalculation*/ )
149 size_t nSize
= std::min(aXList
.size(), aYList
.size());
150 uno::Sequence
< geometry::RealPoint2D
> aResult( nSize
);
151 std::transform(aXList
.begin(), aXList
.begin() + nSize
, aYList
.begin(), aResult
.getArray(),
152 [](const auto& x
, const auto& y
) { return geometry::RealPoint2D(x
, y
); });
156 OUString
MovingAverageRegressionCurveCalculator::ImplGetRepresentation(
157 const uno::Reference
< util::XNumberFormatter
>& /*xNumFormatter*/,
158 sal_Int32
/*nNumberFormatKey*/, sal_Int32
* /*pFormulaLength = nullptr */ ) const
160 OUString aRet
= SchResId( STR_OBJECT_MOVING_AVERAGE_WITH_PARAMETERS
);
161 // change text for Moving Average
162 OUString
aWildcard( u
"%PERIOD"_ustr
);
163 sal_Int32 nIndex
= aRet
.indexOf( aWildcard
);
166 aRet
= aRet
.replaceAt( nIndex
, aWildcard
.getLength(), OUString::number(mPeriod
) );
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */