1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "ColumnLineChartTypeTemplate.hxx"
22 #include "CommonConverters.hxx"
23 #include "DiagramHelper.hxx"
24 #include "DataSeriesHelper.hxx"
25 #include "servicenames_charttypes.hxx"
26 #include "ColumnLineDataInterpreter.hxx"
27 #include "ContainerHelper.hxx"
28 #include "PropertyHelper.hxx"
29 #include <com/sun/star/beans/PropertyAttribute.hpp>
30 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
31 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
32 #include <com/sun/star/drawing/LineStyle.hpp>
36 using namespace ::com::sun::star::chart2
;
37 using namespace ::com::sun::star
;
39 using ::com::sun::star::uno::Reference
;
40 using ::com::sun::star::uno::Sequence
;
41 using ::com::sun::star::beans::Property
;
42 using ::com::sun::star::uno::Any
;
43 using ::osl::MutexGuard
;
50 PROP_COL_LINE_NUMBER_OF_LINES
53 void lcl_AddPropertiesToVector(
54 ::std::vector
< Property
> & rOutProperties
)
56 rOutProperties
.push_back(
57 Property( "NumberOfLines",
58 PROP_COL_LINE_NUMBER_OF_LINES
,
59 cppu::UnoType
<sal_Int32
>::get(),
60 beans::PropertyAttribute::BOUND
61 | beans::PropertyAttribute::MAYBEDEFAULT
));
64 struct StaticColumnLineChartTypeTemplateDefaults_Initializer
66 ::chart::tPropertyValueMap
* operator()()
68 static ::chart::tPropertyValueMap aStaticDefaults
;
69 lcl_AddDefaultsToMap( aStaticDefaults
);
70 return &aStaticDefaults
;
73 static void lcl_AddDefaultsToMap( ::chart::tPropertyValueMap
& rOutMap
)
75 ::chart::PropertyHelper::setPropertyValueDefault
< sal_Int32
>( rOutMap
, PROP_COL_LINE_NUMBER_OF_LINES
, 1 );
79 struct StaticColumnLineChartTypeTemplateDefaults
: public rtl::StaticAggregate
< ::chart::tPropertyValueMap
, StaticColumnLineChartTypeTemplateDefaults_Initializer
>
83 struct StaticColumnLineChartTypeTemplateInfoHelper_Initializer
85 ::cppu::OPropertyArrayHelper
* operator()()
87 static ::cppu::OPropertyArrayHelper
aPropHelper( lcl_GetPropertySequence() );
92 static uno::Sequence
< Property
> lcl_GetPropertySequence()
94 ::std::vector
< ::com::sun::star::beans::Property
> aProperties
;
95 lcl_AddPropertiesToVector( aProperties
);
97 ::std::sort( aProperties
.begin(), aProperties
.end(),
98 ::chart::PropertyNameLess() );
100 return ::chart::ContainerHelper::ContainerToSequence( aProperties
);
105 struct StaticColumnLineChartTypeTemplateInfoHelper
: public rtl::StaticAggregate
< ::cppu::OPropertyArrayHelper
, StaticColumnLineChartTypeTemplateInfoHelper_Initializer
>
109 struct StaticColumnLineChartTypeTemplateInfo_Initializer
111 uno::Reference
< beans::XPropertySetInfo
>* operator()()
113 static uno::Reference
< beans::XPropertySetInfo
> xPropertySetInfo(
114 ::cppu::OPropertySetHelper::createPropertySetInfo(*StaticColumnLineChartTypeTemplateInfoHelper::get() ) );
115 return &xPropertySetInfo
;
119 struct StaticColumnLineChartTypeTemplateInfo
: public rtl::StaticAggregate
< uno::Reference
< beans::XPropertySetInfo
>, StaticColumnLineChartTypeTemplateInfo_Initializer
>
123 } // anonymous namespace
128 ColumnLineChartTypeTemplate::ColumnLineChartTypeTemplate(
130 uno::XComponentContext
> const & xContext
,
131 const OUString
& rServiceName
,
132 StackMode eStackMode
,
133 sal_Int32 nNumberOfLines
) :
134 ChartTypeTemplate( xContext
, rServiceName
),
135 ::property::OPropertySet( m_aMutex
),
136 m_eStackMode( eStackMode
)
138 setFastPropertyValue_NoBroadcast( PROP_COL_LINE_NUMBER_OF_LINES
, uno::makeAny( nNumberOfLines
));
141 ColumnLineChartTypeTemplate::~ColumnLineChartTypeTemplate()
144 // ____ OPropertySet ____
145 uno::Any
ColumnLineChartTypeTemplate::GetDefaultValue( sal_Int32 nHandle
) const
146 throw(beans::UnknownPropertyException
)
148 const tPropertyValueMap
& rStaticDefaults
= *StaticColumnLineChartTypeTemplateDefaults::get();
149 tPropertyValueMap::const_iterator
aFound( rStaticDefaults
.find( nHandle
) );
150 if( aFound
== rStaticDefaults
.end() )
152 return (*aFound
).second
;
155 ::cppu::IPropertyArrayHelper
& SAL_CALL
ColumnLineChartTypeTemplate::getInfoHelper()
157 return *StaticColumnLineChartTypeTemplateInfoHelper::get();
160 // ____ XPropertySet ____
161 uno::Reference
< beans::XPropertySetInfo
> SAL_CALL
ColumnLineChartTypeTemplate::getPropertySetInfo()
162 throw (uno::RuntimeException
, std::exception
)
164 return *StaticColumnLineChartTypeTemplateInfo::get();
167 void ColumnLineChartTypeTemplate::createChartTypes(
168 const Sequence
< Sequence
< Reference
< XDataSeries
> > > & aSeriesSeq
,
169 const Sequence
< Reference
< XCoordinateSystem
> > & rCoordSys
,
170 const Sequence
< Reference
< XChartType
> >& aOldChartTypesSeq
)
172 if( rCoordSys
.getLength() == 0 ||
173 ! rCoordSys
[0].is() )
178 Reference
< lang::XMultiServiceFactory
> xFact(
179 GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW
);
180 Sequence
< Reference
< XDataSeries
> > aFlatSeriesSeq( FlattenSequence( aSeriesSeq
));
181 sal_Int32 nNumberOfSeries
= aFlatSeriesSeq
.getLength();
182 sal_Int32 nNumberOfLines
= 0;
183 sal_Int32 nNumberOfColumns
= 0;
185 getFastPropertyValue( PROP_COL_LINE_NUMBER_OF_LINES
) >>= nNumberOfLines
;
186 OSL_ENSURE( nNumberOfLines
>=0, "number of lines should be not negative" );
187 if( nNumberOfLines
< 0 )
190 if( nNumberOfLines
>= nNumberOfSeries
)
192 if( nNumberOfSeries
> 0 )
194 nNumberOfLines
= nNumberOfSeries
- 1;
195 nNumberOfColumns
= 1;
201 nNumberOfColumns
= nNumberOfSeries
- nNumberOfLines
;
205 Reference
< XChartType
> xCT(
206 xFact
->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN
), uno::UNO_QUERY_THROW
);
208 ChartTypeTemplate::copyPropertiesFromOldToNewCoordianteSystem( aOldChartTypesSeq
, xCT
);
210 Reference
< XChartTypeContainer
> xCTCnt( rCoordSys
[ 0 ], uno::UNO_QUERY_THROW
);
211 xCTCnt
->setChartTypes( Sequence
< Reference
< chart2::XChartType
> >( &xCT
, 1 ));
213 if( nNumberOfColumns
> 0 )
215 Reference
< XDataSeriesContainer
> xDSCnt( xCT
, uno::UNO_QUERY_THROW
);
216 Sequence
< Reference
< XDataSeries
> > aColumnSeq( nNumberOfColumns
);
217 ::std::copy( aFlatSeriesSeq
.getConstArray(),
218 aFlatSeriesSeq
.getConstArray() + nNumberOfColumns
,
219 aColumnSeq
.getArray());
220 xDSCnt
->setDataSeries( aColumnSeq
);
225 xCT
.set( xFact
->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_LINE
), uno::UNO_QUERY_THROW
);
226 xCTCnt
.set( rCoordSys
[ 0 ], uno::UNO_QUERY_THROW
);
227 xCTCnt
->addChartType( xCT
);
229 if( nNumberOfLines
> 0 )
231 Reference
< XDataSeriesContainer
> xDSCnt( xCT
, uno::UNO_QUERY_THROW
);
232 Sequence
< Reference
< XDataSeries
> > aLineSeq( nNumberOfLines
);
233 ::std::copy( aFlatSeriesSeq
.getConstArray() + nNumberOfColumns
,
234 aFlatSeriesSeq
.getConstArray() + aFlatSeriesSeq
.getLength(),
235 aLineSeq
.getArray());
236 xDSCnt
->setDataSeries( aLineSeq
);
239 catch( const uno::Exception
& ex
)
241 ASSERT_EXCEPTION( ex
);
245 void SAL_CALL
ColumnLineChartTypeTemplate::applyStyle(
246 const Reference
< chart2::XDataSeries
>& xSeries
,
247 ::sal_Int32 nChartTypeIndex
,
248 ::sal_Int32 nSeriesIndex
,
249 ::sal_Int32 nSeriesCount
)
250 throw (uno::RuntimeException
, std::exception
)
252 ChartTypeTemplate::applyStyle( xSeries
, nChartTypeIndex
, nSeriesIndex
, nSeriesCount
);
254 if( nChartTypeIndex
==0 ) // columns
256 DataSeriesHelper::setPropertyAlsoToAllAttributedDataPoints( xSeries
, "BorderStyle", uno::makeAny( drawing::LineStyle_NONE
) );
258 else if( nChartTypeIndex
==1 ) // lines
260 Reference
< beans::XPropertySet
> xProp( xSeries
, uno::UNO_QUERY
);
263 DataSeriesHelper::switchLinesOnOrOff( xProp
, true );
264 DataSeriesHelper::switchSymbolsOnOrOff( xProp
, false, nSeriesIndex
);
265 DataSeriesHelper::makeLinesThickOrThin( xProp
, true );
270 StackMode
ColumnLineChartTypeTemplate::getStackMode( sal_Int32 nChartTypeIndex
) const
272 if( nChartTypeIndex
== 0 )
274 return StackMode_NONE
;
277 // ____ XChartTypeTemplate ____
278 sal_Bool SAL_CALL
ColumnLineChartTypeTemplate::matchesTemplate(
279 const uno::Reference
< XDiagram
>& xDiagram
,
280 sal_Bool bAdaptProperties
)
281 throw (uno::RuntimeException
, std::exception
)
283 bool bResult
= false;
290 Reference
< chart2::XChartType
> xColumnChartType
;
291 Reference
< XCoordinateSystem
> xColumnChartCooSys
;
292 Reference
< chart2::XChartType
> xLineChartType
;
293 sal_Int32 nNumberOfChartTypes
= 0;
295 Reference
< XCoordinateSystemContainer
> xCooSysCnt(
296 xDiagram
, uno::UNO_QUERY_THROW
);
297 Sequence
< Reference
< XCoordinateSystem
> > aCooSysSeq(
298 xCooSysCnt
->getCoordinateSystems());
299 for( sal_Int32 i
=0; i
<aCooSysSeq
.getLength(); ++i
)
301 Reference
< XChartTypeContainer
> xCTCnt( aCooSysSeq
[i
], uno::UNO_QUERY_THROW
);
302 Sequence
< Reference
< XChartType
> > aChartTypeSeq( xCTCnt
->getChartTypes());
303 for( sal_Int32 j
=0; j
<aChartTypeSeq
.getLength(); ++j
)
305 if( aChartTypeSeq
[j
].is())
307 ++nNumberOfChartTypes
;
308 if( nNumberOfChartTypes
> 2 )
310 OUString aCTService
= aChartTypeSeq
[j
]->getChartType();
311 if( aCTService
== CHART2_SERVICE_NAME_CHARTTYPE_COLUMN
)
313 xColumnChartType
.set( aChartTypeSeq
[j
] );
314 xColumnChartCooSys
.set( aCooSysSeq
[i
] );
316 else if( aCTService
== CHART2_SERVICE_NAME_CHARTTYPE_LINE
)
317 xLineChartType
.set( aChartTypeSeq
[j
] );
320 if( nNumberOfChartTypes
> 2 )
324 if( nNumberOfChartTypes
== 2 &&
325 xColumnChartType
.is() &&
328 OSL_ASSERT( xColumnChartCooSys
.is());
330 // check stackmode of bars
331 bResult
= (xColumnChartCooSys
->getDimension() == getDimension());
335 bool bAmbiguous
=false;
336 bResult
= ( DiagramHelper::getStackModeFromChartType(
337 xColumnChartType
, bFound
, bAmbiguous
,
339 == getStackMode( 0 ) );
341 if( bResult
&& bAdaptProperties
)
343 Reference
< XDataSeriesContainer
> xSeriesContainer( xLineChartType
, uno::UNO_QUERY
);
344 if( xSeriesContainer
.is() )
346 sal_Int32 nNumberOfLines
= xSeriesContainer
->getDataSeries().getLength();
347 setFastPropertyValue_NoBroadcast( PROP_COL_LINE_NUMBER_OF_LINES
, uno::makeAny( nNumberOfLines
));
353 catch( const uno::Exception
& ex
)
355 ASSERT_EXCEPTION( ex
);
361 Reference
< chart2::XChartType
> ColumnLineChartTypeTemplate::getChartTypeForIndex( sal_Int32 nChartTypeIndex
)
363 Reference
< chart2::XChartType
> xCT
;
364 Reference
< lang::XMultiServiceFactory
> xFact(
365 GetComponentContext()->getServiceManager(), uno::UNO_QUERY
);
368 if( nChartTypeIndex
== 0 )
369 xCT
.set( xFact
->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_COLUMN
), uno::UNO_QUERY
);
371 xCT
.set( xFact
->createInstance( CHART2_SERVICE_NAME_CHARTTYPE_LINE
), uno::UNO_QUERY
);
376 Reference
< XChartType
> SAL_CALL
ColumnLineChartTypeTemplate::getChartTypeForNewSeries(
377 const uno::Sequence
< Reference
< chart2::XChartType
> >& aFormerlyUsedChartTypes
)
378 throw (uno::RuntimeException
, std::exception
)
380 Reference
< chart2::XChartType
> xResult
;
384 Reference
< lang::XMultiServiceFactory
> xFact(
385 GetComponentContext()->getServiceManager(), uno::UNO_QUERY_THROW
);
386 xResult
.set( xFact
->createInstance(
387 CHART2_SERVICE_NAME_CHARTTYPE_LINE
), uno::UNO_QUERY_THROW
);
388 ChartTypeTemplate::copyPropertiesFromOldToNewCoordianteSystem( aFormerlyUsedChartTypes
, xResult
);
390 catch( const uno::Exception
& ex
)
392 ASSERT_EXCEPTION( ex
);
398 Reference
< XDataInterpreter
> SAL_CALL
ColumnLineChartTypeTemplate::getDataInterpreter()
399 throw (uno::RuntimeException
, std::exception
)
401 if( ! m_xDataInterpreter
.is())
403 sal_Int32 nNumberOfLines
= 1;
404 getFastPropertyValue( PROP_COL_LINE_NUMBER_OF_LINES
) >>= nNumberOfLines
;
405 m_xDataInterpreter
.set( new ColumnLineDataInterpreter( nNumberOfLines
, GetComponentContext() ) );
410 OSL_FAIL( "number of lines may not be valid anymore in the datainterpreter" );
414 return m_xDataInterpreter
;
417 IMPLEMENT_FORWARD_XINTERFACE2( ColumnLineChartTypeTemplate
, ChartTypeTemplate
, OPropertySet
)
418 IMPLEMENT_FORWARD_XTYPEPROVIDER2( ColumnLineChartTypeTemplate
, ChartTypeTemplate
, OPropertySet
)
422 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */