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_chart2.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     throw (lang::IllegalArgumentException,
95            uno::RuntimeException)
96 {
97     if( nPointCount < 2 )
98         throw lang::IllegalArgumentException();
99 
100     // determine if scaling and inverse scaling for x-values work
101     bool bDoXScaling( xScalingX.is());
102     uno::Reference< chart2::XScaling > xInverseScaling;
103     if( bDoXScaling )
104         xInverseScaling.set( xScalingX->getInverseScaling());
105     bDoXScaling = bDoXScaling && xInverseScaling.is();
106 
107     Sequence< geometry::RealPoint2D > aResult( nPointCount );
108 
109     double fMin( min );
110     double fFact = (max - min) / double(nPointCount-1);
111     if( bDoXScaling )
112     {
113         fMin = xScalingX->doScaling( min );
114         fFact = (xScalingX->doScaling( max ) - fMin) / double(nPointCount-1);
115     }
116 
117     for(sal_Int32 nP=0; nP<nPointCount; nP++)
118     {
119         double x = fMin + nP * fFact;
120         if( bDoXScaling )
121             x = xInverseScaling->doScaling( x );
122         aResult[nP].X = x;
123         aResult[nP].Y = this->getCurveValue( x );
124     }
125 
126     return aResult;
127 }
128 
getCorrelationCoefficient()129 double SAL_CALL RegressionCurveCalculator::getCorrelationCoefficient()
130     throw (uno::RuntimeException)
131 {
132     return m_fCorrelationCoeffitient;
133 }
134 
getRepresentation()135 OUString SAL_CALL RegressionCurveCalculator::getRepresentation()
136     throw (uno::RuntimeException)
137 {
138     return ImplGetRepresentation( Reference< util::XNumberFormatter >(), 0 );
139 }
140 
getFormattedRepresentation(const Reference<util::XNumberFormatsSupplier> & xNumFmtSupplier,::sal_Int32 nNumberFormatKey)141 OUString SAL_CALL RegressionCurveCalculator::getFormattedRepresentation(
142     const Reference< util::XNumberFormatsSupplier > & xNumFmtSupplier,
143     ::sal_Int32 nNumberFormatKey )
144     throw (uno::RuntimeException)
145 {
146     // create and prepare a number formatter
147     if( !xNumFmtSupplier.is())
148         return getRepresentation();
149     Reference< util::XNumberFormatter > xNumFormatter;
150     Reference< lang::XMultiServiceFactory > xFact( comphelper::getProcessServiceFactory(), uno::UNO_QUERY );
151     if( xFact.is())
152         xNumFormatter.set( xFact->createInstance(
153                                OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.NumberFormatter"))), uno::UNO_QUERY );
154     if( !xNumFormatter.is())
155         return getRepresentation();
156     xNumFormatter->attachNumberFormatsSupplier( xNumFmtSupplier );
157 
158     return ImplGetRepresentation( xNumFormatter, nNumberFormatKey );
159 }
160 
161 
162 } //  namespace chart
163