1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XYDataInterpreter.cxx,v $
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"
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
;
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
)
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
);
85 Sequence
< Reference
< data::XLabeledDataSequence
> > aUnusedData
;
87 bool bCategoriesUsed
= false;
88 for( sal_Int32 nDataIdx
= 0; nDataIdx
< aData
.getLength(); ++nDataIdx
)
92 if( bHasCategories
&& ! bCategoriesUsed
)
94 xCategories
.set( aData
[nDataIdx
] );
96 SetRole( xCategories
->getValues(), C2U("categories"));
97 bCategoriesUsed
= true;
99 else if( !xValuesX
.is() && (aData
.getLength()>(bCategoriesUsed
?2:1)) )
101 xValuesX
.set( aData
[nDataIdx
] );
103 SetRole( xValuesX
->getValues(), C2U("values-x"));
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
);
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() &&
135 xClonedXValues
.set( xCloneable
->createClone(), uno::UNO_QUERY
);
140 aNewData
[0] = xClonedXValues
;
141 aNewData
[1] = (*aSequencesVecIt
);
145 aNewData
[0] = (*aSequencesVecIt
);
148 Reference
< XDataSeries
> xSeries
;
149 if( nSeriesIndex
< aSeriesToReUse
.getLength())
150 xSeries
.set( aSeriesToReUse
[nSeriesIndex
] );
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
));
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
;
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() ||
193 vector
< Reference
< data::XLabeledDataSequence
> > aValueSeqVec(
194 DataSeriesHelper::getAllDataSequencesByRole(
195 xSeriesSource
->getDataSequences(), C2U("values"), true ));
197 aValueSeqVec
.erase( find( aValueSeqVec
.begin(), aValueSeqVec
.end(), xValuesX
));
199 aValueSeqVec
.erase( find( aValueSeqVec
.begin(), aValueSeqVec
.end(), xValuesY
));
202 if( ! xValuesY
.is() &&
203 aValueSeqVec
.size() > nIndex
)
205 xValuesY
.set( aValueSeqVec
[nIndex
++] );
207 SetRole( xValuesY
->getValues(), C2U("values-y"));
210 if( ! xValuesX
.is() &&
211 aValueSeqVec
.size() > nIndex
)
213 xValuesX
.set( aValueSeqVec
[nIndex
++] );
215 SetRole( xValuesY
->getValues(), C2U("values-x"));
222 aNewSequences
.realloc(2);
223 aNewSequences
[0] = xValuesX
;
224 aNewSequences
[1] = xValuesY
;
228 aNewSequences
.realloc(1);
229 aNewSequences
[0] = xValuesY
;
233 Sequence
< Reference
< data::XLabeledDataSequence
> > aSeqs( xSeriesSource
->getDataSequences());
234 if( aSeqs
.getLength() != aNewSequences
.getLength() )
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
);
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 )
272 catch( uno::Exception
& ex
)
274 ASSERT_EXCEPTION( ex
);