[Libreoffice-commits] core.git: Branch 'libreoffice-5-2' - 2 commits - chart2/qa chart2/source

Laurent Balland-Poirier laurent.balland-poirier at laposte.net
Sun Jun 19 07:28:13 UTC 2016


 chart2/qa/extras/chart2_trendcalculators.cxx                 |    2 
 chart2/source/tools/ExponentialRegressionCurveCalculator.cxx |   69 +++++++---
 chart2/source/tools/LogarithmicRegressionCurveCalculator.cxx |   72 +++++++----
 3 files changed, 101 insertions(+), 42 deletions(-)

New commits:
commit d1f56846a72697bf55f0330a7b5d0e385bf4a5cb
Author: Laurent Balland-Poirier <laurent.balland-poirier at laposte.net>
Date:   Tue May 24 20:38:09 2016 +0200

    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 at libreoffice.org>
    Reviewed-by: jan iversen <jani at documentfoundation.org>
    (cherry picked from commit 4d636391e3e588779c88c566ac7df5fd1990afea)
    Reviewed-on: https://gerrit.libreoffice.org/26118
    Reviewed-by: Laurent BP <laurent.balland-poirier at laposte.net>

diff --git a/chart2/source/tools/LogarithmicRegressionCurveCalculator.cxx b/chart2/source/tools/LogarithmicRegressionCurveCalculator.cxx
index e2ca7d4..97f0b85 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();
 }
 
commit 4cbfcf64297f3b8826c351a1dd3306a3510e8073
Author: Laurent Balland-Poirier <laurent.balland-poirier at laposte.net>
Date:   Mon May 23 21:17:29 2016 +0200

    tdf#94004 Wrap Exponential trendline equation
    
    Wrap equation trendline if it is longer than chart width
    Continue https://gerrit.libreoffice.org/18397/
    
    Change-Id: I3938aaeec6c56582ecfe50dbfc54ec711f1b963d
    Reviewed-on: https://gerrit.libreoffice.org/25393
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: jan iversen <jani at documentfoundation.org>
    (cherry picked from commit 9a9c778d81e8ae54dcc42290241a5fc72be29bf2)
    Reviewed-on: https://gerrit.libreoffice.org/26117
    Reviewed-by: Laurent BP <laurent.balland-poirier at laposte.net>

diff --git a/chart2/qa/extras/chart2_trendcalculators.cxx b/chart2/qa/extras/chart2_trendcalculators.cxx
index 0c3b972..04af848 100644
--- a/chart2/qa/extras/chart2_trendcalculators.cxx
+++ b/chart2/qa/extras/chart2_trendcalculators.cxx
@@ -199,7 +199,7 @@ void Chart2TrendCalculators::testExponentialRegression2()
         xValues[i] = d;
         yValues[i] = -2.0 * exp ( 0.3 * d );
     }
-    checkCalculator( xValues, yValues, "f(x) = -2 exp( 0.3 x )");
+    checkCalculator( xValues, yValues, "f(x) = "+ aMinusSign + " 2 exp( 0.3 x )");
 }
 
 
diff --git a/chart2/source/tools/ExponentialRegressionCurveCalculator.cxx b/chart2/source/tools/ExponentialRegressionCurveCalculator.cxx
index 75e92aa..f6a7571 100644
--- a/chart2/source/tools/ExponentialRegressionCurveCalculator.cxx
+++ b/chart2/source/tools/ExponentialRegressionCurveCalculator.cxx
@@ -20,6 +20,7 @@
 #include "ExponentialRegressionCurveCalculator.hxx"
 #include "macros.hxx"
 #include "RegressionCalculationHelper.hxx"
+#include <SpecialUnicodes.hxx>
 
 #include <rtl/math.hxx>
 #include <rtl/ustrbuf.hxx>
