1*cde9e8dcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cde9e8dcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cde9e8dcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cde9e8dcSAndrew Rist  * distributed with this work for additional information
6*cde9e8dcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cde9e8dcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cde9e8dcSAndrew Rist  * "License"); you may not use this file except in compliance
9*cde9e8dcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cde9e8dcSAndrew Rist  *
11*cde9e8dcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cde9e8dcSAndrew Rist  *
13*cde9e8dcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cde9e8dcSAndrew Rist  * software distributed under the License is distributed on an
15*cde9e8dcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cde9e8dcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*cde9e8dcSAndrew Rist  * specific language governing permissions and limitations
18*cde9e8dcSAndrew Rist  * under the License.
19*cde9e8dcSAndrew Rist  *
20*cde9e8dcSAndrew Rist  *************************************************************/
21*cde9e8dcSAndrew Rist 
22*cde9e8dcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_chart2.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "DataSourceHelper.hxx"
28cdf0e10cSrcweir #include "macros.hxx"
29cdf0e10cSrcweir #include "ChartModelHelper.hxx"
30cdf0e10cSrcweir #include "DiagramHelper.hxx"
31cdf0e10cSrcweir #include "DataSeriesHelper.hxx"
32cdf0e10cSrcweir #include "DataSource.hxx"
33cdf0e10cSrcweir #include "ContainerHelper.hxx"
34cdf0e10cSrcweir #include "ControllerLockGuard.hxx"
35cdf0e10cSrcweir #include "PropertyHelper.hxx"
36cdf0e10cSrcweir #include "CachedDataSequence.hxx"
37cdf0e10cSrcweir #include "LabeledDataSequence.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <com/sun/star/chart2/XChartDocument.hpp>
40cdf0e10cSrcweir #include <com/sun/star/chart2/data/XDataSource.hpp>
41cdf0e10cSrcweir #include <com/sun/star/chart2/data/XLabeledDataSequence.hpp>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <com/sun/star/chart/ChartDataRowSource.hpp>
44cdf0e10cSrcweir #include <com/sun/star/chart/ErrorBarStyle.hpp>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir //.............................................................................
47cdf0e10cSrcweir namespace chart
48cdf0e10cSrcweir {
49cdf0e10cSrcweir //.............................................................................
50cdf0e10cSrcweir using namespace ::com::sun::star;
51cdf0e10cSrcweir using namespace ::com::sun::star::chart2;
52cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
53cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
54cdf0e10cSrcweir using ::rtl::OUString;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir namespace
57cdf0e10cSrcweir {
58cdf0e10cSrcweir void lcl_addRanges( ::std::vector< ::rtl::OUString > & rOutResult,
59cdf0e10cSrcweir                     const uno::Reference< data::XLabeledDataSequence > & xLabeledSeq )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     if( ! xLabeledSeq.is())
62cdf0e10cSrcweir         return;
63cdf0e10cSrcweir     uno::Reference< data::XDataSequence > xSeq( xLabeledSeq->getLabel());
64cdf0e10cSrcweir     if( xSeq.is())
65cdf0e10cSrcweir         rOutResult.push_back( xSeq->getSourceRangeRepresentation());
66cdf0e10cSrcweir     xSeq.set( xLabeledSeq->getValues());
67cdf0e10cSrcweir     if( xSeq.is())
68cdf0e10cSrcweir         rOutResult.push_back( xSeq->getSourceRangeRepresentation());
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir void lcl_addDataSourceRanges(
72cdf0e10cSrcweir     ::std::vector< ::rtl::OUString > & rOutResult,
73cdf0e10cSrcweir     const uno::Reference< data::XDataSource > & xDataSource )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     if( xDataSource.is() )
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         uno::Sequence< uno::Reference< data::XLabeledDataSequence > > aDataSequences( xDataSource->getDataSequences() );
78cdf0e10cSrcweir         for( sal_Int32 i=0; i<aDataSequences.getLength(); ++i)
79cdf0e10cSrcweir             lcl_addRanges( rOutResult, aDataSequences[i] );
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir void lcl_addErrorBarRanges(
84cdf0e10cSrcweir     ::std::vector< ::rtl::OUString > & rOutResult,
85cdf0e10cSrcweir     const uno::Reference< XDataSeries > & xDataSeries )
86cdf0e10cSrcweir {
87cdf0e10cSrcweir     uno::Reference< beans::XPropertySet > xSeriesProp( xDataSeries, uno::UNO_QUERY );
88cdf0e10cSrcweir     if( !xSeriesProp.is())
89cdf0e10cSrcweir         return;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir     try
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         uno::Reference< beans::XPropertySet > xErrorBarProp;
94cdf0e10cSrcweir         if( ( xSeriesProp->getPropertyValue( C2U("ErrorBarY")) >>= xErrorBarProp ) &&
95cdf0e10cSrcweir             xErrorBarProp.is())
96cdf0e10cSrcweir         {
97cdf0e10cSrcweir             sal_Int32 eStyle = ::com::sun::star::chart::ErrorBarStyle::NONE;
98cdf0e10cSrcweir             if( ( xErrorBarProp->getPropertyValue( C2U("ErrorBarStyle")) >>= eStyle ) &&
99cdf0e10cSrcweir                 eStyle == ::com::sun::star::chart::ErrorBarStyle::FROM_DATA )
100cdf0e10cSrcweir             {
101cdf0e10cSrcweir                 uno::Reference< data::XDataSource > xErrorBarDataSource( xErrorBarProp, uno::UNO_QUERY );
102cdf0e10cSrcweir                 if( xErrorBarDataSource.is() )
103cdf0e10cSrcweir                     lcl_addDataSourceRanges( rOutResult, xErrorBarDataSource );
104cdf0e10cSrcweir             }
105cdf0e10cSrcweir         }
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir     catch( const uno::Exception & ex )
108cdf0e10cSrcweir     {
109cdf0e10cSrcweir         ASSERT_EXCEPTION( ex );
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir } // anonymous namespace
114cdf0e10cSrcweir 
115cdf0e10cSrcweir Reference< chart2::data::XDataSource > DataSourceHelper::createDataSource(
116cdf0e10cSrcweir         const Sequence< Reference< chart2::data::XLabeledDataSequence > >& rSequences )
117cdf0e10cSrcweir {
118cdf0e10cSrcweir     return new DataSource(rSequences);
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir Reference< chart2::data::XDataSequence > DataSourceHelper::createCachedDataSequence()
122cdf0e10cSrcweir {
123cdf0e10cSrcweir     return new ::chart::CachedDataSequence();
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir Reference< chart2::data::XDataSequence > DataSourceHelper::createCachedDataSequence( const ::rtl::OUString& rSingleText )
127cdf0e10cSrcweir {
128cdf0e10cSrcweir     return new ::chart::CachedDataSequence( rSingleText );
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir Reference< chart2::data::XLabeledDataSequence > DataSourceHelper::createLabeledDataSequence(
132cdf0e10cSrcweir         const Reference< chart2::data::XDataSequence >& xValues ,
133cdf0e10cSrcweir         const Reference< chart2::data::XDataSequence >& xLabels )
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     return new ::chart::LabeledDataSequence( xValues, xLabels );
136cdf0e10cSrcweir }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir Reference< chart2::data::XLabeledDataSequence > DataSourceHelper::createLabeledDataSequence(
139cdf0e10cSrcweir         const Reference< chart2::data::XDataSequence >& xValues )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir     return new ::chart::LabeledDataSequence( xValues );
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir Reference< chart2::data::XLabeledDataSequence > DataSourceHelper::createLabeledDataSequence(
145cdf0e10cSrcweir         const Reference< uno::XComponentContext >& xContext )
146cdf0e10cSrcweir {
147cdf0e10cSrcweir     return new ::chart::LabeledDataSequence( xContext );
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > DataSourceHelper::createArguments(
151cdf0e10cSrcweir                                             bool bUseColumns, bool bFirstCellAsLabel, bool bHasCategories )
152cdf0e10cSrcweir {
153cdf0e10cSrcweir     ::com::sun::star::chart::ChartDataRowSource eRowSource = ::com::sun::star::chart::ChartDataRowSource_ROWS;
154cdf0e10cSrcweir     if( bUseColumns )
155cdf0e10cSrcweir         eRowSource = ::com::sun::star::chart::ChartDataRowSource_COLUMNS;
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     uno::Sequence< beans::PropertyValue > aArguments(3);
158cdf0e10cSrcweir     aArguments[0] = beans::PropertyValue( C2U("DataRowSource")
159cdf0e10cSrcweir         , -1, uno::makeAny( eRowSource )
160cdf0e10cSrcweir         , beans::PropertyState_DIRECT_VALUE );
161cdf0e10cSrcweir     aArguments[1] = beans::PropertyValue( C2U("FirstCellAsLabel")
162cdf0e10cSrcweir         , -1, uno::makeAny( bFirstCellAsLabel )
163cdf0e10cSrcweir         , beans::PropertyState_DIRECT_VALUE );
164cdf0e10cSrcweir     aArguments[2] = beans::PropertyValue( C2U("HasCategories")
165cdf0e10cSrcweir         , -1, uno::makeAny( bHasCategories )
166cdf0e10cSrcweir         , beans::PropertyState_DIRECT_VALUE );
167cdf0e10cSrcweir 
168cdf0e10cSrcweir     return aArguments;
169cdf0e10cSrcweir }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > DataSourceHelper::createArguments(
172cdf0e10cSrcweir                                             const ::rtl::OUString & rRangeRepresentation,
173cdf0e10cSrcweir                                             const uno::Sequence< sal_Int32 >& rSequenceMapping,
174cdf0e10cSrcweir                                             bool bUseColumns, bool bFirstCellAsLabel, bool bHasCategories )
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     uno::Sequence< beans::PropertyValue > aArguments( createArguments( bUseColumns, bFirstCellAsLabel, bHasCategories ));
177cdf0e10cSrcweir     aArguments.realloc( aArguments.getLength() + 1 );
178cdf0e10cSrcweir     aArguments[aArguments.getLength() - 1] =
179cdf0e10cSrcweir         beans::PropertyValue( C2U("CellRangeRepresentation")
180cdf0e10cSrcweir                               , -1, uno::makeAny( rRangeRepresentation )
181cdf0e10cSrcweir                               , beans::PropertyState_DIRECT_VALUE );
182cdf0e10cSrcweir     if( rSequenceMapping.getLength() )
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir         aArguments.realloc( aArguments.getLength() + 1 );
185cdf0e10cSrcweir         aArguments[aArguments.getLength() - 1] =
186cdf0e10cSrcweir             beans::PropertyValue( C2U("SequenceMapping")
187cdf0e10cSrcweir                                 , -1, uno::makeAny( rSequenceMapping )
188cdf0e10cSrcweir                                 , beans::PropertyState_DIRECT_VALUE );
189cdf0e10cSrcweir     }
190cdf0e10cSrcweir     return aArguments;
191cdf0e10cSrcweir }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir void DataSourceHelper::readArguments( const uno::Sequence< beans::PropertyValue >& rArguments
194cdf0e10cSrcweir                                      , ::rtl::OUString & rRangeRepresentation, uno::Sequence< sal_Int32 >& rSequenceMapping
195cdf0e10cSrcweir             , bool& bUseColumns, bool& bFirstCellAsLabel, bool& bHasCategories )
196cdf0e10cSrcweir {
197cdf0e10cSrcweir     const beans::PropertyValue* pArguments = rArguments.getConstArray();
198cdf0e10cSrcweir 	for(sal_Int32 i=0; i<rArguments.getLength(); ++i, ++pArguments)
199cdf0e10cSrcweir 	{
200cdf0e10cSrcweir         const beans::PropertyValue& aProperty = *pArguments;
201cdf0e10cSrcweir 		if( aProperty.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DataRowSource" ) ))
202cdf0e10cSrcweir 		{
203cdf0e10cSrcweir             ::com::sun::star::chart::ChartDataRowSource eRowSource;
204cdf0e10cSrcweir 			if( aProperty.Value >>= eRowSource )
205cdf0e10cSrcweir                 bUseColumns = (eRowSource==::com::sun::star::chart::ChartDataRowSource_COLUMNS);
206cdf0e10cSrcweir 		}
207cdf0e10cSrcweir         else if( aProperty.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "FirstCellAsLabel" ) ))
208cdf0e10cSrcweir 		{
209cdf0e10cSrcweir 			aProperty.Value >>= bFirstCellAsLabel;
210cdf0e10cSrcweir 		}
211cdf0e10cSrcweir         else if( aProperty.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "HasCategories" ) ))
212cdf0e10cSrcweir 		{
213cdf0e10cSrcweir 			aProperty.Value >>= bHasCategories;
214cdf0e10cSrcweir 		}
215cdf0e10cSrcweir         else if( aProperty.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "CellRangeRepresentation" ) ))
216cdf0e10cSrcweir         {
217cdf0e10cSrcweir             aProperty.Value >>= rRangeRepresentation;
218cdf0e10cSrcweir         }
219cdf0e10cSrcweir         else if( aProperty.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "SequenceMapping" ) ))
220cdf0e10cSrcweir         {
221cdf0e10cSrcweir             aProperty.Value >>= rSequenceMapping;
222cdf0e10cSrcweir         }
223cdf0e10cSrcweir     }
224cdf0e10cSrcweir }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir uno::Reference< chart2::data::XDataSource > DataSourceHelper::pressUsedDataIntoRectangularFormat(
227cdf0e10cSrcweir         const uno::Reference< chart2::XChartDocument >& xChartDoc, bool bWithCategories )
228cdf0e10cSrcweir {
229cdf0e10cSrcweir     ::std::vector< Reference< chart2::data::XLabeledDataSequence > > aResultVector;
230cdf0e10cSrcweir 
231cdf0e10cSrcweir     //categories are always the first sequence
232cdf0e10cSrcweir     Reference< chart2::XDiagram > xDiagram( xChartDoc->getFirstDiagram());
233cdf0e10cSrcweir 
234cdf0e10cSrcweir     if( bWithCategories )
235cdf0e10cSrcweir     {
236cdf0e10cSrcweir         Reference< chart2::data::XLabeledDataSequence > xCategories( DiagramHelper::getCategoriesFromDiagram( xDiagram ) );
237cdf0e10cSrcweir         if( xCategories.is() )
238cdf0e10cSrcweir             aResultVector.push_back( xCategories );
239cdf0e10cSrcweir     }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir     ::std::vector< Reference< chart2::XDataSeries > > xSeriesVector( DiagramHelper::getDataSeriesFromDiagram( xDiagram ) );
242cdf0e10cSrcweir     uno::Reference< chart2::data::XDataSource > xSeriesSource(
243cdf0e10cSrcweir         DataSeriesHelper::getDataSource( ContainerHelper::ContainerToSequence(xSeriesVector) ) );
244cdf0e10cSrcweir     Sequence< Reference< chart2::data::XLabeledDataSequence > > aDataSeqences( xSeriesSource->getDataSequences() );
245cdf0e10cSrcweir 
246cdf0e10cSrcweir     //the first x-values is always the next sequence //todo ... other x-values get lost for old format
247cdf0e10cSrcweir     Reference< chart2::data::XLabeledDataSequence > xXValues(
248cdf0e10cSrcweir         DataSeriesHelper::getDataSequenceByRole( xSeriesSource, C2U("values-x") ) );
249cdf0e10cSrcweir     if( xXValues.is() )
250cdf0e10cSrcweir         aResultVector.push_back( xXValues );
251cdf0e10cSrcweir 
252cdf0e10cSrcweir     //add all other sequences now without x-values
253cdf0e10cSrcweir     for( sal_Int32 nN=0; nN<aDataSeqences.getLength(); nN++ )
254cdf0e10cSrcweir     {
255cdf0e10cSrcweir         OUString aRole( DataSeriesHelper::GetRole( aDataSeqences[nN] ) );
256cdf0e10cSrcweir         if( !aRole.equals(C2U("values-x")) )
257cdf0e10cSrcweir             aResultVector.push_back( aDataSeqences[nN] );
258cdf0e10cSrcweir     }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir     Sequence< Reference< chart2::data::XLabeledDataSequence > > aResultSequence( aResultVector.size() );
261cdf0e10cSrcweir     ::std::copy( aResultVector.begin(), aResultVector.end(), aResultSequence.getArray() );
262cdf0e10cSrcweir 
263cdf0e10cSrcweir     return new DataSource( aResultSequence );
264cdf0e10cSrcweir }
265cdf0e10cSrcweir 
266cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > DataSourceHelper::getUsedDataRanges(
267cdf0e10cSrcweir     const uno::Reference< chart2::XDiagram > & xDiagram )
268cdf0e10cSrcweir {
269cdf0e10cSrcweir     ::std::vector< ::rtl::OUString > aResult;
270cdf0e10cSrcweir 
271cdf0e10cSrcweir     if( xDiagram.is())
272cdf0e10cSrcweir     {
273cdf0e10cSrcweir         uno::Reference< data::XLabeledDataSequence > xCategories( DiagramHelper::getCategoriesFromDiagram( xDiagram ) );
274cdf0e10cSrcweir         if( xCategories.is() )
275cdf0e10cSrcweir             lcl_addRanges( aResult, xCategories );
276cdf0e10cSrcweir 
277cdf0e10cSrcweir         ::std::vector< uno::Reference< XDataSeries > > aSeriesVector( DiagramHelper::getDataSeriesFromDiagram( xDiagram ) );
278cdf0e10cSrcweir         for( ::std::vector< uno::Reference< XDataSeries > >::const_iterator aSeriesIt( aSeriesVector.begin() )
279cdf0e10cSrcweir                  ; aSeriesIt != aSeriesVector.end(); ++aSeriesIt )
280cdf0e10cSrcweir         {
281cdf0e10cSrcweir             uno::Reference< data::XDataSource > xDataSource( *aSeriesIt, uno::UNO_QUERY );
282cdf0e10cSrcweir             lcl_addDataSourceRanges( aResult, xDataSource );
283cdf0e10cSrcweir             lcl_addErrorBarRanges( aResult, *aSeriesIt );
284cdf0e10cSrcweir         }
285cdf0e10cSrcweir     }
286cdf0e10cSrcweir 
287cdf0e10cSrcweir     return ContainerHelper::ContainerToSequence( aResult );
288cdf0e10cSrcweir }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > DataSourceHelper::getUsedDataRanges( const uno::Reference< frame::XModel > & xChartModel )
291cdf0e10cSrcweir {
292cdf0e10cSrcweir     uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xChartModel ) );
293cdf0e10cSrcweir     return getUsedDataRanges( xDiagram );
294cdf0e10cSrcweir }
295cdf0e10cSrcweir 
296cdf0e10cSrcweir uno::Reference< chart2::data::XDataSource > DataSourceHelper::getUsedData(
297cdf0e10cSrcweir     const uno::Reference< chart2::XChartDocument >& xChartDoc )
298cdf0e10cSrcweir {
299cdf0e10cSrcweir     return pressUsedDataIntoRectangularFormat( xChartDoc );
300cdf0e10cSrcweir }
301cdf0e10cSrcweir 
302cdf0e10cSrcweir uno::Reference< chart2::data::XDataSource > DataSourceHelper::getUsedData(
303cdf0e10cSrcweir     const uno::Reference< frame::XModel >& xChartModel )
304cdf0e10cSrcweir {
305cdf0e10cSrcweir     ::std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aResult;
306cdf0e10cSrcweir 
307cdf0e10cSrcweir     uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xChartModel ) );
308cdf0e10cSrcweir     uno::Reference< data::XLabeledDataSequence > xCategories( DiagramHelper::getCategoriesFromDiagram( xDiagram ) );
309cdf0e10cSrcweir     if( xCategories.is() )
310cdf0e10cSrcweir         aResult.push_back( xCategories );
311cdf0e10cSrcweir 
312cdf0e10cSrcweir     ::std::vector< uno::Reference< XDataSeries > > aSeriesVector( ChartModelHelper::getDataSeries( xChartModel ) );
313cdf0e10cSrcweir     for( ::std::vector< uno::Reference< XDataSeries > >::const_iterator aSeriesIt( aSeriesVector.begin() )
314cdf0e10cSrcweir         ; aSeriesIt != aSeriesVector.end(); ++aSeriesIt )
315cdf0e10cSrcweir     {
316cdf0e10cSrcweir         uno::Reference< data::XDataSource > xDataSource( *aSeriesIt, uno::UNO_QUERY );
317cdf0e10cSrcweir         if( !xDataSource.is() )
318cdf0e10cSrcweir             continue;
319cdf0e10cSrcweir         uno::Sequence< uno::Reference< data::XLabeledDataSequence > > aDataSequences( xDataSource->getDataSequences() );
320cdf0e10cSrcweir         ::std::copy( aDataSequences.getConstArray(), aDataSequences.getConstArray() + aDataSequences.getLength(),
321cdf0e10cSrcweir                      ::std::back_inserter( aResult ));
322cdf0e10cSrcweir     }
323cdf0e10cSrcweir 
324cdf0e10cSrcweir     return uno::Reference< chart2::data::XDataSource >(
325cdf0e10cSrcweir         new DataSource( ContainerHelper::ContainerToSequence( aResult )));
326cdf0e10cSrcweir }
327cdf0e10cSrcweir 
328cdf0e10cSrcweir bool DataSourceHelper::detectRangeSegmentation(
329cdf0e10cSrcweir     const uno::Reference<
330cdf0e10cSrcweir         frame::XModel >& xChartModel
331cdf0e10cSrcweir     , ::rtl::OUString& rOutRangeString
332cdf0e10cSrcweir     , ::com::sun::star::uno::Sequence< sal_Int32 >& rSequenceMapping
333cdf0e10cSrcweir     , bool& rOutUseColumns
334cdf0e10cSrcweir     , bool& rOutFirstCellAsLabel
335cdf0e10cSrcweir     , bool& rOutHasCategories )
336cdf0e10cSrcweir {
337cdf0e10cSrcweir     bool bSomethingDetected = false;
338cdf0e10cSrcweir 
339cdf0e10cSrcweir     uno::Reference< XChartDocument > xChartDocument( xChartModel, uno::UNO_QUERY );
340cdf0e10cSrcweir     if( !xChartDocument.is() )
341cdf0e10cSrcweir         return bSomethingDetected;
342cdf0e10cSrcweir     uno::Reference< data::XDataProvider >  xDataProvider( xChartDocument->getDataProvider() );
343cdf0e10cSrcweir     if( !xDataProvider.is() )
344cdf0e10cSrcweir         return bSomethingDetected;
345cdf0e10cSrcweir 
346cdf0e10cSrcweir     try
347cdf0e10cSrcweir     {
348cdf0e10cSrcweir         DataSourceHelper::readArguments(
349cdf0e10cSrcweir             xDataProvider->detectArguments( pressUsedDataIntoRectangularFormat( xChartDocument ) ),
350cdf0e10cSrcweir             rOutRangeString, rSequenceMapping, rOutUseColumns, rOutFirstCellAsLabel, rOutHasCategories );
351cdf0e10cSrcweir         bSomethingDetected = (rOutRangeString.getLength() > 0);
352cdf0e10cSrcweir 
353cdf0e10cSrcweir         uno::Reference< chart2::data::XLabeledDataSequence > xCategories(
354cdf0e10cSrcweir                     DiagramHelper::getCategoriesFromDiagram( xChartDocument->getFirstDiagram() ));
355cdf0e10cSrcweir         rOutHasCategories = xCategories.is();
356cdf0e10cSrcweir     }
357cdf0e10cSrcweir     catch( uno::Exception & ex )
358cdf0e10cSrcweir     {
359cdf0e10cSrcweir         ASSERT_EXCEPTION( ex );
360cdf0e10cSrcweir     }
361cdf0e10cSrcweir     return bSomethingDetected;
362cdf0e10cSrcweir }
363cdf0e10cSrcweir 
364cdf0e10cSrcweir bool DataSourceHelper::allArgumentsForRectRangeDetected(
365cdf0e10cSrcweir     const uno::Reference< chart2::XChartDocument >& xChartDocument )
366cdf0e10cSrcweir {
367cdf0e10cSrcweir     bool bHasDataRowSource = false;
368cdf0e10cSrcweir     bool bHasFirstCellAsLabel = false;
369cdf0e10cSrcweir //     bool bHasHasCategories = false;
370cdf0e10cSrcweir     bool bHasCellRangeRepresentation = false;
371cdf0e10cSrcweir //     bool bHasSequenceMapping = false;
372cdf0e10cSrcweir 
373cdf0e10cSrcweir     uno::Reference< data::XDataProvider > xDataProvider( xChartDocument->getDataProvider() );
374cdf0e10cSrcweir     if( !xDataProvider.is() )
375cdf0e10cSrcweir         return false;
376cdf0e10cSrcweir 
377cdf0e10cSrcweir     try
378cdf0e10cSrcweir     {
379cdf0e10cSrcweir         const uno::Sequence< beans::PropertyValue > aArguments(
380cdf0e10cSrcweir             xDataProvider->detectArguments( pressUsedDataIntoRectangularFormat( xChartDocument )));
381cdf0e10cSrcweir         const beans::PropertyValue* pArguments = aArguments.getConstArray();
382cdf0e10cSrcweir         for(sal_Int32 i=0; i<aArguments.getLength(); ++i, ++pArguments)
383cdf0e10cSrcweir         {
384cdf0e10cSrcweir             const beans::PropertyValue& aProperty = *pArguments;
385cdf0e10cSrcweir             if( aProperty.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DataRowSource" ) ))
386cdf0e10cSrcweir             {
387cdf0e10cSrcweir                 bHasDataRowSource =
388cdf0e10cSrcweir                     (aProperty.Value.hasValue() && aProperty.Value.isExtractableTo(
389cdf0e10cSrcweir                         ::getCppuType( reinterpret_cast<
390cdf0e10cSrcweir                                        const ::com::sun::star::chart::ChartDataRowSource * >(0))));
391cdf0e10cSrcweir             }
392cdf0e10cSrcweir             else if( aProperty.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "FirstCellAsLabel" ) ))
393cdf0e10cSrcweir             {
394cdf0e10cSrcweir                 bHasFirstCellAsLabel =
395cdf0e10cSrcweir                     (aProperty.Value.hasValue() && aProperty.Value.isExtractableTo(::getBooleanCppuType()));
396cdf0e10cSrcweir             }
397cdf0e10cSrcweir //             else if( aProperty.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "HasCategories" ) ))
398cdf0e10cSrcweir //             {
399cdf0e10cSrcweir //                 bHasHasCategories =
400cdf0e10cSrcweir //                     (aProperty.Value.hasValue() && aProperty.Value.isExtractableTo(::getBooleanCppuType()));
401cdf0e10cSrcweir //             }
402cdf0e10cSrcweir             else if( aProperty.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "CellRangeRepresentation" ) ))
403cdf0e10cSrcweir             {
404cdf0e10cSrcweir                 ::rtl::OUString aRange;
405cdf0e10cSrcweir                 bHasCellRangeRepresentation =
406cdf0e10cSrcweir                     (aProperty.Value.hasValue() && (aProperty.Value >>= aRange) && aRange.getLength() > 0);
407cdf0e10cSrcweir             }
408cdf0e10cSrcweir //         else if( aProperty.Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "SequenceMapping" ) ))
409cdf0e10cSrcweir //         {
410cdf0e10cSrcweir //             bHasSequenceMapping =
411cdf0e10cSrcweir //                 (aProperty.Value.hasValue() && aProperty.Value.isExtractableTo(
412cdf0e10cSrcweir //                     ::getCppuType( reinterpret_cast<
413cdf0e10cSrcweir //                                    const uno::Sequence< sal_Int32 > * >(0))));
414cdf0e10cSrcweir //         }
415cdf0e10cSrcweir         }
416cdf0e10cSrcweir     }
417cdf0e10cSrcweir     catch( const uno::Exception & ex )
418cdf0e10cSrcweir     {
419cdf0e10cSrcweir         ASSERT_EXCEPTION( ex );
420cdf0e10cSrcweir     }
421cdf0e10cSrcweir 
422cdf0e10cSrcweir     return (bHasCellRangeRepresentation && bHasDataRowSource && bHasFirstCellAsLabel);
423cdf0e10cSrcweir }
424cdf0e10cSrcweir 
425cdf0e10cSrcweir void DataSourceHelper::setRangeSegmentation(
426cdf0e10cSrcweir             const uno::Reference< frame::XModel >& xChartModel
427cdf0e10cSrcweir             , const ::com::sun::star::uno::Sequence< sal_Int32 >& rSequenceMapping
428cdf0e10cSrcweir             , bool bUseColumns , bool bFirstCellAsLabel, bool bUseCategories )
429cdf0e10cSrcweir {
430cdf0e10cSrcweir     uno::Reference< XChartDocument > xChartDocument( xChartModel, uno::UNO_QUERY );
431cdf0e10cSrcweir     if( !xChartDocument.is() )
432cdf0e10cSrcweir         return;
433cdf0e10cSrcweir     uno::Reference< data::XDataProvider > xDataProvider( xChartDocument->getDataProvider() );
434cdf0e10cSrcweir     if( !xDataProvider.is() )
435cdf0e10cSrcweir         return;
436cdf0e10cSrcweir     uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xChartModel ) );
437cdf0e10cSrcweir     if( !xDiagram.is() )
438cdf0e10cSrcweir         return;
439cdf0e10cSrcweir     uno::Reference< chart2::XChartTypeManager > xChartTypeManager( xChartDocument->getChartTypeManager() );
440cdf0e10cSrcweir     if( !xChartTypeManager.is() )
441cdf0e10cSrcweir         return;
442cdf0e10cSrcweir     uno::Reference< lang::XMultiServiceFactory > xTemplateFactory( xChartTypeManager, uno::UNO_QUERY );
443cdf0e10cSrcweir     if( !xTemplateFactory.is() )
444cdf0e10cSrcweir         return;
445cdf0e10cSrcweir 
446cdf0e10cSrcweir     ::rtl::OUString aRangeString;
447cdf0e10cSrcweir     bool bDummy;
448cdf0e10cSrcweir     uno::Sequence< sal_Int32 > aDummy;
449cdf0e10cSrcweir     readArguments( xDataProvider->detectArguments( pressUsedDataIntoRectangularFormat( xChartDocument )),
450cdf0e10cSrcweir                    aRangeString, aDummy, bDummy, bDummy, bDummy );
451cdf0e10cSrcweir 
452cdf0e10cSrcweir     uno::Sequence< beans::PropertyValue > aArguments(
453cdf0e10cSrcweir         createArguments( aRangeString, rSequenceMapping, bUseColumns, bFirstCellAsLabel, bUseCategories ) );
454cdf0e10cSrcweir 
455cdf0e10cSrcweir     uno::Reference< chart2::data::XDataSource > xDataSource( xDataProvider->createDataSource(
456cdf0e10cSrcweir                                                                  aArguments ) );
457cdf0e10cSrcweir     if( !xDataSource.is() )
458cdf0e10cSrcweir         return;
459cdf0e10cSrcweir 
460cdf0e10cSrcweir     ControllerLockGuard aCtrlLockGuard( xChartModel );
461cdf0e10cSrcweir     xDiagram->setDiagramData( xDataSource, aArguments );
462cdf0e10cSrcweir }
463cdf0e10cSrcweir 
464cdf0e10cSrcweir Sequence< OUString > DataSourceHelper::getRangesFromLabeledDataSequence(
465cdf0e10cSrcweir     const Reference< data::XLabeledDataSequence > & xLSeq )
466cdf0e10cSrcweir {
467cdf0e10cSrcweir     Sequence< OUString > aResult;
468cdf0e10cSrcweir     if( xLSeq.is())
469cdf0e10cSrcweir     {
470cdf0e10cSrcweir         Reference< data::XDataSequence > xLabel( xLSeq->getLabel());
471cdf0e10cSrcweir         Reference< data::XDataSequence > xValues( xLSeq->getValues());
472cdf0e10cSrcweir 
473cdf0e10cSrcweir         if( xLabel.is())
474cdf0e10cSrcweir         {
475cdf0e10cSrcweir             if( xValues.is())
476cdf0e10cSrcweir             {
477cdf0e10cSrcweir                 aResult.realloc( 2 );
478cdf0e10cSrcweir                 aResult[0] = xLabel->getSourceRangeRepresentation();
479cdf0e10cSrcweir                 aResult[1] = xValues->getSourceRangeRepresentation();
480cdf0e10cSrcweir             }
481cdf0e10cSrcweir             else
482cdf0e10cSrcweir             {
483cdf0e10cSrcweir                 aResult.realloc( 1 );
484cdf0e10cSrcweir                 aResult[0] = xLabel->getSourceRangeRepresentation();
485cdf0e10cSrcweir             }
486cdf0e10cSrcweir         }
487cdf0e10cSrcweir         else if( xValues.is())
488cdf0e10cSrcweir         {
489cdf0e10cSrcweir             aResult.realloc( 1 );
490cdf0e10cSrcweir             aResult[0] = xValues->getSourceRangeRepresentation();
491cdf0e10cSrcweir         }
492cdf0e10cSrcweir     }
493cdf0e10cSrcweir     return aResult;
494cdf0e10cSrcweir }
495cdf0e10cSrcweir 
496cdf0e10cSrcweir OUString DataSourceHelper::getRangeFromValues(
497cdf0e10cSrcweir     const Reference< data::XLabeledDataSequence > & xLSeq )
498cdf0e10cSrcweir {
499cdf0e10cSrcweir     OUString aResult;
500cdf0e10cSrcweir     if( xLSeq.is() )
501cdf0e10cSrcweir     {
502cdf0e10cSrcweir         Reference< data::XDataSequence > xValues( xLSeq->getValues() );
503cdf0e10cSrcweir         if( xValues.is() )
504cdf0e10cSrcweir             aResult = xValues->getSourceRangeRepresentation();
505cdf0e10cSrcweir     }
506cdf0e10cSrcweir     return aResult;
507cdf0e10cSrcweir }
508cdf0e10cSrcweir 
509cdf0e10cSrcweir Sequence< OUString > DataSourceHelper::getRangesFromDataSource( const Reference< data::XDataSource > & xSource )
510cdf0e10cSrcweir {
511cdf0e10cSrcweir     ::std::vector< OUString > aResult;
512cdf0e10cSrcweir     if( xSource.is())
513cdf0e10cSrcweir     {
514cdf0e10cSrcweir         Sequence< Reference< data::XLabeledDataSequence > > aLSeqSeq( xSource->getDataSequences());
515cdf0e10cSrcweir         for( sal_Int32 i=0; i<aLSeqSeq.getLength(); ++i )
516cdf0e10cSrcweir         {
517cdf0e10cSrcweir             Reference< data::XDataSequence > xLabel( aLSeqSeq[i]->getLabel());
518cdf0e10cSrcweir             Reference< data::XDataSequence > xValues( aLSeqSeq[i]->getValues());
519cdf0e10cSrcweir 
520cdf0e10cSrcweir             if( xLabel.is())
521cdf0e10cSrcweir                 aResult.push_back( xLabel->getSourceRangeRepresentation());
522cdf0e10cSrcweir             if( xValues.is())
523cdf0e10cSrcweir                 aResult.push_back( xValues->getSourceRangeRepresentation());
524cdf0e10cSrcweir         }
525cdf0e10cSrcweir     }
526cdf0e10cSrcweir     return ContainerHelper::ContainerToSequence( aResult );
527cdf0e10cSrcweir }
528cdf0e10cSrcweir 
529cdf0e10cSrcweir //.............................................................................
530cdf0e10cSrcweir } //namespace chart
531cdf0e10cSrcweir //.............................................................................
532