merge the formfield patch from ooo-build
[ooovba.git] / chart2 / source / model / template / XYDataInterpreter.cxx
blob0e0686938f4978a0260398bdf3cfe62a239be734
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: XYDataInterpreter.cxx,v $
10 * $Revision: 1.3 $
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 "XYDataInterpreter.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 XYDataInterpreter::XYDataInterpreter(
56 const uno::Reference< uno::XComponentContext > & xContext ) :
57 DataInterpreter( xContext )
61 XYDataInterpreter::~XYDataInterpreter()
65 // ____ XDataInterpreter ____
66 chart2::InterpretedData SAL_CALL XYDataInterpreter::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 > > aSequencesVec;
80 Reference< data::XLabeledDataSequence > xCategories;
81 // check for categories. If true, the the categories bet parked in the axis scale, but not used via setting the Axistype to Not CATEGORY
82 bool bHasCategories = HasCategories( aArguments, aData );
84 // parse data
85 Sequence< Reference< data::XLabeledDataSequence > > aUnusedData;
87 bool bCategoriesUsed = false;
88 for( sal_Int32 nDataIdx= 0; nDataIdx < aData.getLength(); ++nDataIdx )
90 try
92 if( bHasCategories && ! bCategoriesUsed )
94 xCategories.set( aData[nDataIdx] );
95 if( xCategories.is())
96 SetRole( xCategories->getValues(), C2U("categories"));
97 bCategoriesUsed = true;
99 else if( !xValuesX.is() && (aData.getLength()>(bCategoriesUsed?2:1)) )
101 xValuesX.set( aData[nDataIdx] );
102 if( xValuesX.is())
103 SetRole( xValuesX->getValues(), C2U("values-x"));
105 else
107 aSequencesVec.push_back( aData[nDataIdx] );
108 if( aData[nDataIdx].is())
109 SetRole( aData[nDataIdx]->getValues(), C2U("values-y"));
112 catch( uno::Exception & ex )
114 ASSERT_EXCEPTION( ex );
118 // create DataSeries
119 vector< Reference< data::XLabeledDataSequence > >::const_iterator
120 aSequencesVecIt = aSequencesVec.begin();
122 sal_Int32 nSeriesIndex = 0;
123 vector< Reference< XDataSeries > > aSeriesVec;
124 aSeriesVec.reserve( aSequencesVec.size());
126 Reference< data::XLabeledDataSequence > xClonedXValues = xValuesX;
127 Reference< util::XCloneable > xCloneable( xValuesX, uno::UNO_QUERY );
129 for( ;aSequencesVecIt != aSequencesVec.end(); ++aSequencesVecIt, ++nSeriesIndex )
131 Sequence< Reference< data::XLabeledDataSequence > > aNewData(xValuesX.is()?2:1);
132 if( aSequencesVecIt != aSequencesVec.begin() &&
133 xCloneable.is() )
135 xClonedXValues.set( xCloneable->createClone(), uno::UNO_QUERY );
138 if( xValuesX.is() )
140 aNewData[0] = xClonedXValues;
141 aNewData[1] = (*aSequencesVecIt);
143 else
145 aNewData[0] = (*aSequencesVecIt);
148 Reference< XDataSeries > xSeries;
149 if( nSeriesIndex < aSeriesToReUse.getLength())
150 xSeries.set( aSeriesToReUse[nSeriesIndex] );
151 else
152 xSeries.set( new DataSeries( GetComponentContext() ) );
153 OSL_ASSERT( xSeries.is() );
154 Reference< data::XDataSink > xSink( xSeries, uno::UNO_QUERY );
155 OSL_ASSERT( xSink.is() );
156 xSink->setData( aNewData );
158 aSeriesVec.push_back( xSeries );
161 Sequence< Sequence< Reference< XDataSeries > > > aSeries(1);
162 aSeries[0] = ContainerHelper::ContainerToSequence( aSeriesVec );
163 return InterpretedData( aSeries, xCategories, aUnusedData );
166 chart2::InterpretedData SAL_CALL XYDataInterpreter::reinterpretDataSeries(
167 const chart2::InterpretedData& aInterpretedData )
168 throw (uno::RuntimeException)
170 InterpretedData aResult( aInterpretedData );
171 vector< Reference< data::XLabeledDataSequence > > aUnused(
172 ContainerHelper::SequenceToVector( aInterpretedData.UnusedData ));
174 sal_Int32 i=0;
175 Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
176 const sal_Int32 nCount = aSeries.getLength();
177 for( ; i<nCount; ++i )
181 Reference< data::XDataSource > xSeriesSource( aSeries[i], uno::UNO_QUERY_THROW );
182 Sequence< Reference< data::XLabeledDataSequence > > aNewSequences;
184 // values-y
185 Reference< data::XLabeledDataSequence > xValuesY(
186 DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-y"), false ));
187 Reference< data::XLabeledDataSequence > xValuesX(
188 DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-x"), false ));
189 // re-use values-... as values-x/values-y
190 if( ! xValuesX.is() ||
191 ! xValuesY.is())
193 vector< Reference< data::XLabeledDataSequence > > aValueSeqVec(
194 DataSeriesHelper::getAllDataSequencesByRole(
195 xSeriesSource->getDataSequences(), C2U("values"), true ));
196 if( xValuesX.is())
197 aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesX ));
198 if( xValuesY.is())
199 aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesY ));
201 size_t nIndex = 0;
202 if( ! xValuesY.is() &&
203 aValueSeqVec.size() > nIndex )
205 xValuesY.set( aValueSeqVec[nIndex++] );
206 if( xValuesY.is())
207 SetRole( xValuesY->getValues(), C2U("values-y"));
210 if( ! xValuesX.is() &&
211 aValueSeqVec.size() > nIndex )
213 xValuesX.set( aValueSeqVec[nIndex++] );
214 if( xValuesX.is())
215 SetRole( xValuesY->getValues(), C2U("values-x"));
218 if( xValuesY.is())
220 if( xValuesX.is())
222 aNewSequences.realloc(2);
223 aNewSequences[0] = xValuesX;
224 aNewSequences[1] = xValuesY;
226 else
228 aNewSequences.realloc(1);
229 aNewSequences[0] = xValuesY;
233 Sequence< Reference< data::XLabeledDataSequence > > aSeqs( xSeriesSource->getDataSequences());
234 if( aSeqs.getLength() != aNewSequences.getLength() )
236 sal_Int32 j=0;
237 for( ; j<aSeqs.getLength(); ++j )
239 if( aSeqs[j] != xValuesY &&
240 aSeqs[j] != xValuesX )
241 aUnused.push_back( aSeqs[j] );
243 Reference< data::XDataSink > xSink( xSeriesSource, uno::UNO_QUERY_THROW );
244 xSink->setData( aNewSequences );
245 aResult.UnusedData = ContainerHelper::ContainerToSequence( aUnused );
248 catch( uno::Exception & ex )
250 ASSERT_EXCEPTION( ex );
254 return aResult;
257 // criterion: all series must have exactly two data::XLabeledDataSequences
258 sal_Bool SAL_CALL XYDataInterpreter::isDataCompatible(
259 const chart2::InterpretedData& aInterpretedData )
260 throw (uno::RuntimeException)
262 Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
263 for( sal_Int32 i=0; i<aSeries.getLength(); ++i )
267 Reference< data::XDataSource > xSrc( aSeries[i], uno::UNO_QUERY_THROW );
268 Sequence< Reference< data::XLabeledDataSequence > > aSeq( xSrc->getDataSequences());
269 if( aSeq.getLength() != 2 )
270 return sal_False;
272 catch( uno::Exception & ex )
274 ASSERT_EXCEPTION( ex );
278 return sal_True;
281 } // namespace chart