1*cde9e8dcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cde9e8dcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cde9e8dcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cde9e8dcSAndrew Rist  * distributed with this work for additional information
6*cde9e8dcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cde9e8dcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cde9e8dcSAndrew Rist  * "License"); you may not use this file except in compliance
9*cde9e8dcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cde9e8dcSAndrew Rist  *
11*cde9e8dcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cde9e8dcSAndrew Rist  *
13*cde9e8dcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cde9e8dcSAndrew Rist  * software distributed under the License is distributed on an
15*cde9e8dcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cde9e8dcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*cde9e8dcSAndrew Rist  * specific language governing permissions and limitations
18*cde9e8dcSAndrew Rist  * under the License.
19*cde9e8dcSAndrew Rist  *
20*cde9e8dcSAndrew Rist  *************************************************************/
21*cde9e8dcSAndrew Rist 
22*cde9e8dcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_chart2.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "RegressionCurveCalculator.hxx"
28cdf0e10cSrcweir #include "RegressionCalculationHelper.hxx"
29cdf0e10cSrcweir #include "servicenames_coosystems.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
32cdf0e10cSrcweir #include <rtl/math.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <com/sun/star/lang/XServiceName.hpp>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace ::com::sun::star;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
39cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
40cdf0e10cSrcweir using ::rtl::OUString;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir namespace chart
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 
RegressionCurveCalculator()45cdf0e10cSrcweir RegressionCurveCalculator::RegressionCurveCalculator() :
46cdf0e10cSrcweir         m_fCorrelationCoeffitient( 0.0 )
47cdf0e10cSrcweir {
48cdf0e10cSrcweir     ::rtl::math::setNan( & m_fCorrelationCoeffitient );
49cdf0e10cSrcweir }
50cdf0e10cSrcweir 
~RegressionCurveCalculator()51cdf0e10cSrcweir RegressionCurveCalculator::~RegressionCurveCalculator()
52cdf0e10cSrcweir {}
53cdf0e10cSrcweir 
isLinearScaling(const Reference<chart2::XScaling> & xScaling)54cdf0e10cSrcweir bool RegressionCurveCalculator::isLinearScaling(
55cdf0e10cSrcweir     const Reference< chart2::XScaling > & xScaling )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir     // no scaling means linear
58cdf0e10cSrcweir     if( !xScaling.is())
59cdf0e10cSrcweir         return true;
60cdf0e10cSrcweir     static OUString aLinScalingServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LinearScaling" ));
61cdf0e10cSrcweir     uno::Reference< lang::XServiceName > xServiceName( xScaling, uno::UNO_QUERY );
62cdf0e10cSrcweir     return (xServiceName.is() && xServiceName->getServiceName().equals( aLinScalingServiceName ));
63cdf0e10cSrcweir }
64cdf0e10cSrcweir 
isLogarithmicScaling(const Reference<chart2::XScaling> & xScaling)65cdf0e10cSrcweir bool RegressionCurveCalculator::isLogarithmicScaling(
66cdf0e10cSrcweir     const Reference< chart2::XScaling > & xScaling )
67cdf0e10cSrcweir {
68cdf0e10cSrcweir     static OUString aLogScalingServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LogarithmicScaling" ));
69cdf0e10cSrcweir     uno::Reference< lang::XServiceName > xServiceName( xScaling, uno::UNO_QUERY );
70cdf0e10cSrcweir     return (xServiceName.is() && xServiceName->getServiceName().equals( aLogScalingServiceName ));
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 
getFormattedString(const Reference<util::XNumberFormatter> & xNumFormatter,::sal_Int32 nNumberFormatKey,double fNumber) const74cdf0e10cSrcweir OUString RegressionCurveCalculator::getFormattedString(
75cdf0e10cSrcweir     const Reference< util::XNumberFormatter >& xNumFormatter,
76cdf0e10cSrcweir     ::sal_Int32 nNumberFormatKey,
77cdf0e10cSrcweir     double fNumber ) const
78cdf0e10cSrcweir {
79cdf0e10cSrcweir     OUString aResult;
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     if( xNumFormatter.is())
82cdf0e10cSrcweir         aResult = xNumFormatter->convertNumberToString( nNumberFormatKey, fNumber );
83cdf0e10cSrcweir     else
84cdf0e10cSrcweir         aResult = NUMBER_TO_STR( fNumber );
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     return aResult;
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
getCurveValues(double min,double max,::sal_Int32 nPointCount,const Reference<chart2::XScaling> & xScalingX,const Reference<chart2::XScaling> &,::sal_Bool)89cdf0e10cSrcweir Sequence< geometry::RealPoint2D > SAL_CALL RegressionCurveCalculator::getCurveValues(
90cdf0e10cSrcweir     double min, double max, ::sal_Int32 nPointCount,
91cdf0e10cSrcweir     const Reference< chart2::XScaling >& xScalingX,
92cdf0e10cSrcweir     const Reference< chart2::XScaling >& /* xScalingY */,
93cdf0e10cSrcweir     ::sal_Bool /* bMaySkipPointsInCalculation */ )
94cdf0e10cSrcweir     throw (lang::IllegalArgumentException,
95cdf0e10cSrcweir            uno::RuntimeException)
96cdf0e10cSrcweir {
97cdf0e10cSrcweir     if( nPointCount < 2 )
98cdf0e10cSrcweir         throw lang::IllegalArgumentException();
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     // determine if scaling and inverse scaling for x-values work
101cdf0e10cSrcweir     bool bDoXScaling( xScalingX.is());
102cdf0e10cSrcweir     uno::Reference< chart2::XScaling > xInverseScaling;
103cdf0e10cSrcweir     if( bDoXScaling )
104cdf0e10cSrcweir         xInverseScaling.set( xScalingX->getInverseScaling());
105cdf0e10cSrcweir     bDoXScaling = bDoXScaling && xInverseScaling.is();
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     Sequence< geometry::RealPoint2D > aResult( nPointCount );
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     double fMin( min );
110cdf0e10cSrcweir     double fFact = (max - min) / double(nPointCount-1);
111cdf0e10cSrcweir     if( bDoXScaling )
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         fMin = xScalingX->doScaling( min );
114cdf0e10cSrcweir         fFact = (xScalingX->doScaling( max ) - fMin) / double(nPointCount-1);
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     for(sal_Int32 nP=0; nP<nPointCount; nP++)
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         double x = fMin + nP * fFact;
120cdf0e10cSrcweir         if( bDoXScaling )
121cdf0e10cSrcweir             x = xInverseScaling->doScaling( x );
122cdf0e10cSrcweir         aResult[nP].X = x;
123cdf0e10cSrcweir         aResult[nP].Y = this->getCurveValue( x );
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     return aResult;
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
getCorrelationCoefficient()129cdf0e10cSrcweir double SAL_CALL RegressionCurveCalculator::getCorrelationCoefficient()
130cdf0e10cSrcweir     throw (uno::RuntimeException)
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     return m_fCorrelationCoeffitient;
133cdf0e10cSrcweir }
134cdf0e10cSrcweir 
getRepresentation()135cdf0e10cSrcweir OUString SAL_CALL RegressionCurveCalculator::getRepresentation()
136cdf0e10cSrcweir     throw (uno::RuntimeException)
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     return ImplGetRepresentation( Reference< util::XNumberFormatter >(), 0 );
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
getFormattedRepresentation(const Reference<util::XNumberFormatsSupplier> & xNumFmtSupplier,::sal_Int32 nNumberFormatKey)141cdf0e10cSrcweir OUString SAL_CALL RegressionCurveCalculator::getFormattedRepresentation(
142cdf0e10cSrcweir     const Reference< util::XNumberFormatsSupplier > & xNumFmtSupplier,
143cdf0e10cSrcweir     ::sal_Int32 nNumberFormatKey )
144cdf0e10cSrcweir     throw (uno::RuntimeException)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir     // create and prepare a number formatter
147cdf0e10cSrcweir     if( !xNumFmtSupplier.is())
148cdf0e10cSrcweir         return getRepresentation();
149cdf0e10cSrcweir     Reference< util::XNumberFormatter > xNumFormatter;
150cdf0e10cSrcweir     Reference< lang::XMultiServiceFactory > xFact( comphelper::getProcessServiceFactory(), uno::UNO_QUERY );
151cdf0e10cSrcweir     if( xFact.is())
152cdf0e10cSrcweir         xNumFormatter.set( xFact->createInstance(
153cdf0e10cSrcweir                                OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.NumberFormatter"))), uno::UNO_QUERY );
154cdf0e10cSrcweir     if( !xNumFormatter.is())
155cdf0e10cSrcweir         return getRepresentation();
156cdf0e10cSrcweir     xNumFormatter->attachNumberFormatsSupplier( xNumFmtSupplier );
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     return ImplGetRepresentation( xNumFormatter, nNumberFormatKey );
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 
162cdf0e10cSrcweir } //  namespace chart
163