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