1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: RegressionCurveCalculator.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
34 #include "RegressionCurveCalculator.hxx"
35 #include "RegressionCalculationHelper.hxx"
36 #include "servicenames_coosystems.hxx"
38 #include <comphelper/processfactory.hxx>
39 #include <rtl/math.hxx>
41 #include <com/sun/star/lang/XServiceName.hpp>
43 using namespace ::com::sun::star
;
45 using ::com::sun::star::uno::Reference
;
46 using ::com::sun::star::uno::Sequence
;
47 using ::rtl::OUString
;
52 RegressionCurveCalculator::RegressionCurveCalculator() :
53 m_fCorrelationCoeffitient( 0.0 )
55 ::rtl::math::setNan( & m_fCorrelationCoeffitient
);
58 RegressionCurveCalculator::~RegressionCurveCalculator()
62 bool RegressionCurveCalculator::isLinearScaling(
63 const Reference
< chart2::XScaling
> & xScaling
)
65 // no scaling means linear
68 static OUString
aLinScalingServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LinearScaling" ));
69 uno::Reference
< lang::XServiceName
> xServiceName( xScaling
, uno::UNO_QUERY
);
70 return (xServiceName
.is() && xServiceName
->getServiceName().equals( aLinScalingServiceName
));
74 bool RegressionCurveCalculator::isLogarithmicScaling(
75 const Reference
< chart2::XScaling
> & xScaling
)
77 static OUString
aLogScalingServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LogarithmicScaling" ));
78 uno::Reference
< lang::XServiceName
> xServiceName( xScaling
, uno::UNO_QUERY
);
79 return (xServiceName
.is() && xServiceName
->getServiceName().equals( aLogScalingServiceName
));
83 OUString
RegressionCurveCalculator::getFormattedString(
84 const Reference
< util::XNumberFormatter
>& xNumFormatter
,
85 ::sal_Int32 nNumberFormatKey
,
86 double fNumber
) const
90 if( xNumFormatter
.is())
91 aResult
= xNumFormatter
->convertNumberToString( nNumberFormatKey
, fNumber
);
93 aResult
= NUMBER_TO_STR( fNumber
);
98 Sequence
< geometry::RealPoint2D
> SAL_CALL
RegressionCurveCalculator::getCurveValues(
99 double min
, double max
, ::sal_Int32 nPointCount
,
100 const Reference
< chart2::XScaling
>& xScalingX
,
101 const Reference
< chart2::XScaling
>& /* xScalingY */,
102 ::sal_Bool
/* bMaySkipPointsInCalculation */ )
103 throw (lang::IllegalArgumentException
,
104 uno::RuntimeException
)
106 if( nPointCount
< 2 )
107 throw lang::IllegalArgumentException();
109 // determine if scaling and inverse scaling for x-values work
110 bool bDoXScaling( xScalingX
.is());
111 uno::Reference
< chart2::XScaling
> xInverseScaling
;
113 xInverseScaling
.set( xScalingX
->getInverseScaling());
114 bDoXScaling
= bDoXScaling
&& xInverseScaling
.is();
116 Sequence
< geometry::RealPoint2D
> aResult( nPointCount
);
119 double fFact
= (max
- min
) / double(nPointCount
-1);
122 fMin
= xScalingX
->doScaling( min
);
123 fFact
= (xScalingX
->doScaling( max
) - fMin
) / double(nPointCount
-1);
126 for(sal_Int32 nP
=0; nP
<nPointCount
; nP
++)
128 double x
= fMin
+ nP
* fFact
;
130 x
= xInverseScaling
->doScaling( x
);
132 aResult
[nP
].Y
= this->getCurveValue( x
);
138 double SAL_CALL
RegressionCurveCalculator::getCorrelationCoefficient()
139 throw (uno::RuntimeException
)
141 return m_fCorrelationCoeffitient
;
144 OUString SAL_CALL
RegressionCurveCalculator::getRepresentation()
145 throw (uno::RuntimeException
)
147 return ImplGetRepresentation( Reference
< util::XNumberFormatter
>(), 0 );
150 OUString SAL_CALL
RegressionCurveCalculator::getFormattedRepresentation(
151 const Reference
< util::XNumberFormatsSupplier
> & xNumFmtSupplier
,
152 ::sal_Int32 nNumberFormatKey
)
153 throw (uno::RuntimeException
)
155 // create and prepare a number formatter
156 if( !xNumFmtSupplier
.is())
157 return getRepresentation();
158 Reference
< util::XNumberFormatter
> xNumFormatter
;
159 Reference
< lang::XMultiServiceFactory
> xFact( comphelper::getProcessServiceFactory(), uno::UNO_QUERY
);
161 xNumFormatter
.set( xFact
->createInstance(
162 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.NumberFormatter"))), uno::UNO_QUERY
);
163 if( !xNumFormatter
.is())
164 return getRepresentation();
165 xNumFormatter
->attachNumberFormatsSupplier( xNumFmtSupplier
);
167 return ImplGetRepresentation( xNumFormatter
, nNumberFormatKey
);