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 <ExplicitCategoriesProvider.hxx>
21 #include <DiagramHelper.hxx>
22 #include <ChartTypeHelper.hxx>
23 #include <AxisHelper.hxx>
24 #include <DataSourceHelper.hxx>
25 #include <ChartModel.hxx>
26 #include <ChartModelHelper.hxx>
27 #include <NumberFormatterWrapper.hxx>
28 #include <unonames.hxx>
30 #include <com/sun/star/chart2/AxisType.hpp>
31 #include <tools/diagnose_ex.h>
36 using namespace ::com::sun::star
;
37 using namespace ::com::sun::star::chart2
;
38 using ::com::sun::star::uno::Reference
;
39 using ::com::sun::star::uno::Sequence
;
42 ExplicitCategoriesProvider::ExplicitCategoriesProvider( const Reference
< chart2::XCoordinateSystem
>& xCooSysModel
43 , ChartModel
& rModel
)
45 , m_xCooSysModel( xCooSysModel
)
47 , m_xOriginalCategories()
48 , m_bIsExplicitCategoriesInited(false)
49 , m_bIsDateAxis(false)
50 , m_bIsAutoDate(false)
54 if( xCooSysModel
.is() )
56 uno::Reference
< XAxis
> xAxis( xCooSysModel
->getAxisByDimension(0,0) );
59 ScaleData
aScale( xAxis
->getScaleData() );
60 m_xOriginalCategories
= aScale
.Categories
;
61 m_bIsAutoDate
= (aScale
.AutoDateAxis
&& aScale
.AxisType
==chart2::AxisType::CATEGORY
);
62 m_bIsDateAxis
= (aScale
.AxisType
== chart2::AxisType::DATE
|| m_bIsAutoDate
);
66 if( m_xOriginalCategories
.is() )
68 uno::Reference
< data::XDataProvider
> xDataProvider( mrModel
.getDataProvider() );
70 OUString
aCategoriesRange( DataSourceHelper::getRangeFromValues( m_xOriginalCategories
) );
71 if( xDataProvider
.is() && !aCategoriesRange
.isEmpty() )
73 const bool bFirstCellAsLabel
= false;
74 const bool bHasCategories
= false;
75 const uno::Sequence
< sal_Int32
> aSequenceMapping
;
77 uno::Reference
< data::XDataSource
> xColumnCategoriesSource( xDataProvider
->createDataSource(
78 DataSourceHelper::createArguments( aCategoriesRange
, aSequenceMapping
, true /*bUseColumns*/
79 , bFirstCellAsLabel
, bHasCategories
) ) );
81 uno::Reference
< data::XDataSource
> xRowCategoriesSource( xDataProvider
->createDataSource(
82 DataSourceHelper::createArguments( aCategoriesRange
, aSequenceMapping
, false /*bUseColumns*/
83 , bFirstCellAsLabel
, bHasCategories
) ) );
85 if( xColumnCategoriesSource
.is() && xRowCategoriesSource
.is() )
87 Sequence
< Reference
< data::XLabeledDataSequence
> > aColumns
= xColumnCategoriesSource
->getDataSequences();
88 Sequence
< Reference
< data::XLabeledDataSequence
> > aRows
= xRowCategoriesSource
->getDataSequences();
90 sal_Int32 nColumnCount
= aColumns
.getLength();
91 sal_Int32 nRowCount
= aRows
.getLength();
92 if( nColumnCount
>1 && nRowCount
>1 )
94 //we have complex categories
95 //->split them in the direction of the first series
96 //detect whether the first series is a row or a column
97 bool bSeriesUsesColumns
= true;
98 std::vector
< Reference
< XDataSeries
> > aSeries( ChartModelHelper::getDataSeries( mrModel
) );
99 if( !aSeries
.empty() )
101 uno::Reference
< data::XDataSource
> xSeriesSource( aSeries
.front(), uno::UNO_QUERY
);
102 OUString aStringDummy
;
104 uno::Sequence
< sal_Int32
> aSeqDummy
;
105 DataSourceHelper::readArguments( xDataProvider
->detectArguments( xSeriesSource
),
106 aStringDummy
, aSeqDummy
, bSeriesUsesColumns
, bDummy
, bDummy
);
108 if( bSeriesUsesColumns
)
109 m_aSplitCategoriesList
=aColumns
;
111 m_aSplitCategoriesList
=aRows
;
115 if( !m_aSplitCategoriesList
.hasElements() )
117 m_aSplitCategoriesList
.realloc(1);
118 m_aSplitCategoriesList
[0]=m_xOriginalCategories
;
122 catch( const uno::Exception
& )
124 DBG_UNHANDLED_EXCEPTION("chart2");
128 ExplicitCategoriesProvider::~ExplicitCategoriesProvider()
132 Reference
< chart2::data::XDataSequence
> ExplicitCategoriesProvider::getOriginalCategories()
134 if( m_xOriginalCategories
.is() )
135 return m_xOriginalCategories
->getValues();
139 bool ExplicitCategoriesProvider::hasComplexCategories() const
141 return m_aSplitCategoriesList
.getLength() > 1;
144 sal_Int32
ExplicitCategoriesProvider::getCategoryLevelCount() const
146 sal_Int32 nCount
= m_aSplitCategoriesList
.getLength();
152 static std::vector
<sal_Int32
> lcl_getLimitingBorders( const std::vector
< ComplexCategory
>& rComplexCategories
)
154 std::vector
<sal_Int32
> aLimitingBorders
;
155 sal_Int32 nBorderIndex
= 0; /*border below the index*/
156 for (auto const& complexCategory
: rComplexCategories
)
158 nBorderIndex
+= complexCategory
.Count
;
159 aLimitingBorders
.push_back(nBorderIndex
);
161 return aLimitingBorders
;
164 void ExplicitCategoriesProvider::convertCategoryAnysToText( uno::Sequence
< OUString
>& rOutTexts
, const uno::Sequence
< uno::Any
>& rInAnys
, ChartModel
& rModel
)
166 sal_Int32 nCount
= rInAnys
.getLength();
169 rOutTexts
.realloc(nCount
);
171 sal_Int32 nAxisNumberFormat
= 0;
172 Reference
< XCoordinateSystem
> xCooSysModel( ChartModelHelper::getFirstCoordinateSystem( rModel
) );
173 if( xCooSysModel
.is() )
175 Reference
< chart2::XAxis
> xAxis( xCooSysModel
->getAxisByDimension(0,0) );
176 nAxisNumberFormat
= AxisHelper::getExplicitNumberFormatKeyForAxis(
177 xAxis
, xCooSysModel
, uno::Reference
<chart2::XChartDocument
>(static_cast< ::cppu::OWeakObject
* >(&rModel
), uno::UNO_QUERY
), false );
181 bool bColorChanged
= false;
183 NumberFormatterWrapper
aNumberFormatterWrapper( rModel
.getNumberFormatsSupplier() );
185 for(sal_Int32 nN
=0;nN
<nCount
;nN
++)
188 uno::Any aAny
= rInAnys
[nN
];
189 if( aAny
.hasValue() )
194 if( !::rtl::math::isNan(fDouble
) )
195 aText
= aNumberFormatterWrapper
.getFormattedString(
196 nAxisNumberFormat
, fDouble
, nLabelColor
, bColorChanged
);
203 rOutTexts
[nN
] = aText
;
207 SplitCategoriesProvider::~SplitCategoriesProvider()
211 class SplitCategoriesProvider_ForLabeledDataSequences
: public SplitCategoriesProvider
215 explicit SplitCategoriesProvider_ForLabeledDataSequences(
216 const css::uno::Sequence
<
217 css::uno::Reference
< css::chart2::data::XLabeledDataSequence
> >& rSplitCategoriesList
218 , ChartModel
& rModel
)
219 : m_rSplitCategoriesList( rSplitCategoriesList
)
223 virtual sal_Int32
getLevelCount() const override
;
224 virtual uno::Sequence
< OUString
> getStringsForLevel( sal_Int32 nIndex
) const override
;
227 const css::uno::Sequence
< css::uno::Reference
<
228 css::chart2::data::XLabeledDataSequence
> >& m_rSplitCategoriesList
;
233 sal_Int32
SplitCategoriesProvider_ForLabeledDataSequences::getLevelCount() const
235 return m_rSplitCategoriesList
.getLength();
237 uno::Sequence
< OUString
> SplitCategoriesProvider_ForLabeledDataSequences::getStringsForLevel( sal_Int32 nLevel
) const
239 uno::Sequence
< OUString
> aRet
;
240 Reference
< data::XLabeledDataSequence
> xLabeledDataSequence( m_rSplitCategoriesList
[nLevel
] );
241 if( xLabeledDataSequence
.is() )
243 uno::Reference
< data::XDataSequence
> xDataSequence( xLabeledDataSequence
->getValues() );
244 if( xDataSequence
.is() )
245 ExplicitCategoriesProvider::convertCategoryAnysToText( aRet
, xDataSequence
->getData(), mrModel
);
250 static std::vector
< ComplexCategory
> lcl_DataSequenceToComplexCategoryVector(
251 const uno::Sequence
< OUString
>& rStrings
252 , const std::vector
<sal_Int32
>& rLimitingBorders
, bool bCreateSingleCategories
)
254 std::vector
< ComplexCategory
> aResult
;
256 sal_Int32 nMaxCount
= rStrings
.getLength();
258 sal_Int32 nCurrentCount
=0;
259 for( sal_Int32 nN
=0; nN
<nMaxCount
; nN
++ )
261 const OUString
& aCurrent
= rStrings
[nN
];
262 if( bCreateSingleCategories
|| std::find( rLimitingBorders
.begin(), rLimitingBorders
.end(), nN
) != rLimitingBorders
.end() )
264 aResult
.emplace_back(aPrevious
,nCurrentCount
);
266 aPrevious
= aCurrent
;
270 // Empty value is interpreted as a continuation of the previous
271 // category. Note that having the same value as the previous one
272 // does not equate to a continuation of the category.
274 if (aCurrent
.isEmpty())
278 aResult
.emplace_back(aPrevious
,nCurrentCount
);
280 aPrevious
= aCurrent
;
285 aResult
.emplace_back(aPrevious
,nCurrentCount
);
290 static sal_Int32
lcl_getCategoryCount( std::vector
< ComplexCategory
>& rComplexCategories
)
292 sal_Int32 nCount
= 0;
293 for (auto const& complexCategory
: rComplexCategories
)
294 nCount
+=complexCategory
.Count
;
298 static Sequence
< OUString
> lcl_getExplicitSimpleCategories(
299 const SplitCategoriesProvider
& rSplitCategoriesProvider
,
300 std::vector
< std::vector
< ComplexCategory
> >& rComplexCats
)
302 Sequence
< OUString
> aRet
;
304 rComplexCats
.clear();
305 sal_Int32 nLCount
= rSplitCategoriesProvider
.getLevelCount();
306 for( sal_Int32 nL
= 0; nL
< nLCount
; nL
++ )
308 std::vector
<sal_Int32
> aLimitingBorders
;
310 aLimitingBorders
= lcl_getLimitingBorders( rComplexCats
.back() );
311 rComplexCats
.push_back( lcl_DataSequenceToComplexCategoryVector(
312 rSplitCategoriesProvider
.getStringsForLevel(nL
), aLimitingBorders
, nL
==(nLCount
-1) ) );
315 //ensure that the category count is the same on each level
316 sal_Int32 nMaxCategoryCount
= 0;
318 for (auto & complexCat
: rComplexCats
)
320 sal_Int32 nCurrentCount
= lcl_getCategoryCount(complexCat
);
321 nMaxCategoryCount
= std::max( nCurrentCount
, nMaxCategoryCount
);
323 for (auto & complexCat
: rComplexCats
)
325 if ( !complexCat
.empty() )
327 sal_Int32 nCurrentCount
= lcl_getCategoryCount(complexCat
);
328 if( nCurrentCount
< nMaxCategoryCount
)
330 ComplexCategory
& rComplexCategory
= complexCat
.back();
331 rComplexCategory
.Count
+= (nMaxCategoryCount
-nCurrentCount
);
337 //create a list with an element for every index
338 std::vector
< std::vector
< ComplexCategory
> > aComplexCatsPerIndex
;
339 for (auto const& complexCat
: rComplexCats
)
341 std::vector
< ComplexCategory
> aSingleLevel
;
342 for (auto const& elem
: complexCat
)
344 sal_Int32 nCount
= elem
.Count
;
346 aSingleLevel
.push_back(elem
);
348 aComplexCatsPerIndex
.push_back( aSingleLevel
);
351 if(nMaxCategoryCount
)
353 aRet
.realloc(nMaxCategoryCount
);
354 for(sal_Int32 nN
=0; nN
<nMaxCategoryCount
; nN
++)
356 OUStringBuffer aText
;
357 for (auto const& complexCatPerIndex
: aComplexCatsPerIndex
)
359 if ( static_cast<size_t>(nN
) < complexCatPerIndex
.size() )
361 OUString aAddText
= complexCatPerIndex
[nN
].Text
;
362 if( !aAddText
.isEmpty() )
366 aText
.append(aAddText
);
370 aRet
[nN
]=aText
.makeStringAndClear();
376 Sequence
< OUString
> ExplicitCategoriesProvider::getExplicitSimpleCategories(
377 const SplitCategoriesProvider
& rSplitCategoriesProvider
)
379 vector
< vector
< ComplexCategory
> > aComplexCats
;
380 return lcl_getExplicitSimpleCategories( rSplitCategoriesProvider
, aComplexCats
);
383 static bool lcl_fillDateCategories( const uno::Reference
< data::XDataSequence
>& xDataSequence
, std::vector
< double >& rDateCategories
, bool bIsAutoDate
, ChartModel
& rModel
)
385 bool bOnlyDatesFound
= true;
386 bool bAnyDataFound
= false;
388 if( xDataSequence
.is() )
390 uno::Sequence
< uno::Any
> aValues
= xDataSequence
->getData();
391 sal_Int32 nCount
= aValues
.getLength();
392 rDateCategories
.reserve(nCount
);
393 Reference
< util::XNumberFormats
> xNumberFormats( rModel
.getNumberFormats() );
395 bool bOwnData
= false;
396 bool bOwnDataAnddAxisHasAnyFormat
= false;
397 bool bOwnDataAnddAxisHasDateFormat
= false;
398 Reference
< XCoordinateSystem
> xCooSysModel( ChartModelHelper::getFirstCoordinateSystem( rModel
) );
399 if( xCooSysModel
.is() )
401 if( rModel
.hasInternalDataProvider() )
404 Reference
< beans::XPropertySet
> xAxisProps( xCooSysModel
->getAxisByDimension(0,0), uno::UNO_QUERY
);
405 sal_Int32 nAxisNumberFormat
= 0;
406 if (xAxisProps
.is() && (xAxisProps
->getPropertyValue(CHART_UNONAME_NUMFMT
) >>= nAxisNumberFormat
))
408 bOwnDataAnddAxisHasAnyFormat
= true;
409 bOwnDataAnddAxisHasDateFormat
= DiagramHelper::isDateNumberFormat( nAxisNumberFormat
, xNumberFormats
);
414 for(sal_Int32 nN
=0;nN
<nCount
;nN
++)
416 bool bIsDate
= false;
420 bIsDate
= !bOwnDataAnddAxisHasAnyFormat
|| bOwnDataAnddAxisHasDateFormat
;
422 bIsDate
= DiagramHelper::isDateNumberFormat( xDataSequence
->getNumberFormatKeyByIndex( nN
), xNumberFormats
);
427 bool bContainsEmptyString
= false;
428 uno::Any aAny
= aValues
[nN
];
429 if( aAny
.hasValue() )
433 bool bContainsNan
= false;
434 if( (aAny
>>=aTest
) && aTest
.isEmpty() ) //empty String
435 bContainsEmptyString
= true;
436 else if( (aAny
>>=fTest
) && ::rtl::math::isNan(fTest
) )
439 if( !bContainsEmptyString
&& !bContainsNan
)
440 bAnyDataFound
= true;
443 if( bIsDate
&& (aAny
>>= aDate
) )
444 rDateCategories
.push_back( aDate
);
447 if( aAny
.hasValue() && !bContainsEmptyString
)//empty string does not count as non date value!
448 bOnlyDatesFound
=false;
449 ::rtl::math::setNan( &aDate
);
450 rDateCategories
.push_back( aDate
);
453 std::sort( rDateCategories
.begin(), rDateCategories
.end() );
456 return bAnyDataFound
&& bOnlyDatesFound
;
459 void ExplicitCategoriesProvider::init()
463 m_aComplexCats
.clear();//not one per index
464 m_aDateCategories
.clear();
466 if( m_xOriginalCategories
.is() )
468 if( !hasComplexCategories() )
472 if( ChartTypeHelper::isSupportingDateAxis( AxisHelper::getChartTypeByIndex( m_xCooSysModel
, 0 ), 0 ) )
473 m_bIsDateAxis
= lcl_fillDateCategories( m_xOriginalCategories
->getValues(), m_aDateCategories
, m_bIsAutoDate
, mrModel
);
475 m_bIsDateAxis
= false;
480 m_bIsDateAxis
= false;
489 Sequence
< OUString
> const & ExplicitCategoriesProvider::getSimpleCategories()
491 if( !m_bIsExplicitCategoriesInited
)
494 m_aExplicitCategories
.realloc(0);
495 if( m_xOriginalCategories
.is() )
497 if( !hasComplexCategories() )
499 uno::Reference
< data::XDataSequence
> xDataSequence( m_xOriginalCategories
->getValues() );
500 if( xDataSequence
.is() )
501 ExplicitCategoriesProvider::convertCategoryAnysToText( m_aExplicitCategories
, xDataSequence
->getData(), mrModel
);
505 m_aExplicitCategories
= lcl_getExplicitSimpleCategories(
506 SplitCategoriesProvider_ForLabeledDataSequences( m_aSplitCategoriesList
, mrModel
), m_aComplexCats
);
509 if(!m_aExplicitCategories
.hasElements())
510 m_aExplicitCategories
= DiagramHelper::generateAutomaticCategoriesFromCooSys( m_xCooSysModel
);
511 m_bIsExplicitCategoriesInited
= true;
513 return m_aExplicitCategories
;
516 const std::vector
<ComplexCategory
>* ExplicitCategoriesProvider::getCategoriesByLevel( sal_Int32 nLevel
)
519 sal_Int32 nMaxIndex
= m_aComplexCats
.size()-1;
520 if (nLevel
>= 0 && nLevel
<= nMaxIndex
)
521 return &m_aComplexCats
[nMaxIndex
-nLevel
];
525 OUString
ExplicitCategoriesProvider::getCategoryByIndex(
526 const Reference
< XCoordinateSystem
>& xCooSysModel
530 if( xCooSysModel
.is())
532 ExplicitCategoriesProvider
aExplicitCategoriesProvider( xCooSysModel
, rModel
);
533 Sequence
< OUString
> aCategories( aExplicitCategoriesProvider
.getSimpleCategories());
534 if( nIndex
< aCategories
.getLength())
535 return aCategories
[ nIndex
];
540 bool ExplicitCategoriesProvider::isDateAxis()
543 return m_bIsDateAxis
;
546 const std::vector
< double >& ExplicitCategoriesProvider::getDateCategories()
549 return m_aDateCategories
;
554 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */