LCOV - code coverage report
Current view: top level - chart2/source/tools - RegressionCurveCalculator.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 50 59 84.7 %
Date: 2015-06-13 12:38:46 Functions: 8 11 72.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include "RegressionCurveCalculator.hxx"
      21             : #include "RegressionCalculationHelper.hxx"
      22             : #include "servicenames_coosystems.hxx"
      23             : 
      24             : #include <comphelper/processfactory.hxx>
      25             : #include <rtl/math.hxx>
      26             : 
      27             : #include <com/sun/star/lang/XServiceName.hpp>
      28             : #include <com/sun/star/util/NumberFormatter.hpp>
      29             : 
      30             : using namespace ::com::sun::star;
      31             : 
      32             : using ::com::sun::star::uno::Reference;
      33             : using ::com::sun::star::uno::Sequence;
      34             : 
      35             : namespace chart
      36             : {
      37             : 
      38          60 : RegressionCurveCalculator::RegressionCurveCalculator() :
      39             :         m_fCorrelationCoeffitient(0.0),
      40             :         mDegree(2),
      41             :         mForceIntercept(false),
      42             :         mInterceptValue(0.0),
      43          60 :         mPeriod(2)
      44             : {
      45          60 :     rtl::math::setNan( &m_fCorrelationCoeffitient );
      46          60 :     rtl::math::setNan( &mInterceptValue );
      47          60 : }
      48             : 
      49          60 : RegressionCurveCalculator::~RegressionCurveCalculator()
      50          60 : {}
      51             : 
      52          18 : bool RegressionCurveCalculator::isLinearScaling(
      53             :     const Reference< chart2::XScaling > & xScaling )
      54             : {
      55             :     // no scaling means linear
      56          18 :     if( !xScaling.is())
      57           0 :         return true;
      58          18 :     uno::Reference< lang::XServiceName > xServiceName( xScaling, uno::UNO_QUERY );
      59          18 :     return xServiceName.is() && xServiceName->getServiceName() == "com.sun.star.chart2.LinearScaling";
      60             : }
      61             : 
      62           0 : bool RegressionCurveCalculator::isLogarithmicScaling(
      63             :     const Reference< chart2::XScaling > & xScaling )
      64             : {
      65           0 :     uno::Reference< lang::XServiceName > xServiceName( xScaling, uno::UNO_QUERY );
      66           0 :     return xServiceName.is() && xServiceName->getServiceName() == "com.sun.star.chart2.LogarithmicScaling";
      67             : }
      68             : 
      69          60 : void RegressionCurveCalculator::setRegressionProperties(
      70             :     sal_Int32   aDegree,
      71             :     sal_Bool    aForceIntercept,
      72             :     double      aInterceptValue,
      73             :     sal_Int32   aPeriod )
      74             :         throw (uno::RuntimeException, std::exception)
      75             : {
      76          60 :     mDegree = aDegree;
      77          60 :     mForceIntercept = aForceIntercept;
      78          60 :     mInterceptValue = aInterceptValue;
      79          60 :     mPeriod  = aPeriod;
      80          60 : }
      81             : 
      82          44 : OUString RegressionCurveCalculator::getFormattedString(
      83             :     const Reference< util::XNumberFormatter >& xNumFormatter,
      84             :     sal_Int32 nNumberFormatKey,
      85             :     double fNumber )
      86             : {
      87          44 :     OUString aResult;
      88             : 
      89          44 :     if( xNumFormatter.is())
      90          44 :         aResult = xNumFormatter->convertNumberToString( nNumberFormatKey, fNumber );
      91             :     else
      92           0 :         aResult = NUMBER_TO_STR( fNumber );
      93             : 
      94          44 :     return aResult;
      95             : }
      96             : 
      97           7 : Sequence< geometry::RealPoint2D > SAL_CALL RegressionCurveCalculator::getCurveValues(
      98             :     double min, double max, ::sal_Int32 nPointCount,
      99             :     const Reference< chart2::XScaling >& xScalingX,
     100             :     const Reference< chart2::XScaling >& /* xScalingY */,
     101             :     sal_Bool /* bMaySkipPointsInCalculation */ )
     102             :         throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception)
     103             : {
     104           7 :     if( nPointCount < 2 )
     105           0 :         throw lang::IllegalArgumentException();
     106             : 
     107             :     // determine if scaling and inverse scaling for x-values work
     108           7 :     bool bDoXScaling( xScalingX.is());
     109           7 :     uno::Reference< chart2::XScaling > xInverseScaling;
     110           7 :     if( bDoXScaling )
     111           7 :         xInverseScaling.set( xScalingX->getInverseScaling());
     112           7 :     bDoXScaling = bDoXScaling && xInverseScaling.is();
     113             : 
     114           7 :     Sequence< geometry::RealPoint2D > aResult( nPointCount );
     115             : 
     116           7 :     double fMin( min );
     117           7 :     double fFact = (max - min) / double(nPointCount-1);
     118             : 
     119           7 :     if( bDoXScaling )
     120             :     {
     121           7 :         fMin = xScalingX->doScaling( min );
     122           7 :         fFact = (xScalingX->doScaling( max ) - fMin) / double(nPointCount-1);
     123             :     }
     124             : 
     125         532 :     for(sal_Int32 nP=0; nP<nPointCount; nP++)
     126             :     {
     127         525 :         double x = fMin + nP * fFact;
     128         525 :         if( bDoXScaling )
     129         525 :             x = xInverseScaling->doScaling( x );
     130         525 :         aResult[nP].X = x;
     131         525 :         aResult[nP].Y = this->getCurveValue( x );
     132             :     }
     133             : 
     134           7 :     return aResult;
     135             : }
     136             : 
     137           8 : double SAL_CALL RegressionCurveCalculator::getCorrelationCoefficient()
     138             :     throw (uno::RuntimeException, std::exception)
     139             : {
     140           8 :     return m_fCorrelationCoeffitient;
     141             : }
     142             : 
     143           0 : OUString SAL_CALL RegressionCurveCalculator::getRepresentation()
     144             :     throw (uno::RuntimeException, std::exception)
     145             : {
     146           0 :     return ImplGetRepresentation( Reference< util::XNumberFormatter >(), 0 );
     147             : }
     148             : 
     149          15 : OUString SAL_CALL RegressionCurveCalculator::getFormattedRepresentation(
     150             :     const Reference< util::XNumberFormatsSupplier > & xNumFmtSupplier,
     151             :     sal_Int32 nNumberFormatKey )
     152             :     throw (uno::RuntimeException, std::exception)
     153             : {
     154             :     // create and prepare a number formatter
     155          15 :     if( !xNumFmtSupplier.is())
     156           0 :         return getRepresentation();
     157          15 :     Reference< uno::XComponentContext > xContext( comphelper::getProcessComponentContext(), uno::UNO_QUERY_THROW );
     158          30 :     Reference< util::XNumberFormatter > xNumFormatter( util::NumberFormatter::create(xContext), uno::UNO_QUERY_THROW );
     159          15 :     xNumFormatter->attachNumberFormatsSupplier( xNumFmtSupplier );
     160             : 
     161          30 :     return ImplGetRepresentation( xNumFormatter, nNumberFormatKey );
     162             : }
     163             : 
     164             : } //  namespace chart
     165             : 
     166             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11