1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 
31 #include "RegressionCurveCalculator.hxx"
32 #include "RegressionCalculationHelper.hxx"
33 #include "servicenames_coosystems.hxx"
34 
35 #include <comphelper/processfactory.hxx>
36 #include <rtl/math.hxx>
37 
38 #include <com/sun/star/lang/XServiceName.hpp>
39 
40 using namespace ::com::sun::star;
41 
42 using ::com::sun::star::uno::Reference;
43 using ::com::sun::star::uno::Sequence;
44 using ::rtl::OUString;
45 
46 namespace chart
47 {
48 
49 RegressionCurveCalculator::RegressionCurveCalculator() :
50         m_fCorrelationCoeffitient( 0.0 )
51 {
52     ::rtl::math::setNan( & m_fCorrelationCoeffitient );
53 }
54 
55 RegressionCurveCalculator::~RegressionCurveCalculator()
56 {}
57 
58 bool RegressionCurveCalculator::isLinearScaling(
59     const Reference< chart2::XScaling > & xScaling )
60 {
61     // no scaling means linear
62     if( !xScaling.is())
63         return true;
64     static OUString aLinScalingServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LinearScaling" ));
65     uno::Reference< lang::XServiceName > xServiceName( xScaling, uno::UNO_QUERY );
66     return (xServiceName.is() && xServiceName->getServiceName().equals( aLinScalingServiceName ));
67 }
68 
69 bool RegressionCurveCalculator::isLogarithmicScaling(
70     const Reference< chart2::XScaling > & xScaling )
71 {
72     static OUString aLogScalingServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LogarithmicScaling" ));
73     uno::Reference< lang::XServiceName > xServiceName( xScaling, uno::UNO_QUERY );
74     return (xServiceName.is() && xServiceName->getServiceName().equals( aLogScalingServiceName ));
75 }
76 
77 
78 OUString RegressionCurveCalculator::getFormattedString(
79     const Reference< util::XNumberFormatter >& xNumFormatter,
80     ::sal_Int32 nNumberFormatKey,
81     double fNumber ) const
82 {
83     OUString aResult;
84 
85     if( xNumFormatter.is())
86         aResult = xNumFormatter->convertNumberToString( nNumberFormatKey, fNumber );
87     else
88         aResult = NUMBER_TO_STR( fNumber );
89 
90     return aResult;
91 }
92 
93 Sequence< geometry::RealPoint2D > SAL_CALL RegressionCurveCalculator::getCurveValues(
94     double min, double max, ::sal_Int32 nPointCount,
95     const Reference< chart2::XScaling >& xScalingX,
96     const Reference< chart2::XScaling >& /* xScalingY */,
97     ::sal_Bool /* bMaySkipPointsInCalculation */ )
98     throw (lang::IllegalArgumentException,
99            uno::RuntimeException)
100 {
101     if( nPointCount < 2 )
102         throw lang::IllegalArgumentException();
103 
104     // determine if scaling and inverse scaling for x-values work
105     bool bDoXScaling( xScalingX.is());
106     uno::Reference< chart2::XScaling > xInverseScaling;
107     if( bDoXScaling )
108         xInverseScaling.set( xScalingX->getInverseScaling());
109     bDoXScaling = bDoXScaling && xInverseScaling.is();
110 
111     Sequence< geometry::RealPoint2D > aResult( nPointCount );
112 
113     double fMin( min );
114     double fFact = (max - min) / double(nPointCount-1);
115     if( bDoXScaling )
116     {
117         fMin = xScalingX->doScaling( min );
118         fFact = (xScalingX->doScaling( max ) - fMin) / double(nPointCount-1);
119     }
120 
121     for(sal_Int32 nP=0; nP<nPointCount; nP++)
122     {
123         double x = fMin + nP * fFact;
124         if( bDoXScaling )
125             x = xInverseScaling->doScaling( x );
126         aResult[nP].X = x;
127         aResult[nP].Y = this->getCurveValue( x );
128     }
129 
130     return aResult;
131 }
132 
133 double SAL_CALL RegressionCurveCalculator::getCorrelationCoefficient()
134     throw (uno::RuntimeException)
135 {
136     return m_fCorrelationCoeffitient;
137 }
138 
139 OUString SAL_CALL RegressionCurveCalculator::getRepresentation()
140     throw (uno::RuntimeException)
141 {
142     return ImplGetRepresentation( Reference< util::XNumberFormatter >(), 0 );
143 }
144 
145 OUString SAL_CALL RegressionCurveCalculator::getFormattedRepresentation(
146     const Reference< util::XNumberFormatsSupplier > & xNumFmtSupplier,
147     ::sal_Int32 nNumberFormatKey )
148     throw (uno::RuntimeException)
149 {
150     // create and prepare a number formatter
151     if( !xNumFmtSupplier.is())
152         return getRepresentation();
153     Reference< util::XNumberFormatter > xNumFormatter;
154     Reference< lang::XMultiServiceFactory > xFact( comphelper::getProcessServiceFactory(), uno::UNO_QUERY );
155     if( xFact.is())
156         xNumFormatter.set( xFact->createInstance(
157                                OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.NumberFormatter"))), uno::UNO_QUERY );
158     if( !xNumFormatter.is())
159         return getRepresentation();
160     xNumFormatter->attachNumberFormatsSupplier( xNumFmtSupplier );
161 
162     return ImplGetRepresentation( xNumFormatter, nNumberFormatKey );
163 }
164 
165 
166 } //  namespace chart
167