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 #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 {
54 const sal_Int32 nDataLength = aYValues.getLength();
55 sal_Int32 nMax = nDataLength;
56 double fSumY = 0.0;
57 const double * pY = aYValues.getConstArray();
58
59 for( sal_Int32 i = 0; i < nDataLength; ++i )
60 {
61 if( ::rtl::math::isNan( pY[i] ) ||
62 ::rtl::math::isInf( pY[i] ))
63 --nMax;
64 else
65 fSumY += pY[i];
66 }
67
68 m_fCorrelationCoeffitient = 0.0;
69
70 if( nMax == 0 )
71 {
72 ::rtl::math::setNan( & m_fMeanValue );
73 }
74 else
75 {
76 m_fMeanValue = fSumY / static_cast< double >( nMax );
77
78 // correlation coefficient: standard deviation
79 if( nMax > 1 )
80 {
81 double fErrorSum = 0.0;
82 for( sal_Int32 i = 0; i < nDataLength; ++i )
83 {
84 if( !::rtl::math::isNan( pY[i] ) &&
85 !::rtl::math::isInf( pY[i] ))
86 {
87 double v = m_fMeanValue - pY[i];
88 fErrorSum += (v*v);
89 }
90 }
91 OSL_ASSERT( fErrorSum >= 0.0 );
92 m_fCorrelationCoeffitient = sqrt( fErrorSum / (nMax - 1 ));
93 }
94 }
95 }
96
getCurveValue(double)97 double SAL_CALL MeanValueRegressionCurveCalculator::getCurveValue( double /*x*/ )
98 {
99 return m_fMeanValue;
100 }
101
102
getCurveValues(double min,double max,::sal_Int32 nPointCount,const uno::Reference<chart2::XScaling> & xScalingX,const uno::Reference<chart2::XScaling> & xScalingY,::sal_Bool bMaySkipPointsInCalculation)103 uno::Sequence< geometry::RealPoint2D > SAL_CALL MeanValueRegressionCurveCalculator::getCurveValues(
104 double min, double max, ::sal_Int32 nPointCount,
105 const uno::Reference< chart2::XScaling >& xScalingX,
106 const uno::Reference< chart2::XScaling >& xScalingY,
107 ::sal_Bool bMaySkipPointsInCalculation )
108 {
109 if( bMaySkipPointsInCalculation )
110 {
111 // optimize result
112 uno::Sequence< geometry::RealPoint2D > aResult( 2 );
113 aResult[0].X = min;
114 aResult[0].Y = m_fMeanValue;
115 aResult[1].X = max;
116 aResult[1].Y = m_fMeanValue;
117
118 return aResult;
119 }
120 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
121 }
122
ImplGetRepresentation(const uno::Reference<util::XNumberFormatter> & xNumFormatter,::sal_Int32 nNumberFormatKey) const123 OUString MeanValueRegressionCurveCalculator::ImplGetRepresentation(
124 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
125 ::sal_Int32 nNumberFormatKey ) const
126 {
127 OUStringBuffer aBuf( C2U( "f(x) = " ));
128
129 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fMeanValue ));
130
131 return aBuf.makeStringAndClear();
132 }
133
134 } // namespace chart
135