LibreOffice Module chart2 (master) 1
MovingAverageRegressionCurveCalculator.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
22#include <ResId.hxx>
23#include <strings.hrc>
24
25#include <algorithm>
26#include <limits>
27
28#include <com/sun/star/chart2/MovingAverageType.hpp>
29
30using namespace ::com::sun::star;
31using namespace ::com::sun::star::chart2;
32
33namespace chart
34{
35
37{}
38
40{}
41
42// ____ XRegressionCurveCalculator ____
44 const uno::Sequence< double >& aXValues,
45 const uno::Sequence< double >& aYValues )
46{
47 m_fCorrelationCoefficient = std::numeric_limits<double>::quiet_NaN();
48
51 aXValues, aYValues,
53
54 aYList.clear();
55 aXList.clear();
56
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
59
60 switch (mnMovingType)
61 {
62 case MovingAverageType::Central:
63 {
64
65 calculateValuesCentral(std::move(aValues));
66 break;
67 }
68
69 case MovingAverageType::AveragedAbscissa:
70 {
71 calculateValues(std::move(aValues), true);
72 break;
73 }
74 case MovingAverageType::Prior:
75 default:
76 {
77 calculateValues(std::move(aValues), false);
78 break;
79 }
80 }
81}
82
85{
86 const size_t aSize = aValues.first.size();
87 if (aSize == 0)
88 return;
89 for (size_t i = mPeriod - 1; i < aSize; ++i)
90 {
91 double yAvg = 0.0;
92
93 for (sal_Int32 j = 0; j < mPeriod; j++)
94 {
95 yAvg += aValues.second[i - j];
96 }
97 yAvg /= mPeriod;
98 aYList.push_back(yAvg);
99 }
100 sal_Int32 nPeriodLocal = (mPeriod % 2 == 0) ? (mPeriod / 2) : ((mPeriod - 1) / 2);
101 for (size_t i = nPeriodLocal; i < aSize - 1; ++i)
102 {
103 double x = aValues.first[i];
104 aXList.push_back(x);
105 }
106}
107
110{
111 const size_t aSize = aValues.first.size();
112 for (size_t i = mPeriod - 1; i < aSize; ++i)
113 {
114 double xAvg = 0.0;
115 double yAvg = 0.0;
116
117 for (sal_Int32 j = 0; j < mPeriod; j++)
118 {
119 xAvg += aValues.first[i - j];
120 yAvg += aValues.second[i - j];
121 }
122 yAvg /= mPeriod;
123 xAvg /= mPeriod;
124
125 aYList.push_back(yAvg);
126 if (bUseXAvg)
127 {
128 aXList.push_back(xAvg);
129 }
130 else
131 {
132 double x = aValues.first[i];
133 aXList.push_back(x);
134 }
135 }
136}
137
139{
140 return std::numeric_limits<double>::quiet_NaN();
141}
142
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*/ )
148{
149 size_t nSize = std::min(aXList.size(), aYList.size());
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;
154}
155
157 const uno::Reference< util::XNumberFormatter >& /*xNumFormatter*/,
158 sal_Int32 /*nNumberFormatKey*/, sal_Int32* /*pFormulaLength = nullptr */ ) const
159{
160 OUString aRet = SchResId( STR_OBJECT_MOVING_AVERAGE_WITH_PARAMETERS );
161 // change text for Moving Average
162 OUString aWildcard( "%PERIOD" );
163 sal_Int32 nIndex = aRet.indexOf( aWildcard );
164 if( nIndex != -1 )
165 { // replace period
166 aRet = aRet.replaceAt( nIndex, aWildcard.getLength(), OUString::number(mPeriod) );
167 }
168 return aRet;
169}
170
171} // namespace chart
172
173/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Sequence< css::geometry::RealPoint2D > SAL_CALL getCurveValues(double min, double max, sal_Int32 nPointCount, const css::uno::Reference< css::chart2::XScaling > &xScalingX, const css::uno::Reference< css::chart2::XScaling > &xScalingY, sal_Bool bMaySkipPointsInCalculation) override
void calculateValuesCentral(RegressionCalculationHelper::tDoubleVectorPair aValues)
virtual OUString ImplGetRepresentation(const css::uno::Reference< css::util::XNumberFormatter > &xNumFormatter, sal_Int32 nNumberFormatKey, sal_Int32 *pFormulaLength=nullptr) const override
virtual void SAL_CALL recalculateRegression(const css::uno::Sequence< double > &aXValues, const css::uno::Sequence< double > &aYValues) override
void calculateValues(RegressionCalculationHelper::tDoubleVectorPair aValues, bool bUseXAvg)
float y
float x
OUString aWildcard
sal_Int32 nIndex
std::pair< std::vector< double >, std::vector< double > > tDoubleVectorPair
tDoubleVectorPair cleanup(const css::uno::Sequence< double > &rXValues, const css::uno::Sequence< double > &rYValues, Pred aPred)
takes the given x- and y-values and copies them into the resulting pair, which contains x-values in t...
OUString OOO_DLLPUBLIC_CHARTTOOLS SchResId(TranslateId aId)
Definition: ResId.cxx:24
int i
unsigned char sal_Bool