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 58 : XYDataInterpreter::XYDataInterpreter(
41 : const uno::Reference< uno::XComponentContext > & xContext ) :
42 58 : DataInterpreter( xContext )
43 : {
44 58 : }
45 :
46 116 : XYDataInterpreter::~XYDataInterpreter()
47 : {
48 116 : }
49 :
50 : // ____ XDataInterpreter ____
51 58 : 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 58 : if( ! xSource.is())
58 52 : return InterpretedData();
59 :
60 6 : Sequence< Reference< data::XLabeledDataSequence > > aData( xSource->getDataSequences() );
61 :
62 12 : Reference< data::XLabeledDataSequence > xValuesX;
63 12 : vector< Reference< data::XLabeledDataSequence > > aSequencesVec;
64 :
65 12 : Reference< data::XLabeledDataSequence > xCategories;
66 6 : bool bHasCategories = HasCategories( aArguments, aData );
67 6 : bool bUseCategoriesAsX = UseCategoriesAsX( aArguments );
68 :
69 : // parse data
70 6 : bool bCategoriesUsed = false;
71 6 : bool bSetXValues = aData.getLength()>(bCategoriesUsed?2:1);
72 70 : for( sal_Int32 nDataIdx= 0; nDataIdx < aData.getLength(); ++nDataIdx )
73 : {
74 : try
75 : {
76 64 : if( bHasCategories && ! bCategoriesUsed )
77 : {
78 6 : xCategories.set( aData[nDataIdx] );
79 6 : if( xCategories.is())
80 : {
81 6 : SetRole( xCategories->getValues(), "categories");
82 6 : if( bUseCategoriesAsX )
83 6 : bSetXValues = false;
84 : }
85 6 : bCategoriesUsed = true;
86 : }
87 58 : 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 58 : aSequencesVec.push_back( aData[nDataIdx] );
96 58 : if( aData[nDataIdx].is())
97 58 : 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 6 : aSequencesVecIt = aSequencesVec.begin();
109 :
110 6 : sal_Int32 nSeriesIndex = 0;
111 12 : vector< Reference< XDataSeries > > aSeriesVec;
112 6 : aSeriesVec.reserve( aSequencesVec.size());
113 :
114 12 : Reference< data::XLabeledDataSequence > xClonedXValues = xValuesX;
115 12 : Reference< util::XCloneable > xCloneable( xValuesX, uno::UNO_QUERY );
116 :
117 64 : for( ;aSequencesVecIt != aSequencesVec.end(); ++aSequencesVecIt, ++nSeriesIndex )
118 : {
119 58 : vector< Reference< data::XLabeledDataSequence > > aNewData;
120 :
121 58 : if( aSequencesVecIt != aSequencesVec.begin() && xCloneable.is() )
122 0 : xClonedXValues.set( xCloneable->createClone(), uno::UNO_QUERY );
123 58 : if( xValuesX.is() )
124 0 : aNewData.push_back( xClonedXValues );
125 :
126 58 : aNewData.push_back( *aSequencesVecIt );
127 :
128 116 : Reference< XDataSeries > xSeries;
129 58 : if( nSeriesIndex < aSeriesToReUse.getLength())
130 58 : xSeries.set( aSeriesToReUse[nSeriesIndex] );
131 : else
132 0 : xSeries.set( new DataSeries( GetComponentContext() ) );
133 : OSL_ASSERT( xSeries.is() );
134 116 : Reference< data::XDataSink > xSink( xSeries, uno::UNO_QUERY );
135 : OSL_ASSERT( xSink.is() );
136 58 : xSink->setData( ContainerHelper::ContainerToSequence( aNewData ) );
137 :
138 58 : aSeriesVec.push_back( xSeries );
139 58 : }
140 :
141 12 : Sequence< Sequence< Reference< XDataSeries > > > aSeries(1);
142 6 : aSeries[0] = ContainerHelper::ContainerToSequence( aSeriesVec );
143 12 : 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 6 : sal_Bool SAL_CALL XYDataInterpreter::isDataCompatible(
236 : const chart2::InterpretedData& aInterpretedData )
237 : throw (uno::RuntimeException, std::exception)
238 : {
239 6 : Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
240 6 : for( sal_Int32 i=0; i<aSeries.getLength(); ++i )
241 : {
242 : try
243 : {
244 6 : Reference< data::XDataSource > xSrc( aSeries[i], uno::UNO_QUERY_THROW );
245 6 : Sequence< Reference< data::XLabeledDataSequence > > aSeq( xSrc->getDataSequences());
246 6 : if( aSeq.getLength() != 2 )
247 6 : 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: */
|