Bump for 3.6-28
[LibreOffice.git] / chart2 / source / model / template / XYDataInterpreter.cxx
blob8130288fd5c8e780880cf7a62b4f7211ed79c013
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "XYDataInterpreter.hxx"
31 #include "DataSeries.hxx"
32 #include "macros.hxx"
33 #include "DataSeriesHelper.hxx"
34 #include "CommonConverters.hxx"
35 #include "ContainerHelper.hxx"
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <com/sun/star/chart2/data/XDataSink.hpp>
38 #include <com/sun/star/util/XCloneable.hpp>
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::chart2;
42 using namespace ::std;
44 using ::com::sun::star::uno::Reference;
45 using ::com::sun::star::uno::Sequence;
46 using ::rtl::OUString;
48 namespace chart
51 XYDataInterpreter::XYDataInterpreter(
52 const uno::Reference< uno::XComponentContext > & xContext ) :
53 DataInterpreter( xContext )
57 XYDataInterpreter::~XYDataInterpreter()
61 // ____ XDataInterpreter ____
62 chart2::InterpretedData SAL_CALL XYDataInterpreter::interpretDataSource(
63 const Reference< chart2::data::XDataSource >& xSource,
64 const Sequence< beans::PropertyValue >& aArguments,
65 const Sequence< Reference< XDataSeries > >& aSeriesToReUse )
66 throw (uno::RuntimeException)
68 if( ! xSource.is())
69 return InterpretedData();
71 Sequence< Reference< data::XLabeledDataSequence > > aData( xSource->getDataSequences() );
73 Reference< data::XLabeledDataSequence > xValuesX;
74 vector< Reference< data::XLabeledDataSequence > > aSequencesVec;
76 Reference< data::XLabeledDataSequence > xCategories;
77 bool bHasCategories = HasCategories( aArguments, aData );
78 bool bUseCategoriesAsX = UseCategoriesAsX( aArguments );
80 // parse data
81 bool bCategoriesUsed = false;
82 bool bSetXValues = aData.getLength()>(bCategoriesUsed?2:1);
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())
92 SetRole( xCategories->getValues(), C2U("categories"));
93 if( bUseCategoriesAsX )
94 bSetXValues = false;
96 bCategoriesUsed = true;
98 else if( !xValuesX.is() && bSetXValues )
100 xValuesX.set( aData[nDataIdx] );
101 if( xValuesX.is())
102 SetRole( xValuesX->getValues(), C2U("values-x"));
104 else
106 aSequencesVec.push_back( aData[nDataIdx] );
107 if( aData[nDataIdx].is())
108 SetRole( aData[nDataIdx]->getValues(), C2U("values-y"));
111 catch( const uno::Exception & ex )
113 ASSERT_EXCEPTION( ex );
117 // create DataSeries
118 vector< Reference< data::XLabeledDataSequence > >::const_iterator
119 aSequencesVecIt = aSequencesVec.begin();
121 sal_Int32 nSeriesIndex = 0;
122 vector< Reference< XDataSeries > > aSeriesVec;
123 aSeriesVec.reserve( aSequencesVec.size());
125 Reference< data::XLabeledDataSequence > xClonedXValues = xValuesX;
126 Reference< util::XCloneable > xCloneable( xValuesX, uno::UNO_QUERY );
128 for( ;aSequencesVecIt != aSequencesVec.end(); ++aSequencesVecIt, ++nSeriesIndex )
130 vector< Reference< data::XLabeledDataSequence > > aNewData;
132 if( aSequencesVecIt != aSequencesVec.begin() && xCloneable.is() )
133 xClonedXValues.set( xCloneable->createClone(), uno::UNO_QUERY );
134 if( xValuesX.is() )
135 aNewData.push_back( xClonedXValues );
137 aNewData.push_back( *aSequencesVecIt );
139 Reference< XDataSeries > xSeries;
140 if( nSeriesIndex < aSeriesToReUse.getLength())
141 xSeries.set( aSeriesToReUse[nSeriesIndex] );
142 else
143 xSeries.set( new DataSeries( GetComponentContext() ) );
144 OSL_ASSERT( xSeries.is() );
145 Reference< data::XDataSink > xSink( xSeries, uno::UNO_QUERY );
146 OSL_ASSERT( xSink.is() );
147 xSink->setData( ContainerHelper::ContainerToSequence( aNewData ) );
149 aSeriesVec.push_back( xSeries );
152 Sequence< Sequence< Reference< XDataSeries > > > aSeries(1);
153 aSeries[0] = ContainerHelper::ContainerToSequence( aSeriesVec );
154 return InterpretedData( aSeries, xCategories );
157 chart2::InterpretedData SAL_CALL XYDataInterpreter::reinterpretDataSeries(
158 const chart2::InterpretedData& aInterpretedData )
159 throw (uno::RuntimeException)
161 InterpretedData aResult( aInterpretedData );
163 sal_Int32 i=0;
164 Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
165 const sal_Int32 nCount = aSeries.getLength();
166 for( ; i<nCount; ++i )
170 Reference< data::XDataSource > xSeriesSource( aSeries[i], uno::UNO_QUERY_THROW );
171 Sequence< Reference< data::XLabeledDataSequence > > aNewSequences;
173 // values-y
174 Reference< data::XLabeledDataSequence > xValuesY(
175 DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-y"), false ));
176 Reference< data::XLabeledDataSequence > xValuesX(
177 DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-x"), false ));
178 // re-use values-... as values-x/values-y
179 if( ! xValuesX.is() ||
180 ! xValuesY.is())
182 vector< Reference< data::XLabeledDataSequence > > aValueSeqVec(
183 DataSeriesHelper::getAllDataSequencesByRole(
184 xSeriesSource->getDataSequences(), C2U("values"), true ));
185 if( xValuesX.is())
186 aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesX ));
187 if( xValuesY.is())
188 aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesY ));
190 size_t nIndex = 0;
191 if( ! xValuesY.is() &&
192 aValueSeqVec.size() > nIndex )
194 xValuesY.set( aValueSeqVec[nIndex++] );
195 if( xValuesY.is())
196 SetRole( xValuesY->getValues(), C2U("values-y"));
199 if( ! xValuesX.is() &&
200 aValueSeqVec.size() > nIndex )
202 xValuesX.set( aValueSeqVec[nIndex++] );
203 if( xValuesX.is())
204 SetRole( xValuesY->getValues(), C2U("values-x"));
207 if( xValuesY.is())
209 if( xValuesX.is())
211 aNewSequences.realloc(2);
212 aNewSequences[0] = xValuesX;
213 aNewSequences[1] = xValuesY;
215 else
217 aNewSequences.realloc(1);
218 aNewSequences[0] = xValuesY;
222 Sequence< Reference< data::XLabeledDataSequence > > aSeqs( xSeriesSource->getDataSequences());
223 if( aSeqs.getLength() != aNewSequences.getLength() )
225 #if OSL_DEBUG_LEVEL > 1
226 sal_Int32 j=0;
227 for( ; j<aSeqs.getLength(); ++j )
229 OSL_ENSURE( aSeqs[j] == xValuesY || aSeqs[j] == xValuesX, "All sequences should be used" );
231 #endif
232 Reference< data::XDataSink > xSink( xSeriesSource, uno::UNO_QUERY_THROW );
233 xSink->setData( aNewSequences );
236 catch( const uno::Exception & ex )
238 ASSERT_EXCEPTION( ex );
242 return aResult;
245 // criterion: all series must have exactly two data::XLabeledDataSequences
246 sal_Bool SAL_CALL XYDataInterpreter::isDataCompatible(
247 const chart2::InterpretedData& aInterpretedData )
248 throw (uno::RuntimeException)
250 Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
251 for( sal_Int32 i=0; i<aSeries.getLength(); ++i )
255 Reference< data::XDataSource > xSrc( aSeries[i], uno::UNO_QUERY_THROW );
256 Sequence< Reference< data::XLabeledDataSequence > > aSeq( xSrc->getDataSequences());
257 if( aSeq.getLength() != 2 )
258 return sal_False;
260 catch( const uno::Exception & ex )
262 ASSERT_EXCEPTION( ex );
266 return sal_True;
269 } // namespace chart
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */