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_fCorrelationCoeffitient(0.0),
42 mForceIntercept(false),
45 mXName("x"), mYName("f(x)")
47 rtl::math::setNan( &m_fCorrelationCoeffitient
);
48 rtl::math::setNan( &mInterceptValue
);
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
,
78 mForceIntercept
= aForceIntercept
;
79 mInterceptValue
= aInterceptValue
;
83 OUString
RegressionCurveCalculator::getFormattedString(
84 const Reference
< util::XNumberFormatter
>& xNumFormatter
,
85 sal_Int32 nNumberFormatKey
,
86 double fNumber
, const sal_Int32
* pStringLength
/* = nullptr */ )
88 if ( pStringLength
&& *pStringLength
<= 0 )
92 if( xNumFormatter
.is() )
94 bool bStandard
= ::cppu::any2bool( ::comphelper::getNumberFormatProperty( xNumFormatter
, nNumberFormatKey
, "StandardFormat" ) );
95 if( pStringLength
&& bStandard
)
96 { // round fNumber to *pStringLength characters
97 const sal_Int32 nMinDigit
= 6; // minimum significant digits for General format
98 sal_Int32 nSignificantDigit
= ( *pStringLength
<= nMinDigit
? nMinDigit
: *pStringLength
);
99 aResult
= OStringToOUString(
100 ::rtl::math::doubleToString( fNumber
, rtl_math_StringFormat_G1
, nSignificantDigit
, '.', true ),
101 RTL_TEXTENCODING_ASCII_US
);
102 // count characters different from significant digits (decimal separator, scientific notation)
103 sal_Int32 nExtraChar
= aResult
.getLength() - *pStringLength
;
104 if ( nExtraChar
> 0 && *pStringLength
> nMinDigit
)
106 nSignificantDigit
= *pStringLength
- nExtraChar
;
107 if ( nSignificantDigit
< nMinDigit
)
108 nSignificantDigit
= nMinDigit
;
109 aResult
= OStringToOUString(
110 ::rtl::math::doubleToString( fNumber
, rtl_math_StringFormat_G1
, nSignificantDigit
, '.', true ),
111 RTL_TEXTENCODING_ASCII_US
);
113 fNumber
= ::rtl::math::stringToDouble( aResult
, '.', ',' );
115 aResult
= xNumFormatter
->convertNumberToString( nNumberFormatKey
, fNumber
);
119 sal_Int32 nStringLength
= 4; // default length
121 nStringLength
= *pStringLength
;
122 aResult
= OStringToOUString(
123 ::rtl::math::doubleToString( fNumber
, rtl_math_StringFormat_G1
, nStringLength
, '.', true ),
124 RTL_TEXTENCODING_ASCII_US
);
129 Sequence
< geometry::RealPoint2D
> SAL_CALL
RegressionCurveCalculator::getCurveValues(
130 double min
, double max
, ::sal_Int32 nPointCount
,
131 const Reference
< chart2::XScaling
>& xScalingX
,
132 const Reference
< chart2::XScaling
>& /* xScalingY */,
133 sal_Bool
/* bMaySkipPointsInCalculation */ )
135 if( nPointCount
< 2 )
136 throw lang::IllegalArgumentException();
138 // determine if scaling and inverse scaling for x-values work
139 bool bDoXScaling( xScalingX
.is());
140 uno::Reference
< chart2::XScaling
> xInverseScaling
;
142 xInverseScaling
.set( xScalingX
->getInverseScaling());
143 bDoXScaling
= bDoXScaling
&& xInverseScaling
.is();
145 Sequence
< geometry::RealPoint2D
> aResult( nPointCount
);
148 double fFact
= (max
- min
) / double(nPointCount
-1);
152 fMin
= xScalingX
->doScaling( min
);
153 fFact
= (xScalingX
->doScaling( max
) - fMin
) / double(nPointCount
-1);
156 for(sal_Int32 nP
=0; nP
<nPointCount
; nP
++)
158 double x
= fMin
+ nP
* fFact
;
160 x
= xInverseScaling
->doScaling( x
);
162 aResult
[nP
].Y
= getCurveValue( x
);
168 double SAL_CALL
RegressionCurveCalculator::getCorrelationCoefficient()
170 return m_fCorrelationCoeffitient
;
173 OUString SAL_CALL
RegressionCurveCalculator::getRepresentation()
175 return ImplGetRepresentation( Reference
< util::XNumberFormatter
>(), 0 );
178 OUString SAL_CALL
RegressionCurveCalculator::getFormattedRepresentation(
179 const Reference
< util::XNumberFormatsSupplier
> & xNumFmtSupplier
,
180 sal_Int32 nNumberFormatKey
, sal_Int32 nFormulaLength
)
182 // create and prepare a number formatter
183 if( !xNumFmtSupplier
.is())
184 return getRepresentation();
185 Reference
< uno::XComponentContext
> xContext( comphelper::getProcessComponentContext(), uno::UNO_SET_THROW
);
186 Reference
< util::XNumberFormatter
> xNumFormatter( util::NumberFormatter::create(xContext
), uno::UNO_QUERY_THROW
);
187 xNumFormatter
->attachNumberFormatsSupplier( xNumFmtSupplier
);
189 if ( nFormulaLength
> 0 )
190 return ImplGetRepresentation( xNumFormatter
, nNumberFormatKey
, &nFormulaLength
);
191 return ImplGetRepresentation( xNumFormatter
, nNumberFormatKey
);
194 void RegressionCurveCalculator::addStringToEquation(
195 OUStringBuffer
& aStrEquation
, sal_Int32
& nLineLength
, OUStringBuffer
const & aAddString
, const sal_Int32
* pMaxWidth
)
197 if ( pMaxWidth
&& ( nLineLength
+ aAddString
.getLength() > *pMaxWidth
) )
199 aStrEquation
.append( "\n " ); // start new line with a blank
202 aStrEquation
.append( aAddString
);
203 nLineLength
+= aAddString
.getLength();
206 void SAL_CALL
RegressionCurveCalculator::setXYNames( const OUString
& aXName
, const OUString
& aYName
)
208 if ( aXName
.isEmpty() )
209 mXName
= OUString ("x");
212 if ( aYName
.isEmpty() )
213 mYName
= OUString ("f(x)");
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */