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"
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
;
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
)
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
);
81 bool bCategoriesUsed
= false;
82 bool bSetXValues
= aData
.getLength()>(bCategoriesUsed
?2:1);
83 for( sal_Int32 nDataIdx
= 0; nDataIdx
< aData
.getLength(); ++nDataIdx
)
87 if( bHasCategories
&& ! bCategoriesUsed
)
89 xCategories
.set( aData
[nDataIdx
] );
92 SetRole( xCategories
->getValues(), C2U("categories"));
93 if( bUseCategoriesAsX
)
96 bCategoriesUsed
= true;
98 else if( !xValuesX
.is() && bSetXValues
)
100 xValuesX
.set( aData
[nDataIdx
] );
102 SetRole( xValuesX
->getValues(), C2U("values-x"));
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
);
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
);
135 aNewData
.push_back( xClonedXValues
);
137 aNewData
.push_back( *aSequencesVecIt
);
139 Reference
< XDataSeries
> xSeries
;
140 if( nSeriesIndex
< aSeriesToReUse
.getLength())
141 xSeries
.set( aSeriesToReUse
[nSeriesIndex
] );
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
);
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
;
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() ||
182 vector
< Reference
< data::XLabeledDataSequence
> > aValueSeqVec(
183 DataSeriesHelper::getAllDataSequencesByRole(
184 xSeriesSource
->getDataSequences(), C2U("values"), true ));
186 aValueSeqVec
.erase( find( aValueSeqVec
.begin(), aValueSeqVec
.end(), xValuesX
));
188 aValueSeqVec
.erase( find( aValueSeqVec
.begin(), aValueSeqVec
.end(), xValuesY
));
191 if( ! xValuesY
.is() &&
192 aValueSeqVec
.size() > nIndex
)
194 xValuesY
.set( aValueSeqVec
[nIndex
++] );
196 SetRole( xValuesY
->getValues(), C2U("values-y"));
199 if( ! xValuesX
.is() &&
200 aValueSeqVec
.size() > nIndex
)
202 xValuesX
.set( aValueSeqVec
[nIndex
++] );
204 SetRole( xValuesY
->getValues(), C2U("values-x"));
211 aNewSequences
.realloc(2);
212 aNewSequences
[0] = xValuesX
;
213 aNewSequences
[1] = xValuesY
;
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
227 for( ; j
<aSeqs
.getLength(); ++j
)
229 OSL_ENSURE( aSeqs
[j
] == xValuesY
|| aSeqs
[j
] == xValuesX
, "All sequences should be used" );
232 Reference
< data::XDataSink
> xSink( xSeriesSource
, uno::UNO_QUERY_THROW
);
233 xSink
->setData( aNewSequences
);
236 catch( const uno::Exception
& ex
)
238 ASSERT_EXCEPTION( ex
);
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 )
260 catch( const uno::Exception
& ex
)
262 ASSERT_EXCEPTION( ex
);
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */