1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_chart2.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "XYDataInterpreter.hxx"
32*cdf0e10cSrcweir #include "DataSeries.hxx"
33*cdf0e10cSrcweir #include "macros.hxx"
34*cdf0e10cSrcweir #include "DataSeriesHelper.hxx"
35*cdf0e10cSrcweir #include "CommonConverters.hxx"
36*cdf0e10cSrcweir #include "ContainerHelper.hxx"
37*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/chart2/data/XDataSink.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/util/XCloneable.hpp>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir using namespace ::com::sun::star;
42*cdf0e10cSrcweir using namespace ::com::sun::star::chart2;
43*cdf0e10cSrcweir using namespace ::std;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
46*cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
47*cdf0e10cSrcweir using ::rtl::OUString;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir namespace chart
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir XYDataInterpreter::XYDataInterpreter(
53*cdf0e10cSrcweir     const uno::Reference< uno::XComponentContext > & xContext ) :
54*cdf0e10cSrcweir         DataInterpreter( xContext )
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir }
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir XYDataInterpreter::~XYDataInterpreter()
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir }
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir // ____ XDataInterpreter ____
63*cdf0e10cSrcweir chart2::InterpretedData SAL_CALL XYDataInterpreter::interpretDataSource(
64*cdf0e10cSrcweir     const Reference< chart2::data::XDataSource >& xSource,
65*cdf0e10cSrcweir     const Sequence< beans::PropertyValue >& aArguments,
66*cdf0e10cSrcweir     const Sequence< Reference< XDataSeries > >& aSeriesToReUse )
67*cdf0e10cSrcweir     throw (uno::RuntimeException)
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir     if( ! xSource.is())
70*cdf0e10cSrcweir         return InterpretedData();
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     Sequence< Reference< data::XLabeledDataSequence > > aData( xSource->getDataSequences() );
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     Reference< data::XLabeledDataSequence > xValuesX;
75*cdf0e10cSrcweir     vector< Reference< data::XLabeledDataSequence > > aSequencesVec;
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     Reference< data::XLabeledDataSequence > xCategories;
78*cdf0e10cSrcweir     bool bHasCategories = HasCategories( aArguments, aData );
79*cdf0e10cSrcweir     bool bUseCategoriesAsX = UseCategoriesAsX( aArguments );
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir     // parse data
82*cdf0e10cSrcweir     bool bCategoriesUsed = false;
83*cdf0e10cSrcweir     bool bSetXValues = aData.getLength()>(bCategoriesUsed?2:1);
84*cdf0e10cSrcweir     for( sal_Int32 nDataIdx= 0; nDataIdx < aData.getLength(); ++nDataIdx )
85*cdf0e10cSrcweir     {
86*cdf0e10cSrcweir         try
87*cdf0e10cSrcweir         {
88*cdf0e10cSrcweir             if( bHasCategories && ! bCategoriesUsed )
89*cdf0e10cSrcweir             {
90*cdf0e10cSrcweir                 xCategories.set( aData[nDataIdx] );
91*cdf0e10cSrcweir                 if( xCategories.is())
92*cdf0e10cSrcweir                 {
93*cdf0e10cSrcweir                     SetRole( xCategories->getValues(), C2U("categories"));
94*cdf0e10cSrcweir                     if( bUseCategoriesAsX )
95*cdf0e10cSrcweir                         bSetXValues = false;
96*cdf0e10cSrcweir                 }
97*cdf0e10cSrcweir                 bCategoriesUsed = true;
98*cdf0e10cSrcweir             }
99*cdf0e10cSrcweir             else if( !xValuesX.is() && bSetXValues )
100*cdf0e10cSrcweir             {
101*cdf0e10cSrcweir                 xValuesX.set( aData[nDataIdx] );
102*cdf0e10cSrcweir                 if( xValuesX.is())
103*cdf0e10cSrcweir                     SetRole( xValuesX->getValues(), C2U("values-x"));
104*cdf0e10cSrcweir             }
105*cdf0e10cSrcweir             else
106*cdf0e10cSrcweir             {
107*cdf0e10cSrcweir                 aSequencesVec.push_back( aData[nDataIdx] );
108*cdf0e10cSrcweir                 if( aData[nDataIdx].is())
109*cdf0e10cSrcweir                     SetRole( aData[nDataIdx]->getValues(), C2U("values-y"));
110*cdf0e10cSrcweir             }
111*cdf0e10cSrcweir         }
112*cdf0e10cSrcweir         catch( uno::Exception & ex )
113*cdf0e10cSrcweir         {
114*cdf0e10cSrcweir             ASSERT_EXCEPTION( ex );
115*cdf0e10cSrcweir         }
116*cdf0e10cSrcweir     }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     // create DataSeries
119*cdf0e10cSrcweir     vector< Reference< data::XLabeledDataSequence > >::const_iterator
120*cdf0e10cSrcweir           aSequencesVecIt = aSequencesVec.begin();
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     sal_Int32 nSeriesIndex = 0;
123*cdf0e10cSrcweir     vector< Reference< XDataSeries > > aSeriesVec;
124*cdf0e10cSrcweir     aSeriesVec.reserve( aSequencesVec.size());
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     Reference< data::XLabeledDataSequence > xClonedXValues = xValuesX;
127*cdf0e10cSrcweir     Reference< util::XCloneable > xCloneable( xValuesX, uno::UNO_QUERY );
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir     for( ;aSequencesVecIt != aSequencesVec.end(); ++aSequencesVecIt, ++nSeriesIndex )
130*cdf0e10cSrcweir     {
131*cdf0e10cSrcweir         vector< Reference< data::XLabeledDataSequence > > aNewData;
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir         if( aSequencesVecIt != aSequencesVec.begin() && xCloneable.is() )
134*cdf0e10cSrcweir             xClonedXValues.set( xCloneable->createClone(), uno::UNO_QUERY );
135*cdf0e10cSrcweir         if( xValuesX.is() )
136*cdf0e10cSrcweir             aNewData.push_back( xClonedXValues );
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         aNewData.push_back( *aSequencesVecIt );
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir         Reference< XDataSeries > xSeries;
141*cdf0e10cSrcweir         if( nSeriesIndex < aSeriesToReUse.getLength())
142*cdf0e10cSrcweir             xSeries.set( aSeriesToReUse[nSeriesIndex] );
143*cdf0e10cSrcweir         else
144*cdf0e10cSrcweir             xSeries.set( new DataSeries( GetComponentContext() ) );
145*cdf0e10cSrcweir         OSL_ASSERT( xSeries.is() );
146*cdf0e10cSrcweir         Reference< data::XDataSink > xSink( xSeries, uno::UNO_QUERY );
147*cdf0e10cSrcweir         OSL_ASSERT( xSink.is() );
148*cdf0e10cSrcweir         xSink->setData( ContainerHelper::ContainerToSequence( aNewData ) );
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir         aSeriesVec.push_back( xSeries );
151*cdf0e10cSrcweir     }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir     Sequence< Sequence< Reference< XDataSeries > > > aSeries(1);
154*cdf0e10cSrcweir     aSeries[0] = ContainerHelper::ContainerToSequence( aSeriesVec );
155*cdf0e10cSrcweir     return InterpretedData( aSeries, xCategories );
156*cdf0e10cSrcweir }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir chart2::InterpretedData SAL_CALL XYDataInterpreter::reinterpretDataSeries(
159*cdf0e10cSrcweir     const chart2::InterpretedData& aInterpretedData )
160*cdf0e10cSrcweir     throw (uno::RuntimeException)
161*cdf0e10cSrcweir {
162*cdf0e10cSrcweir     InterpretedData aResult( aInterpretedData );
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     sal_Int32 i=0;
165*cdf0e10cSrcweir     Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
166*cdf0e10cSrcweir     const sal_Int32 nCount = aSeries.getLength();
167*cdf0e10cSrcweir     for( ; i<nCount; ++i )
168*cdf0e10cSrcweir     {
169*cdf0e10cSrcweir         try
170*cdf0e10cSrcweir         {
171*cdf0e10cSrcweir             Reference< data::XDataSource > xSeriesSource( aSeries[i], uno::UNO_QUERY_THROW );
172*cdf0e10cSrcweir             Sequence< Reference< data::XLabeledDataSequence > > aNewSequences;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir             // values-y
175*cdf0e10cSrcweir             Reference< data::XLabeledDataSequence > xValuesY(
176*cdf0e10cSrcweir                 DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-y"), false ));
177*cdf0e10cSrcweir             Reference< data::XLabeledDataSequence > xValuesX(
178*cdf0e10cSrcweir                 DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-x"), false ));
179*cdf0e10cSrcweir             // re-use values-... as values-x/values-y
180*cdf0e10cSrcweir             if( ! xValuesX.is() ||
181*cdf0e10cSrcweir                 ! xValuesY.is())
182*cdf0e10cSrcweir             {
183*cdf0e10cSrcweir                 vector< Reference< data::XLabeledDataSequence > > aValueSeqVec(
184*cdf0e10cSrcweir                     DataSeriesHelper::getAllDataSequencesByRole(
185*cdf0e10cSrcweir                         xSeriesSource->getDataSequences(), C2U("values"), true ));
186*cdf0e10cSrcweir                 if( xValuesX.is())
187*cdf0e10cSrcweir                     aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesX ));
188*cdf0e10cSrcweir                 if( xValuesY.is())
189*cdf0e10cSrcweir                     aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesY ));
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir                 size_t nIndex = 0;
192*cdf0e10cSrcweir                 if( ! xValuesY.is() &&
193*cdf0e10cSrcweir                     aValueSeqVec.size() > nIndex )
194*cdf0e10cSrcweir                 {
195*cdf0e10cSrcweir                     xValuesY.set( aValueSeqVec[nIndex++] );
196*cdf0e10cSrcweir                     if( xValuesY.is())
197*cdf0e10cSrcweir                         SetRole( xValuesY->getValues(), C2U("values-y"));
198*cdf0e10cSrcweir                 }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir                 if( ! xValuesX.is() &&
201*cdf0e10cSrcweir                     aValueSeqVec.size() > nIndex )
202*cdf0e10cSrcweir                 {
203*cdf0e10cSrcweir                     xValuesX.set( aValueSeqVec[nIndex++] );
204*cdf0e10cSrcweir                     if( xValuesX.is())
205*cdf0e10cSrcweir                         SetRole( xValuesY->getValues(), C2U("values-x"));
206*cdf0e10cSrcweir                 }
207*cdf0e10cSrcweir             }
208*cdf0e10cSrcweir             if( xValuesY.is())
209*cdf0e10cSrcweir             {
210*cdf0e10cSrcweir                 if( xValuesX.is())
211*cdf0e10cSrcweir                 {
212*cdf0e10cSrcweir                     aNewSequences.realloc(2);
213*cdf0e10cSrcweir                     aNewSequences[0] = xValuesX;
214*cdf0e10cSrcweir                     aNewSequences[1] = xValuesY;
215*cdf0e10cSrcweir                 }
216*cdf0e10cSrcweir                 else
217*cdf0e10cSrcweir                 {
218*cdf0e10cSrcweir                     aNewSequences.realloc(1);
219*cdf0e10cSrcweir                     aNewSequences[0] = xValuesY;
220*cdf0e10cSrcweir                 }
221*cdf0e10cSrcweir             }
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir             Sequence< Reference< data::XLabeledDataSequence > > aSeqs( xSeriesSource->getDataSequences());
224*cdf0e10cSrcweir             if( aSeqs.getLength() != aNewSequences.getLength() )
225*cdf0e10cSrcweir             {
226*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
227*cdf0e10cSrcweir                 sal_Int32 j=0;
228*cdf0e10cSrcweir                 for( ; j<aSeqs.getLength(); ++j )
229*cdf0e10cSrcweir                 {
230*cdf0e10cSrcweir                     OSL_ENSURE( aSeqs[j] == xValuesY || aSeqs[j] == xValuesX, "All sequences should be used" );
231*cdf0e10cSrcweir                 }
232*cdf0e10cSrcweir #endif
233*cdf0e10cSrcweir                 Reference< data::XDataSink > xSink( xSeriesSource, uno::UNO_QUERY_THROW );
234*cdf0e10cSrcweir                 xSink->setData( aNewSequences );
235*cdf0e10cSrcweir             }
236*cdf0e10cSrcweir         }
237*cdf0e10cSrcweir         catch( uno::Exception & ex )
238*cdf0e10cSrcweir         {
239*cdf0e10cSrcweir             ASSERT_EXCEPTION( ex );
240*cdf0e10cSrcweir         }
241*cdf0e10cSrcweir     }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir     return aResult;
244*cdf0e10cSrcweir }
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir // criterion: all series must have exactly two data::XLabeledDataSequences
247*cdf0e10cSrcweir sal_Bool SAL_CALL XYDataInterpreter::isDataCompatible(
248*cdf0e10cSrcweir     const chart2::InterpretedData& aInterpretedData )
249*cdf0e10cSrcweir     throw (uno::RuntimeException)
250*cdf0e10cSrcweir {
251*cdf0e10cSrcweir     Sequence< Reference< XDataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
252*cdf0e10cSrcweir     for( sal_Int32 i=0; i<aSeries.getLength(); ++i )
253*cdf0e10cSrcweir     {
254*cdf0e10cSrcweir         try
255*cdf0e10cSrcweir         {
256*cdf0e10cSrcweir             Reference< data::XDataSource > xSrc( aSeries[i], uno::UNO_QUERY_THROW );
257*cdf0e10cSrcweir             Sequence< Reference< data::XLabeledDataSequence > > aSeq( xSrc->getDataSequences());
258*cdf0e10cSrcweir             if( aSeq.getLength() != 2 )
259*cdf0e10cSrcweir                 return sal_False;
260*cdf0e10cSrcweir         }
261*cdf0e10cSrcweir         catch( uno::Exception & ex )
262*cdf0e10cSrcweir         {
263*cdf0e10cSrcweir             ASSERT_EXCEPTION( ex );
264*cdf0e10cSrcweir         }
265*cdf0e10cSrcweir     }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir     return sal_True;
268*cdf0e10cSrcweir }
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir } // namespace chart
271