[Libreoffice-commits] core.git: chart2/qa chart2/source

Laurent Balland-Poirier laurent.balland-poirier at laposte.net
Thu Jun 9 11:01:18 UTC 2016


 chart2/qa/extras/chart2_trendcalculators.cxx               |    2 
 chart2/source/tools/PotentialRegressionCurveCalculator.cxx |   49 +++++++++----
 2 files changed, 37 insertions(+), 14 deletions(-)

New commits:
commit e420a335f783bb4d2ee9d74d56f91e16d189566f
Author: Laurent Balland-Poirier <laurent.balland-poirier at laposte.net>
Date:   Tue May 24 21:40:57 2016 +0200

    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 at libreoffice.org>
    Reviewed-by: jan iversen <jani at documentfoundation.org>

diff --git a/chart2/qa/extras/chart2_trendcalculators.cxx b/chart2/qa/extras/chart2_trendcalculators.cxx
index 04af848..ca327f1 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 8ea92c1..3c19a3e 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();


More information about the Libreoffice-commits mailing list