update credits
[LibreOffice.git] / chart2 / source / tools / RegressionCurveCalculator.cxx
blobb2f6b52f6db0a5339481ffc8243d7dc7555025b2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "RegressionCurveCalculator.hxx"
22 #include "RegressionCalculationHelper.hxx"
23 #include "servicenames_coosystems.hxx"
25 #include <comphelper/processfactory.hxx>
26 #include <rtl/math.hxx>
28 #include <com/sun/star/lang/XServiceName.hpp>
29 #include <com/sun/star/util/NumberFormatter.hpp>
31 using namespace ::com::sun::star;
33 using ::com::sun::star::uno::Reference;
34 using ::com::sun::star::uno::Sequence;
36 namespace chart
39 RegressionCurveCalculator::RegressionCurveCalculator() :
40 m_fCorrelationCoeffitient( 0.0 )
42 ::rtl::math::setNan( & m_fCorrelationCoeffitient );
45 RegressionCurveCalculator::~RegressionCurveCalculator()
48 bool RegressionCurveCalculator::isLinearScaling(
49 const Reference< chart2::XScaling > & xScaling )
51 // no scaling means linear
52 if( !xScaling.is())
53 return true;
54 static OUString aLinScalingServiceName( "com.sun.star.chart2.LinearScaling" );
55 uno::Reference< lang::XServiceName > xServiceName( xScaling, uno::UNO_QUERY );
56 return (xServiceName.is() && xServiceName->getServiceName().equals( aLinScalingServiceName ));
59 bool RegressionCurveCalculator::isLogarithmicScaling(
60 const Reference< chart2::XScaling > & xScaling )
62 static OUString aLogScalingServiceName( "com.sun.star.chart2.LogarithmicScaling" );
63 uno::Reference< lang::XServiceName > xServiceName( xScaling, uno::UNO_QUERY );
64 return (xServiceName.is() && xServiceName->getServiceName().equals( aLogScalingServiceName ));
68 OUString RegressionCurveCalculator::getFormattedString(
69 const Reference< util::XNumberFormatter >& xNumFormatter,
70 ::sal_Int32 nNumberFormatKey,
71 double fNumber ) const
73 OUString aResult;
75 if( xNumFormatter.is())
76 aResult = xNumFormatter->convertNumberToString( nNumberFormatKey, fNumber );
77 else
78 aResult = NUMBER_TO_STR( fNumber );
80 return aResult;
83 Sequence< geometry::RealPoint2D > SAL_CALL RegressionCurveCalculator::getCurveValues(
84 double min, double max, ::sal_Int32 nPointCount,
85 const Reference< chart2::XScaling >& xScalingX,
86 const Reference< chart2::XScaling >& /* xScalingY */,
87 ::sal_Bool /* bMaySkipPointsInCalculation */ )
88 throw (lang::IllegalArgumentException,
89 uno::RuntimeException)
91 if( nPointCount < 2 )
92 throw lang::IllegalArgumentException();
94 // determine if scaling and inverse scaling for x-values work
95 bool bDoXScaling( xScalingX.is());
96 uno::Reference< chart2::XScaling > xInverseScaling;
97 if( bDoXScaling )
98 xInverseScaling.set( xScalingX->getInverseScaling());
99 bDoXScaling = bDoXScaling && xInverseScaling.is();
101 Sequence< geometry::RealPoint2D > aResult( nPointCount );
103 double fMin( min );
104 double fFact = (max - min) / double(nPointCount-1);
105 if( bDoXScaling )
107 fMin = xScalingX->doScaling( min );
108 fFact = (xScalingX->doScaling( max ) - fMin) / double(nPointCount-1);
111 for(sal_Int32 nP=0; nP<nPointCount; nP++)
113 double x = fMin + nP * fFact;
114 if( bDoXScaling )
115 x = xInverseScaling->doScaling( x );
116 aResult[nP].X = x;
117 aResult[nP].Y = this->getCurveValue( x );
120 return aResult;
123 double SAL_CALL RegressionCurveCalculator::getCorrelationCoefficient()
124 throw (uno::RuntimeException)
126 return m_fCorrelationCoeffitient;
129 OUString SAL_CALL RegressionCurveCalculator::getRepresentation()
130 throw (uno::RuntimeException)
132 return ImplGetRepresentation( Reference< util::XNumberFormatter >(), 0 );
135 OUString SAL_CALL RegressionCurveCalculator::getFormattedRepresentation(
136 const Reference< util::XNumberFormatsSupplier > & xNumFmtSupplier,
137 ::sal_Int32 nNumberFormatKey )
138 throw (uno::RuntimeException)
140 // create and prepare a number formatter
141 if( !xNumFmtSupplier.is())
142 return getRepresentation();
143 Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext(), uno::UNO_QUERY_THROW );
144 Reference< util::XNumberFormatter > xNumFormatter( util::NumberFormatter::create(xContext), uno::UNO_QUERY_THROW );
145 xNumFormatter->attachNumberFormatsSupplier( xNumFmtSupplier );
147 return ImplGetRepresentation( xNumFormatter, nNumberFormatKey );
151 } // namespace chart
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */