1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_chart2.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "BubbleDataInterpreter.hxx" 32*cdf0e10cSrcweir #include "DataSeries.hxx" 33*cdf0e10cSrcweir #include "macros.hxx" 34*cdf0e10cSrcweir #include "DataSeriesHelper.hxx" 35*cdf0e10cSrcweir #include "CommonConverters.hxx" 36*cdf0e10cSrcweir #include "ContainerHelper.hxx" 37*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 38*cdf0e10cSrcweir #include <com/sun/star/chart2/data/XDataSink.hpp> 39*cdf0e10cSrcweir #include <com/sun/star/util/XCloneable.hpp> 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir using namespace ::com::sun::star; 42*cdf0e10cSrcweir using namespace ::com::sun::star::chart2; 43*cdf0e10cSrcweir using namespace ::std; 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 46*cdf0e10cSrcweir using ::com::sun::star::uno::Sequence; 47*cdf0e10cSrcweir using ::rtl::OUString; 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir namespace chart 50*cdf0e10cSrcweir { 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir BubbleDataInterpreter::BubbleDataInterpreter( 53*cdf0e10cSrcweir const uno::Reference< uno::XComponentContext > & xContext ) : 54*cdf0e10cSrcweir DataInterpreter( xContext ) 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir } 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir BubbleDataInterpreter::~BubbleDataInterpreter() 59*cdf0e10cSrcweir { 60*cdf0e10cSrcweir } 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir // ____ XDataInterpreter ____ 63*cdf0e10cSrcweir chart2::InterpretedData SAL_CALL BubbleDataInterpreter::interpretDataSource( 64*cdf0e10cSrcweir const Reference< chart2::data::XDataSource >& xSource, 65*cdf0e10cSrcweir const Sequence< beans::PropertyValue >& aArguments, 66*cdf0e10cSrcweir const Sequence< Reference< XDataSeries > >& aSeriesToReUse ) 67*cdf0e10cSrcweir throw (uno::RuntimeException) 68*cdf0e10cSrcweir { 69*cdf0e10cSrcweir if( ! xSource.is()) 70*cdf0e10cSrcweir return InterpretedData(); 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir Sequence< Reference< data::XLabeledDataSequence > > aData( xSource->getDataSequences() ); 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir Reference< data::XLabeledDataSequence > xValuesX; 75*cdf0e10cSrcweir vector< Reference< data::XLabeledDataSequence > > aYValuesVector; 76*cdf0e10cSrcweir vector< Reference< data::XLabeledDataSequence > > aSizeValuesVector; 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir Reference< data::XLabeledDataSequence > xCategories; 79*cdf0e10cSrcweir bool bHasCategories = HasCategories( aArguments, aData ); 80*cdf0e10cSrcweir bool bUseCategoriesAsX = UseCategoriesAsX( aArguments ); 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir bool bSetXValues = false; 83*cdf0e10cSrcweir sal_Int32 nDataSeqCount = aData.getLength(); 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir bSetXValues = bHasCategories ? ( (nDataSeqCount-1) > 2 && (nDataSeqCount-1) % 2 != 0 ) 86*cdf0e10cSrcweir :( nDataSeqCount > 2 && nDataSeqCount % 2 != 0 ); 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir bool bCategoriesUsed = false; 89*cdf0e10cSrcweir bool bNextIsYValues = bHasCategories ? nDataSeqCount>2 : nDataSeqCount>1; 90*cdf0e10cSrcweir for( sal_Int32 nDataIdx = 0; nDataIdx < nDataSeqCount; ++nDataIdx ) 91*cdf0e10cSrcweir { 92*cdf0e10cSrcweir try 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir if( bHasCategories && !bCategoriesUsed ) 95*cdf0e10cSrcweir { 96*cdf0e10cSrcweir xCategories.set( aData[nDataIdx] ); 97*cdf0e10cSrcweir if( xCategories.is()) 98*cdf0e10cSrcweir { 99*cdf0e10cSrcweir SetRole( xCategories->getValues(), C2U("categories")); 100*cdf0e10cSrcweir if( bUseCategoriesAsX ) 101*cdf0e10cSrcweir { 102*cdf0e10cSrcweir bSetXValues = false; 103*cdf0e10cSrcweir bNextIsYValues = nDataSeqCount > 2; 104*cdf0e10cSrcweir } 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir bCategoriesUsed = true; 107*cdf0e10cSrcweir } 108*cdf0e10cSrcweir else if( !xValuesX.is() && bSetXValues ) 109*cdf0e10cSrcweir { 110*cdf0e10cSrcweir xValuesX.set( aData[nDataIdx] ); 111*cdf0e10cSrcweir if( xValuesX.is()) 112*cdf0e10cSrcweir SetRole( xValuesX->getValues(), C2U("values-x")); 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir else if( bNextIsYValues ) 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir aYValuesVector.push_back( aData[nDataIdx] ); 117*cdf0e10cSrcweir if( aData[nDataIdx].is()) 118*cdf0e10cSrcweir SetRole( aData[nDataIdx]->getValues(), C2U("values-y")); 119*cdf0e10cSrcweir bNextIsYValues = false; 120*cdf0e10cSrcweir } 121*cdf0e10cSrcweir else if( !bNextIsYValues ) 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir aSizeValuesVector.push_back( aData[nDataIdx] ); 124*cdf0e10cSrcweir if( aData[nDataIdx].is()) 125*cdf0e10cSrcweir SetRole( aData[nDataIdx]->getValues(), C2U("values-size")); 126*cdf0e10cSrcweir bNextIsYValues = (nDataSeqCount-(nDataIdx+1)) >= 2;//two or more left 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir catch( uno::Exception & ex ) 130*cdf0e10cSrcweir { 131*cdf0e10cSrcweir ASSERT_EXCEPTION( ex ); 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir // create DataSeries 136*cdf0e10cSrcweir sal_Int32 nSeriesIndex = 0; 137*cdf0e10cSrcweir vector< Reference< XDataSeries > > aSeriesVec; 138*cdf0e10cSrcweir aSeriesVec.reserve( aSizeValuesVector.size()); 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir Reference< data::XLabeledDataSequence > xClonedXValues = xValuesX; 141*cdf0e10cSrcweir Reference< util::XCloneable > xCloneableX( xValuesX, uno::UNO_QUERY ); 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir for( size_t nN = 0; nN < aSizeValuesVector.size(); ++nN, ++nSeriesIndex ) 144*cdf0e10cSrcweir { 145*cdf0e10cSrcweir vector< Reference< data::XLabeledDataSequence > > aNewData; 146*cdf0e10cSrcweir if( xValuesX.is() ) 147*cdf0e10cSrcweir { 148*cdf0e10cSrcweir if( nN > 0 && xCloneableX.is() ) 149*cdf0e10cSrcweir xClonedXValues.set( xCloneableX->createClone(), uno::UNO_QUERY ); 150*cdf0e10cSrcweir aNewData.push_back( xClonedXValues ); 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir if( aYValuesVector.size() > nN ) 153*cdf0e10cSrcweir aNewData.push_back( aYValuesVector[nN] ); 154*cdf0e10cSrcweir if( aSizeValuesVector.size() > nN ) 155*cdf0e10cSrcweir aNewData.push_back( aSizeValuesVector[nN] ); 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir Reference< XDataSeries > xSeries; 158*cdf0e10cSrcweir if( nSeriesIndex < aSeriesToReUse.getLength()) 159*cdf0e10cSrcweir xSeries.set( aSeriesToReUse[nSeriesIndex] ); 160*cdf0e10cSrcweir else 161*cdf0e10cSrcweir xSeries.set( new DataSeries( GetComponentContext() ) ); 162*cdf0e10cSrcweir OSL_ASSERT( xSeries.is() ); 163*cdf0e10cSrcweir Reference< data::XDataSink > xSink( xSeries, uno::UNO_QUERY ); 164*cdf0e10cSrcweir OSL_ASSERT( xSink.is() ); 165*cdf0e10cSrcweir xSink->setData( ContainerHelper::ContainerToSequence( aNewData ) ); 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir aSeriesVec.push_back( xSeries ); 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir Sequence< Sequence< Reference< XDataSeries > > > aSeries(1); 171*cdf0e10cSrcweir aSeries[0] = ContainerHelper::ContainerToSequence( aSeriesVec ); 172*cdf0e10cSrcweir return InterpretedData( aSeries, xCategories ); 173*cdf0e10cSrcweir } 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir chart2::InterpretedData SAL_CALL BubbleDataInterpreter::reinterpretDataSeries( 176*cdf0e10cSrcweir const chart2::InterpretedData& aInterpretedData ) 177*cdf0e10cSrcweir throw (uno::RuntimeException) 178*cdf0e10cSrcweir { 179*cdf0e10cSrcweir InterpretedData aResult( aInterpretedData ); 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir sal_Int32 i=0; 182*cdf0e10cSrcweir Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series )); 183*cdf0e10cSrcweir const sal_Int32 nCount = aSeries.getLength(); 184*cdf0e10cSrcweir for( ; i<nCount; ++i ) 185*cdf0e10cSrcweir { 186*cdf0e10cSrcweir try 187*cdf0e10cSrcweir { 188*cdf0e10cSrcweir Reference< data::XDataSource > xSeriesSource( aSeries[i], uno::UNO_QUERY_THROW ); 189*cdf0e10cSrcweir Sequence< Reference< data::XLabeledDataSequence > > aNewSequences; 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir Reference< data::XLabeledDataSequence > xValuesSize( 192*cdf0e10cSrcweir DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-size"), false )); 193*cdf0e10cSrcweir Reference< data::XLabeledDataSequence > xValuesY( 194*cdf0e10cSrcweir DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-y"), false )); 195*cdf0e10cSrcweir Reference< data::XLabeledDataSequence > xValuesX( 196*cdf0e10cSrcweir DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-x"), false )); 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir if( ! xValuesX.is() || 199*cdf0e10cSrcweir ! xValuesY.is() || 200*cdf0e10cSrcweir ! xValuesSize.is() ) 201*cdf0e10cSrcweir { 202*cdf0e10cSrcweir vector< Reference< data::XLabeledDataSequence > > aValueSeqVec( 203*cdf0e10cSrcweir DataSeriesHelper::getAllDataSequencesByRole( 204*cdf0e10cSrcweir xSeriesSource->getDataSequences(), C2U("values"), true )); 205*cdf0e10cSrcweir if( xValuesX.is()) 206*cdf0e10cSrcweir aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesX )); 207*cdf0e10cSrcweir if( xValuesY.is()) 208*cdf0e10cSrcweir aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesY )); 209*cdf0e10cSrcweir if( xValuesSize.is()) 210*cdf0e10cSrcweir aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesSize )); 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir size_t nIndex = 0; 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir if( ! xValuesSize.is() && 215*cdf0e10cSrcweir aValueSeqVec.size() > nIndex ) 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir xValuesSize.set( aValueSeqVec[nIndex++] ); 218*cdf0e10cSrcweir if( xValuesSize.is()) 219*cdf0e10cSrcweir SetRole( xValuesSize->getValues(), C2U("values-size")); 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir if( ! xValuesY.is() && 223*cdf0e10cSrcweir aValueSeqVec.size() > nIndex ) 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir xValuesY.set( aValueSeqVec[nIndex++] ); 226*cdf0e10cSrcweir if( xValuesY.is()) 227*cdf0e10cSrcweir SetRole( xValuesY->getValues(), C2U("values-y")); 228*cdf0e10cSrcweir } 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir if( ! xValuesX.is() && 231*cdf0e10cSrcweir aValueSeqVec.size() > nIndex ) 232*cdf0e10cSrcweir { 233*cdf0e10cSrcweir xValuesX.set( aValueSeqVec[nIndex++] ); 234*cdf0e10cSrcweir if( xValuesX.is()) 235*cdf0e10cSrcweir SetRole( xValuesY->getValues(), C2U("values-x")); 236*cdf0e10cSrcweir } 237*cdf0e10cSrcweir } 238*cdf0e10cSrcweir if( xValuesSize.is()) 239*cdf0e10cSrcweir { 240*cdf0e10cSrcweir if( xValuesY.is() ) 241*cdf0e10cSrcweir { 242*cdf0e10cSrcweir if( xValuesX.is() ) 243*cdf0e10cSrcweir { 244*cdf0e10cSrcweir aNewSequences.realloc(3); 245*cdf0e10cSrcweir aNewSequences[0] = xValuesX; 246*cdf0e10cSrcweir aNewSequences[1] = xValuesY; 247*cdf0e10cSrcweir aNewSequences[2] = xValuesSize; 248*cdf0e10cSrcweir } 249*cdf0e10cSrcweir else 250*cdf0e10cSrcweir { 251*cdf0e10cSrcweir aNewSequences.realloc(2); 252*cdf0e10cSrcweir aNewSequences[0] = xValuesY; 253*cdf0e10cSrcweir aNewSequences[1] = xValuesSize; 254*cdf0e10cSrcweir } 255*cdf0e10cSrcweir } 256*cdf0e10cSrcweir else 257*cdf0e10cSrcweir { 258*cdf0e10cSrcweir aNewSequences.realloc(1); 259*cdf0e10cSrcweir aNewSequences[0] = xValuesSize; 260*cdf0e10cSrcweir } 261*cdf0e10cSrcweir } 262*cdf0e10cSrcweir 263*cdf0e10cSrcweir Sequence< Reference< data::XLabeledDataSequence > > aSeqs( xSeriesSource->getDataSequences()); 264*cdf0e10cSrcweir if( aSeqs.getLength() != aNewSequences.getLength() ) 265*cdf0e10cSrcweir { 266*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1 267*cdf0e10cSrcweir sal_Int32 j=0; 268*cdf0e10cSrcweir for( ; j<aSeqs.getLength(); ++j ) 269*cdf0e10cSrcweir { 270*cdf0e10cSrcweir OSL_ENSURE( aSeqs[j] == xValuesY || aSeqs[j] == xValuesX || aSeqs[j] == xValuesSize, "All sequences should be used" ); 271*cdf0e10cSrcweir } 272*cdf0e10cSrcweir #endif 273*cdf0e10cSrcweir Reference< data::XDataSink > xSink( xSeriesSource, uno::UNO_QUERY_THROW ); 274*cdf0e10cSrcweir xSink->setData( aNewSequences ); 275*cdf0e10cSrcweir } 276*cdf0e10cSrcweir } 277*cdf0e10cSrcweir catch( uno::Exception & ex ) 278*cdf0e10cSrcweir { 279*cdf0e10cSrcweir ASSERT_EXCEPTION( ex ); 280*cdf0e10cSrcweir } 281*cdf0e10cSrcweir } 282*cdf0e10cSrcweir 283*cdf0e10cSrcweir return aResult; 284*cdf0e10cSrcweir } 285*cdf0e10cSrcweir 286*cdf0e10cSrcweir sal_Bool SAL_CALL BubbleDataInterpreter::isDataCompatible( 287*cdf0e10cSrcweir const chart2::InterpretedData& aInterpretedData ) 288*cdf0e10cSrcweir throw (uno::RuntimeException) 289*cdf0e10cSrcweir { 290*cdf0e10cSrcweir Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series )); 291*cdf0e10cSrcweir for( sal_Int32 i=0; i<aSeries.getLength(); ++i ) 292*cdf0e10cSrcweir { 293*cdf0e10cSrcweir try 294*cdf0e10cSrcweir { 295*cdf0e10cSrcweir Reference< data::XDataSource > xSrc( aSeries[i], uno::UNO_QUERY_THROW ); 296*cdf0e10cSrcweir Sequence< Reference< data::XLabeledDataSequence > > aSeq( xSrc->getDataSequences()); 297*cdf0e10cSrcweir if( aSeq.getLength() != 3 ) 298*cdf0e10cSrcweir return sal_False; 299*cdf0e10cSrcweir } 300*cdf0e10cSrcweir catch( uno::Exception & ex ) 301*cdf0e10cSrcweir { 302*cdf0e10cSrcweir ASSERT_EXCEPTION( ex ); 303*cdf0e10cSrcweir } 304*cdf0e10cSrcweir } 305*cdf0e10cSrcweir 306*cdf0e10cSrcweir return sal_True; 307*cdf0e10cSrcweir } 308*cdf0e10cSrcweir 309*cdf0e10cSrcweir } // namespace chart 310