merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / tools / RegressionCurveCalculator.cxx
blob68e041e668ca09e313700c0884fd714f5115c797
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
31 #include "RegressionCurveCalculator.hxx"
32 #include "RegressionCalculationHelper.hxx"
33 #include "servicenames_coosystems.hxx"
35 #include <comphelper/processfactory.hxx>
36 #include <rtl/math.hxx>
38 #include <com/sun/star/lang/XServiceName.hpp>
40 using namespace ::com::sun::star;
42 using ::com::sun::star::uno::Reference;
43 using ::com::sun::star::uno::Sequence;
44 using ::rtl::OUString;
46 namespace chart
49 RegressionCurveCalculator::RegressionCurveCalculator() :
50 m_fCorrelationCoeffitient( 0.0 )
52 ::rtl::math::setNan( & m_fCorrelationCoeffitient );
55 RegressionCurveCalculator::~RegressionCurveCalculator()
58 // static
59 bool RegressionCurveCalculator::isLinearScaling(
60 const Reference< chart2::XScaling > & xScaling )
62 // no scaling means linear
63 if( !xScaling.is())
64 return true;
65 static OUString aLinScalingServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LinearScaling" ));
66 uno::Reference< lang::XServiceName > xServiceName( xScaling, uno::UNO_QUERY );
67 return (xServiceName.is() && xServiceName->getServiceName().equals( aLinScalingServiceName ));
70 // static
71 bool RegressionCurveCalculator::isLogarithmicScaling(
72 const Reference< chart2::XScaling > & xScaling )
74 static OUString aLogScalingServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LogarithmicScaling" ));
75 uno::Reference< lang::XServiceName > xServiceName( xScaling, uno::UNO_QUERY );
76 return (xServiceName.is() && xServiceName->getServiceName().equals( aLogScalingServiceName ));
80 OUString RegressionCurveCalculator::getFormattedString(
81 const Reference< util::XNumberFormatter >& xNumFormatter,
82 ::sal_Int32 nNumberFormatKey,
83 double fNumber ) const
85 OUString aResult;
87 if( xNumFormatter.is())
88 aResult = xNumFormatter->convertNumberToString( nNumberFormatKey, fNumber );
89 else
90 aResult = NUMBER_TO_STR( fNumber );
92 return aResult;
95 Sequence< geometry::RealPoint2D > SAL_CALL RegressionCurveCalculator::getCurveValues(
96 double min, double max, ::sal_Int32 nPointCount,
97 const Reference< chart2::XScaling >& xScalingX,
98 const Reference< chart2::XScaling >& /* xScalingY */,
99 ::sal_Bool /* bMaySkipPointsInCalculation */ )
100 throw (lang::IllegalArgumentException,
101 uno::RuntimeException)
103 if( nPointCount < 2 )
104 throw lang::IllegalArgumentException();
106 // determine if scaling and inverse scaling for x-values work
107 bool bDoXScaling( xScalingX.is());
108 uno::Reference< chart2::XScaling > xInverseScaling;
109 if( bDoXScaling )
110 xInverseScaling.set( xScalingX->getInverseScaling());
111 bDoXScaling = bDoXScaling && xInverseScaling.is();
113 Sequence< geometry::RealPoint2D > aResult( nPointCount );
115 double fMin( min );
116 double fFact = (max - min) / double(nPointCount-1);
117 if( bDoXScaling )
119 fMin = xScalingX->doScaling( min );
120 fFact = (xScalingX->doScaling( max ) - fMin) / double(nPointCount-1);
123 for(sal_Int32 nP=0; nP<nPointCount; nP++)
125 double x = fMin + nP * fFact;
126 if( bDoXScaling )
127 x = xInverseScaling->doScaling( x );
128 aResult[nP].X = x;
129 aResult[nP].Y = this->getCurveValue( x );
132 return aResult;
135 double SAL_CALL RegressionCurveCalculator::getCorrelationCoefficient()
136 throw (uno::RuntimeException)
138 return m_fCorrelationCoeffitient;
141 OUString SAL_CALL RegressionCurveCalculator::getRepresentation()
142 throw (uno::RuntimeException)
144 return ImplGetRepresentation( Reference< util::XNumberFormatter >(), 0 );
147 OUString SAL_CALL RegressionCurveCalculator::getFormattedRepresentation(
148 const Reference< util::XNumberFormatsSupplier > & xNumFmtSupplier,
149 ::sal_Int32 nNumberFormatKey )
150 throw (uno::RuntimeException)
152 // create and prepare a number formatter
153 if( !xNumFmtSupplier.is())
154 return getRepresentation();
155 Reference< util::XNumberFormatter > xNumFormatter;
156 Reference< lang::XMultiServiceFactory > xFact( comphelper::getProcessServiceFactory(), uno::UNO_QUERY );
157 if( xFact.is())
158 xNumFormatter.set( xFact->createInstance(
159 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.NumberFormatter"))), uno::UNO_QUERY );
160 if( !xNumFormatter.is())
161 return getRepresentation();
162 xNumFormatter->attachNumberFormatsSupplier( xNumFmtSupplier );
164 return ImplGetRepresentation( xNumFormatter, nNumberFormatKey );
168 } // namespace chart