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 <RegressionCurveCalculator.hxx>
22 #include <comphelper/processfactory.hxx>
23 #include <rtl/math.hxx>
25 #include <com/sun/star/lang/XServiceName.hpp>
26 #include <com/sun/star/util/NumberFormatter.hpp>
28 #include <comphelper/numbers.hxx>
29 #include <comphelper/extract.hxx>
31 using namespace ::com::sun::star
;
33 using ::com::sun::star::uno::Reference
;
34 using ::com::sun::star::uno::Sequence
;
39 RegressionCurveCalculator::RegressionCurveCalculator()
40 : m_fCorrelationCoefficient(std::numeric_limits
<double>::quiet_NaN())
42 , mForceIntercept(false)
43 , mInterceptValue(std::numeric_limits
<double>::quiet_NaN())
51 RegressionCurveCalculator::~RegressionCurveCalculator()
54 bool RegressionCurveCalculator::isLinearScaling(
55 const Reference
< chart2::XScaling
> & xScaling
)
57 // no scaling means linear
60 uno::Reference
< lang::XServiceName
> xServiceName( xScaling
, uno::UNO_QUERY
);
61 return xServiceName
.is() && xServiceName
->getServiceName() == "com.sun.star.chart2.LinearScaling";
64 bool RegressionCurveCalculator::isLogarithmicScaling(
65 const Reference
< chart2::XScaling
> & xScaling
)
67 uno::Reference
< lang::XServiceName
> xServiceName( xScaling
, uno::UNO_QUERY
);
68 return xServiceName
.is() && xServiceName
->getServiceName() == "com.sun.star.chart2.LogarithmicScaling";
71 void RegressionCurveCalculator::setRegressionProperties(
73 sal_Bool aForceIntercept
,
74 double aInterceptValue
,
76 sal_Int32 nMovingType
)
79 throw lang::IllegalArgumentException("aPeriod may not be < 0", static_cast<cppu::OWeakObject
*>(this), 3);
81 mForceIntercept
= aForceIntercept
;
82 mInterceptValue
= aInterceptValue
;
84 mnMovingType
= nMovingType
;
87 OUString
RegressionCurveCalculator::getFormattedString(
88 const Reference
< util::XNumberFormatter
>& xNumFormatter
,
89 sal_Int32 nNumberFormatKey
,
90 double fNumber
, const sal_Int32
* pStringLength
/* = nullptr */ )
92 if ( pStringLength
&& *pStringLength
<= 0 )
96 if( xNumFormatter
.is() )
98 bool bStandard
= ::cppu::any2bool( ::comphelper::getNumberFormatProperty( xNumFormatter
, nNumberFormatKey
, "StandardFormat" ) );
99 if( pStringLength
&& bStandard
)
100 { // round fNumber to *pStringLength characters
101 const sal_Int32 nMinDigit
= 6; // minimum significant digits for General format
102 sal_Int32 nSignificantDigit
= ( *pStringLength
<= nMinDigit
? nMinDigit
: *pStringLength
);
103 aResult
= ::rtl::math::doubleToUString( fNumber
, rtl_math_StringFormat_G1
, nSignificantDigit
, '.', true );
104 // count characters different from significant digits (decimal separator, scientific notation)
105 sal_Int32 nExtraChar
= aResult
.getLength() - *pStringLength
;
106 if ( nExtraChar
> 0 && *pStringLength
> nMinDigit
)
108 nSignificantDigit
= *pStringLength
- nExtraChar
;
109 if ( nSignificantDigit
< nMinDigit
)
110 nSignificantDigit
= nMinDigit
;
111 aResult
= ::rtl::math::doubleToUString( fNumber
, rtl_math_StringFormat_G1
, nSignificantDigit
, '.', true );
113 fNumber
= ::rtl::math::stringToDouble( aResult
, '.', ',' );
115 aResult
= xNumFormatter
->convertNumberToString( nNumberFormatKey
, fNumber
);
119 sal_Int32 nStringLength
= 4; // default length
121 nStringLength
= *pStringLength
;
122 aResult
= ::rtl::math::doubleToUString( fNumber
, rtl_math_StringFormat_G1
, nStringLength
, '.', true );
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 */ )
133 if( nPointCount
< 2 )
134 throw lang::IllegalArgumentException("too few points", static_cast<cppu::OWeakObject
*>(this), 2);
136 // determine if scaling and inverse scaling for x-values work
137 bool bDoXScaling( xScalingX
.is());
138 uno::Reference
< chart2::XScaling
> xInverseScaling
;
140 xInverseScaling
.set( xScalingX
->getInverseScaling());
141 bDoXScaling
= bDoXScaling
&& xInverseScaling
.is();
143 Sequence
< geometry::RealPoint2D
> aResult( nPointCount
);
144 auto pResult
= aResult
.getArray();
147 double fFact
= (max
- min
) / double(nPointCount
-1);
151 fMin
= xScalingX
->doScaling( min
);
152 fFact
= (xScalingX
->doScaling( max
) - fMin
) / double(nPointCount
-1);
155 for(sal_Int32 nP
=0; nP
<nPointCount
; nP
++)
157 double x
= fMin
+ nP
* fFact
;
159 x
= xInverseScaling
->doScaling( x
);
161 pResult
[nP
].Y
= getCurveValue( x
);
167 double SAL_CALL
RegressionCurveCalculator::getCorrelationCoefficient()
169 return m_fCorrelationCoefficient
;
172 OUString SAL_CALL
RegressionCurveCalculator::getRepresentation()
174 return ImplGetRepresentation( Reference
< util::XNumberFormatter
>(), 0 );
177 OUString SAL_CALL
RegressionCurveCalculator::getFormattedRepresentation(
178 const Reference
< util::XNumberFormatsSupplier
> & xNumFmtSupplier
,
179 sal_Int32 nNumberFormatKey
, sal_Int32 nFormulaLength
)
181 // create and prepare a number formatter
182 if( !xNumFmtSupplier
.is())
183 return getRepresentation();
184 Reference
< uno::XComponentContext
> xContext( comphelper::getProcessComponentContext(), uno::UNO_SET_THROW
);
185 Reference
< util::XNumberFormatter
> xNumFormatter( util::NumberFormatter::create(xContext
), uno::UNO_QUERY_THROW
);
186 xNumFormatter
->attachNumberFormatsSupplier( xNumFmtSupplier
);
188 if ( nFormulaLength
> 0 )
189 return ImplGetRepresentation( xNumFormatter
, nNumberFormatKey
, &nFormulaLength
);
190 return ImplGetRepresentation( xNumFormatter
, nNumberFormatKey
);
193 void RegressionCurveCalculator::addStringToEquation(
194 OUStringBuffer
& aStrEquation
, sal_Int32
& nLineLength
, OUStringBuffer
const & aAddString
, const sal_Int32
* pMaxWidth
)
196 if ( pMaxWidth
&& ( nLineLength
+ aAddString
.getLength() > *pMaxWidth
) )
198 aStrEquation
.append( "\n " ); // start new line with a blank
201 aStrEquation
.append( aAddString
);
202 nLineLength
+= aAddString
.getLength();
205 void SAL_CALL
RegressionCurveCalculator::setXYNames( const OUString
& aXName
, const OUString
& aYName
)
207 if ( aXName
.isEmpty() )
208 mXName
= OUString ("x");
211 if ( aYName
.isEmpty() )
212 mYName
= OUString ("f(x)");
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */