update dev300-m58
[ooovba.git] / chart2 / source / model / template / BubbleDataInterpreter.cxx
blob77e090cb4587a24878343d086f98d999aa0d2425
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ,v $
10 * $Revision: $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
34 #include "BubbleDataInterpreter.hxx"
35 #include "DataSeries.hxx"
36 #include "macros.hxx"
37 #include "DataSeriesHelper.hxx"
38 #include "CommonConverters.hxx"
39 #include "ContainerHelper.hxx"
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include <com/sun/star/chart2/data/XDataSink.hpp>
42 #include <com/sun/star/util/XCloneable.hpp>
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::chart2;
46 using namespace ::std;
48 using ::com::sun::star::uno::Reference;
49 using ::com::sun::star::uno::Sequence;
50 using ::rtl::OUString;
52 namespace chart
55 BubbleDataInterpreter::BubbleDataInterpreter(
56 const uno::Reference< uno::XComponentContext > & xContext ) :
57 DataInterpreter( xContext )
61 BubbleDataInterpreter::~BubbleDataInterpreter()
65 // ____ XDataInterpreter ____
66 chart2::InterpretedData SAL_CALL BubbleDataInterpreter::interpretDataSource(
67 const Reference< chart2::data::XDataSource >& xSource,
68 const Sequence< beans::PropertyValue >& aArguments,
69 const Sequence< Reference< XDataSeries > >& aSeriesToReUse )
70 throw (uno::RuntimeException)
72 if( ! xSource.is())
73 return InterpretedData();
75 Sequence< Reference< data::XLabeledDataSequence > > aData( xSource->getDataSequences() );
77 Reference< data::XLabeledDataSequence > xValuesX;
78 vector< Reference< data::XLabeledDataSequence > > aYValuesVector;
79 vector< Reference< data::XLabeledDataSequence > > aSizeValuesVector;
81 Reference< data::XLabeledDataSequence > xCategories;
82 bool bHasCategories = HasCategories( aArguments, aData );
84 Sequence< Reference< data::XLabeledDataSequence > > aUnusedData;
86 bool bHasXValues = false;
87 sal_Int32 nDataSeqCount = aData.getLength();
89 bHasXValues = bHasCategories ? ( (nDataSeqCount-1) > 2 && (nDataSeqCount-1) % 2 != 0 )
90 :( nDataSeqCount > 2 && nDataSeqCount % 2 != 0 );
92 bool bCategoriesUsed = false;
93 bool bNextIsYValues = bHasCategories ? nDataSeqCount>2 : nDataSeqCount>1;
94 for( sal_Int32 nDataIdx = 0; nDataIdx < aData.getLength(); ++nDataIdx )
96 try
98 if( bHasCategories && !bCategoriesUsed )
100 xCategories.set( aData[nDataIdx] );
101 if( xCategories.is())
102 SetRole( xCategories->getValues(), C2U("categories"));
103 bCategoriesUsed = true;
105 else if( !xValuesX.is() && bHasXValues )
107 xValuesX.set( aData[nDataIdx] );
108 if( xValuesX.is())
109 SetRole( xValuesX->getValues(), C2U("values-x"));
111 else if( bNextIsYValues )
113 aYValuesVector.push_back( aData[nDataIdx] );
114 if( aData[nDataIdx].is())
115 SetRole( aData[nDataIdx]->getValues(), C2U("values-y"));
116 bNextIsYValues = false;
118 else if( !bNextIsYValues )
120 aSizeValuesVector.push_back( aData[nDataIdx] );
121 if( aData[nDataIdx].is())
122 SetRole( aData[nDataIdx]->getValues(), C2U("values-size"));
123 bNextIsYValues = true;
126 catch( uno::Exception & ex )
128 ASSERT_EXCEPTION( ex );
132 // create DataSeries
133 sal_Int32 nSeriesIndex = 0;
134 vector< Reference< XDataSeries > > aSeriesVec;
135 aSeriesVec.reserve( aSizeValuesVector.size());
137 Reference< data::XLabeledDataSequence > xClonedXValues = xValuesX;
138 Reference< util::XCloneable > xCloneableX( xValuesX, uno::UNO_QUERY );
140 for( size_t nCount = 0; nCount < aSizeValuesVector.size(); ++nCount, ++nSeriesIndex )
142 sal_Int32 nDataSequenceCount = 2;
143 if( xValuesX.is() )
144 nDataSequenceCount = 3;
145 else if( aYValuesVector.empty() )
146 nDataSequenceCount = 1;
148 Sequence< Reference< data::XLabeledDataSequence > > aNewData( nDataSequenceCount );
149 sal_Int32 nDataIndex = 0;
150 if( xValuesX.is() )
152 if( nCount > 0 && xCloneableX.is() )
153 xClonedXValues.set( xCloneableX->createClone(), uno::UNO_QUERY );
154 aNewData[nDataIndex++] = xClonedXValues;
156 if( aYValuesVector.size() > nCount )
157 aNewData[nDataIndex++] = aYValuesVector[nCount];
158 if( aSizeValuesVector.size() > nCount )
159 aNewData[nDataIndex++] = aSizeValuesVector[nCount];
161 Reference< XDataSeries > xSeries;
162 if( nSeriesIndex < aSeriesToReUse.getLength())
163 xSeries.set( aSeriesToReUse[nSeriesIndex] );
164 else
165 xSeries.set( new DataSeries( GetComponentContext() ) );
166 OSL_ASSERT( xSeries.is() );
167 Reference< data::XDataSink > xSink( xSeries, uno::UNO_QUERY );
168 OSL_ASSERT( xSink.is() );
169 xSink->setData( aNewData );
171 aSeriesVec.push_back( xSeries );
174 Sequence< Sequence< Reference< XDataSeries > > > aSeries(1);
175 aSeries[0] = ContainerHelper::ContainerToSequence( aSeriesVec );
176 return InterpretedData( aSeries, xCategories, aUnusedData );
179 chart2::InterpretedData SAL_CALL BubbleDataInterpreter::reinterpretDataSeries(
180 const chart2::InterpretedData& aInterpretedData )
181 throw (uno::RuntimeException)
183 InterpretedData aResult( aInterpretedData );
184 vector< Reference< data::XLabeledDataSequence > > aUnused(
185 ContainerHelper::SequenceToVector( aInterpretedData.UnusedData ));
187 sal_Int32 i=0;
188 Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
189 const sal_Int32 nCount = aSeries.getLength();
190 for( ; i<nCount; ++i )
194 Reference< data::XDataSource > xSeriesSource( aSeries[i], uno::UNO_QUERY_THROW );
195 Sequence< Reference< data::XLabeledDataSequence > > aNewSequences;
197 Reference< data::XLabeledDataSequence > xValuesSize(
198 DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-size"), false ));
199 Reference< data::XLabeledDataSequence > xValuesY(
200 DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-y"), false ));
201 Reference< data::XLabeledDataSequence > xValuesX(
202 DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-x"), false ));
204 if( ! xValuesX.is() ||
205 ! xValuesY.is() ||
206 ! xValuesSize.is() )
208 vector< Reference< data::XLabeledDataSequence > > aValueSeqVec(
209 DataSeriesHelper::getAllDataSequencesByRole(
210 xSeriesSource->getDataSequences(), C2U("values"), true ));
211 if( xValuesX.is())
212 aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesX ));
213 if( xValuesY.is())
214 aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesY ));
215 if( xValuesSize.is())
216 aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesSize ));
218 size_t nIndex = 0;
220 if( ! xValuesSize.is() &&
221 aValueSeqVec.size() > nIndex )
223 xValuesSize.set( aValueSeqVec[nIndex++] );
224 if( xValuesSize.is())
225 SetRole( xValuesSize->getValues(), C2U("values-size"));
228 if( ! xValuesY.is() &&
229 aValueSeqVec.size() > nIndex )
231 xValuesY.set( aValueSeqVec[nIndex++] );
232 if( xValuesY.is())
233 SetRole( xValuesY->getValues(), C2U("values-y"));
236 if( ! xValuesX.is() &&
237 aValueSeqVec.size() > nIndex )
239 xValuesX.set( aValueSeqVec[nIndex++] );
240 if( xValuesX.is())
241 SetRole( xValuesY->getValues(), C2U("values-x"));
244 if( xValuesSize.is())
246 if( xValuesY.is() )
248 if( xValuesX.is() )
250 aNewSequences.realloc(3);
251 aNewSequences[0] = xValuesX;
252 aNewSequences[1] = xValuesY;
253 aNewSequences[2] = xValuesSize;
255 else
257 aNewSequences.realloc(2);
258 aNewSequences[0] = xValuesY;
259 aNewSequences[1] = xValuesSize;
262 else
264 aNewSequences.realloc(1);
265 aNewSequences[0] = xValuesSize;
269 Sequence< Reference< data::XLabeledDataSequence > > aSeqs( xSeriesSource->getDataSequences());
270 if( aSeqs.getLength() != aNewSequences.getLength() )
272 sal_Int32 j=0;
273 for( ; j<aSeqs.getLength(); ++j )
275 if( aSeqs[j] != xValuesY &&
276 aSeqs[j] != xValuesX &&
277 aSeqs[j] != xValuesSize )
278 aUnused.push_back( aSeqs[j] );
280 Reference< data::XDataSink > xSink( xSeriesSource, uno::UNO_QUERY_THROW );
281 xSink->setData( aNewSequences );
282 aResult.UnusedData = ContainerHelper::ContainerToSequence( aUnused );
285 catch( uno::Exception & ex )
287 ASSERT_EXCEPTION( ex );
291 return aResult;
294 sal_Bool SAL_CALL BubbleDataInterpreter::isDataCompatible(
295 const chart2::InterpretedData& aInterpretedData )
296 throw (uno::RuntimeException)
298 Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
299 for( sal_Int32 i=0; i<aSeries.getLength(); ++i )
303 Reference< data::XDataSource > xSrc( aSeries[i], uno::UNO_QUERY_THROW );
304 Sequence< Reference< data::XLabeledDataSequence > > aSeq( xSrc->getDataSequences());
305 if( aSeq.getLength() != 3 )
306 return sal_False;
308 catch( uno::Exception & ex )
310 ASSERT_EXCEPTION( ex );
314 return sal_True;
317 } // namespace chart