diff options
author | Laurent Balland-Poirier <laurent.balland-poirier@laposte.net> | 2016-05-24 20:38:09 +0200 |
---|---|---|
committer | jan iversen <jani@documentfoundation.org> | 2016-06-09 10:31:35 +0000 |
commit | 4d636391e3e588779c88c566ac7df5fd1990afea (patch) | |
tree | 50bf1125c1c0410452067c3c2894ec7414fd30c5 /chart2 | |
parent | 9a9c778d81e8ae54dcc42290241a5fc72be29bf2 (diff) |
tdf#94004 Wrap Logarithmic trendline equation
Wrap equation trendline if it is longer than chart width
Continue https://gerrit.libreoffice.org/18397/
Change-Id: Iee374e5db56178a9e87b0f462c3e7deb5e913ab8
Reviewed-on: https://gerrit.libreoffice.org/25416
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: jan iversen <jani@documentfoundation.org>
Diffstat (limited to 'chart2')
-rw-r--r-- | chart2/source/tools/LogarithmicRegressionCurveCalculator.cxx | 72 |
1 files changed, 49 insertions, 23 deletions
diff --git a/chart2/source/tools/LogarithmicRegressionCurveCalculator.cxx b/chart2/source/tools/LogarithmicRegressionCurveCalculator.cxx index e2ca7d4bfe8d..97f0b8524637 100644 --- a/chart2/source/tools/LogarithmicRegressionCurveCalculator.cxx +++ b/chart2/source/tools/LogarithmicRegressionCurveCalculator.cxx @@ -20,6 +20,7 @@ #include "LogarithmicRegressionCurveCalculator.hxx" #include "macros.hxx" #include "RegressionCalculationHelper.hxx" +#include <SpecialUnicodes.hxx> #include <rtl/math.hxx> #include <rtl/ustrbuf.hxx> @@ -130,42 +131,67 @@ uno::Sequence< geometry::RealPoint2D > SAL_CALL LogarithmicRegressionCurveCalcul OUString LogarithmicRegressionCurveCalculator::ImplGetRepresentation( const uno::Reference< util::XNumberFormatter >& xNumFormatter, - sal_Int32 nNumberFormatKey, sal_Int32* /* pFormulaLength = nullptr */ ) const + sal_Int32 nNumberFormatKey, sal_Int32* pFormulaMaxWidth /* = nullptr */ ) const { - OUStringBuffer aBuf( "f(x) = "); - - if( m_fSlope != 0.0 ) + bool bHasSlope = !rtl::math::approxEqual( fabs( m_fSlope ), 1.0 ); + OUStringBuffer aBuf( "f(x) = " ); + sal_Int32 nLineLength = aBuf.getLength(); + sal_Int32 nValueLength=0; + if ( pFormulaMaxWidth && *pFormulaMaxWidth > 0 ) // count nValueLength { - if( ::rtl::math::approxEqual( fabs( m_fSlope ), 1.0 )) - { - if( m_fSlope < 0.0 ) - { - aBuf.append( "-" ); - } - } - else + sal_Int32 nCharMin = nLineLength + 7; // 7 = "ln(x)" + 2 extra characters + if( m_fSlope < 0.0 ) + nCharMin += 2; // "- " + if( m_fSlope != 0.0 && m_fIntercept != 0.0 ) { - aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope )); - aBuf.append( " " ); + nCharMin += 3; // " + " + if ( bHasSlope ) + nValueLength = (*pFormulaMaxWidth - nCharMin) / 2; } - aBuf.append( "ln(x)" ); + if ( nValueLength == 0 ) // not yet calculated + nValueLength = *pFormulaMaxWidth - nCharMin; + if ( nValueLength <= 0 ) + nValueLength = 1; + } - if( m_fIntercept < 0.0 ) + // temporary buffer + OUStringBuffer aTmpBuf(""); + // if nValueLength not calculated then nullptr + sal_Int32* pValueLength = nValueLength ? &nValueLength : nullptr; + if( m_fSlope != 0.0 ) // add slope value + { + if( m_fSlope < 0.0 ) { - aBuf.append( " - " ); - aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs( m_fIntercept ))); + aTmpBuf.append( aMinusSign + " " ); } - else if( m_fIntercept > 0.0 ) + if( bHasSlope ) { - aBuf.append( " + " ); - aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept )); + OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fSlope), pValueLength ); + if ( aValueString != "1" ) // aValueString may be rounded to 1 if nValueLength is small + { + aTmpBuf.append( aValueString + " " ); + } } + aTmpBuf.append( "ln(x) " ); + addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth ); + aTmpBuf.truncate(); + + if( m_fIntercept > 0.0 ) + aTmpBuf.append( "+ " ); } - else + // add intercept value + if( m_fIntercept < 0.0 ) + aTmpBuf.append( aMinusSign+" " ); + OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fIntercept), pValueLength ); + if ( aValueString != "0" ) // aValueString may be rounded to 0 if nValueLength is small { - aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept )); + aTmpBuf.append( aValueString ); + addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth ); } + if ( aBuf.toString() == "f(x) = " ) + aBuf.append( "0" ); + return aBuf.makeStringAndClear(); } |