Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include "ErrorBarItemConverter.hxx"
21 : : #include "SchWhichPairs.hxx"
22 : : #include "macros.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"
29 : :
30 : : #include "GraphicPropertyItemConverter.hxx"
31 : :
32 : : #include <svl/stritem.hxx>
33 : : #include <svx/chrtitem.hxx>
34 : : #include <svl/intitem.hxx>
35 : : #include <rtl/math.hxx>
36 : :
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>
41 : :
42 : : #include <functional>
43 : : #include <algorithm>
44 : : #include <vector>
45 : :
46 : : using namespace ::com::sun::star;
47 : :
48 : : namespace
49 : : {
50 : :
51 : 0 : void lcl_getErrorValues( const uno::Reference< beans::XPropertySet > & xErrorBarProp,
52 : : double & rOutPosError, double & rOutNegError )
53 : : {
54 [ # # ]: 0 : if( ! xErrorBarProp.is())
55 : 0 : return;
56 : :
57 : : try
58 : : {
59 [ # # ][ # # ]: 0 : xErrorBarProp->getPropertyValue( C2U( "PositiveError" )) >>= rOutPosError;
[ # # ]
60 [ # # ][ # # ]: 0 : xErrorBarProp->getPropertyValue( C2U( "NegativeError" )) >>= rOutNegError;
[ # # ][ # # ]
61 : : }
62 : 0 : catch( const uno::Exception & ex )
63 : : {
64 : : ASSERT_EXCEPTION( ex );
65 : : }
66 : : }
67 : :
68 : 0 : void lcl_getErrorIndicatorValues(
69 : : const uno::Reference< beans::XPropertySet > & xErrorBarProp,
70 : : bool & rOutShowPosError, bool & rOutShowNegError )
71 : : {
72 [ # # ]: 0 : if( ! xErrorBarProp.is())
73 : 0 : return;
74 : :
75 : : try
76 : : {
77 [ # # ][ # # ]: 0 : xErrorBarProp->getPropertyValue( C2U( "ShowPositiveError" )) >>= rOutShowPosError;
[ # # ]
78 [ # # ][ # # ]: 0 : xErrorBarProp->getPropertyValue( C2U( "ShowNegativeError" )) >>= rOutShowNegError;
[ # # ][ # # ]
79 : : }
80 : 0 : catch( const uno::Exception & ex )
81 : : {
82 : : ASSERT_EXCEPTION( ex );
83 : : }
84 : : }
85 : :
86 : : } // anonymous namespace
87 : :
88 : : namespace chart
89 : : {
90 : : namespace wrapper
91 : : {
92 : :
93 : 0 : ErrorBarItemConverter::ErrorBarItemConverter(
94 : : const uno::Reference< frame::XModel > & xModel,
95 : : const uno::Reference< beans::XPropertySet > & rPropertySet,
96 : : SfxItemPool& rItemPool,
97 : : SdrModel& rDrawModel,
98 : : const uno::Reference< lang::XMultiServiceFactory > & xNamedPropertyContainerFactory ) :
99 : : ItemConverter( rPropertySet, rItemPool ),
100 : : m_spGraphicConverter( new GraphicPropertyItemConverter(
101 : : rPropertySet, rItemPool, rDrawModel,
102 : : xNamedPropertyContainerFactory,
103 [ # # ]: 0 : GraphicPropertyItemConverter::LINE_PROPERTIES )),
104 [ # # ][ # # ]: 0 : m_xModel( xModel )
105 : 0 : {}
106 : :
107 [ # # ]: 0 : ErrorBarItemConverter::~ErrorBarItemConverter()
108 [ # # ]: 0 : {}
109 : :
110 : 0 : void ErrorBarItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
111 : : {
112 : 0 : m_spGraphicConverter->FillItemSet( rOutItemSet );
113 : :
114 : : // own items
115 : 0 : ItemConverter::FillItemSet( rOutItemSet );
116 : 0 : }
117 : :
118 : 0 : bool ErrorBarItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
119 : : {
120 : 0 : bool bResult = m_spGraphicConverter->ApplyItemSet( rItemSet );
121 : :
122 : : // own items
123 [ # # ][ # # ]: 0 : return ItemConverter::ApplyItemSet( rItemSet ) || bResult;
124 : : }
125 : :
126 : 0 : const sal_uInt16 * ErrorBarItemConverter::GetWhichPairs() const
127 : : {
128 : : // must span all used items!
129 : 0 : return nErrorBarWhichPairs;
130 : : }
131 : :
132 : 0 : bool ErrorBarItemConverter::GetItemProperty(
133 : : tWhichIdType /* nWhichId */,
134 : : tPropertyNameWithMemberId & /* rOutProperty */ ) const
135 : : {
136 : 0 : return false;
137 : : }
138 : :
139 : 0 : bool ErrorBarItemConverter::ApplySpecialItem(
140 : : sal_uInt16 nWhichId, const SfxItemSet & rItemSet )
141 : : throw( uno::Exception )
142 : : {
143 : 0 : bool bChanged = false;
144 : 0 : uno::Any aValue;
145 : :
146 [ # # # # : 0 : switch( nWhichId )
# # # ]
147 : : {
148 : : // Attention !!! This case must be passed before SCHATTR_STAT_PERCENT,
149 : : // SCHATTR_STAT_BIGERROR, SCHATTR_STAT_CONSTPLUS,
150 : : // SCHATTR_STAT_CONSTMINUS and SCHATTR_STAT_INDICATE
151 : : case SCHATTR_STAT_KIND_ERROR:
152 : : {
153 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
154 : :
155 : : SvxChartKindError eErrorKind =
156 : : static_cast< const SvxChartKindErrorItem & >(
157 [ # # ]: 0 : rItemSet.Get( nWhichId )).GetValue();
158 : :
159 [ # # ][ # # ]: 0 : if( !xErrorBarProp.is() && eErrorKind == CHERROR_NONE)
[ # # ]
160 : : {
161 : : //nothing to do
162 : : }
163 : : else
164 : : {
165 : 0 : sal_Int32 nStyle = ::com::sun::star::chart::ErrorBarStyle::NONE;
166 : :
167 [ # # # # : 0 : switch( eErrorKind )
# # # #
# ]
168 : : {
169 : : case CHERROR_NONE:
170 : 0 : nStyle = ::com::sun::star::chart::ErrorBarStyle::NONE; break;
171 : : case CHERROR_VARIANT:
172 : 0 : nStyle = ::com::sun::star::chart::ErrorBarStyle::VARIANCE; break;
173 : : case CHERROR_SIGMA:
174 : 0 : nStyle = ::com::sun::star::chart::ErrorBarStyle::STANDARD_DEVIATION; break;
175 : : case CHERROR_PERCENT:
176 : 0 : nStyle = ::com::sun::star::chart::ErrorBarStyle::RELATIVE; break;
177 : : case CHERROR_BIGERROR:
178 : 0 : nStyle = ::com::sun::star::chart::ErrorBarStyle::ERROR_MARGIN; break;
179 : : case CHERROR_CONST:
180 : 0 : nStyle = ::com::sun::star::chart::ErrorBarStyle::ABSOLUTE; break;
181 : : case CHERROR_STDERROR:
182 : 0 : nStyle = ::com::sun::star::chart::ErrorBarStyle::STANDARD_ERROR; break;
183 : : case CHERROR_RANGE:
184 : 0 : nStyle = ::com::sun::star::chart::ErrorBarStyle::FROM_DATA; break;
185 : : }
186 : :
187 [ # # ]: 0 : xErrorBarProp->setPropertyValue( C2U( "ErrorBarStyle" ),
188 [ # # ][ # # ]: 0 : uno::makeAny( nStyle ));
[ # # ]
189 : 0 : bChanged = true;
190 : 0 : }
191 : : }
192 : 0 : break;
193 : :
194 : : case SCHATTR_STAT_PERCENT:
195 : : case SCHATTR_STAT_BIGERROR:
196 : : {
197 : : OSL_FAIL( "Deprectaed item" );
198 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
199 : :
200 : : double fValue =
201 : : static_cast< const SvxDoubleItem & >(
202 [ # # ]: 0 : rItemSet.Get( nWhichId )).GetValue();
203 : : double fPos, fNeg;
204 [ # # ]: 0 : lcl_getErrorValues( xErrorBarProp, fPos, fNeg );
205 : :
206 [ # # ]: 0 : if( ! ( ::rtl::math::approxEqual( fPos, fValue ) &&
207 [ # # ][ # # ]: 0 : ::rtl::math::approxEqual( fNeg, fValue )))
208 : : {
209 [ # # ]: 0 : xErrorBarProp->setPropertyValue( C2U( "PositiveError" ),
210 [ # # ][ # # ]: 0 : uno::makeAny( fValue ));
[ # # ]
211 [ # # ]: 0 : xErrorBarProp->setPropertyValue( C2U( "NegativeError" ),
212 [ # # ][ # # ]: 0 : uno::makeAny( fValue ));
[ # # ]
213 : 0 : bChanged = true;
214 : 0 : }
215 : : }
216 : 0 : break;
217 : :
218 : : case SCHATTR_STAT_CONSTPLUS:
219 : : {
220 : : double fValue =
221 : : static_cast< const SvxDoubleItem & >(
222 [ # # ]: 0 : rItemSet.Get( nWhichId )).GetValue();
223 : : double fPos, fNeg;
224 [ # # ][ # # ]: 0 : lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
225 : :
226 [ # # ]: 0 : if( ! ::rtl::math::approxEqual( fPos, fValue ))
227 : : {
228 [ # # ][ # # ]: 0 : GetPropertySet()->setPropertyValue( C2U( "PositiveError" ), uno::makeAny( fValue ));
[ # # ][ # # ]
[ # # ]
229 : 0 : bChanged = true;
230 : : }
231 : : }
232 : 0 : break;
233 : :
234 : : case SCHATTR_STAT_CONSTMINUS:
235 : : {
236 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
237 : :
238 : : double fValue =
239 : : static_cast< const SvxDoubleItem & >(
240 [ # # ]: 0 : rItemSet.Get( nWhichId )).GetValue();
241 : : double fPos, fNeg;
242 [ # # ]: 0 : lcl_getErrorValues( xErrorBarProp, fPos, fNeg );
243 : :
244 [ # # ]: 0 : if( ! ::rtl::math::approxEqual( fNeg, fValue ))
245 : : {
246 [ # # ][ # # ]: 0 : xErrorBarProp->setPropertyValue( C2U( "NegativeError" ), uno::makeAny( fValue ));
[ # # ][ # # ]
247 : 0 : bChanged = true;
248 : 0 : }
249 : : }
250 : 0 : break;
251 : :
252 : : case SCHATTR_STAT_INDICATE:
253 : : {
254 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
255 : :
256 : : SvxChartIndicate eIndicate =
257 : : static_cast< const SvxChartIndicateItem & >(
258 [ # # ]: 0 : rItemSet.Get( nWhichId )).GetValue();
259 : :
260 [ # # ][ # # ]: 0 : bool bNewIndPos = (eIndicate == CHINDICATE_BOTH || eIndicate == CHINDICATE_UP );
261 [ # # ][ # # ]: 0 : bool bNewIndNeg = (eIndicate == CHINDICATE_BOTH || eIndicate == CHINDICATE_DOWN );
262 : :
263 : : bool bShowPos, bShowNeg;
264 [ # # ]: 0 : lcl_getErrorIndicatorValues( xErrorBarProp, bShowPos, bShowNeg );
265 : :
266 [ # # ][ # # ]: 0 : if( ( bShowPos != bNewIndPos ||
267 : : bShowNeg != bNewIndNeg ))
268 : : {
269 [ # # ][ # # ]: 0 : xErrorBarProp->setPropertyValue( C2U( "ShowPositiveError" ), uno::makeAny( bNewIndPos ));
[ # # ][ # # ]
270 [ # # ][ # # ]: 0 : xErrorBarProp->setPropertyValue( C2U( "ShowNegativeError" ), uno::makeAny( bNewIndNeg ));
[ # # ][ # # ]
271 : 0 : bChanged = true;
272 : 0 : }
273 : : }
274 : 0 : break;
275 : :
276 : : case SCHATTR_STAT_RANGE_POS:
277 : : case SCHATTR_STAT_RANGE_NEG:
278 : : {
279 : : // @todo: also be able to deal with x-error bars
280 : : const bool bYError =
281 [ # # ]: 0 : static_cast<const SfxBoolItem&>(rItemSet.Get(SCHATTR_STAT_ERRORBAR_TYPE)).GetValue();
282 : :
283 [ # # ][ # # ]: 0 : uno::Reference< chart2::data::XDataSource > xErrorBarSource( GetPropertySet(), uno::UNO_QUERY );
284 [ # # ]: 0 : uno::Reference< chart2::XChartDocument > xChartDoc( m_xModel, uno::UNO_QUERY );
285 : 0 : uno::Reference< chart2::data::XDataProvider > xDataProvider;
286 : :
287 [ # # ]: 0 : if( xChartDoc.is())
288 [ # # ][ # # ]: 0 : xDataProvider.set( xChartDoc->getDataProvider());
[ # # ]
289 [ # # ][ # # ]: 0 : if( xErrorBarSource.is() && xDataProvider.is())
[ # # ]
290 : : {
291 [ # # ][ # # ]: 0 : ::rtl::OUString aNewRange( static_cast< const SfxStringItem & >( rItemSet.Get( nWhichId )).GetValue());
292 : 0 : bool bApplyNewRange = false;
293 : :
294 : 0 : bool bIsPositiveValue( nWhichId == SCHATTR_STAT_RANGE_POS );
295 [ # # ][ # # ]: 0 : if( xChartDoc->hasInternalDataProvider())
[ # # ]
296 : : {
297 [ # # ]: 0 : if( !aNewRange.isEmpty())
298 : : {
299 : : uno::Reference< chart2::data::XDataSequence > xSeq(
300 : : StatisticsHelper::getErrorDataSequenceFromDataSource(
301 [ # # ]: 0 : xErrorBarSource, bIsPositiveValue, bYError ));
302 [ # # ]: 0 : if( ! xSeq.is())
303 : : {
304 : : // no data range for error bars yet => create
305 [ # # ]: 0 : uno::Reference< chart2::XInternalDataProvider > xIntDataProvider( xDataProvider, uno::UNO_QUERY );
306 : : OSL_ASSERT( xIntDataProvider.is());
307 [ # # ]: 0 : if( xIntDataProvider.is())
308 : : {
309 [ # # ][ # # ]: 0 : xIntDataProvider->appendSequence();
310 [ # # ]: 0 : aNewRange = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("last"));
311 : 0 : bApplyNewRange = true;
312 : 0 : }
313 : 0 : }
314 : : }
315 : : }
316 : : else
317 : : {
318 : : uno::Reference< chart2::data::XDataSequence > xSeq(
319 : : StatisticsHelper::getErrorDataSequenceFromDataSource(
320 [ # # ]: 0 : xErrorBarSource, bIsPositiveValue, bYError ));
321 : : bApplyNewRange =
322 [ # # ][ # # ]: 0 : ! ( xSeq.is() && aNewRange.equals( xSeq->getSourceRangeRepresentation()));
[ # # ][ # # ]
[ # # ][ # # ]
323 : : }
324 : :
325 [ # # ]: 0 : if( bApplyNewRange )
326 : : StatisticsHelper::setErrorDataSequence(
327 [ # # ]: 0 : xErrorBarSource, xDataProvider, aNewRange, bIsPositiveValue, bYError );
328 : 0 : }
329 : : }
330 : 0 : break;
331 : : }
332 : :
333 : 0 : return bChanged;
334 : : }
335 : :
336 : 0 : void ErrorBarItemConverter::FillSpecialItem(
337 : : sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const
338 : : throw( uno::Exception )
339 : : {
340 [ # # # # : 0 : switch( nWhichId )
# # # # ]
341 : : {
342 : : case SCHATTR_STAT_KIND_ERROR:
343 : : {
344 : 0 : SvxChartKindError eErrorKind = CHERROR_NONE;
345 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xErrorBarProp( GetPropertySet());
346 : :
347 : 0 : sal_Int32 nStyle = 0;
348 [ # # ][ # # ]: 0 : if( xErrorBarProp->getPropertyValue( C2U( "ErrorBarStyle" )) >>= nStyle )
[ # # ][ # # ]
349 : : {
350 [ # # # # : 0 : switch( nStyle )
# # # #
# ]
351 : : {
352 : : case ::com::sun::star::chart::ErrorBarStyle::NONE:
353 : 0 : break;
354 : : case ::com::sun::star::chart::ErrorBarStyle::VARIANCE:
355 : 0 : eErrorKind = CHERROR_VARIANT; break;
356 : : case ::com::sun::star::chart::ErrorBarStyle::STANDARD_DEVIATION:
357 : 0 : eErrorKind = CHERROR_SIGMA; break;
358 : : case ::com::sun::star::chart::ErrorBarStyle::ABSOLUTE:
359 : 0 : eErrorKind = CHERROR_CONST; break;
360 : : case ::com::sun::star::chart::ErrorBarStyle::RELATIVE:
361 : 0 : eErrorKind = CHERROR_PERCENT; break;
362 : : case ::com::sun::star::chart::ErrorBarStyle::ERROR_MARGIN:
363 : 0 : eErrorKind = CHERROR_BIGERROR; break;
364 : : case ::com::sun::star::chart::ErrorBarStyle::STANDARD_ERROR:
365 : 0 : eErrorKind = CHERROR_STDERROR; break;
366 : : case ::com::sun::star::chart::ErrorBarStyle::FROM_DATA:
367 : 0 : eErrorKind = CHERROR_RANGE; break;
368 : : }
369 : : }
370 [ # # ][ # # ]: 0 : rOutItemSet.Put( SvxChartKindErrorItem( eErrorKind, SCHATTR_STAT_KIND_ERROR ));
[ # # ]
371 : : }
372 : 0 : break;
373 : :
374 : : case SCHATTR_STAT_PERCENT:
375 : : {
376 : : double fPos, fNeg;
377 [ # # ][ # # ]: 0 : lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
378 [ # # ][ # # ]: 0 : rOutItemSet.Put( SvxDoubleItem( ( fPos + fNeg ) / 2.0, nWhichId ));
[ # # ]
379 : : }
380 : 0 : break;
381 : :
382 : : case SCHATTR_STAT_BIGERROR:
383 : : {
384 : : double fPos, fNeg;
385 [ # # ][ # # ]: 0 : lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
386 [ # # ][ # # ]: 0 : rOutItemSet.Put( SvxDoubleItem( ( fPos + fNeg ) / 2.0, nWhichId ));
[ # # ]
387 : : }
388 : 0 : break;
389 : :
390 : : case SCHATTR_STAT_CONSTPLUS:
391 : : {
392 : : double fPos, fNeg;
393 [ # # ][ # # ]: 0 : lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
394 [ # # ][ # # ]: 0 : rOutItemSet.Put( SvxDoubleItem( fPos, nWhichId ));
[ # # ]
395 : : }
396 : 0 : break;
397 : :
398 : : case SCHATTR_STAT_CONSTMINUS:
399 : : {
400 : : double fPos, fNeg;
401 [ # # ][ # # ]: 0 : lcl_getErrorValues( GetPropertySet(), fPos, fNeg );
402 [ # # ][ # # ]: 0 : rOutItemSet.Put( SvxDoubleItem( fNeg, nWhichId ));
[ # # ]
403 : : }
404 : 0 : break;
405 : :
406 : : case SCHATTR_STAT_INDICATE:
407 : : {
408 : 0 : SvxChartIndicate eIndicate = CHINDICATE_BOTH;
409 : : bool bShowPos, bShowNeg;
410 [ # # ][ # # ]: 0 : lcl_getErrorIndicatorValues( GetPropertySet(), bShowPos, bShowNeg );
411 : :
412 [ # # ]: 0 : if( bShowPos )
413 : : {
414 [ # # ]: 0 : if( bShowNeg )
415 : 0 : eIndicate = CHINDICATE_BOTH;
416 : : else
417 : 0 : eIndicate = CHINDICATE_UP;
418 : : }
419 : : else
420 : : {
421 [ # # ]: 0 : if( bShowNeg )
422 : 0 : eIndicate = CHINDICATE_DOWN;
423 : : else
424 : 0 : eIndicate = CHINDICATE_NONE;
425 : : }
426 [ # # ][ # # ]: 0 : rOutItemSet.Put( SvxChartIndicateItem( eIndicate, SCHATTR_STAT_INDICATE ));
[ # # ]
427 : : }
428 : 0 : break;
429 : :
430 : : case SCHATTR_STAT_RANGE_POS:
431 : : case SCHATTR_STAT_RANGE_NEG:
432 : : {
433 : : const bool bYError =
434 [ # # ]: 0 : static_cast<const SfxBoolItem&>(rOutItemSet.Get(SCHATTR_STAT_ERRORBAR_TYPE)).GetValue();
435 : :
436 [ # # ][ # # ]: 0 : uno::Reference< chart2::data::XDataSource > xErrorBarSource( GetPropertySet(), uno::UNO_QUERY );
437 [ # # ]: 0 : if( xErrorBarSource.is())
438 : : {
439 : : uno::Reference< chart2::data::XDataSequence > xSeq(
440 : : StatisticsHelper::getErrorDataSequenceFromDataSource(
441 [ # # ]: 0 : xErrorBarSource, (nWhichId == SCHATTR_STAT_RANGE_POS), bYError ));
442 [ # # ]: 0 : if( xSeq.is())
443 [ # # ][ # # ]: 0 : rOutItemSet.Put( SfxStringItem( nWhichId, String( xSeq->getSourceRangeRepresentation())));
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
444 : 0 : }
445 : : }
446 : 0 : break;
447 : : }
448 : 0 : }
449 : :
450 : : } // namespace wrapper
451 : : } // namespace chart
452 : :
453 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|