1*cde9e8dcSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*cde9e8dcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*cde9e8dcSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*cde9e8dcSAndrew Rist * distributed with this work for additional information 6*cde9e8dcSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*cde9e8dcSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*cde9e8dcSAndrew Rist * "License"); you may not use this file except in compliance 9*cde9e8dcSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*cde9e8dcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*cde9e8dcSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*cde9e8dcSAndrew Rist * software distributed under the License is distributed on an 15*cde9e8dcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*cde9e8dcSAndrew Rist * KIND, either express or implied. See the License for the 17*cde9e8dcSAndrew Rist * specific language governing permissions and limitations 18*cde9e8dcSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*cde9e8dcSAndrew Rist *************************************************************/ 21*cde9e8dcSAndrew Rist 22*cde9e8dcSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_chart2.hxx" 26cdf0e10cSrcweir #include "RegressionCurveHelper.hxx" 27cdf0e10cSrcweir #include "MeanValueRegressionCurveCalculator.hxx" 28cdf0e10cSrcweir #include "LinearRegressionCurveCalculator.hxx" 29cdf0e10cSrcweir #include "LogarithmicRegressionCurveCalculator.hxx" 30cdf0e10cSrcweir #include "ExponentialRegressionCurveCalculator.hxx" 31cdf0e10cSrcweir #include "PotentialRegressionCurveCalculator.hxx" 32cdf0e10cSrcweir #include "CommonConverters.hxx" 33cdf0e10cSrcweir #include "RegressionCurveModel.hxx" 34cdf0e10cSrcweir #include "ChartTypeHelper.hxx" 35cdf0e10cSrcweir #include "ChartModelHelper.hxx" 36cdf0e10cSrcweir #include "macros.hxx" 37cdf0e10cSrcweir #include "PropertyHelper.hxx" 38cdf0e10cSrcweir #include "ResId.hxx" 39cdf0e10cSrcweir #include "Strings.hrc" 40cdf0e10cSrcweir #include "DiagramHelper.hxx" 41cdf0e10cSrcweir #include <com/sun/star/chart2/XChartDocument.hpp> 42cdf0e10cSrcweir 43cdf0e10cSrcweir using namespace ::com::sun::star; 44cdf0e10cSrcweir using namespace ::com::sun::star::chart2; 45cdf0e10cSrcweir 46cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 47cdf0e10cSrcweir using ::com::sun::star::uno::Sequence; 48cdf0e10cSrcweir using ::com::sun::star::uno::XComponentContext; 49cdf0e10cSrcweir using ::com::sun::star::lang::XServiceName; 50cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySet; 51cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 52cdf0e10cSrcweir using ::rtl::OUString; 53cdf0e10cSrcweir 54cdf0e10cSrcweir namespace 55cdf0e10cSrcweir { 56cdf0e10cSrcweir OUString lcl_getServiceNameForType( ::chart::RegressionCurveHelper::tRegressionType eType ) 57cdf0e10cSrcweir { 58cdf0e10cSrcweir OUString aServiceName; 59cdf0e10cSrcweir switch( eType ) 60cdf0e10cSrcweir { 61cdf0e10cSrcweir case ::chart::RegressionCurveHelper::REGRESSION_TYPE_LINEAR: 62cdf0e10cSrcweir aServiceName = C2U( "com.sun.star.chart2.LinearRegressionCurve" ); 63cdf0e10cSrcweir break; 64cdf0e10cSrcweir case ::chart::RegressionCurveHelper::REGRESSION_TYPE_LOG: 65cdf0e10cSrcweir aServiceName = C2U( "com.sun.star.chart2.LogarithmicRegressionCurve" ); 66cdf0e10cSrcweir break; 67cdf0e10cSrcweir case ::chart::RegressionCurveHelper::REGRESSION_TYPE_EXP: 68cdf0e10cSrcweir aServiceName = C2U( "com.sun.star.chart2.ExponentialRegressionCurve" ); 69cdf0e10cSrcweir break; 70cdf0e10cSrcweir case ::chart::RegressionCurveHelper::REGRESSION_TYPE_POWER: 71cdf0e10cSrcweir aServiceName = C2U( "com.sun.star.chart2.PotentialRegressionCurve" ); 72cdf0e10cSrcweir break; 73cdf0e10cSrcweir default: 74cdf0e10cSrcweir OSL_ENSURE(false,"unknown regression curve type - use linear instead"); 75cdf0e10cSrcweir aServiceName = C2U( "com.sun.star.chart2.LinearRegressionCurve" ); 76cdf0e10cSrcweir break; 77cdf0e10cSrcweir } 78cdf0e10cSrcweir return aServiceName; 79cdf0e10cSrcweir } 80cdf0e10cSrcweir } // anonymous namespace 81cdf0e10cSrcweir 82cdf0e10cSrcweir //............................................................................. 83cdf0e10cSrcweir namespace chart 84cdf0e10cSrcweir { 85cdf0e10cSrcweir //............................................................................. 86cdf0e10cSrcweir 87cdf0e10cSrcweir Reference< XRegressionCurve > RegressionCurveHelper::createMeanValueLine( 88cdf0e10cSrcweir const Reference< XComponentContext > & xContext ) 89cdf0e10cSrcweir { 90cdf0e10cSrcweir return Reference< XRegressionCurve >( 91cdf0e10cSrcweir new MeanValueRegressionCurve( xContext )); 92cdf0e10cSrcweir } 93cdf0e10cSrcweir 94cdf0e10cSrcweir Reference< XRegressionCurve > RegressionCurveHelper::createRegressionCurveByServiceName( 95cdf0e10cSrcweir const Reference< XComponentContext > & xContext, 96cdf0e10cSrcweir ::rtl::OUString aServiceName ) 97cdf0e10cSrcweir { 98cdf0e10cSrcweir Reference< XRegressionCurve > xResult; 99cdf0e10cSrcweir 100cdf0e10cSrcweir // todo: use factory methods with service name 101cdf0e10cSrcweir if( aServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( 102cdf0e10cSrcweir "com.sun.star.chart2.LinearRegressionCurve" ))) 103cdf0e10cSrcweir { 104cdf0e10cSrcweir xResult.set( 105cdf0e10cSrcweir new LinearRegressionCurve( xContext )); 106cdf0e10cSrcweir } 107cdf0e10cSrcweir else if( aServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( 108cdf0e10cSrcweir "com.sun.star.chart2.LogarithmicRegressionCurve" ))) 109cdf0e10cSrcweir { 110cdf0e10cSrcweir xResult.set( 111cdf0e10cSrcweir new LogarithmicRegressionCurve( xContext )); 112cdf0e10cSrcweir } 113cdf0e10cSrcweir else if( aServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( 114cdf0e10cSrcweir "com.sun.star.chart2.ExponentialRegressionCurve" ))) 115cdf0e10cSrcweir { 116cdf0e10cSrcweir xResult.set( 117cdf0e10cSrcweir new ExponentialRegressionCurve( xContext )); 118cdf0e10cSrcweir } 119cdf0e10cSrcweir else if( aServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( 120cdf0e10cSrcweir "com.sun.star.chart2.PotentialRegressionCurve" ))) 121cdf0e10cSrcweir { 122cdf0e10cSrcweir xResult.set( 123cdf0e10cSrcweir new PotentialRegressionCurve( xContext )); 124cdf0e10cSrcweir } 125cdf0e10cSrcweir 126cdf0e10cSrcweir return xResult; 127cdf0e10cSrcweir } 128cdf0e10cSrcweir 129cdf0e10cSrcweir // ------------------------------------------------------------ 130cdf0e10cSrcweir 131cdf0e10cSrcweir Reference< XRegressionCurveCalculator > RegressionCurveHelper::createRegressionCurveCalculatorByServiceName( 132cdf0e10cSrcweir ::rtl::OUString aServiceName ) 133cdf0e10cSrcweir { 134cdf0e10cSrcweir Reference< XRegressionCurveCalculator > xResult; 135cdf0e10cSrcweir 136cdf0e10cSrcweir // todo: use factory methods with service name 137cdf0e10cSrcweir if( aServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( 138cdf0e10cSrcweir "com.sun.star.chart2.MeanValueRegressionCurve" ))) 139cdf0e10cSrcweir { 140cdf0e10cSrcweir xResult.set( new MeanValueRegressionCurveCalculator()); 141cdf0e10cSrcweir } 142cdf0e10cSrcweir if( aServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( 143cdf0e10cSrcweir "com.sun.star.chart2.LinearRegressionCurve" ))) 144cdf0e10cSrcweir { 145cdf0e10cSrcweir xResult.set( new LinearRegressionCurveCalculator()); 146cdf0e10cSrcweir } 147cdf0e10cSrcweir else if( aServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( 148cdf0e10cSrcweir "com.sun.star.chart2.LogarithmicRegressionCurve" ))) 149cdf0e10cSrcweir { 150cdf0e10cSrcweir xResult.set( new LogarithmicRegressionCurveCalculator()); 151cdf0e10cSrcweir } 152cdf0e10cSrcweir else if( aServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( 153cdf0e10cSrcweir "com.sun.star.chart2.ExponentialRegressionCurve" ))) 154cdf0e10cSrcweir { 155cdf0e10cSrcweir xResult.set( new ExponentialRegressionCurveCalculator()); 156cdf0e10cSrcweir } 157cdf0e10cSrcweir else if( aServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( 158cdf0e10cSrcweir "com.sun.star.chart2.PotentialRegressionCurve" ))) 159cdf0e10cSrcweir { 160cdf0e10cSrcweir xResult.set( new PotentialRegressionCurveCalculator()); 161cdf0e10cSrcweir } 162cdf0e10cSrcweir 163cdf0e10cSrcweir return xResult; 164cdf0e10cSrcweir } 165cdf0e10cSrcweir 166cdf0e10cSrcweir void RegressionCurveHelper::initializeCurveCalculator( 167cdf0e10cSrcweir const Reference< XRegressionCurveCalculator > & xOutCurveCalculator, 168cdf0e10cSrcweir const Reference< data::XDataSource > & xSource, 169cdf0e10cSrcweir bool bUseXValuesIfAvailable /* = true */ ) 170cdf0e10cSrcweir { 171cdf0e10cSrcweir if( ! (xOutCurveCalculator.is() && 172cdf0e10cSrcweir xSource.is() )) 173cdf0e10cSrcweir return; 174cdf0e10cSrcweir 175cdf0e10cSrcweir Sequence< double > aXValues, aYValues; 176cdf0e10cSrcweir bool bXValuesFound = false, bYValuesFound = false; 177cdf0e10cSrcweir 178cdf0e10cSrcweir Sequence< Reference< data::XLabeledDataSequence > > aDataSeqs( xSource->getDataSequences()); 179cdf0e10cSrcweir sal_Int32 i = 0; 180cdf0e10cSrcweir for( i=0; 181cdf0e10cSrcweir ! (bXValuesFound && bYValuesFound) && i<aDataSeqs.getLength(); 182cdf0e10cSrcweir ++i ) 183cdf0e10cSrcweir { 184cdf0e10cSrcweir try 185cdf0e10cSrcweir { 186cdf0e10cSrcweir Reference< data::XDataSequence > xSeq( aDataSeqs[i]->getValues()); 187cdf0e10cSrcweir Reference< XPropertySet > xProp( xSeq, uno::UNO_QUERY_THROW ); 188cdf0e10cSrcweir ::rtl::OUString aRole; 189cdf0e10cSrcweir if( xProp->getPropertyValue( 190cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Role" ))) >>= aRole ) 191cdf0e10cSrcweir { 192cdf0e10cSrcweir if( bUseXValuesIfAvailable && 193cdf0e10cSrcweir ! bXValuesFound && 194cdf0e10cSrcweir aRole.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "values-x" ))) 195cdf0e10cSrcweir { 196cdf0e10cSrcweir aXValues = DataSequenceToDoubleSequence( xSeq ); 197cdf0e10cSrcweir bXValuesFound = true; 198cdf0e10cSrcweir } 199cdf0e10cSrcweir else if( ! bYValuesFound && 200cdf0e10cSrcweir aRole.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "values-y" ))) 201cdf0e10cSrcweir { 202cdf0e10cSrcweir aYValues = DataSequenceToDoubleSequence( xSeq ); 203cdf0e10cSrcweir bYValuesFound = true; 204cdf0e10cSrcweir } 205cdf0e10cSrcweir } 206cdf0e10cSrcweir } 207cdf0e10cSrcweir catch( Exception & ex ) 208cdf0e10cSrcweir { 209cdf0e10cSrcweir ASSERT_EXCEPTION( ex ); 210cdf0e10cSrcweir } 211cdf0e10cSrcweir } 212cdf0e10cSrcweir 213cdf0e10cSrcweir if( ! bXValuesFound && 214cdf0e10cSrcweir bYValuesFound ) 215cdf0e10cSrcweir { 216cdf0e10cSrcweir // initialize with 1, 2, ... 217cdf0e10cSrcweir //first category (index 0) matches with real number 1.0 218cdf0e10cSrcweir aXValues.realloc( aYValues.getLength()); 219cdf0e10cSrcweir for( i=0; i<aXValues.getLength(); ++i ) 220cdf0e10cSrcweir aXValues[i] = i+1; 221cdf0e10cSrcweir bXValuesFound = true; 222cdf0e10cSrcweir } 223cdf0e10cSrcweir 224cdf0e10cSrcweir if( bXValuesFound && bYValuesFound && 225cdf0e10cSrcweir aXValues.getLength() > 0 && 226cdf0e10cSrcweir aYValues.getLength() > 0 ) 227cdf0e10cSrcweir xOutCurveCalculator->recalculateRegression( aXValues, aYValues ); 228cdf0e10cSrcweir } 229cdf0e10cSrcweir 230cdf0e10cSrcweir void RegressionCurveHelper::initializeCurveCalculator( 231cdf0e10cSrcweir const Reference< XRegressionCurveCalculator > & xOutCurveCalculator, 232cdf0e10cSrcweir const Reference< XDataSeries > & xSeries, 233cdf0e10cSrcweir const Reference< frame::XModel > & xModel ) 234cdf0e10cSrcweir { 235cdf0e10cSrcweir sal_Int32 nAxisType = ChartTypeHelper::getAxisType( 236cdf0e10cSrcweir ChartModelHelper::getChartTypeOfSeries( xModel, xSeries ), 0 ); // x-axis 237cdf0e10cSrcweir 238cdf0e10cSrcweir initializeCurveCalculator( xOutCurveCalculator, 239cdf0e10cSrcweir uno::Reference< data::XDataSource >( xSeries, uno::UNO_QUERY ), 240cdf0e10cSrcweir (nAxisType == AxisType::REALNUMBER) ); 241cdf0e10cSrcweir } 242cdf0e10cSrcweir 243cdf0e10cSrcweir // ---------------------------------------- 244cdf0e10cSrcweir 245cdf0e10cSrcweir bool RegressionCurveHelper::hasMeanValueLine( 246cdf0e10cSrcweir const uno::Reference< XRegressionCurveContainer > & xRegCnt ) 247cdf0e10cSrcweir { 248cdf0e10cSrcweir if( !xRegCnt.is()) 249cdf0e10cSrcweir return false; 250cdf0e10cSrcweir 251cdf0e10cSrcweir try 252cdf0e10cSrcweir { 253cdf0e10cSrcweir uno::Sequence< uno::Reference< XRegressionCurve > > aCurves( 254cdf0e10cSrcweir xRegCnt->getRegressionCurves()); 255cdf0e10cSrcweir for( sal_Int32 i = 0; i < aCurves.getLength(); ++i ) 256cdf0e10cSrcweir { 257cdf0e10cSrcweir if( isMeanValueLine( aCurves[i] )) 258cdf0e10cSrcweir return true; 259cdf0e10cSrcweir } 260cdf0e10cSrcweir } 261cdf0e10cSrcweir catch( Exception & ex ) 262cdf0e10cSrcweir { 263cdf0e10cSrcweir ASSERT_EXCEPTION( ex ); 264cdf0e10cSrcweir } 265cdf0e10cSrcweir 266cdf0e10cSrcweir return false; 267cdf0e10cSrcweir } 268cdf0e10cSrcweir 269cdf0e10cSrcweir bool RegressionCurveHelper::isMeanValueLine( 270cdf0e10cSrcweir const uno::Reference< chart2::XRegressionCurve > & xRegCurve ) 271cdf0e10cSrcweir { 272cdf0e10cSrcweir uno::Reference< XServiceName > xServName( xRegCurve, uno::UNO_QUERY ); 273cdf0e10cSrcweir if( xServName.is() && 274cdf0e10cSrcweir xServName->getServiceName().equals( 275cdf0e10cSrcweir C2U( "com.sun.star.chart2.MeanValueRegressionCurve" ))) 276cdf0e10cSrcweir return true; 277cdf0e10cSrcweir return false; 278cdf0e10cSrcweir } 279cdf0e10cSrcweir 280cdf0e10cSrcweir uno::Reference< chart2::XRegressionCurve > 281cdf0e10cSrcweir RegressionCurveHelper::getMeanValueLine( 282cdf0e10cSrcweir const uno::Reference< chart2::XRegressionCurveContainer > & xRegCnt ) 283cdf0e10cSrcweir { 284cdf0e10cSrcweir if( xRegCnt.is()) 285cdf0e10cSrcweir { 286cdf0e10cSrcweir try 287cdf0e10cSrcweir { 288cdf0e10cSrcweir uno::Sequence< uno::Reference< XRegressionCurve > > aCurves( 289cdf0e10cSrcweir xRegCnt->getRegressionCurves()); 290cdf0e10cSrcweir for( sal_Int32 i = 0; i < aCurves.getLength(); ++i ) 291cdf0e10cSrcweir { 292cdf0e10cSrcweir if( isMeanValueLine( aCurves[i] )) 293cdf0e10cSrcweir return aCurves[i]; 294cdf0e10cSrcweir } 295cdf0e10cSrcweir } 296cdf0e10cSrcweir catch( Exception & ex ) 297cdf0e10cSrcweir { 298cdf0e10cSrcweir ASSERT_EXCEPTION( ex ); 299cdf0e10cSrcweir } 300cdf0e10cSrcweir } 301cdf0e10cSrcweir 302cdf0e10cSrcweir return uno::Reference< chart2::XRegressionCurve >(); 303cdf0e10cSrcweir } 304cdf0e10cSrcweir 305cdf0e10cSrcweir void RegressionCurveHelper::addMeanValueLine( 306cdf0e10cSrcweir uno::Reference< XRegressionCurveContainer > & xRegCnt, 307cdf0e10cSrcweir const uno::Reference< XComponentContext > & xContext, 308cdf0e10cSrcweir const uno::Reference< XPropertySet > & xSeriesProp ) 309cdf0e10cSrcweir { 310cdf0e10cSrcweir if( !xRegCnt.is() || 311cdf0e10cSrcweir ::chart::RegressionCurveHelper::hasMeanValueLine( xRegCnt ) ) 312cdf0e10cSrcweir return; 313cdf0e10cSrcweir 314cdf0e10cSrcweir // todo: use a valid context 315cdf0e10cSrcweir uno::Reference< XRegressionCurve > xCurve( createMeanValueLine( xContext )); 316cdf0e10cSrcweir xRegCnt->addRegressionCurve( xCurve ); 317cdf0e10cSrcweir 318cdf0e10cSrcweir if( xSeriesProp.is()) 319cdf0e10cSrcweir { 320cdf0e10cSrcweir uno::Reference< XPropertySet > xProp( xCurve, uno::UNO_QUERY ); 321cdf0e10cSrcweir if( xProp.is()) 322cdf0e10cSrcweir { 323cdf0e10cSrcweir xProp->setPropertyValue( C2U( "LineColor" ), 324cdf0e10cSrcweir xSeriesProp->getPropertyValue( C2U( "Color" ))); 325cdf0e10cSrcweir } 326cdf0e10cSrcweir } 327cdf0e10cSrcweir } 328cdf0e10cSrcweir 329cdf0e10cSrcweir void RegressionCurveHelper::removeMeanValueLine( 330cdf0e10cSrcweir Reference< XRegressionCurveContainer > & xRegCnt ) 331cdf0e10cSrcweir { 332cdf0e10cSrcweir if( !xRegCnt.is()) 333cdf0e10cSrcweir return; 334cdf0e10cSrcweir 335cdf0e10cSrcweir try 336cdf0e10cSrcweir { 337cdf0e10cSrcweir Sequence< Reference< XRegressionCurve > > aCurves( 338cdf0e10cSrcweir xRegCnt->getRegressionCurves()); 339cdf0e10cSrcweir for( sal_Int32 i = 0; i < aCurves.getLength(); ++i ) 340cdf0e10cSrcweir { 341cdf0e10cSrcweir if( isMeanValueLine( aCurves[i] )) 342cdf0e10cSrcweir { 343cdf0e10cSrcweir xRegCnt->removeRegressionCurve( aCurves[i] ); 344cdf0e10cSrcweir // attention: the iterator i has become invalid now 345cdf0e10cSrcweir 346cdf0e10cSrcweir // note: assume that there is only one mean-value curve 347cdf0e10cSrcweir // to remove multiple mean-value curves remove the break 348cdf0e10cSrcweir break; 349cdf0e10cSrcweir } 350cdf0e10cSrcweir } 351cdf0e10cSrcweir } 352cdf0e10cSrcweir catch( Exception & ex ) 353cdf0e10cSrcweir { 354cdf0e10cSrcweir ASSERT_EXCEPTION( ex ); 355cdf0e10cSrcweir } 356cdf0e10cSrcweir } 357cdf0e10cSrcweir 358cdf0e10cSrcweir void RegressionCurveHelper::addRegressionCurve( 359cdf0e10cSrcweir tRegressionType eType, 360cdf0e10cSrcweir uno::Reference< XRegressionCurveContainer > & xRegCnt, 361cdf0e10cSrcweir const uno::Reference< XComponentContext > & /* xContext */, 362cdf0e10cSrcweir const uno::Reference< beans::XPropertySet >& xPropertySource, 363cdf0e10cSrcweir const uno::Reference< beans::XPropertySet >& xEquationProperties ) 364cdf0e10cSrcweir { 365cdf0e10cSrcweir if( !xRegCnt.is() ) 366cdf0e10cSrcweir return; 367cdf0e10cSrcweir 368cdf0e10cSrcweir if( eType == REGRESSION_TYPE_NONE ) 369cdf0e10cSrcweir { 370cdf0e10cSrcweir OSL_ENSURE(false,"don't create a regression curve of type none"); 371cdf0e10cSrcweir return; 372cdf0e10cSrcweir } 373cdf0e10cSrcweir 374cdf0e10cSrcweir uno::Reference< chart2::XRegressionCurve > xCurve; 375cdf0e10cSrcweir ::rtl::OUString aServiceName( lcl_getServiceNameForType( eType )); 376cdf0e10cSrcweir 377cdf0e10cSrcweir if( aServiceName.getLength()) 378cdf0e10cSrcweir { 379cdf0e10cSrcweir // todo: use a valid context 380cdf0e10cSrcweir xCurve.set( createRegressionCurveByServiceName( 381cdf0e10cSrcweir uno::Reference< uno::XComponentContext >(), aServiceName )); 382cdf0e10cSrcweir 383cdf0e10cSrcweir if( xEquationProperties.is()) 384cdf0e10cSrcweir xCurve->setEquationProperties( xEquationProperties ); 385cdf0e10cSrcweir 386cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xProp( xCurve, uno::UNO_QUERY ); 387cdf0e10cSrcweir if( xProp.is()) 388cdf0e10cSrcweir { 389cdf0e10cSrcweir if( xPropertySource.is()) 390cdf0e10cSrcweir comphelper::copyProperties( xPropertySource, xProp ); 391cdf0e10cSrcweir else 392cdf0e10cSrcweir { 393cdf0e10cSrcweir uno::Reference< XPropertySet > xSeriesProp( xRegCnt, uno::UNO_QUERY ); 394cdf0e10cSrcweir if( xSeriesProp.is()) 395cdf0e10cSrcweir { 396cdf0e10cSrcweir xProp->setPropertyValue( C2U( "LineColor" ), 397cdf0e10cSrcweir xSeriesProp->getPropertyValue( C2U( "Color" ))); 398cdf0e10cSrcweir } 399cdf0e10cSrcweir // xProp->setPropertyValue( C2U( "LineWidth" ), uno::makeAny( sal_Int32( 100 ))); 400cdf0e10cSrcweir } 401cdf0e10cSrcweir } 402cdf0e10cSrcweir } 403cdf0e10cSrcweir xRegCnt->addRegressionCurve( xCurve ); 404cdf0e10cSrcweir } 405cdf0e10cSrcweir 406cdf0e10cSrcweir /** removes all regression curves that are not of type mean value 407cdf0e10cSrcweir and returns true, if anything was removed 408cdf0e10cSrcweir */ 409cdf0e10cSrcweir bool RegressionCurveHelper::removeAllExceptMeanValueLine( 410cdf0e10cSrcweir uno::Reference< chart2::XRegressionCurveContainer > & xRegCnt ) 411cdf0e10cSrcweir { 412cdf0e10cSrcweir bool bRemovedSomething = false; 413cdf0e10cSrcweir if( xRegCnt.is()) 414cdf0e10cSrcweir { 415cdf0e10cSrcweir try 416cdf0e10cSrcweir { 417cdf0e10cSrcweir uno::Sequence< uno::Reference< chart2::XRegressionCurve > > aCurves( 418cdf0e10cSrcweir xRegCnt->getRegressionCurves()); 419cdf0e10cSrcweir ::std::vector< uno::Reference< chart2::XRegressionCurve > > aCurvesToDelete; 420cdf0e10cSrcweir for( sal_Int32 i = 0; i < aCurves.getLength(); ++i ) 421cdf0e10cSrcweir { 422cdf0e10cSrcweir if( ! isMeanValueLine( aCurves[i] )) 423cdf0e10cSrcweir { 424cdf0e10cSrcweir aCurvesToDelete.push_back( aCurves[ i ] ); 425cdf0e10cSrcweir } 426cdf0e10cSrcweir } 427cdf0e10cSrcweir 428cdf0e10cSrcweir for( ::std::vector< uno::Reference< chart2::XRegressionCurve > >::const_iterator aIt = aCurvesToDelete.begin(); 429cdf0e10cSrcweir aIt != aCurvesToDelete.end(); ++aIt ) 430cdf0e10cSrcweir { 431cdf0e10cSrcweir xRegCnt->removeRegressionCurve( *aIt ); 432cdf0e10cSrcweir bRemovedSomething = true; 433cdf0e10cSrcweir } 434cdf0e10cSrcweir } 435cdf0e10cSrcweir catch( uno::Exception & ex ) 436cdf0e10cSrcweir { 437cdf0e10cSrcweir ASSERT_EXCEPTION( ex ); 438cdf0e10cSrcweir } 439cdf0e10cSrcweir } 440cdf0e10cSrcweir return bRemovedSomething; 441cdf0e10cSrcweir } 442cdf0e10cSrcweir 443cdf0e10cSrcweir void RegressionCurveHelper::removeEquations( 444cdf0e10cSrcweir uno::Reference< chart2::XRegressionCurveContainer > & xRegCnt ) 445cdf0e10cSrcweir { 446cdf0e10cSrcweir if( xRegCnt.is()) 447cdf0e10cSrcweir { 448cdf0e10cSrcweir try 449cdf0e10cSrcweir { 450cdf0e10cSrcweir uno::Sequence< uno::Reference< chart2::XRegressionCurve > > aCurves( 451cdf0e10cSrcweir xRegCnt->getRegressionCurves()); 452cdf0e10cSrcweir for( sal_Int32 i = 0; i < aCurves.getLength(); ++i ) 453cdf0e10cSrcweir { 454cdf0e10cSrcweir if( !isMeanValueLine( aCurves[i] ) ) 455cdf0e10cSrcweir { 456cdf0e10cSrcweir uno::Reference< chart2::XRegressionCurve > xRegCurve( aCurves[ i ] ); 457cdf0e10cSrcweir if( xRegCurve.is() ) 458cdf0e10cSrcweir { 459cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xEqProp( xRegCurve->getEquationProperties() ) ; 460cdf0e10cSrcweir if( xEqProp.is()) 461cdf0e10cSrcweir { 462cdf0e10cSrcweir xEqProp->setPropertyValue( C2U("ShowEquation"), uno::makeAny( false )); 463cdf0e10cSrcweir xEqProp->setPropertyValue( C2U("ShowCorrelationCoefficient"), uno::makeAny( false )); 464cdf0e10cSrcweir } 465cdf0e10cSrcweir } 466cdf0e10cSrcweir } 467cdf0e10cSrcweir } 468cdf0e10cSrcweir } 469cdf0e10cSrcweir catch( uno::Exception & ex ) 470cdf0e10cSrcweir { 471cdf0e10cSrcweir ASSERT_EXCEPTION( ex ); 472cdf0e10cSrcweir } 473cdf0e10cSrcweir } 474cdf0e10cSrcweir } 475cdf0e10cSrcweir 476cdf0e10cSrcweir void RegressionCurveHelper::replaceOrAddCurveAndReduceToOne( 477cdf0e10cSrcweir tRegressionType eType, 478cdf0e10cSrcweir uno::Reference< XRegressionCurveContainer > & xRegCnt, 479cdf0e10cSrcweir const uno::Reference< XComponentContext > & xContext ) 480cdf0e10cSrcweir { 481cdf0e10cSrcweir uno::Reference< chart2::XRegressionCurve > xRegressionCurve( getFirstCurveNotMeanValueLine( xRegCnt )); 482cdf0e10cSrcweir if( ! xRegressionCurve.is()) 483cdf0e10cSrcweir RegressionCurveHelper::addRegressionCurve( eType, xRegCnt, xContext ); 484cdf0e10cSrcweir else 485cdf0e10cSrcweir { 486cdf0e10cSrcweir OUString aServiceName( lcl_getServiceNameForType( eType )); 487cdf0e10cSrcweir if( aServiceName.getLength()) 488cdf0e10cSrcweir { 489cdf0e10cSrcweir RegressionCurveHelper::removeAllExceptMeanValueLine( xRegCnt ); 490cdf0e10cSrcweir RegressionCurveHelper::addRegressionCurve( 491cdf0e10cSrcweir eType, xRegCnt, xContext, 492cdf0e10cSrcweir Reference< beans::XPropertySet >( xRegressionCurve, uno::UNO_QUERY ), 493cdf0e10cSrcweir xRegressionCurve->getEquationProperties()); 494cdf0e10cSrcweir } 495cdf0e10cSrcweir } 496cdf0e10cSrcweir } 497cdf0e10cSrcweir 498cdf0e10cSrcweir uno::Reference< chart2::XRegressionCurve > RegressionCurveHelper::getFirstCurveNotMeanValueLine( 499cdf0e10cSrcweir const Reference< XRegressionCurveContainer > & xRegCnt ) 500cdf0e10cSrcweir { 501cdf0e10cSrcweir if( !xRegCnt.is()) 502cdf0e10cSrcweir return NULL; 503cdf0e10cSrcweir 504cdf0e10cSrcweir try 505cdf0e10cSrcweir { 506cdf0e10cSrcweir uno::Sequence< uno::Reference< chart2::XRegressionCurve > > aCurves( 507cdf0e10cSrcweir xRegCnt->getRegressionCurves()); 508cdf0e10cSrcweir ::std::vector< uno::Reference< chart2::XRegressionCurve > > aCurvesToDelete; 509cdf0e10cSrcweir for( sal_Int32 i = 0; i < aCurves.getLength(); ++i ) 510cdf0e10cSrcweir { 511cdf0e10cSrcweir if( ! isMeanValueLine( aCurves[i] )) 512cdf0e10cSrcweir { 513cdf0e10cSrcweir return aCurves[ i ]; 514cdf0e10cSrcweir } 515cdf0e10cSrcweir } 516cdf0e10cSrcweir } 517cdf0e10cSrcweir catch( Exception & ex ) 518cdf0e10cSrcweir { 519cdf0e10cSrcweir ASSERT_EXCEPTION( ex ); 520cdf0e10cSrcweir } 521cdf0e10cSrcweir 522cdf0e10cSrcweir return NULL; 523cdf0e10cSrcweir } 524cdf0e10cSrcweir 525cdf0e10cSrcweir RegressionCurveHelper::tRegressionType RegressionCurveHelper::getRegressionType( 526cdf0e10cSrcweir const Reference< XRegressionCurve > & xCurve ) 527cdf0e10cSrcweir { 528cdf0e10cSrcweir tRegressionType eResult = REGRESSION_TYPE_UNKNOWN; 529cdf0e10cSrcweir 530cdf0e10cSrcweir try 531cdf0e10cSrcweir { 532cdf0e10cSrcweir Reference< lang::XServiceName > xServName( xCurve, uno::UNO_QUERY ); 533cdf0e10cSrcweir if( xServName.is()) 534cdf0e10cSrcweir { 535cdf0e10cSrcweir ::rtl::OUString aServiceName( xServName->getServiceName() ); 536cdf0e10cSrcweir 537cdf0e10cSrcweir if( aServiceName.equalsAsciiL( 538cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM( "com.sun.star.chart2.LinearRegressionCurve" ))) 539cdf0e10cSrcweir { 540cdf0e10cSrcweir eResult = REGRESSION_TYPE_LINEAR; 541cdf0e10cSrcweir } 542cdf0e10cSrcweir else if( aServiceName.equalsAsciiL( 543cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM( "com.sun.star.chart2.LogarithmicRegressionCurve" ))) 544cdf0e10cSrcweir { 545cdf0e10cSrcweir eResult = REGRESSION_TYPE_LOG; 546cdf0e10cSrcweir } 547cdf0e10cSrcweir else if( aServiceName.equalsAsciiL( 548cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM( "com.sun.star.chart2.ExponentialRegressionCurve" ))) 549cdf0e10cSrcweir { 550cdf0e10cSrcweir eResult = REGRESSION_TYPE_EXP; 551cdf0e10cSrcweir } 552cdf0e10cSrcweir else if( aServiceName.equalsAsciiL( 553cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM( "com.sun.star.chart2.PotentialRegressionCurve" ))) 554cdf0e10cSrcweir { 555cdf0e10cSrcweir eResult = REGRESSION_TYPE_POWER; 556cdf0e10cSrcweir } 557cdf0e10cSrcweir else if( aServiceName.equalsAsciiL( 558cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM( "com.sun.star.chart2.MeanValueRegressionCurve" ))) 559cdf0e10cSrcweir { 560cdf0e10cSrcweir eResult = REGRESSION_TYPE_MEAN_VALUE; 561cdf0e10cSrcweir } 562cdf0e10cSrcweir } 563cdf0e10cSrcweir } 564cdf0e10cSrcweir catch( Exception & ex ) 565cdf0e10cSrcweir { 566cdf0e10cSrcweir ASSERT_EXCEPTION( ex ); 567cdf0e10cSrcweir } 568cdf0e10cSrcweir 569cdf0e10cSrcweir return eResult; 570cdf0e10cSrcweir } 571cdf0e10cSrcweir 572cdf0e10cSrcweir RegressionCurveHelper::tRegressionType RegressionCurveHelper::getFirstRegressTypeNotMeanValueLine( 573cdf0e10cSrcweir const Reference< XRegressionCurveContainer > & xRegCnt ) 574cdf0e10cSrcweir { 575cdf0e10cSrcweir tRegressionType eResult = REGRESSION_TYPE_NONE; 576cdf0e10cSrcweir 577cdf0e10cSrcweir if( xRegCnt.is()) 578cdf0e10cSrcweir { 579cdf0e10cSrcweir Sequence< Reference< XRegressionCurve > > aCurves( 580cdf0e10cSrcweir xRegCnt->getRegressionCurves()); 581cdf0e10cSrcweir for( sal_Int32 i = 0; i < aCurves.getLength(); ++i ) 582cdf0e10cSrcweir { 583cdf0e10cSrcweir tRegressionType eType = getRegressionType( aCurves[i] ); 584cdf0e10cSrcweir if( eType != REGRESSION_TYPE_MEAN_VALUE && 585cdf0e10cSrcweir eType != REGRESSION_TYPE_UNKNOWN ) 586cdf0e10cSrcweir { 587cdf0e10cSrcweir eResult = eType; 588cdf0e10cSrcweir break; 589cdf0e10cSrcweir } 590cdf0e10cSrcweir } 591cdf0e10cSrcweir } 592cdf0e10cSrcweir 593cdf0e10cSrcweir return eResult; 594cdf0e10cSrcweir } 595cdf0e10cSrcweir 596cdf0e10cSrcweir OUString RegressionCurveHelper::getUINameForRegressionCurve( const Reference< XRegressionCurve >& xRegressionCurve ) 597cdf0e10cSrcweir { 598cdf0e10cSrcweir OUString aResult; 599cdf0e10cSrcweir Reference< lang::XServiceName > xServiceName( xRegressionCurve, uno::UNO_QUERY ); 600cdf0e10cSrcweir if( ! xServiceName.is()) 601cdf0e10cSrcweir return aResult; 602cdf0e10cSrcweir 603cdf0e10cSrcweir OUString aServiceName( xServiceName->getServiceName()); 604cdf0e10cSrcweir if( aServiceName.equalsAsciiL( 605cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM( "com.sun.star.chart2.MeanValueRegressionCurve" ))) 606cdf0e10cSrcweir { 607cdf0e10cSrcweir aResult = ::chart::SchResId::getResString( STR_REGRESSION_MEAN ); 608cdf0e10cSrcweir } 609cdf0e10cSrcweir else if( aServiceName.equalsAsciiL( 610cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM( "com.sun.star.chart2.LinearRegressionCurve" ))) 611cdf0e10cSrcweir { 612cdf0e10cSrcweir aResult = ::chart::SchResId::getResString( STR_REGRESSION_LINEAR ); 613cdf0e10cSrcweir } 614cdf0e10cSrcweir else if( aServiceName.equalsAsciiL( 615cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM( "com.sun.star.chart2.LogarithmicRegressionCurve" ))) 616cdf0e10cSrcweir { 617cdf0e10cSrcweir aResult = ::chart::SchResId::getResString( STR_REGRESSION_LOG ); 618cdf0e10cSrcweir } 619cdf0e10cSrcweir else if( aServiceName.equalsAsciiL( 620cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM( "com.sun.star.chart2.ExponentialRegressionCurve" ))) 621cdf0e10cSrcweir { 622cdf0e10cSrcweir aResult = ::chart::SchResId::getResString( STR_REGRESSION_EXP ); 623cdf0e10cSrcweir } 624cdf0e10cSrcweir else if( aServiceName.equalsAsciiL( 625cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM( "com.sun.star.chart2.PotentialRegressionCurve" ))) 626cdf0e10cSrcweir { 627cdf0e10cSrcweir aResult = ::chart::SchResId::getResString( STR_REGRESSION_POWER ); 628cdf0e10cSrcweir } 629cdf0e10cSrcweir 630cdf0e10cSrcweir return aResult; 631cdf0e10cSrcweir } 632cdf0e10cSrcweir 633cdf0e10cSrcweir ::std::vector< Reference< chart2::XRegressionCurve > > 634cdf0e10cSrcweir RegressionCurveHelper::getAllRegressionCurvesNotMeanValueLine( 635cdf0e10cSrcweir const Reference< chart2::XDiagram > & xDiagram ) 636cdf0e10cSrcweir { 637cdf0e10cSrcweir ::std::vector< Reference< chart2::XRegressionCurve > > aResult; 638cdf0e10cSrcweir ::std::vector< Reference< chart2::XDataSeries > > aSeries( DiagramHelper::getDataSeriesFromDiagram( xDiagram )); 639cdf0e10cSrcweir for( ::std::vector< Reference< chart2::XDataSeries > >::iterator aIt( aSeries.begin()); 640cdf0e10cSrcweir aIt != aSeries.end(); ++aIt ) 641cdf0e10cSrcweir { 642cdf0e10cSrcweir Reference< chart2::XRegressionCurveContainer > xCurveCnt( *aIt, uno::UNO_QUERY ); 643cdf0e10cSrcweir if( xCurveCnt.is()) 644cdf0e10cSrcweir { 645cdf0e10cSrcweir uno::Sequence< uno::Reference< chart2::XRegressionCurve > > aCurves( 646cdf0e10cSrcweir xCurveCnt->getRegressionCurves()); 647cdf0e10cSrcweir for( sal_Int32 i = 0; i < aCurves.getLength(); ++i ) 648cdf0e10cSrcweir { 649cdf0e10cSrcweir if( ! isMeanValueLine( aCurves[i] )) 650cdf0e10cSrcweir aResult.push_back( aCurves[i] ); 651cdf0e10cSrcweir } 652cdf0e10cSrcweir } 653cdf0e10cSrcweir } 654cdf0e10cSrcweir 655cdf0e10cSrcweir return aResult; 656cdf0e10cSrcweir } 657cdf0e10cSrcweir 658cdf0e10cSrcweir void RegressionCurveHelper::resetEquationPosition( 659cdf0e10cSrcweir const Reference< chart2::XRegressionCurve > & xCurve ) 660cdf0e10cSrcweir { 661cdf0e10cSrcweir if( xCurve.is()) 662cdf0e10cSrcweir { 663cdf0e10cSrcweir try 664cdf0e10cSrcweir { 665cdf0e10cSrcweir const OUString aPosPropertyName( RTL_CONSTASCII_USTRINGPARAM( "RelativePosition" )); 666cdf0e10cSrcweir Reference< beans::XPropertySet > xEqProp( xCurve->getEquationProperties()); // since m233: , uno::UNO_SET_THROW ); 667cdf0e10cSrcweir if( xEqProp->getPropertyValue( aPosPropertyName ).hasValue()) 668cdf0e10cSrcweir xEqProp->setPropertyValue( aPosPropertyName, uno::Any()); 669cdf0e10cSrcweir } 670cdf0e10cSrcweir catch( const uno::Exception & ex ) 671cdf0e10cSrcweir { 672cdf0e10cSrcweir ASSERT_EXCEPTION( ex ); 673cdf0e10cSrcweir } 674cdf0e10cSrcweir } 675cdf0e10cSrcweir } 676cdf0e10cSrcweir 677cdf0e10cSrcweir sal_Int32 RegressionCurveHelper::getRegressionCurveIndex( 678cdf0e10cSrcweir const Reference< chart2::XRegressionCurveContainer > & xContainer, 679cdf0e10cSrcweir const Reference< chart2::XRegressionCurve > & xCurve ) 680cdf0e10cSrcweir { 681cdf0e10cSrcweir if( xContainer.is()) 682cdf0e10cSrcweir { 683cdf0e10cSrcweir uno::Sequence< uno::Reference< XRegressionCurve > > aCurves( 684cdf0e10cSrcweir xContainer->getRegressionCurves()); 685cdf0e10cSrcweir for( sal_Int32 i = 0; i < aCurves.getLength(); ++i ) 686cdf0e10cSrcweir { 687cdf0e10cSrcweir if( xCurve == aCurves[i] ) 688cdf0e10cSrcweir return i; 689cdf0e10cSrcweir } 690cdf0e10cSrcweir } 691cdf0e10cSrcweir return -1; 692cdf0e10cSrcweir } 693cdf0e10cSrcweir 694cdf0e10cSrcweir bool RegressionCurveHelper::hasEquation( const Reference< chart2::XRegressionCurve > & xCurve ) 695cdf0e10cSrcweir { 696cdf0e10cSrcweir bool bHasEquation = false; 697cdf0e10cSrcweir if( xCurve.is()) 698cdf0e10cSrcweir { 699cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xEquationProp( xCurve->getEquationProperties()); 700cdf0e10cSrcweir if( xEquationProp.is()) 701cdf0e10cSrcweir { 702cdf0e10cSrcweir bool bShowEquation = false; 703cdf0e10cSrcweir bool bShowCoefficient = false; 704cdf0e10cSrcweir xEquationProp->getPropertyValue( C2U("ShowEquation")) >>= bShowEquation; 705cdf0e10cSrcweir xEquationProp->getPropertyValue( C2U("ShowCorrelationCoefficient")) >>= bShowCoefficient; 706cdf0e10cSrcweir bHasEquation = bShowEquation || bShowCoefficient; 707cdf0e10cSrcweir } 708cdf0e10cSrcweir } 709cdf0e10cSrcweir return bHasEquation; 710cdf0e10cSrcweir } 711cdf0e10cSrcweir 712cdf0e10cSrcweir //............................................................................. 713cdf0e10cSrcweir } //namespace chart 714cdf0e10cSrcweir //............................................................................. 715