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"
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
;
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
)
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
);
82 bool bCategoriesUsed
= false;
83 for( sal_Int32 nDataIdx
= 0; nDataIdx
< aData
.getLength(); ++nDataIdx
)
87 if( bHasCategories
&& ! bCategoriesUsed
)
89 xCategories
.set( aData
[nDataIdx
] );
91 SetRole( xCategories
->getValues(), C2U("categories"));
92 bCategoriesUsed
= true;
94 else if( !xValuesX
.is() && (aData
.getLength()>(bCategoriesUsed
?2:1)) )
96 xValuesX
.set( aData
[nDataIdx
] );
98 SetRole( xValuesX
->getValues(), C2U("values-x"));
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
);
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() &&
130 xClonedXValues
.set( xCloneable
->createClone(), uno::UNO_QUERY
);
135 aNewData
[0] = xClonedXValues
;
136 aNewData
[1] = (*aSequencesVecIt
);
140 aNewData
[0] = (*aSequencesVecIt
);
143 Reference
< XDataSeries
> xSeries
;
144 if( nSeriesIndex
< aSeriesToReUse
.getLength())
145 xSeries
.set( aSeriesToReUse
[nSeriesIndex
] );
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
);
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
;
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() ||
186 vector
< Reference
< data::XLabeledDataSequence
> > aValueSeqVec(
187 DataSeriesHelper::getAllDataSequencesByRole(
188 xSeriesSource
->getDataSequences(), C2U("values"), true ));
190 aValueSeqVec
.erase( find( aValueSeqVec
.begin(), aValueSeqVec
.end(), xValuesX
));
192 aValueSeqVec
.erase( find( aValueSeqVec
.begin(), aValueSeqVec
.end(), xValuesY
));
195 if( ! xValuesY
.is() &&
196 aValueSeqVec
.size() > nIndex
)
198 xValuesY
.set( aValueSeqVec
[nIndex
++] );
200 SetRole( xValuesY
->getValues(), C2U("values-y"));
203 if( ! xValuesX
.is() &&
204 aValueSeqVec
.size() > nIndex
)
206 xValuesX
.set( aValueSeqVec
[nIndex
++] );
208 SetRole( xValuesY
->getValues(), C2U("values-x"));
215 aNewSequences
.realloc(2);
216 aNewSequences
[0] = xValuesX
;
217 aNewSequences
[1] = xValuesY
;
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
231 for( ; j
<aSeqs
.getLength(); ++j
)
233 OSL_ENSURE( aSeqs
[j
] == xValuesY
|| aSeqs
[j
] == xValuesX
, "All sequences should be used" );
236 Reference
< data::XDataSink
> xSink( xSeriesSource
, uno::UNO_QUERY_THROW
);
237 xSink
->setData( aNewSequences
);
240 catch( uno::Exception
& ex
)
242 ASSERT_EXCEPTION( ex
);
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 )
264 catch( uno::Exception
& ex
)
266 ASSERT_EXCEPTION( ex
);