@@ -154,40 +155,72 @@ uno::Sequence< geometry::RealPoint2D > SAL_CALL ExponentialRegressionCurveCalcul
 
 OUString ExponentialRegressionCurveCalculator::ImplGetRepresentation(
     const uno::Reference< util::XNumberFormatter >& xNumFormatter,
-    sal_Int32 nNumberFormatKey, sal_Int32* /*pFormulaLength = nullptr */ ) const
+    sal_Int32 nNumberFormatKey, sal_Int32* pFormulaMaxWidth /* = nullptr */ ) const
 {
-    double fIntercept = m_fSign * exp(m_fLogIntercept);
+    double fIntercept = exp(m_fLogIntercept);
     bool bHasSlope = !rtl::math::approxEqual( exp(m_fLogSlope), 1.0 );
     bool bHasLogSlope = !rtl::math::approxEqual( fabs(m_fLogSlope), 1.0 );
-    bool bHasIntercept = !rtl::math::approxEqual( m_fSign*fIntercept, 1.0 ) && fIntercept != 0.0;
+    bool bHasIntercept = !rtl::math::approxEqual( fIntercept, 1.0 ) && fIntercept != 0.0;
 
     OUStringBuffer aBuf( "f(x) = " );
-
+    sal_Int32 nLineLength = aBuf.getLength();
+    sal_Int32 nValueLength=0;
+    if ( pFormulaMaxWidth && *pFormulaMaxWidth > 0 )
+    {          // count characters different from coefficients
+        sal_Int32 nCharMin = nLineLength + 11;  // 11 = "exp( ", " x )" + 2 extra characters
+        if ( m_fSign < 0.0 )
+            nCharMin += 2;
+        if ( fIntercept == 0.0 || ( !bHasSlope && m_fLogIntercept != 0.0 ) )
+            nCharMin += 3; // " + " special case where equation is writen exp( a + b x )
+        if ( ( bHasIntercept || fIntercept == 0.0 || ( !bHasSlope && m_fLogIntercept != 0.0 ) ) &&
+               bHasLogSlope )
+            nValueLength = ( *pFormulaMaxWidth - nCharMin ) / 2;
+        else
+            nValueLength = *pFormulaMaxWidth - nCharMin;
+        if ( nValueLength <= 0 )
+            nValueLength = 1;
+    }
+                    // temporary buffer
+    OUStringBuffer aTmpBuf("");
+        // if nValueLength not calculated then nullptr
+    sal_Int32* pValueLength = nValueLength ? &nValueLength : nullptr;
+    if ( m_fSign < 0.0 )
+        aTmpBuf.append( aMinusSign + " " );
     if ( bHasIntercept )
     {
-        aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fIntercept) );
-        aBuf.append( " exp( " );
+        OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fIntercept, pValueLength );
+        if ( aValueString != "1" )  // aValueString may be rounded to 1 if nValueLength is small
+        {
+            aTmpBuf.append( aValueString + " " );
+            addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth );
+            aTmpBuf.truncate();
+        }
     }
-    else
+    aTmpBuf.append( "exp( " );
+    if ( !bHasIntercept )
     {
-       if ( m_fSign < 0.0 )
-            aBuf.append( "- " );
-       aBuf.append( "exp( " );
-       if ( fIntercept == 0.0 ||  // underflow, a true zero is impossible
-          ( !bHasSlope && m_fLogIntercept != 0.0 ) )    // show logarithmic output, if intercept and slope both are near one
+        if ( fIntercept == 0.0 ||  // underflow, a true zero is impossible
+           ( !bHasSlope && m_fLogIntercept != 0.0 ) )   // show logarithmic output, if intercept and slope both are near one
         {                                               // otherwise drop output of intercept, which is 1 here
-            aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogIntercept) );
-            aBuf.append( (m_fLogSlope < 0.0) ? OUStringLiteral(" ") : OUStringLiteral(" + "));
+            OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogIntercept, pValueLength );
+            if ( aValueString != "0" )  // aValueString may be rounded to 0 if nValueLength is small
+            {
+                aTmpBuf.append( aValueString + ( (m_fLogSlope < 0.0) ? OUStringBuffer(" ") : OUStringBuffer(" + ") ) );
+            }
         }
     }
     if ( m_fLogSlope < 0.0 )
-        aBuf.append( "- ");
+        aTmpBuf.append( aMinusSign + " " );
     if ( bHasLogSlope )
     {
-        aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fLogSlope)) );
-        aBuf.append( ' ' );
+        OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fLogSlope), pValueLength );
+        if ( aValueString != "1" )  // aValueString may be rounded to 1 if nValueLength is small
+        {
+            aTmpBuf.append( aValueString + " " );
+        }
     }
-    aBuf.append( "x )");
+    aTmpBuf.append( "x )");
+    addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth );
 
     return aBuf.makeStringAndClear();
 }


More information about the Libreoffice-commits mailing list