Get the style color and number just once
[LibreOffice.git] / chart2 / source / tools / RegressionCurveCalculator.cxx
blob36cb6c4c621a39a9964be5b950480933426d94d5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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;
36 namespace chart
39 RegressionCurveCalculator::RegressionCurveCalculator()
40 : m_fCorrelationCoefficient(std::numeric_limits<double>::quiet_NaN())
41 , mDegree(2)
42 , mForceIntercept(false)
43 , mInterceptValue(std::numeric_limits<double>::quiet_NaN())
44 , mPeriod(2)
45 , mXName(u"x"_ustr)
46 , mYName(u"f(x)"_ustr)
47 , mnMovingType(0)
51 RegressionCurveCalculator::~RegressionCurveCalculator()
54 bool RegressionCurveCalculator::isLinearScaling(
55 const Reference< chart2::XScaling > & xScaling )
57 // no scaling means linear
58 if( !xScaling.is())
59 return true;
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(
72 sal_Int32 aDegree,
73 sal_Bool aForceIntercept,
74 double aInterceptValue,
75 sal_Int32 aPeriod,
76 sal_Int32 nMovingType )
78 if( aPeriod < 0 )
79 throw lang::IllegalArgumentException(u"aPeriod may not be < 0"_ustr, static_cast<cppu::OWeakObject*>(this), 3);
80 mDegree = aDegree;
81 mForceIntercept = aForceIntercept;
82 mInterceptValue = aInterceptValue;
83 mPeriod = aPeriod;
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 )
93 return u"###"_ustr;
94 OUString aResult;
96 if( xNumFormatter.is() )
98 bool bStandard = ::cppu::any2bool( ::comphelper::getNumberFormatProperty( xNumFormatter, nNumberFormatKey, u"StandardFormat"_ustr ) );
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 );
117 else
119 sal_Int32 nStringLength = 4; // default length
120 if ( pStringLength )
121 nStringLength = *pStringLength;
122 aResult = ::rtl::math::doubleToUString( fNumber, rtl_math_StringFormat_G1, nStringLength, '.', true );
124 return aResult;
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(u"too few points"_ustr, 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;
139 if( bDoXScaling )
140 xInverseScaling.set( xScalingX->getInverseScaling());
141 bDoXScaling = bDoXScaling && xInverseScaling.is();
143 Sequence< geometry::RealPoint2D > aResult( nPointCount );
144 auto pResult = aResult.getArray();
146 double fMin( min );
147 double fFact = (max - min) / double(nPointCount-1);
149 if( bDoXScaling )
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;
158 if( bDoXScaling )
159 x = xInverseScaling->doScaling( x );
160 pResult[nP].X = x;
161 pResult[nP].Y = getCurveValue( x );
164 return aResult;
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 ) )
197 { // wrap line
198 aStrEquation.append( "\n " ); // start new line with a blank
199 nLineLength = 1;
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 = u"x"_ustr;
209 else
210 mXName = aXName;
211 if ( aYName.isEmpty() )
212 mYName = u"f(x)"_ustr;
213 else
214 mYName = aYName;
217 } // namespace chart
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */