LibreOffice Module chart2 (master) 1
LogarithmicRegressionCurveCalculator.cxx
Go to the documentation of this file.
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
22#include <SpecialCharacters.hxx>
23
24#include <rtl/math.hxx>
25#include <rtl/ustrbuf.hxx>
26
27using namespace ::com::sun::star;
28
29namespace chart
30{
31
33 m_fSlope( std::numeric_limits<double>::quiet_NaN() ),
34 m_fIntercept( std::numeric_limits<double>::quiet_NaN() )
35{
36}
37
39{}
40
41// ____ XRegressionCurve ____
43 const uno::Sequence< double >& aXValues,
44 const uno::Sequence< double >& aYValues )
45{
48 aXValues, aYValues,
50
51 const size_t nMax = aValues.first.size();
52 if( nMax <= 1 ) // at least 2 points
53 {
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;
58 }
59
60 double fAverageX = 0.0, fAverageY = 0.0;
61 size_t i = 0;
62 for( i = 0; i < nMax; ++i )
63 {
64 fAverageX += log( aValues.first[i] );
65 fAverageY += aValues.second[i];
66 }
67
68 const double fN = static_cast< double >( nMax );
69 fAverageX /= fN;
70 fAverageY /= fN;
71
72 double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
73 for( i = 0; i < nMax; ++i )
74 {
75 double fDeltaX = log( aValues.first[i] ) - fAverageX;
76 double fDeltaY = aValues.second[i] - fAverageY;
77
78 fQx += fDeltaX * fDeltaX;
79 fQy += fDeltaY * fDeltaY;
80 fQxy += fDeltaX * fDeltaY;
81 }
82
83 m_fSlope = fQxy / fQx;
84 m_fIntercept = fAverageY - m_fSlope * fAverageX;
85 m_fCorrelationCoefficient = fQxy / sqrt( fQx * fQy );
86}
87
89{
90 if( ! ( std::isnan( m_fSlope ) ||
91 std::isnan( m_fIntercept )))
92 {
93 return m_fSlope * log( x ) + m_fIntercept;
94 }
95
96 return std::numeric_limits<double>::quiet_NaN();
97}
98
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 )
104{
105 if( bMaySkipPointsInCalculation &&
106 isLogarithmicScaling( xScalingX ) &&
107 isLinearScaling( xScalingY ))
108 {
109 // optimize result
111 { max, getCurveValue( max ) } };
112
113 return aResult;
114 }
115 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
116}
117
119 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
120 sal_Int32 nNumberFormatKey, sal_Int32* pFormulaMaxWidth /* = nullptr */ ) const
121{
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
127 {
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 )
132 {
133 nCharMin += 3; // " + "
134 if ( bHasSlope )
135 nValueLength = (*pFormulaMaxWidth - nCharMin) / 2;
136 }
137 if ( nValueLength == 0 ) // not yet calculated
138 nValueLength = *pFormulaMaxWidth - nCharMin;
139 if ( nValueLength <= 0 )
140 nValueLength = 1;
141 }
142
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
148 {
149 if( m_fSlope < 0.0 )
150 {
151 aTmpBuf.append( OUStringChar(aMinusSign) + " " );
152 }
153 if( bHasSlope )
154 {
155 OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fSlope), pValueLength );
156 if ( aValueString != "1" ) // aValueString may be rounded to 1 if nValueLength is small
157 {
158 aTmpBuf.append( aValueString + " " );
159 }
160 }
161 aTmpBuf.append( "ln(" + mXName + ") " );
162 addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth );
163 aTmpBuf.truncate();
164
165 if( m_fIntercept > 0.0 )
166 aTmpBuf.append( "+ " );
167 }
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
173 {
174 aTmpBuf.append( aValueString );
175 addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth );
176 }
177
178 if ( std::u16string_view(aBuf) == Concat2View(mYName + " = ") )
179 aBuf.append( "0" );
180
181 return aBuf.makeStringAndClear();
182}
183
184} // namespace chart
185
186/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const sal_Unicode aMinusSign
virtual void SAL_CALL recalculateRegression(const css::uno::Sequence< double > &aXValues, const css::uno::Sequence< double > &aYValues) override
virtual double SAL_CALL getCurveValue(double x) override
virtual css::uno::Sequence< css::geometry::RealPoint2D > SAL_CALL getCurveValues(double min, double max, sal_Int32 nPointCount, const css::uno::Reference< css::chart2::XScaling > &xScalingX, const css::uno::Reference< css::chart2::XScaling > &xScalingY, sal_Bool bMaySkipPointsInCalculation) override
virtual OUString ImplGetRepresentation(const css::uno::Reference< css::util::XNumberFormatter > &xNumFormatter, sal_Int32 nNumberFormatKey, sal_Int32 *pFormulaLength=nullptr) const override
static bool isLogarithmicScaling(const css::uno::Reference< css::chart2::XScaling > &xScaling)
static void addStringToEquation(OUStringBuffer &aStrEquation, sal_Int32 &nLineLength, OUStringBuffer const &aAddString, const sal_Int32 *pMaxLength)
static OUString getFormattedString(const css::uno::Reference< css::util::XNumberFormatter > &xNumFormatter, sal_Int32 nNumberFormatKey, double fNumber, const sal_Int32 *pStringLength)
static bool isLinearScaling(const css::uno::Reference< css::chart2::XScaling > &xScaling)
virtual css::uno::Sequence< css::geometry::RealPoint2D > SAL_CALL getCurveValues(double min, double max, sal_Int32 nPointCount, const css::uno::Reference< css::chart2::XScaling > &xScalingX, const css::uno::Reference< css::chart2::XScaling > &xScalingY, sal_Bool bMaySkipPointsInCalculation) override
float x
#define max(a, b)
aBuf
std::pair< std::vector< double >, std::vector< double > > tDoubleVectorPair
tDoubleVectorPair cleanup(const css::uno::Sequence< double > &rXValues, const css::uno::Sequence< double > &rYValues, Pred aPred)
takes the given x- and y-values and copies them into the resulting pair, which contains x-values in t...
int i
log
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)
unsigned char sal_Bool