LibreOffice Module chart2 (master) 1
RegressionCurveCalculator.cxx
Go to the documentation of this file.
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
21
23#include <rtl/math.hxx>
24
25#include <com/sun/star/lang/XServiceName.hpp>
26#include <com/sun/star/util/NumberFormatter.hpp>
27
30
31using namespace ::com::sun::star;
32
33using ::com::sun::star::uno::Reference;
34using ::com::sun::star::uno::Sequence;
35
36namespace chart
37{
38
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("x")
46 , mYName("f(x)")
47 , mnMovingType(0)
48{
49}
50
52{}
53
55 const Reference< chart2::XScaling > & xScaling )
56{
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";
62}
63
65 const Reference< chart2::XScaling > & xScaling )
66{
67 uno::Reference< lang::XServiceName > xServiceName( xScaling, uno::UNO_QUERY );
68 return xServiceName.is() && xServiceName->getServiceName() == "com.sun.star.chart2.LogarithmicScaling";
69}
70
72 sal_Int32 aDegree,
73 sal_Bool aForceIntercept,
74 double aInterceptValue,
75 sal_Int32 aPeriod,
76 sal_Int32 nMovingType )
77{
78 if( aPeriod < 0 )
79 throw lang::IllegalArgumentException("aPeriod may not be < 0", static_cast<cppu::OWeakObject*>(this), 3);
80 mDegree = aDegree;
81 mForceIntercept = aForceIntercept;
82 mInterceptValue = aInterceptValue;
83 mPeriod = aPeriod;
84 mnMovingType = nMovingType;
85}
86
88 const Reference< util::XNumberFormatter >& xNumFormatter,
89 sal_Int32 nNumberFormatKey,
90 double fNumber, const sal_Int32* pStringLength /* = nullptr */ )
91{
92 if ( pStringLength && *pStringLength <= 0 )
93 return "###";
94 OUString aResult;
95
96 if( xNumFormatter.is() )
97 {
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 )
107 {
108 nSignificantDigit = *pStringLength - nExtraChar;
109 if ( nSignificantDigit < nMinDigit )
110 nSignificantDigit = nMinDigit;
111 aResult = ::rtl::math::doubleToUString( fNumber, rtl_math_StringFormat_G1, nSignificantDigit, '.', true );
112 }
113 fNumber = ::rtl::math::stringToDouble( aResult, '.', ',' );
114 }
115 aResult = xNumFormatter->convertNumberToString( nNumberFormatKey, fNumber );
116 }
117 else
118 {
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 );
123 }
124 return aResult;
125}
126
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{
133 if( nPointCount < 2 )
134 throw lang::IllegalArgumentException("too few points", static_cast<cppu::OWeakObject*>(this), 2);
135
136 // determine if scaling and inverse scaling for x-values work
137 bool bDoXScaling( xScalingX.is());
139 if( bDoXScaling )
140 xInverseScaling.set( xScalingX->getInverseScaling());
141 bDoXScaling = bDoXScaling && xInverseScaling.is();
142
143 Sequence< geometry::RealPoint2D > aResult( nPointCount );
144 auto pResult = aResult.getArray();
145
146 double fMin( min );
147 double fFact = (max - min) / double(nPointCount-1);
148
149 if( bDoXScaling )
150 {
151 fMin = xScalingX->doScaling( min );
152 fFact = (xScalingX->doScaling( max ) - fMin) / double(nPointCount-1);
153 }
154
155 for(sal_Int32 nP=0; nP<nPointCount; nP++)
156 {
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 );
162 }
163
164 return aResult;
165}
166
168{
170}
171
173{
175}
176
178 const Reference< util::XNumberFormatsSupplier > & xNumFmtSupplier,
179 sal_Int32 nNumberFormatKey, sal_Int32 nFormulaLength )
180{
181 // create and prepare a number formatter
182 if( !xNumFmtSupplier.is())
183 return getRepresentation();
185 Reference< util::XNumberFormatter > xNumFormatter( util::NumberFormatter::create(xContext), uno::UNO_QUERY_THROW );
186 xNumFormatter->attachNumberFormatsSupplier( xNumFmtSupplier );
187
188 if ( nFormulaLength > 0 )
189 return ImplGetRepresentation( xNumFormatter, nNumberFormatKey, &nFormulaLength );
190 return ImplGetRepresentation( xNumFormatter, nNumberFormatKey );
191}
192
194 OUStringBuffer& aStrEquation, sal_Int32& nLineLength, OUStringBuffer const & aAddString, const sal_Int32* pMaxWidth)
195{
196 if ( pMaxWidth && ( nLineLength + aAddString.getLength() > *pMaxWidth ) )
197 { // wrap line
198 aStrEquation.append( "\n " ); // start new line with a blank
199 nLineLength = 1;
200 }
201 aStrEquation.append( aAddString );
202 nLineLength += aAddString.getLength();
203}
204
205void SAL_CALL RegressionCurveCalculator::setXYNames( const OUString& aXName, const OUString& aYName )
206{
207 if ( aXName.isEmpty() )
208 mXName = OUString ("x");
209 else
210 mXName = aXName;
211 if ( aYName.isEmpty() )
212 mYName = OUString ("f(x)");
213 else
214 mYName = aYName;
215}
216
217} // namespace chart
218
219/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL setXYNames(const OUString &aXName, const OUString &aYName) override
static bool isLogarithmicScaling(const css::uno::Reference< css::chart2::XScaling > &xScaling)
virtual OUString ImplGetRepresentation(const css::uno::Reference< css::util::XNumberFormatter > &xNumFormatter, sal_Int32 nNumberFormatKey, sal_Int32 *pFormulaLength=nullptr) const =0
static void addStringToEquation(OUStringBuffer &aStrEquation, sal_Int32 &nLineLength, OUStringBuffer const &aAddString, const sal_Int32 *pMaxLength)
virtual void SAL_CALL setRegressionProperties(sal_Int32 aDegree, sal_Bool aForceIntercept, double aInterceptValue, sal_Int32 aPeriod, sal_Int32 nMovingType) override
static OUString getFormattedString(const css::uno::Reference< css::util::XNumberFormatter > &xNumFormatter, sal_Int32 nNumberFormatKey, double fNumber, const sal_Int32 *pStringLength)
static bool isLinearScaling(const css::uno::Reference< css::chart2::XScaling > &xScaling)
virtual css::uno::Sequence< css::geometry::RealPoint2D > SAL_CALL getCurveValues(double min, double max, sal_Int32 nPointCount, const css::uno::Reference< css::chart2::XScaling > &xScalingX, const css::uno::Reference< css::chart2::XScaling > &xScalingY, sal_Bool bMaySkipPointsInCalculation) override
virtual double SAL_CALL getCurveValue(double x) override=0
virtual OUString SAL_CALL getRepresentation() override
virtual double SAL_CALL getCorrelationCoefficient() override
virtual OUString SAL_CALL getFormattedRepresentation(const css::uno::Reference< css::util::XNumberFormatsSupplier > &xNumFmtSupplier, sal_Int32 nNumberFormatKey, sal_Int32 nFormulaLength) override
float x
#define max(a, b)
Reference< XComponentContext > getProcessComponentContext()
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)
unsigned char sal_Bool