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 "ErrorBarItemConverter.hxx"
21 #include "SchWhichPairs.hxx"
23 #include "ItemPropertyMap.hxx"
24 #include "ErrorBar.hxx"
25 #include "PropertyHelper.hxx"
26 #include "ChartModelHelper.hxx"
27 #include "ChartTypeHelper.hxx"
28 #include "StatisticsHelper.hxx"
30 #include "GraphicPropertyItemConverter.hxx"
32 #include <svl/stritem.hxx>
33 #include <svx/chrtitem.hxx>
34 #include <svl/intitem.hxx>
35 #include <rtl/math.hxx>
37 #include <com/sun/star/chart2/DataPointLabel.hpp>
38 #include <com/sun/star/chart2/XInternalDataProvider.hpp>
39 #include <com/sun/star/chart/ErrorBarStyle.hpp>
40 #include <com/sun/star/lang/XServiceName.hpp>
46 using namespace ::com::sun::star
;
51 void lcl_getErrorValues( const uno::Reference
< beans::XPropertySet
> & xErrorBarProp
,
52 double & rOutPosError
, double & rOutNegError
)
54 if( ! xErrorBarProp
.is())
59 xErrorBarProp
->getPropertyValue( "PositiveError" ) >>= rOutPosError
;
60 xErrorBarProp
->getPropertyValue( "NegativeError" ) >>= rOutNegError
;
62 catch( const uno::Exception
& ex
)
64 ASSERT_EXCEPTION( ex
);
68 void lcl_getErrorIndicatorValues(
69 const uno::Reference
< beans::XPropertySet
> & xErrorBarProp
,
70 bool & rOutShowPosError
, bool & rOutShowNegError
)
72 if( ! xErrorBarProp
.is())
77 xErrorBarProp
->getPropertyValue( "ShowPositiveError" ) >>= rOutShowPosError
;
78 xErrorBarProp
->getPropertyValue( "ShowNegativeError" ) >>= rOutShowNegError
;
80 catch( const uno::Exception
& ex
)
82 ASSERT_EXCEPTION( ex
);
86 } // anonymous namespace
93 ErrorBarItemConverter::ErrorBarItemConverter(
94 const uno::Reference
< frame::XModel
> & xModel
,
95 const uno::Reference
< beans::XPropertySet
> & rPropertySet
,
96 SfxItemPool
& rItemPool
,
98 const uno::Reference
< lang::XMultiServiceFactory
> & xNamedPropertyContainerFactory
) :
99 ItemConverter( rPropertySet
, rItemPool
),
100 m_spGraphicConverter( new GraphicPropertyItemConverter(
101 rPropertySet
, rItemPool
, rDrawModel
,
102 xNamedPropertyContainerFactory
,
103 GraphicPropertyItemConverter::LINE_PROPERTIES
)),
107 ErrorBarItemConverter::~ErrorBarItemConverter()
110 void ErrorBarItemConverter::FillItemSet( SfxItemSet
& rOutItemSet
) const
112 m_spGraphicConverter
->FillItemSet( rOutItemSet
);
115 ItemConverter::FillItemSet( rOutItemSet
);
118 bool ErrorBarItemConverter::ApplyItemSet( const SfxItemSet
& rItemSet
)
120 bool bResult
= m_spGraphicConverter
->ApplyItemSet( rItemSet
);
123 return ItemConverter::ApplyItemSet( rItemSet
) || bResult
;
126 const sal_uInt16
* ErrorBarItemConverter::GetWhichPairs() const
128 // must span all used items!
129 return nErrorBarWhichPairs
;
132 bool ErrorBarItemConverter::GetItemProperty(
133 tWhichIdType
/* nWhichId */,
134 tPropertyNameWithMemberId
& /* rOutProperty */ ) const
139 bool ErrorBarItemConverter::ApplySpecialItem(
140 sal_uInt16 nWhichId
, const SfxItemSet
& rItemSet
)
141 throw( uno::Exception
)
143 bool bChanged
= false;
147 // Attention !!! This case must be passed before SCHATTR_STAT_PERCENT,
148 // SCHATTR_STAT_BIGERROR, SCHATTR_STAT_CONSTPLUS,
149 // SCHATTR_STAT_CONSTMINUS and SCHATTR_STAT_INDICATE
150 case SCHATTR_STAT_KIND_ERROR
:
152 uno::Reference
< beans::XPropertySet
> xErrorBarProp( GetPropertySet());
154 SvxChartKindError eErrorKind
=
155 static_cast< const SvxChartKindErrorItem
& >(
156 rItemSet
.Get( nWhichId
)).GetValue();
158 if( !xErrorBarProp
.is() && eErrorKind
== CHERROR_NONE
)
164 sal_Int32 nStyle
= ::com::sun::star::chart::ErrorBarStyle::NONE
;
169 nStyle
= ::com::sun::star::chart::ErrorBarStyle::NONE
; break;
170 case CHERROR_VARIANT
:
171 nStyle
= ::com::sun::star::chart::ErrorBarStyle::VARIANCE
; break;
173 nStyle
= ::com::sun::star::chart::ErrorBarStyle::STANDARD_DEVIATION
; break;
174 case CHERROR_PERCENT
:
175 nStyle
= ::com::sun::star::chart::ErrorBarStyle::RELATIVE
; break;
176 case CHERROR_BIGERROR
:
177 nStyle
= ::com::sun::star::chart::ErrorBarStyle::ERROR_MARGIN
; break;
179 nStyle
= ::com::sun::star::chart::ErrorBarStyle::ABSOLUTE
; break;
180 case CHERROR_STDERROR
:
181 nStyle
= ::com::sun::star::chart::ErrorBarStyle::STANDARD_ERROR
; break;
183 nStyle
= ::com::sun::star::chart::ErrorBarStyle::FROM_DATA
; break;
186 xErrorBarProp
->setPropertyValue( "ErrorBarStyle" , uno::makeAny( nStyle
));
192 case SCHATTR_STAT_PERCENT
:
193 case SCHATTR_STAT_BIGERROR
:
195 OSL_FAIL( "Deprecated item" );
196 uno::Reference
< beans::XPropertySet
> xErrorBarProp( GetPropertySet());
199 static_cast< const SvxDoubleItem
& >(
200 rItemSet
.Get( nWhichId
)).GetValue();
201 double fPos(0.0), fNeg(0.0);
202 lcl_getErrorValues( xErrorBarProp
, fPos
, fNeg
);
204 if( ! ( ::rtl::math::approxEqual( fPos
, fValue
) &&
205 ::rtl::math::approxEqual( fNeg
, fValue
)))
207 xErrorBarProp
->setPropertyValue( "PositiveError" , uno::makeAny( fValue
));
208 xErrorBarProp
->setPropertyValue( "NegativeError" , uno::makeAny( fValue
));
214 case SCHATTR_STAT_CONSTPLUS
:
217 static_cast< const SvxDoubleItem
& >(
218 rItemSet
.Get( nWhichId
)).GetValue();
219 double fPos(0.0), fNeg(0.0);
220 lcl_getErrorValues( GetPropertySet(), fPos
, fNeg
);
222 if( ! ::rtl::math::approxEqual( fPos
, fValue
))
224 GetPropertySet()->setPropertyValue( "PositiveError" , uno::makeAny( fValue
));
230 case SCHATTR_STAT_CONSTMINUS
:
232 uno::Reference
< beans::XPropertySet
> xErrorBarProp( GetPropertySet());
235 static_cast< const SvxDoubleItem
& >(
236 rItemSet
.Get( nWhichId
)).GetValue();
237 double fPos(0.0), fNeg(0.0);
238 lcl_getErrorValues( xErrorBarProp
, fPos
, fNeg
);
240 if( ! ::rtl::math::approxEqual( fNeg
, fValue
))
242 xErrorBarProp
->setPropertyValue( "NegativeError" , uno::makeAny( fValue
));
248 case SCHATTR_STAT_INDICATE
:
250 uno::Reference
< beans::XPropertySet
> xErrorBarProp( GetPropertySet());
252 SvxChartIndicate eIndicate
=
253 static_cast< const SvxChartIndicateItem
& >(
254 rItemSet
.Get( nWhichId
)).GetValue();
256 bool bNewIndPos
= (eIndicate
== CHINDICATE_BOTH
|| eIndicate
== CHINDICATE_UP
);
257 bool bNewIndNeg
= (eIndicate
== CHINDICATE_BOTH
|| eIndicate
== CHINDICATE_DOWN
);
259 bool bShowPos(false), bShowNeg(false);
260 lcl_getErrorIndicatorValues( xErrorBarProp
, bShowPos
, bShowNeg
);
262 if( ( bShowPos
!= bNewIndPos
||
263 bShowNeg
!= bNewIndNeg
))
265 xErrorBarProp
->setPropertyValue( "ShowPositiveError" , uno::makeAny( bNewIndPos
));
266 xErrorBarProp
->setPropertyValue( "ShowNegativeError" , uno::makeAny( bNewIndNeg
));
272 case SCHATTR_STAT_RANGE_POS
:
273 case SCHATTR_STAT_RANGE_NEG
:
275 // @todo: also be able to deal with x-error bars
277 static_cast<const SfxBoolItem
&>(rItemSet
.Get(SCHATTR_STAT_ERRORBAR_TYPE
)).GetValue();
279 uno::Reference
< chart2::data::XDataSource
> xErrorBarSource( GetPropertySet(), uno::UNO_QUERY
);
280 uno::Reference
< chart2::XChartDocument
> xChartDoc( m_xModel
, uno::UNO_QUERY
);
281 uno::Reference
< chart2::data::XDataProvider
> xDataProvider
;
284 xDataProvider
.set( xChartDoc
->getDataProvider());
285 if( xErrorBarSource
.is() && xDataProvider
.is())
287 OUString
aNewRange( static_cast< const SfxStringItem
& >( rItemSet
.Get( nWhichId
)).GetValue());
288 bool bApplyNewRange
= false;
290 bool bIsPositiveValue( nWhichId
== SCHATTR_STAT_RANGE_POS
);
291 if( xChartDoc
->hasInternalDataProvider())
293 if( !aNewRange
.isEmpty())
295 uno::Reference
< chart2::data::XDataSequence
> xSeq(
296 StatisticsHelper::getErrorDataSequenceFromDataSource(
297 xErrorBarSource
, bIsPositiveValue
, bYError
));
300 // no data range for error bars yet => create
301 uno::Reference
< chart2::XInternalDataProvider
> xIntDataProvider( xDataProvider
, uno::UNO_QUERY
);
302 OSL_ASSERT( xIntDataProvider
.is());
303 if( xIntDataProvider
.is())
305 xIntDataProvider
->appendSequence();
307 bApplyNewRange
= true;
314 uno::Reference
< chart2::data::XDataSequence
> xSeq(
315 StatisticsHelper::getErrorDataSequenceFromDataSource(
316 xErrorBarSource
, bIsPositiveValue
, bYError
));
318 ! ( xSeq
.is() && (aNewRange
== xSeq
->getSourceRangeRepresentation()));
322 StatisticsHelper::setErrorDataSequence(
323 xErrorBarSource
, xDataProvider
, aNewRange
, bIsPositiveValue
, bYError
);
332 void ErrorBarItemConverter::FillSpecialItem(
333 sal_uInt16 nWhichId
, SfxItemSet
& rOutItemSet
) const
334 throw( uno::Exception
)
338 case SCHATTR_STAT_KIND_ERROR
:
340 SvxChartKindError eErrorKind
= CHERROR_NONE
;
341 uno::Reference
< beans::XPropertySet
> xErrorBarProp( GetPropertySet());
343 sal_Int32 nStyle
= 0;
344 if( xErrorBarProp
->getPropertyValue( "ErrorBarStyle" ) >>= nStyle
)
348 case ::com::sun::star::chart::ErrorBarStyle::NONE
:
350 case ::com::sun::star::chart::ErrorBarStyle::VARIANCE
:
351 eErrorKind
= CHERROR_VARIANT
; break;
352 case ::com::sun::star::chart::ErrorBarStyle::STANDARD_DEVIATION
:
353 eErrorKind
= CHERROR_SIGMA
; break;
354 case ::com::sun::star::chart::ErrorBarStyle::ABSOLUTE
:
355 eErrorKind
= CHERROR_CONST
; break;
356 case ::com::sun::star::chart::ErrorBarStyle::RELATIVE
:
357 eErrorKind
= CHERROR_PERCENT
; break;
358 case ::com::sun::star::chart::ErrorBarStyle::ERROR_MARGIN
:
359 eErrorKind
= CHERROR_BIGERROR
; break;
360 case ::com::sun::star::chart::ErrorBarStyle::STANDARD_ERROR
:
361 eErrorKind
= CHERROR_STDERROR
; break;
362 case ::com::sun::star::chart::ErrorBarStyle::FROM_DATA
:
363 eErrorKind
= CHERROR_RANGE
; break;
366 rOutItemSet
.Put( SvxChartKindErrorItem( eErrorKind
, SCHATTR_STAT_KIND_ERROR
));
370 case SCHATTR_STAT_PERCENT
:
372 double fPos(0.0), fNeg(0.0);
373 lcl_getErrorValues( GetPropertySet(), fPos
, fNeg
);
374 rOutItemSet
.Put( SvxDoubleItem( ( fPos
+ fNeg
) / 2.0, nWhichId
));
378 case SCHATTR_STAT_BIGERROR
:
380 double fPos(0.0), fNeg(0.0);
381 lcl_getErrorValues( GetPropertySet(), fPos
, fNeg
);
382 rOutItemSet
.Put( SvxDoubleItem( ( fPos
+ fNeg
) / 2.0, nWhichId
));
386 case SCHATTR_STAT_CONSTPLUS
:
388 double fPos(0.0), fNeg(0.0);
389 lcl_getErrorValues( GetPropertySet(), fPos
, fNeg
);
390 rOutItemSet
.Put( SvxDoubleItem( fPos
, nWhichId
));
394 case SCHATTR_STAT_CONSTMINUS
:
396 double fPos(0.0), fNeg(0.0);
397 lcl_getErrorValues( GetPropertySet(), fPos
, fNeg
);
398 rOutItemSet
.Put( SvxDoubleItem( fNeg
, nWhichId
));
402 case SCHATTR_STAT_INDICATE
:
404 SvxChartIndicate eIndicate
= CHINDICATE_BOTH
;
405 bool bShowPos(false), bShowNeg(false);
406 lcl_getErrorIndicatorValues( GetPropertySet(), bShowPos
, bShowNeg
);
411 eIndicate
= CHINDICATE_BOTH
;
413 eIndicate
= CHINDICATE_UP
;
418 eIndicate
= CHINDICATE_DOWN
;
420 eIndicate
= CHINDICATE_NONE
;
422 rOutItemSet
.Put( SvxChartIndicateItem( eIndicate
, SCHATTR_STAT_INDICATE
));
426 case SCHATTR_STAT_RANGE_POS
:
427 case SCHATTR_STAT_RANGE_NEG
:
430 static_cast<const SfxBoolItem
&>(rOutItemSet
.Get(SCHATTR_STAT_ERRORBAR_TYPE
)).GetValue();
432 uno::Reference
< chart2::data::XDataSource
> xErrorBarSource( GetPropertySet(), uno::UNO_QUERY
);
433 if( xErrorBarSource
.is())
435 uno::Reference
< chart2::data::XDataSequence
> xSeq(
436 StatisticsHelper::getErrorDataSequenceFromDataSource(
437 xErrorBarSource
, (nWhichId
== SCHATTR_STAT_RANGE_POS
), bYError
));
439 rOutItemSet
.Put( SfxStringItem( nWhichId
, xSeq
->getSourceRangeRepresentation()));
446 } // namespace wrapper
449 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */