1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ErrorBarItemConverter.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
33 #include "ErrorBarItemConverter.hxx"
34 #include "SchWhichPairs.hxx"
36 #include "ItemPropertyMap.hxx"
37 #include "ErrorBar.hxx"
38 #include "PropertyHelper.hxx"
39 #include "ChartModelHelper.hxx"
40 #include "ChartTypeHelper.hxx"
41 #include "StatisticsHelper.hxx"
43 #include "GraphicPropertyItemConverter.hxx"
45 #include <svtools/stritem.hxx>
46 #include <svx/chrtitem.hxx>
47 #include <svtools/intitem.hxx>
48 #include <rtl/math.hxx>
50 #include <com/sun/star/chart2/DataPointLabel.hpp>
51 #include <com/sun/star/chart2/XInternalDataProvider.hpp>
52 #include <com/sun/star/chart/ErrorBarStyle.hpp>
53 #include <com/sun/star/lang/XServiceName.hpp>
59 using namespace ::com::sun::star
;
64 void lcl_getErrorValues( const uno::Reference
< beans::XPropertySet
> & xErrorBarProp
,
65 double & rOutPosError
, double & rOutNegError
)
67 if( ! xErrorBarProp
.is())
72 xErrorBarProp
->getPropertyValue( C2U( "PositiveError" )) >>= rOutPosError
;
73 xErrorBarProp
->getPropertyValue( C2U( "NegativeError" )) >>= rOutNegError
;
75 catch( uno::Exception
& ex
)
77 ASSERT_EXCEPTION( ex
);
81 void lcl_getErrorIndicatorValues(
82 const uno::Reference
< beans::XPropertySet
> & xErrorBarProp
,
83 bool & rOutShowPosError
, bool & rOutShowNegError
)
85 if( ! xErrorBarProp
.is())
90 xErrorBarProp
->getPropertyValue( C2U( "ShowPositiveError" )) >>= rOutShowPosError
;
91 xErrorBarProp
->getPropertyValue( C2U( "ShowNegativeError" )) >>= rOutShowNegError
;
93 catch( uno::Exception
& ex
)
95 ASSERT_EXCEPTION( ex
);
99 } // anonymous namespace
106 ErrorBarItemConverter::ErrorBarItemConverter(
107 const uno::Reference
< frame::XModel
> & xModel
,
108 const uno::Reference
< beans::XPropertySet
> & rPropertySet
,
109 SfxItemPool
& rItemPool
,
110 SdrModel
& rDrawModel
,
111 const uno::Reference
< lang::XMultiServiceFactory
> & xNamedPropertyContainerFactory
) :
112 ItemConverter( rPropertySet
, rItemPool
),
113 m_spGraphicConverter( new GraphicPropertyItemConverter(
114 rPropertySet
, rItemPool
, rDrawModel
,
115 xNamedPropertyContainerFactory
,
116 GraphicPropertyItemConverter::LINE_PROPERTIES
)),
120 ErrorBarItemConverter::~ErrorBarItemConverter()
123 void ErrorBarItemConverter::FillItemSet( SfxItemSet
& rOutItemSet
) const
125 m_spGraphicConverter
->FillItemSet( rOutItemSet
);
128 ItemConverter::FillItemSet( rOutItemSet
);
131 bool ErrorBarItemConverter::ApplyItemSet( const SfxItemSet
& rItemSet
)
133 bool bResult
= m_spGraphicConverter
->ApplyItemSet( rItemSet
);
136 return ItemConverter::ApplyItemSet( rItemSet
) || bResult
;
139 const USHORT
* ErrorBarItemConverter::GetWhichPairs() const
141 // must span all used items!
142 return nErrorBarWhichPairs
;
145 bool ErrorBarItemConverter::GetItemProperty(
146 tWhichIdType
/* nWhichId */,
147 tPropertyNameWithMemberId
& /* rOutProperty */ ) const
152 bool ErrorBarItemConverter::ApplySpecialItem(
153 USHORT nWhichId
, const SfxItemSet
& rItemSet
)
154 throw( uno::Exception
)
156 bool bChanged
= false;
161 // Attention !!! This case must be passed before SCHATTR_STAT_PERCENT,
162 // SCHATTR_STAT_BIGERROR, SCHATTR_STAT_CONSTPLUS,
163 // SCHATTR_STAT_CONSTMINUS and SCHATTR_STAT_INDICATE
164 case SCHATTR_STAT_KIND_ERROR
:
166 uno::Reference
< beans::XPropertySet
> xErrorBarProp( GetPropertySet());
168 SvxChartKindError eErrorKind
=
169 static_cast< const SvxChartKindErrorItem
& >(
170 rItemSet
.Get( nWhichId
)).GetValue();
172 if( !xErrorBarProp
.is() && eErrorKind
== CHERROR_NONE
)
178 sal_Int32 nStyle
= ::com::sun::star::chart::ErrorBarStyle::NONE
;
183 nStyle
= ::com::sun::star::chart::ErrorBarStyle::NONE
; break;
184 case CHERROR_VARIANT
:
185 nStyle
= ::com::sun::star::chart::ErrorBarStyle::VARIANCE
; break;
187 nStyle
= ::com::sun::star::chart::ErrorBarStyle::STANDARD_DEVIATION
; break;
188 case CHERROR_PERCENT
:
189 nStyle
= ::com::sun::star::chart::ErrorBarStyle::RELATIVE
; break;
190 case CHERROR_BIGERROR
:
191 nStyle
= ::com::sun::star::chart::ErrorBarStyle::ERROR_MARGIN
; break;
193 nStyle
= ::com::sun::star::chart::ErrorBarStyle::ABSOLUTE
; break;
194 case CHERROR_STDERROR
:
195 nStyle
= ::com::sun::star::chart::ErrorBarStyle::STANDARD_ERROR
; break;
197 nStyle
= ::com::sun::star::chart::ErrorBarStyle::FROM_DATA
; break;
200 xErrorBarProp
->setPropertyValue( C2U( "ErrorBarStyle" ),
201 uno::makeAny( nStyle
));
207 case SCHATTR_STAT_PERCENT
:
208 case SCHATTR_STAT_BIGERROR
:
210 OSL_ENSURE( false, "Deprectaed item" );
211 uno::Reference
< beans::XPropertySet
> xErrorBarProp( GetPropertySet());
214 static_cast< const SvxDoubleItem
& >(
215 rItemSet
.Get( nWhichId
)).GetValue();
217 lcl_getErrorValues( xErrorBarProp
, fPos
, fNeg
);
219 if( ! ( ::rtl::math::approxEqual( fPos
, fValue
) &&
220 ::rtl::math::approxEqual( fNeg
, fValue
)))
222 xErrorBarProp
->setPropertyValue( C2U( "PositiveError" ),
223 uno::makeAny( fValue
));
224 xErrorBarProp
->setPropertyValue( C2U( "NegativeError" ),
225 uno::makeAny( fValue
));
231 case SCHATTR_STAT_CONSTPLUS
:
234 static_cast< const SvxDoubleItem
& >(
235 rItemSet
.Get( nWhichId
)).GetValue();
237 lcl_getErrorValues( GetPropertySet(), fPos
, fNeg
);
239 if( ! ::rtl::math::approxEqual( fPos
, fValue
))
241 GetPropertySet()->setPropertyValue( C2U( "PositiveError" ), uno::makeAny( fValue
));
247 case SCHATTR_STAT_CONSTMINUS
:
249 uno::Reference
< beans::XPropertySet
> xErrorBarProp( GetPropertySet());
252 static_cast< const SvxDoubleItem
& >(
253 rItemSet
.Get( nWhichId
)).GetValue();
255 lcl_getErrorValues( xErrorBarProp
, fPos
, fNeg
);
257 if( ! ::rtl::math::approxEqual( fNeg
, fValue
))
259 xErrorBarProp
->setPropertyValue( C2U( "NegativeError" ), uno::makeAny( fValue
));
265 case SCHATTR_STAT_INDICATE
:
267 uno::Reference
< beans::XPropertySet
> xErrorBarProp( GetPropertySet());
269 SvxChartIndicate eIndicate
=
270 static_cast< const SvxChartIndicateItem
& >(
271 rItemSet
.Get( nWhichId
)).GetValue();
273 bool bNewIndPos
= (eIndicate
== CHINDICATE_BOTH
|| eIndicate
== CHINDICATE_UP
);
274 bool bNewIndNeg
= (eIndicate
== CHINDICATE_BOTH
|| eIndicate
== CHINDICATE_DOWN
);
276 bool bShowPos
, bShowNeg
;
277 lcl_getErrorIndicatorValues( xErrorBarProp
, bShowPos
, bShowNeg
);
279 if( ( bShowPos
!= bNewIndPos
||
280 bShowNeg
!= bNewIndNeg
))
282 xErrorBarProp
->setPropertyValue( C2U( "ShowPositiveError" ), uno::makeAny( bNewIndPos
));
283 xErrorBarProp
->setPropertyValue( C2U( "ShowNegativeError" ), uno::makeAny( bNewIndNeg
));
289 case SCHATTR_STAT_RANGE_POS
:
290 case SCHATTR_STAT_RANGE_NEG
:
292 // @todo: also be able to deal with x-error bars
293 const bool bYError
= true;
294 uno::Reference
< chart2::data::XDataSource
> xErrorBarSource( GetPropertySet(), uno::UNO_QUERY
);
295 uno::Reference
< chart2::XChartDocument
> xChartDoc( m_xModel
, uno::UNO_QUERY
);
296 uno::Reference
< chart2::data::XDataProvider
> xDataProvider
;
299 xDataProvider
.set( xChartDoc
->getDataProvider());
300 if( xErrorBarSource
.is() && xDataProvider
.is())
302 ::rtl::OUString
aNewRange( static_cast< const SfxStringItem
& >( rItemSet
.Get( nWhichId
)).GetValue());
303 bool bApplyNewRange
= false;
305 bool bIsPositiveValue( nWhichId
== SCHATTR_STAT_RANGE_POS
);
306 if( xChartDoc
->hasInternalDataProvider())
308 if( aNewRange
.getLength())
310 uno::Reference
< chart2::data::XDataSequence
> xSeq(
311 StatisticsHelper::getErrorDataSequenceFromDataSource(
312 xErrorBarSource
, bIsPositiveValue
, bYError
));
315 // no data range for error bars yet => create
316 uno::Reference
< chart2::XInternalDataProvider
> xIntDataProvider( xDataProvider
, uno::UNO_QUERY
);
317 OSL_ASSERT( xIntDataProvider
.is());
318 if( xIntDataProvider
.is())
320 xIntDataProvider
->appendSequence();
321 aNewRange
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("last"));
322 bApplyNewRange
= true;
329 uno::Reference
< chart2::data::XDataSequence
> xSeq(
330 StatisticsHelper::getErrorDataSequenceFromDataSource(
331 xErrorBarSource
, bIsPositiveValue
, bYError
));
333 ! ( xSeq
.is() && aNewRange
.equals( xSeq
->getSourceRangeRepresentation()));
337 StatisticsHelper::setErrorDataSequence(
338 xErrorBarSource
, xDataProvider
, aNewRange
, bIsPositiveValue
, bYError
);
347 void ErrorBarItemConverter::FillSpecialItem(
348 USHORT nWhichId
, SfxItemSet
& rOutItemSet
) const
349 throw( uno::Exception
)
353 case SCHATTR_STAT_KIND_ERROR
:
355 SvxChartKindError eErrorKind
= CHERROR_NONE
;
356 uno::Reference
< beans::XPropertySet
> xErrorBarProp( GetPropertySet());
358 sal_Int32 nStyle
= 0;
359 if( xErrorBarProp
->getPropertyValue( C2U( "ErrorBarStyle" )) >>= nStyle
)
363 case ::com::sun::star::chart::ErrorBarStyle::NONE
:
365 case ::com::sun::star::chart::ErrorBarStyle::VARIANCE
:
366 eErrorKind
= CHERROR_VARIANT
; break;
367 case ::com::sun::star::chart::ErrorBarStyle::STANDARD_DEVIATION
:
368 eErrorKind
= CHERROR_SIGMA
; break;
369 case ::com::sun::star::chart::ErrorBarStyle::ABSOLUTE
:
370 eErrorKind
= CHERROR_CONST
; break;
371 case ::com::sun::star::chart::ErrorBarStyle::RELATIVE
:
372 eErrorKind
= CHERROR_PERCENT
; break;
373 case ::com::sun::star::chart::ErrorBarStyle::ERROR_MARGIN
:
374 eErrorKind
= CHERROR_BIGERROR
; break;
375 case ::com::sun::star::chart::ErrorBarStyle::STANDARD_ERROR
:
376 eErrorKind
= CHERROR_STDERROR
; break;
377 case ::com::sun::star::chart::ErrorBarStyle::FROM_DATA
:
378 eErrorKind
= CHERROR_RANGE
; break;
381 rOutItemSet
.Put( SvxChartKindErrorItem( eErrorKind
, SCHATTR_STAT_KIND_ERROR
));
385 case SCHATTR_STAT_PERCENT
:
388 lcl_getErrorValues( GetPropertySet(), fPos
, fNeg
);
389 rOutItemSet
.Put( SvxDoubleItem( ( fPos
+ fNeg
) / 2.0, nWhichId
));
393 case SCHATTR_STAT_BIGERROR
:
396 lcl_getErrorValues( GetPropertySet(), fPos
, fNeg
);
397 rOutItemSet
.Put( SvxDoubleItem( ( fPos
+ fNeg
) / 2.0, nWhichId
));
401 case SCHATTR_STAT_CONSTPLUS
:
404 lcl_getErrorValues( GetPropertySet(), fPos
, fNeg
);
405 rOutItemSet
.Put( SvxDoubleItem( fPos
, nWhichId
));
409 case SCHATTR_STAT_CONSTMINUS
:
412 lcl_getErrorValues( GetPropertySet(), fPos
, fNeg
);
413 rOutItemSet
.Put( SvxDoubleItem( fNeg
, nWhichId
));
417 case SCHATTR_STAT_INDICATE
:
419 SvxChartIndicate eIndicate
= CHINDICATE_BOTH
;
420 bool bShowPos
, bShowNeg
;
421 lcl_getErrorIndicatorValues( GetPropertySet(), bShowPos
, bShowNeg
);
426 eIndicate
= CHINDICATE_BOTH
;
428 eIndicate
= CHINDICATE_UP
;
433 eIndicate
= CHINDICATE_DOWN
;
435 eIndicate
= CHINDICATE_NONE
;
437 rOutItemSet
.Put( SvxChartIndicateItem( eIndicate
, SCHATTR_STAT_INDICATE
));
441 case SCHATTR_STAT_RANGE_POS
:
442 case SCHATTR_STAT_RANGE_NEG
:
444 uno::Reference
< chart2::data::XDataSource
> xErrorBarSource( GetPropertySet(), uno::UNO_QUERY
);
445 if( xErrorBarSource
.is())
447 uno::Reference
< chart2::data::XDataSequence
> xSeq(
448 StatisticsHelper::getErrorDataSequenceFromDataSource(
449 xErrorBarSource
, (nWhichId
== SCHATTR_STAT_RANGE_POS
) /*, true */ /* y */ ));
451 rOutItemSet
.Put( SfxStringItem( nWhichId
, String( xSeq
->getSourceRangeRepresentation())));
458 } // namespace wrapper