Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/chart2/source/tools/MovingAverageRegressionCurveCalculator.cxx
Line
Count
Source
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
20
#include <MovingAverageRegressionCurveCalculator.hxx>
21
#include <RegressionCalculationHelper.hxx>
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
30
using namespace ::com::sun::star;
31
using namespace ::com::sun::star::chart2;
32
33
namespace chart
34
{
35
36
MovingAverageRegressionCurveCalculator::MovingAverageRegressionCurveCalculator()
37
0
{}
38
39
MovingAverageRegressionCurveCalculator::~MovingAverageRegressionCurveCalculator()
40
0
{}
41
42
// ____ XRegressionCurveCalculator ____
43
void SAL_CALL MovingAverageRegressionCurveCalculator::recalculateRegression(
44
    const uno::Sequence< double >& aXValues,
45
    const uno::Sequence< double >& aYValues )
46
0
{
47
0
    m_fCorrelationCoefficient = std::numeric_limits<double>::quiet_NaN();
48
49
0
    RegressionCalculationHelper::tDoubleVectorPair aValues(
50
0
        RegressionCalculationHelper::cleanup(
51
0
            aXValues, aYValues,
52
0
            RegressionCalculationHelper::isValid()));
53
54
0
    aYList.clear();
55
0
    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
0
    switch (mnMovingType)
61
0
    {
62
0
        case MovingAverageType::Central:
63
0
        {
64
65
0
            calculateValuesCentral(std::move(aValues));
66
0
            break;
67
0
        }
68
69
0
        case MovingAverageType::AveragedAbscissa:
70
0
        {
71
0
            calculateValues(std::move(aValues), true);
72
0
            break;
73
0
        }
74
0
        case MovingAverageType::Prior:
75
0
        default:
76
0
        {
77
0
            calculateValues(std::move(aValues), false);
78
0
            break;
79
0
        }
80
0
    }
81
0
}
82
83
void MovingAverageRegressionCurveCalculator::calculateValuesCentral(
84
    const RegressionCalculationHelper::tDoubleVectorPair& aValues)
85
0
{
86
0
    const size_t aSize = aValues.first.size();
87
0
    if (aSize == 0)
88
0
        return;
89
0
    for (size_t i = mPeriod - 1; i < aSize; ++i)
90
0
    {
91
0
        double yAvg = 0.0;
92
93
0
        for (sal_Int32 j = 0; j < mPeriod; j++)
94
0
        {
95
0
            yAvg += aValues.second[i - j];
96
0
        }
97
0
        yAvg /= mPeriod;
98
0
        aYList.push_back(yAvg);
99
0
    }
100
0
    sal_Int32 nPeriodLocal = (mPeriod % 2 == 0) ? (mPeriod / 2) : ((mPeriod - 1) / 2);
101
0
    for (size_t i = nPeriodLocal; i < aSize - 1; ++i)
102
0
    {
103
0
        double x = aValues.first[i];
104
0
        aXList.push_back(x);
105
0
    }
106
0
}
107
108
void MovingAverageRegressionCurveCalculator::calculateValues(
109
    const RegressionCalculationHelper::tDoubleVectorPair& aValues, bool bUseXAvg)
110
0
{
111
0
    const size_t aSize = aValues.first.size();
112
0
    for (size_t i = mPeriod - 1; i < aSize; ++i)
113
0
    {
114
0
        double xAvg = 0.0;
115
0
        double yAvg = 0.0;
116
117
0
        for (sal_Int32 j = 0; j < mPeriod; j++)
118
0
        {
119
0
            xAvg += aValues.first[i - j];
120
0
            yAvg += aValues.second[i - j];
121
0
        }
122
0
        yAvg /= mPeriod;
123
0
        xAvg /= mPeriod;
124
125
0
        aYList.push_back(yAvg);
126
0
        if (bUseXAvg)
127
0
        {
128
0
            aXList.push_back(xAvg);
129
0
        }
130
0
        else
131
0
        {
132
0
            double x = aValues.first[i];
133
0
            aXList.push_back(x);
134
0
        }
135
0
    }
136
0
}
137
138
double SAL_CALL MovingAverageRegressionCurveCalculator::getCurveValue( double /*x*/ )
139
0
{
140
0
    return std::numeric_limits<double>::quiet_NaN();
141
0
}
142
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*/ )
148
0
{
149
0
    size_t nSize = std::min(aXList.size(), aYList.size());
150
0
    uno::Sequence< geometry::RealPoint2D > aResult( nSize );
151
0
    std::transform(aXList.begin(), aXList.begin() + nSize, aYList.begin(), aResult.getArray(),
152
0
                   [](const auto& x, const auto& y) { return geometry::RealPoint2D(x, y); });
153
0
    return aResult;
154
0
}
155
156
OUString MovingAverageRegressionCurveCalculator::ImplGetRepresentation(
157
    const uno::Reference< util::XNumberFormatter >& /*xNumFormatter*/,
158
    sal_Int32 /*nNumberFormatKey*/, sal_Int32* /*pFormulaLength = nullptr */ ) const
159
0
{
160
0
    OUString aRet = SchResId( STR_OBJECT_MOVING_AVERAGE_WITH_PARAMETERS );
161
    // change text for Moving Average
162
0
    OUString aWildcard( u"%PERIOD"_ustr );
163
0
    sal_Int32 nIndex = aRet.indexOf( aWildcard );
164
0
    if( nIndex != -1 )
165
0
    {  // replace period
166
0
        aRet = aRet.replaceAt( nIndex, aWildcard.getLength(), OUString::number(mPeriod) );
167
0
    }
168
0
    return aRet;
169
0
}
170
171
} //  namespace chart
172
173
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */