merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / model / template / XYDataInterpreter.cxx
blob839c0cbac510205e14f9e1b06506f76455d25c89
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
31 #include "XYDataInterpreter.hxx"
32 #include "DataSeries.hxx"
33 #include "macros.hxx"
34 #include "DataSeriesHelper.hxx"
35 #include "CommonConverters.hxx"
36 #include "ContainerHelper.hxx"
37 #include <com/sun/star/beans/XPropertySet.hpp>
38 #include <com/sun/star/chart2/data/XDataSink.hpp>
39 #include <com/sun/star/util/XCloneable.hpp>
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::chart2;
43 using namespace ::std;
45 using ::com::sun::star::uno::Reference;
46 using ::com::sun::star::uno::Sequence;
47 using ::rtl::OUString;
49 namespace chart
52 XYDataInterpreter::XYDataInterpreter(
53 const uno::Reference< uno::XComponentContext > & xContext ) :
54 DataInterpreter( xContext )
58 XYDataInterpreter::~XYDataInterpreter()
62 // ____ XDataInterpreter ____
63 chart2::InterpretedData SAL_CALL XYDataInterpreter::interpretDataSource(
64 const Reference< chart2::data::XDataSource >& xSource,
65 const Sequence< beans::PropertyValue >& aArguments,
66 const Sequence< Reference< XDataSeries > >& aSeriesToReUse )
67 throw (uno::RuntimeException)
69 if( ! xSource.is())
70 return InterpretedData();
72 Sequence< Reference< data::XLabeledDataSequence > > aData( xSource->getDataSequences() );
74 Reference< data::XLabeledDataSequence > xValuesX;
75 vector< Reference< data::XLabeledDataSequence > > aSequencesVec;
77 Reference< data::XLabeledDataSequence > xCategories;
78 // check for categories. If true, the the categories bet parked in the axis scale, but not used via setting the Axistype to Not CATEGORY
79 bool bHasCategories = HasCategories( aArguments, aData );
81 // parse data
82 bool bCategoriesUsed = false;
83 for( sal_Int32 nDataIdx= 0; nDataIdx < aData.getLength(); ++nDataIdx )
85 try
87 if( bHasCategories && ! bCategoriesUsed )
89 xCategories.set( aData[nDataIdx] );
90 if( xCategories.is())
91 SetRole( xCategories->getValues(), C2U("categories"));
92 bCategoriesUsed = true;
94 else if( !xValuesX.is() && (aData.getLength()>(bCategoriesUsed?2:1)) )
96 xValuesX.set( aData[nDataIdx] );
97 if( xValuesX.is())
98 SetRole( xValuesX->getValues(), C2U("values-x"));
100 else
102 aSequencesVec.push_back( aData[nDataIdx] );
103 if( aData[nDataIdx].is())
104 SetRole( aData[nDataIdx]->getValues(), C2U("values-y"));
107 catch( uno::Exception & ex )
109 ASSERT_EXCEPTION( ex );
113 // create DataSeries
114 vector< Reference< data::XLabeledDataSequence > >::const_iterator
115 aSequencesVecIt = aSequencesVec.begin();
117 sal_Int32 nSeriesIndex = 0;
118 vector< Reference< XDataSeries > > aSeriesVec;
119 aSeriesVec.reserve( aSequencesVec.size());
121 Reference< data::XLabeledDataSequence > xClonedXValues = xValuesX;
122 Reference< util::XCloneable > xCloneable( xValuesX, uno::UNO_QUERY );
124 for( ;aSequencesVecIt != aSequencesVec.end(); ++aSequencesVecIt, ++nSeriesIndex )
126 Sequence< Reference< data::XLabeledDataSequence > > aNewData(xValuesX.is()?2:1);
127 if( aSequencesVecIt != aSequencesVec.begin() &&
128 xCloneable.is() )
130 xClonedXValues.set( xCloneable->createClone(), uno::UNO_QUERY );
133 if( xValuesX.is() )
135 aNewData[0] = xClonedXValues;
136 aNewData[1] = (*aSequencesVecIt);
138 else
140 aNewData[0] = (*aSequencesVecIt);
143 Reference< XDataSeries > xSeries;
144 if( nSeriesIndex < aSeriesToReUse.getLength())
145 xSeries.set( aSeriesToReUse[nSeriesIndex] );
146 else
147 xSeries.set( new DataSeries( GetComponentContext() ) );
148 OSL_ASSERT( xSeries.is() );
149 Reference< data::XDataSink > xSink( xSeries, uno::UNO_QUERY );
150 OSL_ASSERT( xSink.is() );
151 xSink->setData( aNewData );
153 aSeriesVec.push_back( xSeries );
156 Sequence< Sequence< Reference< XDataSeries > > > aSeries(1);
157 aSeries[0] = ContainerHelper::ContainerToSequence( aSeriesVec );
158 return InterpretedData( aSeries, xCategories );
161 chart2::InterpretedData SAL_CALL XYDataInterpreter::reinterpretDataSeries(
162 const chart2::InterpretedData& aInterpretedData )
163 throw (uno::RuntimeException)
165 InterpretedData aResult( aInterpretedData );
167 sal_Int32 i=0;
168 Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
169 const sal_Int32 nCount = aSeries.getLength();
170 for( ; i<nCount; ++i )
174 Reference< data::XDataSource > xSeriesSource( aSeries[i], uno::UNO_QUERY_THROW );
175 Sequence< Reference< data::XLabeledDataSequence > > aNewSequences;
177 // values-y
178 Reference< data::XLabeledDataSequence > xValuesY(
179 DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-y"), false ));
180 Reference< data::XLabeledDataSequence > xValuesX(
181 DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-x"), false ));
182 // re-use values-... as values-x/values-y
183 if( ! xValuesX.is() ||
184 ! xValuesY.is())
186 vector< Reference< data::XLabeledDataSequence > > aValueSeqVec(
187 DataSeriesHelper::getAllDataSequencesByRole(
188 xSeriesSource->getDataSequences(), C2U("values"), true ));
189 if( xValuesX.is())
190 aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesX ));
191 if( xValuesY.is())
192 aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesY ));
194 size_t nIndex = 0;
195 if( ! xValuesY.is() &&
196 aValueSeqVec.size() > nIndex )
198 xValuesY.set( aValueSeqVec[nIndex++] );
199 if( xValuesY.is())
200 SetRole( xValuesY->getValues(), C2U("values-y"));
203 if( ! xValuesX.is() &&
204 aValueSeqVec.size() > nIndex )
206 xValuesX.set( aValueSeqVec[nIndex++] );
207 if( xValuesX.is())
208 SetRole( xValuesY->getValues(), C2U("values-x"));
211 if( xValuesY.is())
213 if( xValuesX.is())
215 aNewSequences.realloc(2);
216 aNewSequences[0] = xValuesX;
217 aNewSequences[1] = xValuesY;
219 else
221 aNewSequences.realloc(1);
222 aNewSequences[0] = xValuesY;
226 Sequence< Reference< data::XLabeledDataSequence > > aSeqs( xSeriesSource->getDataSequences());
227 if( aSeqs.getLength() != aNewSequences.getLength() )
229 #if OSL_DEBUG_LEVEL > 1
230 sal_Int32 j=0;
231 for( ; j<aSeqs.getLength(); ++j )
233 OSL_ENSURE( aSeqs[j] == xValuesY || aSeqs[j] == xValuesX, "All sequences should be used" );
235 #endif
236 Reference< data::XDataSink > xSink( xSeriesSource, uno::UNO_QUERY_THROW );
237 xSink->setData( aNewSequences );
240 catch( uno::Exception & ex )
242 ASSERT_EXCEPTION( ex );
246 return aResult;
249 // criterion: all series must have exactly two data::XLabeledDataSequences
250 sal_Bool SAL_CALL XYDataInterpreter::isDataCompatible(
251 const chart2::InterpretedData& aInterpretedData )
252 throw (uno::RuntimeException)
254 Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
255 for( sal_Int32 i=0; i<aSeries.getLength(); ++i )
259 Reference< data::XDataSource > xSrc( aSeries[i], uno::UNO_QUERY_THROW );
260 Sequence< Reference< data::XLabeledDataSequence > > aSeq( xSrc->getDataSequences());
261 if( aSeq.getLength() != 2 )
262 return sal_False;
264 catch( uno::Exception & ex )
266 ASSERT_EXCEPTION( ex );
270 return sal_True;
273 } // namespace chart