Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "XYDataInterpreter.hxx"
21 : #include "DataSeries.hxx"
22 : #include "macros.hxx"
23 : #include "DataSeriesHelper.hxx"
24 : #include "CommonConverters.hxx"
25 : #include "ContainerHelper.hxx"
26 : #include <com/sun/star/beans/XPropertySet.hpp>
27 : #include <com/sun/star/chart2/data/XDataSink.hpp>
28 : #include <com/sun/star/util/XCloneable.hpp>
29 :
30 : using namespace ::com::sun::star;
31 : using namespace ::com::sun::star::chart2;
32 : using namespace ::std;
33 :
34 : using ::com::sun::star::uno::Reference;
35 : using ::com::sun::star::uno::Sequence;
36 :
37 : namespace chart
38 : {
39 :
40 29 : XYDataInterpreter::XYDataInterpreter(
41 : const uno::Reference< uno::XComponentContext > & xContext ) :
42 29 : DataInterpreter( xContext )
43 : {
44 29 : }
45 :
46 58 : XYDataInterpreter::~XYDataInterpreter()
47 : {
48 58 : }
49 :
50 : // ____ XDataInterpreter ____
51 29 : chart2::InterpretedData SAL_CALL XYDataInterpreter::interpretDataSource(
52 : const Reference< chart2::data::XDataSource >& xSource,
53 : const Sequence< beans::PropertyValue >& aArguments,
54 : const Sequence< Reference< XDataSeries > >& aSeriesToReUse )
55 : throw (uno::RuntimeException, std::exception)
56 : {
57 29 : if( ! xSource.is())
58 26 : return InterpretedData();
59 :
60 3 : Sequence< Reference< data::XLabeledDataSequence > > aData( xSource->getDataSequences() );
61 :
62 6 : Reference< data::XLabeledDataSequence > xValuesX;
63 6 : vector< Reference< data::XLabeledDataSequence > > aSequencesVec;
64 :
65 6 : Reference< data::XLabeledDataSequence > xCategories;
66 3 : bool bHasCategories = HasCategories( aArguments, aData );
67 3 : bool bUseCategoriesAsX = UseCategoriesAsX( aArguments );
68 :
69 : // parse data
70 3 : bool bCategoriesUsed = false;
71 3 : bool bSetXValues = aData.getLength()>1;
72 35 : for( sal_Int32 nDataIdx= 0; nDataIdx < aData.getLength(); ++nDataIdx )
73 : {
74 : try
75 : {
76 32 : if( bHasCategories && ! bCategoriesUsed )
77 : {
78 3 : xCategories.set( aData[nDataIdx] );
79 3 : if( xCategories.is())
80 : {
81 3 : SetRole( xCategories->getValues(), "categories");
82 3 : if( bUseCategoriesAsX )
83 3 : bSetXValues = false;
84 : }
85 3 : bCategoriesUsed = true;
86 : }
87 29 : else if( !xValuesX.is() && bSetXValues )
88 : {
89 0 : xValuesX.set( aData[nDataIdx] );
90 0 : if( xValuesX.is())
91 0 : SetRole( xValuesX->getValues(), "values-x");
92 : }
93 : else
94 : {
95 29 : aSequencesVec.push_back( aData[nDataIdx] );
96 29 : if( aData[nDataIdx].is())
97 29 : SetRole( aData[nDataIdx]->getValues(), "values-y");
98 : }
99 : }
100 0 : catch( const uno::Exception & ex )
101 : {
102 : ASSERT_EXCEPTION( ex );
103 : }
104 : }
105 :
106 : // create DataSeries
107 : vector< Reference< data::XLabeledDataSequence > >::const_iterator
108 3 : aSequencesVecIt = aSequencesVec.begin();
109 :
110 3 : sal_Int32 nSeriesIndex = 0;
111 6 : vector< Reference< XDataSeries > > aSeriesVec;
112 3 : aSeriesVec.reserve( aSequencesVec.size());
113 :
114 6 : Reference< data::XLabeledDataSequence > xClonedXValues = xValuesX;
115 6 : Reference< util::XCloneable > xCloneable( xValuesX, uno::UNO_QUERY );
116 :
117 32 : for( ;aSequencesVecIt != aSequencesVec.end(); ++aSequencesVecIt, ++nSeriesIndex )
118 : {
119 29 : vector< Reference< data::XLabeledDataSequence > > aNewData;
120 :
121 29 : if( aSequencesVecIt != aSequencesVec.begin() && xCloneable.is() )
122 0 : xClonedXValues.set( xCloneable->createClone(), uno::UNO_QUERY );
123 29 : if( xValuesX.is() )
124 0 : aNewData.push_back( xClonedXValues );
125 :
126 29 : aNewData.push_back( *aSequencesVecIt );
127 :
128 58 : Reference< XDataSeries > xSeries;
129 29 : if( nSeriesIndex < aSeriesToReUse.getLength())
130 29 : xSeries.set( aSeriesToReUse[nSeriesIndex] );
131 : else
132 0 : xSeries.set( new DataSeries( GetComponentContext() ) );
133 : OSL_ASSERT( xSeries.is() );
134 58 : Reference< data::XDataSink > xSink( xSeries, uno::UNO_QUERY );
135 : OSL_ASSERT( xSink.is() );
136 29 : xSink->setData( ContainerHelper::ContainerToSequence( aNewData ) );
137 :
138 29 : aSeriesVec.push_back( xSeries );
139 29 : }
140 :
141 6 : Sequence< Sequence< Reference< XDataSeries > > > aSeries(1);
142 3 : aSeries[0] = ContainerHelper::ContainerToSequence( aSeriesVec );
143 6 : return InterpretedData( aSeries, xCategories );
144 : }
145 :
146 0 : chart2::InterpretedData SAL_CALL XYDataInterpreter::reinterpretDataSeries(
147 : const chart2::InterpretedData& aInterpretedData )
148 : throw (uno::RuntimeException, std::exception)
149 : {
150 0 : InterpretedData aResult( aInterpretedData );
151 :
152 0 : sal_Int32 i=0;
153 0 : Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
154 0 : const sal_Int32 nCount = aSeries.getLength();
155 0 : for( ; i<nCount; ++i )
156 : {
157 : try
158 : {
159 0 : Reference< data::XDataSource > xSeriesSource( aSeries[i], uno::UNO_QUERY_THROW );
160 0 : Sequence< Reference< data::XLabeledDataSequence > > aNewSequences;
161 :
162 : // values-y
163 : Reference< data::XLabeledDataSequence > xValuesY(
164 0 : DataSeriesHelper::getDataSequenceByRole( xSeriesSource, "values-y", false ));
165 : Reference< data::XLabeledDataSequence > xValuesX(
166 0 : DataSeriesHelper::getDataSequenceByRole( xSeriesSource, "values-x", false ));
167 : // re-use values-... as values-x/values-y
168 0 : if( ! xValuesX.is() ||
169 0 : ! xValuesY.is())
170 : {
171 : vector< Reference< data::XLabeledDataSequence > > aValueSeqVec(
172 : DataSeriesHelper::getAllDataSequencesByRole(
173 0 : xSeriesSource->getDataSequences(), "values", true ));
174 0 : if( xValuesX.is())
175 0 : aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesX ));
176 0 : if( xValuesY.is())
177 0 : aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesY ));
178 :
179 0 : size_t nIndex = 0;
180 0 : if( ! xValuesY.is() &&
181 0 : aValueSeqVec.size() > nIndex )
182 : {
183 0 : xValuesY.set( aValueSeqVec[nIndex++] );
184 0 : if( xValuesY.is())
185 0 : SetRole( xValuesY->getValues(), "values-y");
186 : }
187 :
188 0 : if( ! xValuesX.is() &&
189 0 : aValueSeqVec.size() > nIndex )
190 : {
191 0 : xValuesX.set( aValueSeqVec[nIndex++] );
192 0 : if( xValuesX.is())
193 0 : SetRole( xValuesY->getValues(), "values-x");
194 0 : }
195 : }
196 0 : if( xValuesY.is())
197 : {
198 0 : if( xValuesX.is())
199 : {
200 0 : aNewSequences.realloc(2);
201 0 : aNewSequences[0] = xValuesX;
202 0 : aNewSequences[1] = xValuesY;
203 : }
204 : else
205 : {
206 0 : aNewSequences.realloc(1);
207 0 : aNewSequences[0] = xValuesY;
208 : }
209 : }
210 :
211 0 : Sequence< Reference< data::XLabeledDataSequence > > aSeqs( xSeriesSource->getDataSequences());
212 0 : if( aSeqs.getLength() != aNewSequences.getLength() )
213 : {
214 : #if OSL_DEBUG_LEVEL > 1
215 : sal_Int32 j=0;
216 : for( ; j<aSeqs.getLength(); ++j )
217 : {
218 : OSL_ENSURE( aSeqs[j] == xValuesY || aSeqs[j] == xValuesX, "All sequences should be used" );
219 : }
220 : #endif
221 0 : Reference< data::XDataSink > xSink( xSeriesSource, uno::UNO_QUERY_THROW );
222 0 : xSink->setData( aNewSequences );
223 0 : }
224 : }
225 0 : catch( const uno::Exception & ex )
226 : {
227 : ASSERT_EXCEPTION( ex );
228 : }
229 : }
230 :
231 0 : return aResult;
232 : }
233 :
234 : // criterion: all series must have exactly two data::XLabeledDataSequences
235 3 : sal_Bool SAL_CALL XYDataInterpreter::isDataCompatible(
236 : const chart2::InterpretedData& aInterpretedData )
237 : throw (uno::RuntimeException, std::exception)
238 : {
239 3 : Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
240 3 : for( sal_Int32 i=0; i<aSeries.getLength(); ++i )
241 : {
242 : try
243 : {
244 3 : Reference< data::XDataSource > xSrc( aSeries[i], uno::UNO_QUERY_THROW );
245 3 : Sequence< Reference< data::XLabeledDataSequence > > aSeq( xSrc->getDataSequences());
246 3 : if( aSeq.getLength() != 2 )
247 3 : return sal_False;
248 : }
249 0 : catch( const uno::Exception & ex )
250 : {
251 : ASSERT_EXCEPTION( ex );
252 : }
253 : }
254 :
255 0 : return sal_True;
256 : }
257 :
258 : } // namespace chart
259 :
260 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|