Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/chart2/source/tools/RegressionCurveCalculator.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 <RegressionCurveCalculator.hxx>
21
22
#include <comphelper/processfactory.hxx>
23
#include <rtl/math.hxx>
24
25
#include <com/sun/star/lang/XServiceName.hpp>
26
#include <com/sun/star/util/NumberFormatter.hpp>
27
28
#include <comphelper/numbers.hxx>
29
#include <comphelper/extract.hxx>
30
31
using namespace ::com::sun::star;
32
33
using ::com::sun::star::uno::Reference;
34
using ::com::sun::star::uno::Sequence;
35
36
namespace chart
37
{
38
39
RegressionCurveCalculator::RegressionCurveCalculator()
40
0
    : m_fCorrelationCoefficient(std::numeric_limits<double>::quiet_NaN())
41
0
    , mDegree(2)
42
0
    , mForceIntercept(false)
43
0
    , mInterceptValue(std::numeric_limits<double>::quiet_NaN())
44
0
    , mPeriod(2)
45
0
    , mXName(u"x"_ustr)
46
0
    , mYName(u"f(x)"_ustr)
47
0
    , mnMovingType(0)
48
0
{
49
0
}
50
51
RegressionCurveCalculator::~RegressionCurveCalculator()
52
0
{}
53
54
bool RegressionCurveCalculator::isLinearScaling(
55
    const Reference< chart2::XScaling > & xScaling )
56
0
{
57
    // no scaling means linear
58
0
    if( !xScaling.is())
59
0
        return true;
60
0
    uno::Reference< lang::XServiceName > xServiceName( xScaling, uno::UNO_QUERY );
61
0
    return xServiceName.is() && xServiceName->getServiceName() == "com.sun.star.chart2.LinearScaling";
62
0
}
63
64
bool RegressionCurveCalculator::isLogarithmicScaling(
65
    const Reference< chart2::XScaling > & xScaling )
66
0
{
67
0
    uno::Reference< lang::XServiceName > xServiceName( xScaling, uno::UNO_QUERY );
68
0
    return xServiceName.is() && xServiceName->getServiceName() == "com.sun.star.chart2.LogarithmicScaling";
69
0
}
70
71
void RegressionCurveCalculator::setRegressionProperties(
72
    sal_Int32   aDegree,
73
    sal_Bool    aForceIntercept,
74
    double      aInterceptValue,
75
    sal_Int32   aPeriod,
76
    sal_Int32   nMovingType )
77
0
{
78
0
    if( aPeriod < 0 )
79
0
        throw lang::IllegalArgumentException(u"aPeriod may not be < 0"_ustr, static_cast<cppu::OWeakObject*>(this), 3);
80
0
    mDegree = aDegree;
81
0
    mForceIntercept = aForceIntercept;
82
0
    mInterceptValue = aInterceptValue;
83
0
    mPeriod  = aPeriod;
84
0
    mnMovingType = nMovingType;
85
0
}
86
87
OUString RegressionCurveCalculator::getFormattedString(
88
    const Reference< util::XNumberFormatter >& xNumFormatter,
89
    sal_Int32 nNumberFormatKey,
90
    double fNumber, const sal_Int32* pStringLength /* = nullptr */ )
91
0
{
92
0
    if ( pStringLength && *pStringLength <= 0 )
93
0
        return u"###"_ustr;
94
0
    OUString aResult;
95
96
0
    if( xNumFormatter.is() )
97
0
    {
98
0
        bool bStandard = ::cppu::any2bool( ::comphelper::getNumberFormatProperty( xNumFormatter, nNumberFormatKey, u"StandardFormat"_ustr ) );
99
0
        if( pStringLength && bStandard )
100
0
        {   // round fNumber to *pStringLength characters
101
0
            const sal_Int32 nMinDigit = 6; // minimum significant digits for General format
102
0
            sal_Int32 nSignificantDigit = ( *pStringLength <= nMinDigit ? nMinDigit : *pStringLength );
103
0
            aResult = ::rtl::math::doubleToUString( fNumber, rtl_math_StringFormat_G1, nSignificantDigit, '.', true );
104
            // count characters different from significant digits (decimal separator, scientific notation)
105
0
            sal_Int32 nExtraChar = aResult.getLength() - *pStringLength;
106
0
            if ( nExtraChar > 0 && *pStringLength > nMinDigit )
107
0
            {
108
0
                nSignificantDigit = *pStringLength - nExtraChar;
109
0
                if ( nSignificantDigit < nMinDigit )
110
0
                    nSignificantDigit = nMinDigit;
111
0
                aResult = ::rtl::math::doubleToUString( fNumber, rtl_math_StringFormat_G1, nSignificantDigit, '.', true );
112
0
            }
113
0
            fNumber = ::rtl::math::stringToDouble( aResult, '.', ',' );
114
0
        }
115
0
        aResult = xNumFormatter->convertNumberToString( nNumberFormatKey, fNumber );
116
0
    }
117
0
    else
118
0
    {
119
0
        sal_Int32 nStringLength = 4;  // default length
120
0
        if ( pStringLength )
121
0
            nStringLength = *pStringLength;
122
0
        aResult = ::rtl::math::doubleToUString( fNumber, rtl_math_StringFormat_G1, nStringLength, '.', true );
123
0
    }
124
0
    return aResult;
125
0
}
126
127
Sequence< geometry::RealPoint2D > SAL_CALL RegressionCurveCalculator::getCurveValues(
128
    double min, double max, ::sal_Int32 nPointCount,
129
    const Reference< chart2::XScaling >& xScalingX,
130
    const Reference< chart2::XScaling >& /* xScalingY */,
131
    sal_Bool /* bMaySkipPointsInCalculation */ )
132
0
{
133
0
    if( nPointCount < 2 )
134
0
        throw lang::IllegalArgumentException(u"too few points"_ustr, static_cast<cppu::OWeakObject*>(this), 2);
135
136
    // determine if scaling and inverse scaling for x-values work
137
0
    bool bDoXScaling( xScalingX.is());
138
0
    uno::Reference< chart2::XScaling > xInverseScaling;
139
0
    if( bDoXScaling )
140
0
        xInverseScaling.set( xScalingX->getInverseScaling());
141
0
    bDoXScaling = bDoXScaling && xInverseScaling.is();
142
143
0
    Sequence< geometry::RealPoint2D > aResult( nPointCount );
144
0
    auto pResult = aResult.getArray();
145
146
0
    double fMin( min );
147
0
    double fFact = (max - min) / double(nPointCount-1);
148
149
0
    if( bDoXScaling )
150
0
    {
151
0
        fMin = xScalingX->doScaling( min );
152
0
        fFact = (xScalingX->doScaling( max ) - fMin) / double(nPointCount-1);
153
0
    }
154
155
0
    for(sal_Int32 nP=0; nP<nPointCount; nP++)
156
0
    {
157
0
        double x = fMin + nP * fFact;
158
0
        if( bDoXScaling )
159
0
            x = xInverseScaling->doScaling( x );
160
0
        pResult[nP].X = x;
161
0
        pResult[nP].Y = getCurveValue( x );
162
0
    }
163
164
0
    return aResult;
165
0
}
166
167
double SAL_CALL RegressionCurveCalculator::getCorrelationCoefficient()
168
0
{
169
0
    return m_fCorrelationCoefficient;
170
0
}
171
172
OUString SAL_CALL RegressionCurveCalculator::getRepresentation()
173
0
{
174
0
    return ImplGetRepresentation( Reference< util::XNumberFormatter >(), 0 );
175
0
}
176
177
OUString SAL_CALL RegressionCurveCalculator::getFormattedRepresentation(
178
    const Reference< util::XNumberFormatsSupplier > & xNumFmtSupplier,
179
    sal_Int32 nNumberFormatKey, sal_Int32 nFormulaLength )
180
0
{
181
    // create and prepare a number formatter
182
0
    if( !xNumFmtSupplier.is())
183
0
        return getRepresentation();
184
0
    Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext(), uno::UNO_SET_THROW );
185
0
    Reference< util::XNumberFormatter > xNumFormatter( util::NumberFormatter::create(xContext), uno::UNO_QUERY_THROW );
186
0
    xNumFormatter->attachNumberFormatsSupplier( xNumFmtSupplier );
187
188
0
    if ( nFormulaLength > 0 )
189
0
        return ImplGetRepresentation( xNumFormatter, nNumberFormatKey, &nFormulaLength );
190
0
    return ImplGetRepresentation( xNumFormatter, nNumberFormatKey );
191
0
}
192
193
void RegressionCurveCalculator::addStringToEquation(
194
        OUStringBuffer& aStrEquation, sal_Int32& nLineLength, OUStringBuffer const & aAddString, const sal_Int32* pMaxWidth)
195
0
{
196
0
    if ( pMaxWidth && ( nLineLength + aAddString.getLength() > *pMaxWidth ) )
197
0
    {  // wrap line
198
0
        aStrEquation.append( "\n " ); // start new line with a blank
199
0
        nLineLength = 1;
200
0
    }
201
0
    aStrEquation.append( aAddString );
202
0
    nLineLength += aAddString.getLength();
203
0
}
204
205
void SAL_CALL RegressionCurveCalculator::setXYNames( const OUString& aXName, const OUString& aYName )
206
0
{
207
0
    if ( aXName.isEmpty() )
208
0
        mXName = u"x"_ustr;
209
0
    else
210
0
        mXName = aXName;
211
0
    if ( aYName.isEmpty() )
212
0
        mYName = u"f(x)"_ustr;
213
0
    else
214
0
        mYName = aYName;
215
0
}
216
217
} //  namespace chart
218
219
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */