xref: /trunk/main/chart2/source/tools/DataSeriesHelper.cxx (revision e8df90a080f9dc7518c4b937d7dcd461d7b4d178)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_charttools.hxx"
26 #include "DataSeriesHelper.hxx"
27 #include "DiagramHelper.hxx"
28 #include "DataSource.hxx"
29 #include "macros.hxx"
30 #include "ContainerHelper.hxx"
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/chart2/DataPointLabel.hpp>
33 #include <com/sun/star/chart2/data/XTextualDataSequence.hpp>
34 #include <com/sun/star/chart2/StackingDirection.hpp>
35 #include <com/sun/star/chart2/data/LabelOrigin.hpp>
36 #include <com/sun/star/chart2/AxisType.hpp>
37 #include <com/sun/star/chart2/SymbolStyle.hpp>
38 #include <com/sun/star/chart2/Symbol.hpp>
39 #include <com/sun/star/drawing/LineStyle.hpp>
40 
41 
42 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
43 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
44 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
45 #include <rtl/ustrbuf.hxx>
46 
47 #include <functional>
48 #include <algorithm>
49 #include <iterator>
50 #include <vector>
51 #include <set>
52 
53 using namespace ::com::sun::star;
54 using namespace ::com::sun::star::chart2;
55 
56 using ::com::sun::star::uno::Reference;
57 using ::com::sun::star::uno::Sequence;
58 using ::rtl::OUString;
59 using ::rtl::OUStringBuffer;
60 
61 // ----------------------------------------
62 namespace
63 {
64 
65 class lcl_MatchesRole
66 {
67 public:
68     typedef Reference< chart2::data::XLabeledDataSequence > argument_type;
69     typedef bool result_type;
lcl_MatchesRole(const OUString & aRole,bool bMatchPrefix)70     explicit lcl_MatchesRole( const OUString & aRole, bool bMatchPrefix ) :
71             m_aRole( aRole ),
72             m_bMatchPrefix( bMatchPrefix )
73     {}
74 
operator ()(const Reference<chart2::data::XLabeledDataSequence> & xSeq) const75     bool operator () ( const Reference< chart2::data::XLabeledDataSequence > & xSeq ) const
76     {
77         if(!xSeq.is())
78             return false;
79         Reference< beans::XPropertySet > xProp( xSeq->getValues(), uno::UNO_QUERY );
80         OUString aRole;
81 
82         if( m_bMatchPrefix )
83             return ( xProp.is() &&
84                      (xProp->getPropertyValue(
85                          OUString( RTL_CONSTASCII_USTRINGPARAM( "Role" )) ) >>= aRole ) &&
86                      aRole.match( m_aRole ));
87 
88         return ( xProp.is() &&
89                  (xProp->getPropertyValue(
90                      OUString( RTL_CONSTASCII_USTRINGPARAM( "Role" )) ) >>= aRole ) &&
91                  m_aRole.equals( aRole ));
92     }
93 
94 private:
95     OUString m_aRole;
96     bool     m_bMatchPrefix;
97 };
98 
lcl_findLSequenceWithOnlyLabel(const Reference<chart2::data::XDataSource> & xDataSource)99 Reference< chart2::data::XLabeledDataSequence > lcl_findLSequenceWithOnlyLabel(
100     const Reference< chart2::data::XDataSource > & xDataSource )
101 {
102     Reference< chart2::data::XLabeledDataSequence > xResult;
103     Sequence< Reference< chart2::data::XLabeledDataSequence > > aSequences( xDataSource->getDataSequences());
104 
105     for( sal_Int32 i=0; i<aSequences.getLength(); ++i )
106     {
107         OSL_ENSURE( aSequences[i].is(), "empty LabeledDataSequence" );
108         // no values are set but a label exists
109         if( aSequences[i].is() &&
110             ( ! aSequences[i]->getValues().is() &&
111               aSequences[i]->getLabel().is()))
112         {
113             xResult.set( aSequences[i] );
114             break;
115         }
116     }
117 
118     return xResult;
119 }
120 
lcl_getCooSysAndChartTypeOfSeries(const Reference<chart2::XDataSeries> & xSeries,const Reference<chart2::XDiagram> & xDiagram,Reference<chart2::XCoordinateSystem> & xOutCooSys,Reference<chart2::XChartType> & xOutChartType)121 void lcl_getCooSysAndChartTypeOfSeries(
122     const Reference< chart2::XDataSeries > & xSeries,
123     const Reference< chart2::XDiagram > & xDiagram,
124     Reference< chart2::XCoordinateSystem > & xOutCooSys,
125     Reference< chart2::XChartType > & xOutChartType )
126 {
127     Reference< chart2::XCoordinateSystemContainer > xCooSysCnt( xDiagram, uno::UNO_QUERY );
128     if( xCooSysCnt.is())
129     {
130         Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems());
131         for( sal_Int32 nCooSysIdx=0; nCooSysIdx<aCooSysSeq.getLength(); ++nCooSysIdx )
132         {
133             Reference< chart2::XChartTypeContainer > xCTCnt( aCooSysSeq[nCooSysIdx], uno::UNO_QUERY_THROW );
134             Sequence< Reference< chart2::XChartType > > aChartTypes( xCTCnt->getChartTypes());
135             for( sal_Int32 nCTIdx=0; nCTIdx<aChartTypes.getLength(); ++nCTIdx )
136             {
137                 Reference< chart2::XDataSeriesContainer > xSeriesCnt( aChartTypes[nCTIdx], uno::UNO_QUERY );
138                 if( xSeriesCnt.is())
139                 {
140                     Sequence< Reference< chart2::XDataSeries > > aSeries( xSeriesCnt->getDataSeries());
141                     for( sal_Int32 nSeriesIdx=0; nSeriesIdx<aSeries.getLength(); ++nSeriesIdx )
142                     {
143                         if( aSeries[nSeriesIdx] == xSeries )
144                         {
145                             xOutCooSys.set( aCooSysSeq[nCooSysIdx] );
146                             xOutChartType.set( aChartTypes[nCTIdx] );
147                         }
148                     }
149                 }
150             }
151         }
152     }
153 }
154 
lcl_insertOrDeleteDataLabelsToSeriesAndAllPoints(const Reference<chart2::XDataSeries> & xSeries,bool bInsert)155 void lcl_insertOrDeleteDataLabelsToSeriesAndAllPoints( const Reference< chart2::XDataSeries >& xSeries, bool bInsert )
156 {
157     try
158     {
159         Reference< beans::XPropertySet > xSeriesProperties( xSeries, uno::UNO_QUERY );
160         if( xSeriesProperties.is() )
161         {
162             DataPointLabel aLabelAtSeries;
163             xSeriesProperties->getPropertyValue( C2U( "Label" ) ) >>= aLabelAtSeries;
164             aLabelAtSeries.ShowNumber = bInsert;
165             if( !bInsert )
166             {
167                 aLabelAtSeries.ShowNumberInPercent = false;
168                 aLabelAtSeries.ShowCategoryName = false;
169             }
170             xSeriesProperties->setPropertyValue( C2U( "Label" ), uno::makeAny( aLabelAtSeries ) );
171             uno::Sequence< sal_Int32 > aAttributedDataPointIndexList;
172             if( xSeriesProperties->getPropertyValue( C2U( "AttributedDataPoints" ) ) >>= aAttributedDataPointIndexList )
173             {
174                 for(sal_Int32 nN=aAttributedDataPointIndexList.getLength();nN--;)
175                 {
176                     Reference< beans::XPropertySet > xPointProp( xSeries->getDataPointByIndex(aAttributedDataPointIndexList[nN]) );
177                     if( xPointProp.is() )
178                     {
179                         DataPointLabel aLabel;
180                         xPointProp->getPropertyValue( C2U( "Label" ) ) >>= aLabel;
181                         aLabel.ShowNumber = bInsert;
182                         if( !bInsert )
183                         {
184                             aLabel.ShowNumberInPercent = false;
185                             aLabel.ShowCategoryName = false;
186                         }
187                         xPointProp->setPropertyValue( C2U( "Label" ), uno::makeAny( aLabel ) );
188                     }
189                 }
190             }
191         }
192     }
193     catch( uno::Exception &e)
194     {
195         ASSERT_EXCEPTION( e );
196     }
197 }
198 
199 } // anonymous namespace
200 // ----------------------------------------
201 
202 namespace chart
203 {
204 
205 namespace DataSeriesHelper
206 {
207 
GetRole(const uno::Reference<chart2::data::XLabeledDataSequence> & xLabeledDataSequence)208 OUString GetRole( const uno::Reference< chart2::data::XLabeledDataSequence >& xLabeledDataSequence )
209 {
210     OUString aRet;
211     if( xLabeledDataSequence.is() )
212     {
213         Reference< beans::XPropertySet > xProp( xLabeledDataSequence->getValues(), uno::UNO_QUERY );
214         if( xProp.is() )
215             xProp->getPropertyValue( C2U("Role") ) >>= aRet;
216     }
217     return aRet;
218 }
219 
220 Reference< chart2::data::XLabeledDataSequence >
getDataSequenceByRole(const Reference<chart2::data::XDataSource> & xSource,OUString aRole,bool bMatchPrefix)221     getDataSequenceByRole(
222         const Reference< chart2::data::XDataSource > & xSource, OUString aRole,
223         bool bMatchPrefix /* = false */ )
224 {
225     Reference< chart2::data::XLabeledDataSequence > aNoResult;
226     if( ! xSource.is())
227         return aNoResult;
228     Sequence< Reference< chart2::data::XLabeledDataSequence > > aLabeledSeq( xSource->getDataSequences());
229 
230     const Reference< chart2::data::XLabeledDataSequence > * pBegin = aLabeledSeq.getConstArray();
231     const Reference< chart2::data::XLabeledDataSequence > * pEnd = pBegin + aLabeledSeq.getLength();
232     const Reference< chart2::data::XLabeledDataSequence > * pMatch =
233         ::std::find_if( pBegin, pEnd, lcl_MatchesRole( aRole, bMatchPrefix ));
234 
235     if( pMatch != pEnd )
236         return *pMatch;
237 
238     return aNoResult;
239 }
240 
241 ::std::vector< Reference< chart2::data::XLabeledDataSequence > >
getAllDataSequencesByRole(const Sequence<Reference<chart2::data::XLabeledDataSequence>> & aDataSequences,OUString aRole,bool bMatchPrefix)242     getAllDataSequencesByRole( const Sequence< Reference< chart2::data::XLabeledDataSequence > > & aDataSequences,
243                                OUString aRole, bool bMatchPrefix /* = false */ )
244 {
245     ::std::vector< Reference< chart2::data::XLabeledDataSequence > > aResultVec;
246     ::std::remove_copy_if( aDataSequences.getConstArray(), aDataSequences.getConstArray() + aDataSequences.getLength(),
247                            ::std::back_inserter( aResultVec ),
248                            ::std::not1( lcl_MatchesRole( aRole, bMatchPrefix )));
249     return aResultVec;
250 }
251 
252 Reference< chart2::data::XDataSource >
getDataSource(const Sequence<Reference<chart2::XDataSeries>> & aSeries)253     getDataSource( const Sequence< Reference< chart2::XDataSeries > > & aSeries )
254 {
255     ::std::vector< Reference< chart2::data::XLabeledDataSequence > > aSeqVec;
256 
257     for( sal_Int32 i = 0; i < aSeries.getLength(); ++i )
258     {
259         Reference< chart2::data::XDataSource > xSource( aSeries[ i ], uno::UNO_QUERY );
260         if( xSource.is())
261         {
262             Sequence< Reference< chart2::data::XLabeledDataSequence > > aSeq( xSource->getDataSequences());
263             ::std::copy( aSeq.getConstArray(), aSeq.getConstArray() + aSeq.getLength(),
264                          ::std::back_inserter( aSeqVec ));
265         }
266     }
267 
268     return Reference< chart2::data::XDataSource >(
269         new DataSource( ContainerHelper::ContainerToSequence( aSeqVec )));
270 }
271 
272 namespace
273 {
lcl_getDataSequenceLabel(const Reference<chart2::data::XDataSequence> & xSequence)274 OUString lcl_getDataSequenceLabel( const Reference< chart2::data::XDataSequence > & xSequence )
275 {
276     OUString aResult;
277 
278     Reference< chart2::data::XTextualDataSequence > xTextSeq( xSequence, uno::UNO_QUERY );
279     if( xTextSeq.is())
280     {
281         Sequence< OUString > aSeq( xTextSeq->getTextualData());
282 
283         const sal_Int32 nMax = aSeq.getLength() - 1;
284         OUString aVal;
285         OUStringBuffer aBuf;
286 
287         for( sal_Int32 i = 0; i <= nMax; ++i )
288         {
289             aBuf.append( aSeq[i] );
290             if( i < nMax )
291                 aBuf.append( sal_Unicode( ' ' ));
292         }
293         aResult = aBuf.makeStringAndClear();
294     }
295     else if( xSequence.is())
296     {
297         Sequence< uno::Any > aSeq( xSequence->getData());
298 
299         const sal_Int32 nMax = aSeq.getLength() - 1;
300         OUString aVal;
301         OUStringBuffer aBuf;
302         double fNum = 0;
303 
304         for( sal_Int32 i = 0; i <= nMax; ++i )
305         {
306             if( aSeq[i] >>= aVal )
307             {
308                 aBuf.append( aVal );
309                 if( i < nMax )
310                     aBuf.append( sal_Unicode( ' ' ));
311             }
312             else if( aSeq[ i ] >>= fNum )
313             {
314                 aBuf.append( fNum );
315                 if( i < nMax )
316                     aBuf.append( sal_Unicode( ' ' ));
317             }
318         }
319         aResult = aBuf.makeStringAndClear();
320     }
321 
322     return aResult;
323 }
324 }
325 
getLabelForLabeledDataSequence(const Reference<chart2::data::XLabeledDataSequence> & xLabeledSeq)326 OUString getLabelForLabeledDataSequence(
327     const Reference< chart2::data::XLabeledDataSequence > & xLabeledSeq )
328 {
329     OUString aResult;
330     if( xLabeledSeq.is())
331     {
332         Reference< chart2::data::XDataSequence > xSeq( xLabeledSeq->getLabel());
333         if( xSeq.is() )
334             aResult = lcl_getDataSequenceLabel( xSeq );
335         if( !xSeq.is() || aResult.isEmpty() )
336         {
337             // no label set or label content is empty -> use auto-generated one
338             Reference< chart2::data::XDataSequence > xValueSeq( xLabeledSeq->getValues() );
339             if( xValueSeq.is() )
340             {
341                 Sequence< OUString > aLabels( xValueSeq->generateLabel(
342                     chart2::data::LabelOrigin_SHORT_SIDE ) );
343                 // no labels returned is interpreted as: auto-generation not
344                 // supported by sequence
345                 if( aLabels.getLength() )
346                     aResult=aLabels[0];
347                 else
348                 {
349                     //todo?: maybe use the index of the series as name
350                     //but as the index may change it would be better to have such a name persistent
351                     //what is not possible at the moment
352                     //--> maybe use the identifier as part of the name ...
353                     aResult = lcl_getDataSequenceLabel( xValueSeq );
354                 }
355             }
356         }
357     }
358     return aResult;
359 }
360 
getDataSeriesLabel(const Reference<chart2::XDataSeries> & xSeries,const OUString & rLabelSequenceRole)361 OUString getDataSeriesLabel(
362     const Reference< chart2::XDataSeries > & xSeries,
363     const OUString & rLabelSequenceRole )
364 {
365     OUString aResult;
366 
367     Reference< chart2::data::XDataSource > xSource( xSeries, uno::UNO_QUERY );
368     if( xSource.is())
369     {
370         Reference< chart2::data::XLabeledDataSequence > xLabeledSeq(
371             ::chart::DataSeriesHelper::getDataSequenceByRole( xSource, rLabelSequenceRole ));
372         if( xLabeledSeq.is())
373             aResult = getLabelForLabeledDataSequence( xLabeledSeq );
374         else
375         {
376             // special case: labeled data series with only a label and no values may
377             // serve as label
378             xLabeledSeq.set( lcl_findLSequenceWithOnlyLabel( xSource ));
379             if( xLabeledSeq.is())
380             {
381                 Reference< chart2::data::XDataSequence > xSeq( xLabeledSeq->getLabel());
382                 if( xSeq.is())
383                     aResult = lcl_getDataSequenceLabel( xSeq );
384             }
385         }
386 
387     }
388 
389     return aResult;
390 }
391 
setStackModeAtSeries(const Sequence<Reference<chart2::XDataSeries>> & aSeries,const Reference<chart2::XCoordinateSystem> & xCorrespondingCoordinateSystem,StackMode eStackMode)392 void setStackModeAtSeries(
393     const Sequence< Reference< chart2::XDataSeries > > & aSeries,
394     const Reference< chart2::XCoordinateSystem > & xCorrespondingCoordinateSystem,
395     StackMode eStackMode )
396 {
397     if( eStackMode == StackMode_AMBIGUOUS )
398         return;
399 
400     const OUString aPropName( RTL_CONSTASCII_USTRINGPARAM( "StackingDirection" ));
401     const uno::Any aPropValue = uno::makeAny(
402         ( (eStackMode == StackMode_Y_STACKED) ||
403           (eStackMode == StackMode_Y_STACKED_PERCENT) )
404         ? chart2::StackingDirection_Y_STACKING
405         : (eStackMode == StackMode_Z_STACKED )
406         ? chart2::StackingDirection_Z_STACKING
407         : chart2::StackingDirection_NO_STACKING );
408 
409     std::set< sal_Int32 > aAxisIndexSet;
410     for( sal_Int32 i=0; i<aSeries.getLength(); ++i )
411     {
412         try
413         {
414             Reference< beans::XPropertySet > xProp( aSeries[i], uno::UNO_QUERY );
415             if( xProp.is() )
416             {
417                 xProp->setPropertyValue( aPropName, aPropValue );
418 
419                 sal_Int32 nAxisIndex;
420                 xProp->getPropertyValue( C2U("AttachedAxisIndex") ) >>= nAxisIndex;
421                 aAxisIndexSet.insert(nAxisIndex);
422             }
423         }
424         catch( uno::Exception & ex )
425         {
426             ASSERT_EXCEPTION( ex );
427         }
428     }
429 
430     if( xCorrespondingCoordinateSystem.is() &&
431         1 < xCorrespondingCoordinateSystem->getDimension() )
432     {
433         sal_Int32 nAxisIndexCount = aAxisIndexSet.size();
434         if( !nAxisIndexCount )
435         {
436             aAxisIndexSet.insert(0);
437             nAxisIndexCount = aAxisIndexSet.size();
438         }
439 
440         for( ::std::set< sal_Int32 >::const_iterator aIt = aAxisIndexSet.begin();
441             aIt != aAxisIndexSet.end(); ++aIt )
442         {
443             sal_Int32 nAxisIndex = *aIt;
444             Reference< chart2::XAxis > xAxis(
445                 xCorrespondingCoordinateSystem->getAxisByDimension( 1, nAxisIndex ));
446             if( xAxis.is())
447             {
448                 sal_Bool bPercent = (eStackMode == StackMode_Y_STACKED_PERCENT);
449                 chart2::ScaleData aScaleData = xAxis->getScaleData();
450 
451                 if( bPercent != (aScaleData.AxisType==chart2::AxisType::PERCENT) )
452                 {
453                     if( bPercent )
454                         aScaleData.AxisType = chart2::AxisType::PERCENT;
455                     else
456                         aScaleData.AxisType = chart2::AxisType::REALNUMBER;
457                     xAxis->setScaleData( aScaleData );
458                 }
459             }
460         }
461     }
462 }
463 
getAttachedAxisIndex(const Reference<chart2::XDataSeries> & xSeries)464 sal_Int32 getAttachedAxisIndex( const Reference< chart2::XDataSeries > & xSeries )
465 {
466     sal_Int32 nRet = 0;
467     try
468     {
469         Reference< beans::XPropertySet > xProp( xSeries, uno::UNO_QUERY );
470         if( xProp.is() )
471         {
472             xProp->getPropertyValue( C2U("AttachedAxisIndex") ) >>= nRet;
473         }
474     }
475     catch( uno::Exception & ex )
476     {
477         ASSERT_EXCEPTION( ex );
478     }
479     return nRet;
480 }
481 
getNumberFormatKeyFromAxis(const Reference<chart2::XDataSeries> & xSeries,const Reference<chart2::XCoordinateSystem> & xCorrespondingCoordinateSystem,sal_Int32 nDimensionIndex,sal_Int32 nAxisIndex)482 sal_Int32 getNumberFormatKeyFromAxis(
483     const Reference< chart2::XDataSeries > & xSeries,
484     const Reference< chart2::XCoordinateSystem > & xCorrespondingCoordinateSystem,
485     sal_Int32 nDimensionIndex,
486     sal_Int32 nAxisIndex /* = -1 */ )
487 {
488     sal_Int32 nResult =  0;
489     if( nAxisIndex == -1 )
490         nAxisIndex = getAttachedAxisIndex( xSeries );
491     try
492     {
493         Reference< beans::XPropertySet > xAxisProp(
494             xCorrespondingCoordinateSystem->getAxisByDimension( nDimensionIndex, nAxisIndex ), uno::UNO_QUERY );
495         if( xAxisProp.is())
496             xAxisProp->getPropertyValue( C2U("NumberFormat")) >>= nResult;
497     }
498     catch( const uno::Exception & ex )
499     {
500         ASSERT_EXCEPTION( ex );
501     }
502 
503     return nResult;
504 }
505 
getCoordinateSystemOfSeries(const Reference<chart2::XDataSeries> & xSeries,const Reference<chart2::XDiagram> & xDiagram)506 Reference< chart2::XCoordinateSystem > getCoordinateSystemOfSeries(
507     const Reference< chart2::XDataSeries > & xSeries,
508     const Reference< chart2::XDiagram > & xDiagram )
509 {
510     Reference< chart2::XCoordinateSystem > xResult;
511     Reference< chart2::XChartType > xDummy;
512     lcl_getCooSysAndChartTypeOfSeries( xSeries, xDiagram, xResult, xDummy );
513 
514     return xResult;
515 }
516 
getChartTypeOfSeries(const Reference<chart2::XDataSeries> & xSeries,const Reference<chart2::XDiagram> & xDiagram)517 Reference< chart2::XChartType > getChartTypeOfSeries(
518     const Reference< chart2::XDataSeries > & xSeries,
519     const Reference< chart2::XDiagram > & xDiagram )
520 {
521     Reference< chart2::XChartType > xResult;
522     Reference< chart2::XCoordinateSystem > xDummy;
523     lcl_getCooSysAndChartTypeOfSeries( xSeries, xDiagram, xDummy, xResult );
524 
525     return xResult;
526 }
527 
deleteSeries(const Reference<chart2::XDataSeries> & xSeries,const Reference<chart2::XChartType> & xChartType)528 void deleteSeries(
529     const Reference< chart2::XDataSeries > & xSeries,
530     const Reference< chart2::XChartType > & xChartType )
531 {
532     try
533     {
534         Reference< chart2::XDataSeriesContainer > xSeriesCnt( xChartType, uno::UNO_QUERY_THROW );
535         ::std::vector< Reference< chart2::XDataSeries > > aSeries(
536             ContainerHelper::SequenceToVector( xSeriesCnt->getDataSeries()));
537         ::std::vector< Reference< chart2::XDataSeries > >::iterator aIt =
538               ::std::find( aSeries.begin(), aSeries.end(), xSeries );
539         if( aIt != aSeries.end())
540         {
541             aSeries.erase( aIt );
542             xSeriesCnt->setDataSeries( ContainerHelper::ContainerToSequence( aSeries ));
543         }
544     }
545     catch( uno::Exception & ex )
546     {
547         ASSERT_EXCEPTION( ex );
548     }
549 }
550 
switchSymbolsOnOrOff(const Reference<beans::XPropertySet> & xSeriesProperties,bool bSymbolsOn,sal_Int32 nSeriesIndex)551 void switchSymbolsOnOrOff( const Reference< beans::XPropertySet > & xSeriesProperties,
552                     bool bSymbolsOn, sal_Int32 nSeriesIndex )
553 {
554     if( !xSeriesProperties.is() )
555         return;
556 
557     chart2::Symbol aSymbProp;
558     if( (xSeriesProperties->getPropertyValue( C2U( "Symbol" )) >>= aSymbProp ) )
559     {
560         if( !bSymbolsOn )
561             aSymbProp.Style = chart2::SymbolStyle_NONE;
562         else if( aSymbProp.Style == chart2::SymbolStyle_NONE )
563         {
564             aSymbProp.Style = chart2::SymbolStyle_STANDARD;
565             aSymbProp.StandardSymbol = nSeriesIndex;
566         }
567         xSeriesProperties->setPropertyValue( C2U( "Symbol" ), uno::makeAny( aSymbProp ));
568     }
569     //todo: check attributed data points
570 }
571 
switchLinesOnOrOff(const Reference<beans::XPropertySet> & xSeriesProperties,bool bLinesOn)572 void switchLinesOnOrOff( const Reference< beans::XPropertySet > & xSeriesProperties, bool bLinesOn )
573 {
574     if( !xSeriesProperties.is() )
575         return;
576 
577     if( bLinesOn )
578     {
579         // keep line-styles that are not NONE
580         drawing::LineStyle eLineStyle;
581         if( (xSeriesProperties->getPropertyValue( C2U( "LineStyle" )) >>= eLineStyle ) &&
582             eLineStyle == drawing::LineStyle_NONE )
583         {
584             xSeriesProperties->setPropertyValue( C2U( "LineStyle" ), uno::makeAny( drawing::LineStyle_SOLID ) );
585         }
586     }
587     else
588         xSeriesProperties->setPropertyValue( C2U( "LineStyle" ), uno::makeAny( drawing::LineStyle_NONE ) );
589 }
590 
makeLinesThickOrThin(const Reference<beans::XPropertySet> & xSeriesProperties,bool bThick)591 void makeLinesThickOrThin( const Reference< beans::XPropertySet > & xSeriesProperties, bool bThick )
592 {
593     if( !xSeriesProperties.is() )
594         return;
595 
596     sal_Int32 nNewValue = bThick ? 80 : 0;
597     sal_Int32 nOldValue = 0;
598     if( (xSeriesProperties->getPropertyValue( C2U( "LineWidth" )) >>= nOldValue ) &&
599         nOldValue != nNewValue )
600     {
601         if( !(bThick && nOldValue>0))
602             xSeriesProperties->setPropertyValue( C2U( "LineWidth" ), uno::makeAny( nNewValue ) );
603     }
604 }
605 
setPropertyAlsoToAllAttributedDataPoints(const Reference<chart2::XDataSeries> & xSeries,const OUString & rPropertyName,const uno::Any & rPropertyValue)606 void setPropertyAlsoToAllAttributedDataPoints( const Reference< chart2::XDataSeries >& xSeries,
607                                               const OUString& rPropertyName, const uno::Any& rPropertyValue )
608 {
609     Reference< beans::XPropertySet > xSeriesProperties( xSeries, uno::UNO_QUERY );
610     if( !xSeriesProperties.is() )
611         return;
612 
613     xSeriesProperties->setPropertyValue( rPropertyName, rPropertyValue );
614     uno::Sequence< sal_Int32 > aAttributedDataPointIndexList;
615     if( xSeriesProperties->getPropertyValue( C2U( "AttributedDataPoints" ) ) >>= aAttributedDataPointIndexList )
616     {
617         for(sal_Int32 nN=aAttributedDataPointIndexList.getLength();nN--;)
618         {
619             Reference< beans::XPropertySet > xPointProp( xSeries->getDataPointByIndex(aAttributedDataPointIndexList[nN]) );
620             if(!xPointProp.is())
621                 continue;
622             xPointProp->setPropertyValue( rPropertyName, rPropertyValue );
623         }
624     }
625 }
626 
hasAttributedDataPointDifferentValue(const Reference<chart2::XDataSeries> & xSeries,const OUString & rPropertyName,const uno::Any & rPropertyValue)627 bool hasAttributedDataPointDifferentValue( const Reference< chart2::XDataSeries >& xSeries,
628                                               const OUString& rPropertyName, const uno::Any& rPropertyValue )
629 {
630     Reference< beans::XPropertySet > xSeriesProperties( xSeries, uno::UNO_QUERY );
631     if( !xSeriesProperties.is() )
632         return false;
633 
634     uno::Sequence< sal_Int32 > aAttributedDataPointIndexList;
635     if( xSeriesProperties->getPropertyValue( C2U( "AttributedDataPoints" ) ) >>= aAttributedDataPointIndexList )
636     {
637         for(sal_Int32 nN=aAttributedDataPointIndexList.getLength();nN--;)
638         {
639             Reference< beans::XPropertySet > xPointProp( xSeries->getDataPointByIndex(aAttributedDataPointIndexList[nN]) );
640             if(!xPointProp.is())
641                 continue;
642             uno::Any aPointValue( xPointProp->getPropertyValue( rPropertyName ) );
643             if( !( rPropertyValue==aPointValue ) )
644                 return true;
645         }
646     }
647     return false;
648 }
649 
areAllSeriesAttachedToSameAxis(const uno::Reference<chart2::XChartType> & xChartType,sal_Int32 & rOutAxisIndex)650 bool areAllSeriesAttachedToSameAxis( const uno::Reference< chart2::XChartType >& xChartType, sal_Int32 & rOutAxisIndex )
651 {
652     try
653     {
654         uno::Reference< chart2::XDataSeriesContainer > xDataSeriesContainer( xChartType, uno::UNO_QUERY_THROW );
655         uno::Sequence< uno::Reference< chart2::XDataSeries > > aSeriesSeq( xDataSeriesContainer->getDataSeries());
656 
657         const sal_Int32 nSeriesCount( aSeriesSeq.getLength());
658         // AxisIndex can only be 0 or 1
659         sal_Int32 nSeriesAtFirstAxis = 0;
660         sal_Int32 nSeriesAtSecondAxis = 0;
661 
662         for( sal_Int32 nI = 0; nI < nSeriesCount; ++nI )
663         {
664             uno::Reference< chart2::XDataSeries > xSeries( aSeriesSeq[nI], uno::UNO_QUERY );
665             sal_Int32 nAxisIndex = DataSeriesHelper::getAttachedAxisIndex( xSeries );
666             if( nAxisIndex == 0 )
667                 ++nSeriesAtFirstAxis;
668             else if( nAxisIndex == 1 )
669                 ++nSeriesAtSecondAxis;
670         }
671         OSL_ENSURE( nSeriesAtFirstAxis + nSeriesAtSecondAxis == nSeriesCount, "Invalid axis index found" );
672 
673         if( nSeriesAtFirstAxis == nSeriesCount )
674             rOutAxisIndex = 0;
675         else if( nSeriesAtSecondAxis == nSeriesCount )
676             rOutAxisIndex = 1;
677 
678         return ( nSeriesAtFirstAxis == nSeriesCount ||
679                  nSeriesAtSecondAxis == nSeriesCount );
680     }
681     catch( const uno::Exception & ex )
682     {
683         ASSERT_EXCEPTION( ex );
684         return false;
685     }
686 }
687 
688 namespace
689 {
690 
lcl_SequenceHasUnhiddenData(const uno::Reference<chart2::data::XDataSequence> & xDataSequence)691 bool lcl_SequenceHasUnhiddenData( const uno::Reference< chart2::data::XDataSequence >& xDataSequence )
692 {
693     if( !xDataSequence.is() )
694         return false;
695     uno::Reference< beans::XPropertySet > xProp( xDataSequence, uno::UNO_QUERY );
696     if( xProp.is() )
697     {
698         uno::Sequence< sal_Int32 > aHiddenValues;
699         try
700         {
701             xProp->getPropertyValue( C2U( "HiddenValues" ) ) >>= aHiddenValues;
702             if( !aHiddenValues.getLength() )
703                 return true;
704         }
705         catch( uno::Exception& e )
706         {
707             (void)e; // avoid warning
708             return true;
709         }
710     }
711     if( xDataSequence->getData().getLength() )
712         return true;
713     return false;
714 }
715 
716 }
717 
hasUnhiddenData(const uno::Reference<chart2::XDataSeries> & xSeries)718 bool hasUnhiddenData( const uno::Reference< chart2::XDataSeries >& xSeries )
719 {
720     uno::Reference< chart2::data::XDataSource > xDataSource =
721         uno::Reference< chart2::data::XDataSource >( xSeries, uno::UNO_QUERY );
722 
723     uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aDataSequences = xDataSource->getDataSequences();
724 
725     for(sal_Int32 nN = aDataSequences.getLength();nN--;)
726     {
727         if( !aDataSequences[nN].is() )
728             continue;
729         if( lcl_SequenceHasUnhiddenData( aDataSequences[nN]->getValues() ) )
730             return true;
731         if( lcl_SequenceHasUnhiddenData( aDataSequences[nN]->getLabel() ) )
732             return true;
733     }
734     return false;
735 }
736 
737 struct lcl_LessIndex
738 {
operator ()chart::DataSeriesHelper::lcl_LessIndex739     inline bool operator() ( const sal_Int32& first, const sal_Int32& second )
740     {
741         return ( first < second );
742     }
743 };
744 
translateIndexFromHiddenToFullSequence(sal_Int32 nIndex,const Reference<chart2::data::XDataSequence> & xDataSequence,bool bTranslate)745 sal_Int32 translateIndexFromHiddenToFullSequence( sal_Int32 nIndex, const Reference< chart2::data::XDataSequence >& xDataSequence, bool bTranslate )
746 {
747     if( !bTranslate )
748         return nIndex;
749 
750     try
751     {
752         uno::Reference<beans::XPropertySet> xProp( xDataSequence, uno::UNO_QUERY );
753         if( xProp.is())
754         {
755             Sequence<sal_Int32> aHiddenIndicesSeq;
756             xProp->getPropertyValue( C2U("HiddenValues") ) >>= aHiddenIndicesSeq;
757             if( aHiddenIndicesSeq.getLength() )
758             {
759                 ::std::vector< sal_Int32 > aHiddenIndices( ContainerHelper::SequenceToVector( aHiddenIndicesSeq ) );
760                 ::std::sort( aHiddenIndices.begin(), aHiddenIndices.end(), lcl_LessIndex() );
761 
762                 sal_Int32 nHiddenCount = static_cast<sal_Int32>(aHiddenIndices.size());
763                 for( sal_Int32 nN = 0; nN < nHiddenCount; ++nN)
764                 {
765                     if( aHiddenIndices[nN] <= nIndex )
766                         nIndex += 1;
767                     else
768                         break;
769                 }
770             }
771         }
772     }
773     catch (const beans::UnknownPropertyException&)
774     {
775     }
776     return nIndex;
777 }
778 
hasDataLabelsAtSeries(const Reference<chart2::XDataSeries> & xSeries)779 bool hasDataLabelsAtSeries( const Reference< chart2::XDataSeries >& xSeries )
780 {
781     bool bRet = false;
782     try
783     {
784         Reference< beans::XPropertySet > xProp( xSeries, uno::UNO_QUERY );
785         if( xProp.is() )
786         {
787             DataPointLabel aLabel;
788             if( (xProp->getPropertyValue( C2U( "Label" ) ) >>= aLabel) )
789                 bRet = aLabel.ShowNumber || aLabel.ShowNumberInPercent || aLabel.ShowCategoryName;
790         }
791     }
792     catch( uno::Exception &e)
793     {
794         ASSERT_EXCEPTION( e );
795     }
796     return bRet;
797 }
798 
hasDataLabelsAtPoints(const Reference<chart2::XDataSeries> & xSeries)799 bool hasDataLabelsAtPoints( const Reference< chart2::XDataSeries >& xSeries )
800 {
801     bool bRet = false;
802     try
803     {
804         Reference< beans::XPropertySet > xSeriesProperties( xSeries, uno::UNO_QUERY );
805         if( xSeriesProperties.is() )
806         {
807             uno::Sequence< sal_Int32 > aAttributedDataPointIndexList;
808             if( xSeriesProperties->getPropertyValue( C2U( "AttributedDataPoints" ) ) >>= aAttributedDataPointIndexList )
809             {
810                 for(sal_Int32 nN=aAttributedDataPointIndexList.getLength();nN--;)
811                 {
812                     Reference< beans::XPropertySet > xPointProp( xSeries->getDataPointByIndex(aAttributedDataPointIndexList[nN]) );
813                     if( xPointProp.is() )
814                     {
815                         DataPointLabel aLabel;
816                         if( (xPointProp->getPropertyValue( C2U( "Label" ) ) >>= aLabel) )
817                             bRet = aLabel.ShowNumber || aLabel.ShowNumberInPercent || aLabel.ShowCategoryName;
818                         if( bRet )
819                             break;
820                     }
821                 }
822             }
823         }
824     }
825     catch( uno::Exception &e)
826     {
827         ASSERT_EXCEPTION( e );
828     }
829     return bRet;
830 }
831 
hasDataLabelAtPoint(const Reference<chart2::XDataSeries> & xSeries,sal_Int32 nPointIndex)832 bool hasDataLabelAtPoint( const Reference< chart2::XDataSeries >& xSeries, sal_Int32 nPointIndex )
833 {
834     bool bRet = false;
835     try
836     {
837         Reference< beans::XPropertySet > xProp;
838         Reference< beans::XPropertySet > xSeriesProperties( xSeries, uno::UNO_QUERY );
839         if( xSeriesProperties.is() )
840         {
841             uno::Sequence< sal_Int32 > aAttributedDataPointIndexList;
842             if( xSeriesProperties->getPropertyValue( C2U( "AttributedDataPoints" ) ) >>= aAttributedDataPointIndexList )
843             {
844                 ::std::vector< sal_Int32 > aIndices( ContainerHelper::SequenceToVector( aAttributedDataPointIndexList ) );
845                 ::std::vector< sal_Int32 >::iterator aIt = ::std::find( aIndices.begin(), aIndices.end(), nPointIndex );
846                 if( aIt != aIndices.end())
847                     xProp = xSeries->getDataPointByIndex(nPointIndex);
848                 else
849                     xProp = xSeriesProperties;
850             }
851             if( xProp.is() )
852             {
853                 DataPointLabel aLabel;
854                 if( (xProp->getPropertyValue( C2U( "Label" ) ) >>= aLabel) )
855                     bRet = aLabel.ShowNumber || aLabel.ShowNumberInPercent || aLabel.ShowCategoryName;
856             }
857         }
858     }
859     catch( uno::Exception &e)
860     {
861         ASSERT_EXCEPTION( e );
862     }
863     return bRet;
864 }
865 
insertDataLabelsToSeriesAndAllPoints(const Reference<chart2::XDataSeries> & xSeries)866 void insertDataLabelsToSeriesAndAllPoints( const Reference< chart2::XDataSeries >& xSeries )
867 {
868     lcl_insertOrDeleteDataLabelsToSeriesAndAllPoints( xSeries, true /*bInsert*/ );
869 }
870 
deleteDataLabelsFromSeriesAndAllPoints(const Reference<chart2::XDataSeries> & xSeries)871 void deleteDataLabelsFromSeriesAndAllPoints( const Reference< chart2::XDataSeries >& xSeries )
872 {
873     lcl_insertOrDeleteDataLabelsToSeriesAndAllPoints( xSeries, false /*bInsert*/ );
874 }
875 
876 
insertDataLabelToPoint(const Reference<beans::XPropertySet> & xPointProp)877 void insertDataLabelToPoint( const Reference< beans::XPropertySet >& xPointProp )
878 {
879     try
880     {
881         if( xPointProp.is() )
882         {
883             DataPointLabel aLabel;
884             xPointProp->getPropertyValue( C2U( "Label" ) ) >>= aLabel;
885             aLabel.ShowNumber = true;
886             xPointProp->setPropertyValue( C2U( "Label" ), uno::makeAny( aLabel ) );
887         }
888     }
889     catch( uno::Exception &e)
890     {
891         ASSERT_EXCEPTION( e );
892     }
893 }
894 
deleteDataLabelsFromPoint(const Reference<beans::XPropertySet> & xPointProp)895 void deleteDataLabelsFromPoint( const Reference< beans::XPropertySet >& xPointProp )
896 {
897     try
898     {
899         if( xPointProp.is() )
900         {
901             DataPointLabel aLabel;
902             xPointProp->getPropertyValue( C2U( "Label" ) ) >>= aLabel;
903             aLabel.ShowNumber = false;
904             aLabel.ShowNumberInPercent = false;
905             aLabel.ShowCategoryName = false;
906             xPointProp->setPropertyValue( C2U( "Label" ), uno::makeAny( aLabel ) );
907         }
908     }
909     catch( uno::Exception &e)
910     {
911         ASSERT_EXCEPTION( e );
912     }
913 }
914 
915 } //  namespace DataSeriesHelper
916 } //  namespace chart
917