diff options
author | Laurent Balland-Poirier <laurent.balland-poirier@laposte.net> | 2016-05-24 21:40:57 +0200 |
---|---|---|
committer | jan iversen <jani@documentfoundation.org> | 2016-06-09 11:00:55 +0000 |
commit | e420a335f783bb4d2ee9d74d56f91e16d189566f (patch) | |
tree | 18be16f8b630280cd1c9522a349738ef1cc7f464 /chart2 | |
parent | 0159ef4fbfd23ba97b20f97eb0677564bebd4ee7 (diff) |
tdf#94004 Wrap Power trendline equation
Wrap equation trendline if it is longer than chart width
Continue https://gerrit.libreoffice.org/18397/
Change-Id: If805f712a29c412a01209533842f9a6c797cbaf1
Reviewed-on: https://gerrit.libreoffice.org/25418
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: jan iversen <jani@documentfoundation.org>
Diffstat (limited to 'chart2')
-rw-r--r-- | chart2/qa/extras/chart2_trendcalculators.cxx | 2 | ||||
-rw-r--r-- | chart2/source/tools/PotentialRegressionCurveCalculator.cxx | 49 |
2 files changed, 37 insertions, 14 deletions
diff --git a/chart2/qa/extras/chart2_trendcalculators.cxx b/chart2/qa/extras/chart2_trendcalculators.cxx index 04af84869237..ca327f1e2c94 100644 --- a/chart2/qa/extras/chart2_trendcalculators.cxx +++ b/chart2/qa/extras/chart2_trendcalculators.cxx @@ -136,7 +136,7 @@ void Chart2TrendCalculators::testPotentialRegression2() xValues[i] = d; yValues[i] = -2.0 * pow ( d, 3 ); } - checkCalculator( xValues, yValues, "f(x) = -2 x^3"); + checkCalculator( xValues, yValues, "f(x) = "+ aMinusSign +" 2 x^3"); } // test y = - 2 X - 5 diff --git a/chart2/source/tools/PotentialRegressionCurveCalculator.cxx b/chart2/source/tools/PotentialRegressionCurveCalculator.cxx index 8ea92c1d8909..3c19a3e2804e 100644 --- a/chart2/source/tools/PotentialRegressionCurveCalculator.cxx +++ b/chart2/source/tools/PotentialRegressionCurveCalculator.cxx @@ -20,6 +20,7 @@ #include "PotentialRegressionCurveCalculator.hxx" #include "macros.hxx" #include "RegressionCalculationHelper.hxx" +#include <SpecialUnicodes.hxx> #include <rtl/math.hxx> #include <rtl/ustrbuf.hxx> @@ -142,32 +143,54 @@ uno::Sequence< geometry::RealPoint2D > SAL_CALL PotentialRegressionCurveCalculat OUString PotentialRegressionCurveCalculator::ImplGetRepresentation( const uno::Reference< util::XNumberFormatter >& xNumFormatter, - sal_Int32 nNumberFormatKey, sal_Int32* /* pFormulaLength = nullptr */ ) const + sal_Int32 nNumberFormatKey, sal_Int32* pFormulaMaxWidth /* = nullptr */ ) const { + bool bHasIntercept = !rtl::math::approxEqual( fabs(m_fIntercept), 1.0 ); OUStringBuffer aBuf( "f(x) = "); + sal_Int32 nLineLength = aBuf.getLength(); + sal_Int32 nValueLength=0; + if ( pFormulaMaxWidth && *pFormulaMaxWidth > 0 ) // count nValueLength + { + sal_Int32 nCharMin = nLineLength + 4; // 4 = "x^" + 2 extra characters + if ( m_fIntercept != 0.0 && m_fSlope != 0.0 ) + { + if ( m_fIntercept < 0.0 ) + nCharMin += 2; // "- " + if ( bHasIntercept ) + nValueLength = (*pFormulaMaxWidth - nCharMin) / 2; + } + if ( nValueLength == 0 ) // not yet calculated + nValueLength = *pFormulaMaxWidth - nCharMin; + if ( nValueLength <= 0 ) + nValueLength = 1; + } if( m_fIntercept == 0.0 ) { aBuf.append( '0' ); } - else if( m_fSlope == 0.0 ) - { - aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept )); - } else { - if( ! rtl::math::approxEqual( fabs(m_fIntercept), 1.0 ) ) + // temporary buffer + OUStringBuffer aTmpBuf(""); + // if nValueLength not calculated then nullptr + sal_Int32* pValueLength = nValueLength ? &nValueLength : nullptr; + if ( m_fIntercept < 0.0 ) // add intercept value + aTmpBuf.append( aMinusSign+" " ); + if( bHasIntercept ) { - aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept )); - aBuf.append( ' '); + OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fIntercept), pValueLength ); + if ( aValueString != "1" ) // aValueString may be rounded to 1 if nValueLength is small + { + aTmpBuf.append( aValueString + " " ); + } } - else // skip intercept if its value is 1 (or near 1) + if( m_fSlope != 0.0 ) // add slope value { - if ( m_fIntercept < 0.0 ) - aBuf.append( "- " ); + aTmpBuf.append( "x^" ); + aTmpBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope, pValueLength )); } - aBuf.append( "x^" ); - aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope )); + addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth ); } return aBuf.makeStringAndClear(); |