Revert "tdf#158280 Replace usage of InputDialog with SvxNameDialog"
[LibreOffice.git] / chart2 / source / tools / MovingAverageRegressionCurveCalculator.cxx
bloba846d4d224f70fb665c4def5f3537b236816a755
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #include <ResId.hxx>
23 #include <strings.hrc>
25 #include <algorithm>
26 #include <limits>
28 #include <com/sun/star/chart2/MovingAverageType.hpp>
30 using namespace ::com::sun::star;
31 using namespace ::com::sun::star::chart2;
33 namespace chart
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(
51 aXValues, aYValues,
52 RegressionCalculationHelper::isValid()));
54 aYList.clear();
55 aXList.clear();
57 // For formulas, see
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
60 switch (mnMovingType)
62 case MovingAverageType::Central:
65 calculateValuesCentral(std::move(aValues));
66 break;
69 case MovingAverageType::AveragedAbscissa:
71 calculateValues(std::move(aValues), true);
72 break;
74 case MovingAverageType::Prior:
75 default:
77 calculateValues(std::move(aValues), false);
78 break;
83 void MovingAverageRegressionCurveCalculator::calculateValuesCentral(
84 const RegressionCalculationHelper::tDoubleVectorPair& aValues)
86 const size_t aSize = aValues.first.size();
87 if (aSize == 0)
88 return;
89 for (size_t i = mPeriod - 1; i < aSize; ++i)
91 double yAvg = 0.0;
93 for (sal_Int32 j = 0; j < mPeriod; j++)
95 yAvg += aValues.second[i - j];
97 yAvg /= mPeriod;
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];
104 aXList.push_back(x);
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)
114 double xAvg = 0.0;
115 double yAvg = 0.0;
117 for (sal_Int32 j = 0; j < mPeriod; j++)
119 xAvg += aValues.first[i - j];
120 yAvg += aValues.second[i - j];
122 yAvg /= mPeriod;
123 xAvg /= mPeriod;
125 aYList.push_back(yAvg);
126 if (bUseXAvg)
128 aXList.push_back(xAvg);
130 else
132 double x = aValues.first[i];
133 aXList.push_back(x);
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); });
153 return aResult;
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 );
164 if( nIndex != -1 )
165 { // replace period
166 aRet = aRet.replaceAt( nIndex, aWildcard.getLength(), OUString::number(mPeriod) );
168 return aRet;
171 } // namespace chart
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */