Get the style color and number just once
[LibreOffice.git] / chart2 / source / tools / LogarithmicRegressionCurveCalculator.cxx
blob5e68235d14cd436eb588a2464d5613725049d7b3
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 .
20 #include <LogarithmicRegressionCurveCalculator.hxx>
21 #include <RegressionCalculationHelper.hxx>
22 #include <SpecialCharacters.hxx>
24 #include <rtl/math.hxx>
25 #include <rtl/ustrbuf.hxx>
27 using namespace ::com::sun::star;
29 namespace chart
32 LogarithmicRegressionCurveCalculator::LogarithmicRegressionCurveCalculator() :
33 m_fSlope( std::numeric_limits<double>::quiet_NaN() ),
34 m_fIntercept( std::numeric_limits<double>::quiet_NaN() )
38 LogarithmicRegressionCurveCalculator::~LogarithmicRegressionCurveCalculator()
41 // ____ XRegressionCurve ____
42 void SAL_CALL LogarithmicRegressionCurveCalculator::recalculateRegression(
43 const uno::Sequence< double >& aXValues,
44 const uno::Sequence< double >& aYValues )
46 RegressionCalculationHelper::tDoubleVectorPair aValues(
47 RegressionCalculationHelper::cleanup(
48 aXValues, aYValues,
49 RegressionCalculationHelper::isValidAndXPositive()));
51 const size_t nMax = aValues.first.size();
52 if( nMax <= 1 ) // at least 2 points
54 m_fSlope = std::numeric_limits<double>::quiet_NaN();
55 m_fIntercept = std::numeric_limits<double>::quiet_NaN();
56 m_fCorrelationCoefficient = std::numeric_limits<double>::quiet_NaN();
57 return;
60 double fAverageX = 0.0, fAverageY = 0.0;
61 size_t i = 0;
62 for( i = 0; i < nMax; ++i )
64 fAverageX += log( aValues.first[i] );
65 fAverageY += aValues.second[i];
68 const double fN = static_cast< double >( nMax );
69 fAverageX /= fN;
70 fAverageY /= fN;
72 double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
73 for( i = 0; i < nMax; ++i )
75 double fDeltaX = log( aValues.first[i] ) - fAverageX;
76 double fDeltaY = aValues.second[i] - fAverageY;
78 fQx += fDeltaX * fDeltaX;
79 fQy += fDeltaY * fDeltaY;
80 fQxy += fDeltaX * fDeltaY;
83 m_fSlope = fQxy / fQx;
84 m_fIntercept = fAverageY - m_fSlope * fAverageX;
85 m_fCorrelationCoefficient = fQxy / sqrt( fQx * fQy );
88 double SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValue( double x )
90 if( ! ( std::isnan( m_fSlope ) ||
91 std::isnan( m_fIntercept )))
93 return m_fSlope * log( x ) + m_fIntercept;
96 return std::numeric_limits<double>::quiet_NaN();
99 uno::Sequence< geometry::RealPoint2D > SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValues(
100 double min, double max, ::sal_Int32 nPointCount,
101 const uno::Reference< chart2::XScaling >& xScalingX,
102 const uno::Reference< chart2::XScaling >& xScalingY,
103 sal_Bool bMaySkipPointsInCalculation )
105 if( bMaySkipPointsInCalculation &&
106 isLogarithmicScaling( xScalingX ) &&
107 isLinearScaling( xScalingY ))
109 // optimize result
110 uno::Sequence< geometry::RealPoint2D > aResult{ { min, getCurveValue( min ) },
111 { max, getCurveValue( max ) } };
113 return aResult;
115 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
118 OUString LogarithmicRegressionCurveCalculator::ImplGetRepresentation(
119 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
120 sal_Int32 nNumberFormatKey, sal_Int32* pFormulaMaxWidth /* = nullptr */ ) const
122 bool bHasSlope = !rtl::math::approxEqual( fabs( m_fSlope ), 1.0 );
123 OUStringBuffer aBuf( mYName + " = " );
124 sal_Int32 nLineLength = aBuf.getLength();
125 sal_Int32 nValueLength=0;
126 if ( pFormulaMaxWidth && *pFormulaMaxWidth > 0 ) // count nValueLength
128 sal_Int32 nCharMin = nLineLength + 6 + mXName.getLength(); // 6 = "ln(x)" + 2 extra characters
129 if( m_fSlope < 0.0 )
130 nCharMin += 2; // "- "
131 if( m_fSlope != 0.0 && m_fIntercept != 0.0 )
133 nCharMin += 3; // " + "
134 if ( bHasSlope )
135 nValueLength = (*pFormulaMaxWidth - nCharMin) / 2;
137 if ( nValueLength == 0 ) // not yet calculated
138 nValueLength = *pFormulaMaxWidth - nCharMin;
139 if ( nValueLength <= 0 )
140 nValueLength = 1;
143 // temporary buffer
144 OUStringBuffer aTmpBuf("");
145 // if nValueLength not calculated then nullptr
146 sal_Int32* pValueLength = nValueLength ? &nValueLength : nullptr;
147 if( m_fSlope != 0.0 ) // add slope value
149 if( m_fSlope < 0.0 )
151 aTmpBuf.append( OUStringChar(aMinusSign) + " " );
153 if( bHasSlope )
155 OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fSlope), pValueLength );
156 if ( aValueString != "1" ) // aValueString may be rounded to 1 if nValueLength is small
158 aTmpBuf.append( aValueString + " " );
161 aTmpBuf.append( "ln(" + mXName + ") " );
162 addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth );
163 aTmpBuf.truncate();
165 if( m_fIntercept > 0.0 )
166 aTmpBuf.append( "+ " );
168 // add intercept value
169 if( m_fIntercept < 0.0 )
170 aTmpBuf.append( OUStringChar(aMinusSign) + " " );
171 OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fIntercept), pValueLength );
172 if ( aValueString != "0" ) // aValueString may be rounded to 0 if nValueLength is small
174 aTmpBuf.append( aValueString );
175 addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth );
178 if ( std::u16string_view(aBuf) == Concat2View(mYName + " = ") )
179 aBuf.append( "0" );
181 return aBuf.makeStringAndClear();
184 } // namespace chart
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */