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 #include "MeanValueRegressionCurveCalculator.hxx"
27 #include "macros.hxx"
28 
29 #include <rtl/math.hxx>
30 #include <rtl/ustrbuf.hxx>
31 
32 using namespace ::com::sun::star;
33 
34 using ::rtl::OUString;
35 using ::rtl::OUStringBuffer;
36 
37 namespace chart
38 {
39 
MeanValueRegressionCurveCalculator()40 MeanValueRegressionCurveCalculator::MeanValueRegressionCurveCalculator() :
41         m_fMeanValue( 0.0 )
42 {
43     ::rtl::math::setNan( & m_fMeanValue );
44 }
45 
~MeanValueRegressionCurveCalculator()46 MeanValueRegressionCurveCalculator::~MeanValueRegressionCurveCalculator()
47 {}
48 
49 // ____ XRegressionCurveCalculator ____
recalculateRegression(const uno::Sequence<double> &,const uno::Sequence<double> & aYValues)50 void SAL_CALL MeanValueRegressionCurveCalculator::recalculateRegression(
51     const uno::Sequence< double >& /*aXValues*/,
52     const uno::Sequence< double >& aYValues )
53     throw (uno::RuntimeException)
54 {
55     const sal_Int32 nDataLength = aYValues.getLength();
56     sal_Int32 nMax = nDataLength;
57     double fSumY = 0.0;
58     const double * pY = aYValues.getConstArray();
59 
60     for( sal_Int32 i = 0; i < nDataLength; ++i )
61     {
62         if( ::rtl::math::isNan( pY[i] ) ||
63             ::rtl::math::isInf( pY[i] ))
64             --nMax;
65         else
66             fSumY += pY[i];
67     }
68 
69     m_fCorrelationCoeffitient = 0.0;
70 
71     if( nMax == 0 )
72     {
73         ::rtl::math::setNan( & m_fMeanValue );
74     }
75     else
76     {
77         m_fMeanValue = fSumY / static_cast< double >( nMax );
78 
79         // correlation coefficient: standard deviation
80         if( nMax > 1 )
81         {
82             double fErrorSum = 0.0;
83             for( sal_Int32 i = 0; i < nDataLength; ++i )
84             {
85                 if( !::rtl::math::isNan( pY[i] ) &&
86                     !::rtl::math::isInf( pY[i] ))
87                 {
88                     double v = m_fMeanValue - pY[i];
89                     fErrorSum += (v*v);
90                 }
91             }
92             OSL_ASSERT( fErrorSum >= 0.0 );
93             m_fCorrelationCoeffitient = sqrt( fErrorSum / (nMax - 1 ));
94         }
95     }
96 }
97 
getCurveValue(double)98 double SAL_CALL MeanValueRegressionCurveCalculator::getCurveValue( double /*x*/ )
99     throw (lang::IllegalArgumentException,
100            uno::RuntimeException)
101 {
102     return m_fMeanValue;
103 }
104 
105 
getCurveValues(double min,double max,::sal_Int32 nPointCount,const uno::Reference<chart2::XScaling> & xScalingX,const uno::Reference<chart2::XScaling> & xScalingY,::sal_Bool bMaySkipPointsInCalculation)106 uno::Sequence< geometry::RealPoint2D > SAL_CALL MeanValueRegressionCurveCalculator::getCurveValues(
107     double min, double max, ::sal_Int32 nPointCount,
108     const uno::Reference< chart2::XScaling >& xScalingX,
109     const uno::Reference< chart2::XScaling >& xScalingY,
110     ::sal_Bool bMaySkipPointsInCalculation )
111     throw (lang::IllegalArgumentException,
112            uno::RuntimeException)
113 {
114     if( bMaySkipPointsInCalculation )
115     {
116         // optimize result
117         uno::Sequence< geometry::RealPoint2D > aResult( 2 );
118         aResult[0].X = min;
119         aResult[0].Y = m_fMeanValue;
120         aResult[1].X = max;
121         aResult[1].Y = m_fMeanValue;
122 
123         return aResult;
124     }
125     return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
126 }
127 
ImplGetRepresentation(const uno::Reference<util::XNumberFormatter> & xNumFormatter,::sal_Int32 nNumberFormatKey) const128 OUString MeanValueRegressionCurveCalculator::ImplGetRepresentation(
129     const uno::Reference< util::XNumberFormatter >& xNumFormatter,
130     ::sal_Int32 nNumberFormatKey ) const
131 {
132     OUStringBuffer aBuf( C2U( "f(x) = " ));
133 
134     aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fMeanValue ));
135 
136     return aBuf.makeStringAndClear();
137 }
138 
139 } //  namespace chart
140