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 <ChartType.hxx>
23 #include <ChartTypeHelper.hxx>
25 #include <AxisHelper.hxx>
26 #include <DataSourceHelper.hxx>
27 #include <ChartModel.hxx>
28 #include <ChartModelHelper.hxx>
29 #include <NumberFormatterWrapper.hxx>
30 #include <unonames.hxx>
31 #include <BaseCoordinateSystem.hxx>
32 #include <DataSeries.hxx>
34 #include <com/sun/star/chart2/AxisType.hpp>
35 #include <o3tl/safeint.hxx>
36 #include <rtl/ustrbuf.hxx>
37 #include <comphelper/diagnose_ex.hxx>
44 using namespace ::com::sun::star
;
45 using namespace ::com::sun::star::chart2
;
46 using ::com::sun::star::uno::Reference
;
47 using ::com::sun::star::uno::Sequence
;
50 ExplicitCategoriesProvider::ExplicitCategoriesProvider( const rtl::Reference
< BaseCoordinateSystem
>& xCooSysModel
51 , ChartModel
& rModel
)
53 , m_xCooSysModel( xCooSysModel
.get() )
55 , m_bIsExplicitCategoriesInited(false)
56 , m_bIsDateAxis(false)
57 , m_bIsAutoDate(false)
61 if( xCooSysModel
.is() )
63 // TODO: handle different category names on the primary and secondary category axis.
64 rtl::Reference
< Axis
> xAxis
= xCooSysModel
->getAxisByDimension2(0,0);
67 ScaleData
aScale( xAxis
->getScaleData() );
68 m_xOriginalCategories
= aScale
.Categories
;
69 m_bIsAutoDate
= (aScale
.AutoDateAxis
&& aScale
.AxisType
==chart2::AxisType::CATEGORY
);
70 m_bIsDateAxis
= (aScale
.AxisType
== chart2::AxisType::DATE
|| m_bIsAutoDate
);
74 if( m_xOriginalCategories
.is() )
76 uno::Reference
< data::XDataProvider
> xDataProvider( mrModel
.getDataProvider() );
78 OUString
aCategoriesRange( DataSourceHelper::getRangeFromValues( m_xOriginalCategories
) );
79 if( xDataProvider
.is() && !aCategoriesRange
.isEmpty() )
81 const bool bFirstCellAsLabel
= false;
82 const bool bHasCategories
= false;
83 const uno::Sequence
< sal_Int32
> aSequenceMapping
;
85 uno::Reference
< data::XDataSource
> xColumnCategoriesSource( xDataProvider
->createDataSource(
86 DataSourceHelper::createArguments( aCategoriesRange
, aSequenceMapping
, true /*bUseColumns*/
87 , bFirstCellAsLabel
, bHasCategories
) ) );
89 uno::Reference
< data::XDataSource
> xRowCategoriesSource( xDataProvider
->createDataSource(
90 DataSourceHelper::createArguments( aCategoriesRange
, aSequenceMapping
, false /*bUseColumns*/
91 , bFirstCellAsLabel
, bHasCategories
) ) );
93 if( xColumnCategoriesSource
.is() && xRowCategoriesSource
.is() )
95 Sequence
< Reference
< data::XLabeledDataSequence
> > aColumns
= xColumnCategoriesSource
->getDataSequences();
96 Sequence
< Reference
< data::XLabeledDataSequence
> > aRows
= xRowCategoriesSource
->getDataSequences();
98 sal_Int32 nColumnCount
= aColumns
.getLength();
99 sal_Int32 nRowCount
= aRows
.getLength();
100 if( nColumnCount
>1 && nRowCount
>1 )
102 //we have complex categories
103 //->split them in the direction of the first series
104 //detect whether the first series is a row or a column
105 bool bSeriesUsesColumns
= true;
106 std::vector
< rtl::Reference
< DataSeries
> > aSeries
= ChartModelHelper::getDataSeries( &mrModel
);
107 if( !aSeries
.empty() )
109 rtl::Reference
< DataSeries
> xSeriesSource
= aSeries
.front();
110 OUString aStringDummy
;
112 uno::Sequence
< sal_Int32
> aSeqDummy
;
113 DataSourceHelper::readArguments( xDataProvider
->detectArguments( xSeriesSource
),
114 aStringDummy
, aSeqDummy
, bSeriesUsesColumns
, bDummy
, bDummy
);
116 if( bSeriesUsesColumns
)
117 m_aSplitCategoriesList
= comphelper::sequenceToContainer
<std::vector
<Reference
<data::XLabeledDataSequence
>>>(aColumns
);
119 m_aSplitCategoriesList
= comphelper::sequenceToContainer
<std::vector
<Reference
<data::XLabeledDataSequence
>>>(aRows
);
123 if( m_aSplitCategoriesList
.empty() )
125 m_aSplitCategoriesList
= { m_xOriginalCategories
};
129 catch( const uno::Exception
& )
131 DBG_UNHANDLED_EXCEPTION("chart2");
135 ExplicitCategoriesProvider::~ExplicitCategoriesProvider()
139 Reference
< chart2::data::XDataSequence
> ExplicitCategoriesProvider::getOriginalCategories()
141 if( m_xOriginalCategories
.is() )
142 return m_xOriginalCategories
->getValues();
146 bool ExplicitCategoriesProvider::hasComplexCategories() const
148 return m_aSplitCategoriesList
.size() > 1;
151 sal_Int32
ExplicitCategoriesProvider::getCategoryLevelCount() const
153 sal_Int32 nCount
= m_aSplitCategoriesList
.size();
159 static std::vector
<sal_Int32
> lcl_getLimitingBorders( const std::vector
< ComplexCategory
>& rComplexCategories
)
161 std::vector
<sal_Int32
> aLimitingBorders
;
162 sal_Int32 nBorderIndex
= 0; /*border below the index*/
163 for (auto const& complexCategory
: rComplexCategories
)
165 nBorderIndex
+= complexCategory
.Count
;
166 aLimitingBorders
.push_back(nBorderIndex
);
168 return aLimitingBorders
;
171 void ExplicitCategoriesProvider::convertCategoryAnysToText( uno::Sequence
< OUString
>& rOutTexts
, const uno::Sequence
< uno::Any
>& rInAnys
, ChartModel
& rModel
)
173 sal_Int32 nCount
= rInAnys
.getLength();
176 rOutTexts
.realloc(nCount
);
177 auto pOutTexts
= rOutTexts
.getArray();
179 sal_Int32 nAxisNumberFormat
= 0;
180 rtl::Reference
< BaseCoordinateSystem
> xCooSysModel( ChartModelHelper::getFirstCoordinateSystem( &rModel
) );
181 if( xCooSysModel
.is() )
183 rtl::Reference
< Axis
> xAxis
= xCooSysModel
->getAxisByDimension2(0,0);
184 nAxisNumberFormat
= AxisHelper::getExplicitNumberFormatKeyForAxis(
185 xAxis
, xCooSysModel
, &rModel
, false );
189 bool bColorChanged
= false;
191 NumberFormatterWrapper
aNumberFormatterWrapper( rModel
.getNumberFormatsSupplier() );
193 for(sal_Int32 nN
=0;nN
<nCount
;nN
++)
196 uno::Any aAny
= rInAnys
[nN
];
197 if( aAny
.hasValue() )
202 if( !std::isnan(fDouble
) )
203 aText
= aNumberFormatterWrapper
.getFormattedString(
204 nAxisNumberFormat
, fDouble
, nLabelColor
, bColorChanged
);
211 pOutTexts
[nN
] = aText
;
215 SplitCategoriesProvider::~SplitCategoriesProvider()
221 class SplitCategoriesProvider_ForLabeledDataSequences
: public SplitCategoriesProvider
225 explicit SplitCategoriesProvider_ForLabeledDataSequences(
226 const std::vector
< Reference
< data::XLabeledDataSequence
> >& rSplitCategoriesList
227 , ChartModel
& rModel
)
228 : m_rSplitCategoriesList( rSplitCategoriesList
)
232 virtual sal_Int32
getLevelCount() const override
;
233 virtual uno::Sequence
< OUString
> getStringsForLevel( sal_Int32 nIndex
) const override
;
236 const std::vector
< Reference
< data::XLabeledDataSequence
> >& m_rSplitCategoriesList
;
243 sal_Int32
SplitCategoriesProvider_ForLabeledDataSequences::getLevelCount() const
245 return m_rSplitCategoriesList
.size();
247 uno::Sequence
< OUString
> SplitCategoriesProvider_ForLabeledDataSequences::getStringsForLevel( sal_Int32 nLevel
) const
249 uno::Sequence
< OUString
> aRet
;
250 Reference
< data::XLabeledDataSequence
> xLabeledDataSequence( m_rSplitCategoriesList
[nLevel
] );
251 if( xLabeledDataSequence
.is() )
253 uno::Reference
< data::XDataSequence
> xDataSequence( xLabeledDataSequence
->getValues() );
254 if( xDataSequence
.is() )
255 ExplicitCategoriesProvider::convertCategoryAnysToText( aRet
, xDataSequence
->getData(), mrModel
);
260 static std::vector
< ComplexCategory
> lcl_DataSequenceToComplexCategoryVector(
261 const uno::Sequence
< OUString
>& rStrings
262 , const std::vector
<sal_Int32
>& rLimitingBorders
, bool bCreateSingleCategories
)
264 std::vector
< ComplexCategory
> aResult
;
266 sal_Int32 nMaxCount
= rStrings
.getLength();
268 sal_Int32 nCurrentCount
=0;
269 for( sal_Int32 nN
=0; nN
<nMaxCount
; nN
++ )
271 const OUString
& aCurrent
= rStrings
[nN
];
272 if( bCreateSingleCategories
|| std::find( rLimitingBorders
.begin(), rLimitingBorders
.end(), nN
) != rLimitingBorders
.end() )
274 aResult
.emplace_back(aPrevious
,nCurrentCount
);
276 aPrevious
= aCurrent
;
280 // Empty value is interpreted as a continuation of the previous
281 // category. Note that having the same value as the previous one
282 // does not equate to a continuation of the category.
284 if (aCurrent
.isEmpty())
288 aResult
.emplace_back(aPrevious
,nCurrentCount
);
290 aPrevious
= aCurrent
;
295 aResult
.emplace_back(aPrevious
,nCurrentCount
);
300 static sal_Int32
lcl_getCategoryCount( std::vector
< ComplexCategory
>& rComplexCategories
)
302 sal_Int32 nCount
= 0;
303 for (auto const& complexCategory
: rComplexCategories
)
304 nCount
+=complexCategory
.Count
;
308 static Sequence
< OUString
> lcl_getExplicitSimpleCategories(
309 const SplitCategoriesProvider
& rSplitCategoriesProvider
,
310 std::vector
< std::vector
< ComplexCategory
> >& rComplexCats
)
312 Sequence
< OUString
> aRet
;
314 rComplexCats
.clear();
315 sal_Int32 nLCount
= rSplitCategoriesProvider
.getLevelCount();
316 for( sal_Int32 nL
= 0; nL
< nLCount
; nL
++ )
318 std::vector
<sal_Int32
> aLimitingBorders
;
320 aLimitingBorders
= lcl_getLimitingBorders( rComplexCats
.back() );
321 rComplexCats
.push_back( lcl_DataSequenceToComplexCategoryVector(
322 rSplitCategoriesProvider
.getStringsForLevel(nL
), aLimitingBorders
, nL
==(nLCount
-1) ) );
325 //ensure that the category count is the same on each level
326 sal_Int32 nMaxCategoryCount
= 0;
328 for (auto & complexCat
: rComplexCats
)
330 sal_Int32 nCurrentCount
= lcl_getCategoryCount(complexCat
);
331 nMaxCategoryCount
= std::max( nCurrentCount
, nMaxCategoryCount
);
333 for (auto & complexCat
: rComplexCats
)
335 if ( !complexCat
.empty() )
337 sal_Int32 nCurrentCount
= lcl_getCategoryCount(complexCat
);
338 if( nCurrentCount
< nMaxCategoryCount
)
340 ComplexCategory
& rComplexCategory
= complexCat
.back();
341 rComplexCategory
.Count
+= (nMaxCategoryCount
-nCurrentCount
);
347 //create a list with an element for every index
348 std::vector
< std::vector
< ComplexCategory
> > aComplexCatsPerIndex
;
349 for (auto const& complexCat
: rComplexCats
)
351 std::vector
< ComplexCategory
> aSingleLevel
;
352 for (auto const& elem
: complexCat
)
354 sal_Int32 nCount
= elem
.Count
;
356 aSingleLevel
.push_back(elem
);
358 aComplexCatsPerIndex
.push_back( aSingleLevel
);
361 if(nMaxCategoryCount
)
363 aRet
.realloc(nMaxCategoryCount
);
364 auto pRet
= aRet
.getArray();
365 for(sal_Int32 nN
=0; nN
<nMaxCategoryCount
; nN
++)
367 OUStringBuffer aText
;
368 for (auto const& complexCatPerIndex
: aComplexCatsPerIndex
)
370 if ( o3tl::make_unsigned(nN
) < complexCatPerIndex
.size() )
372 OUString aAddText
= complexCatPerIndex
[nN
].Text
;
373 if( !aAddText
.isEmpty() )
377 aText
.append(aAddText
);
381 pRet
[nN
]=aText
.makeStringAndClear();
387 Sequence
< OUString
> ExplicitCategoriesProvider::getExplicitSimpleCategories(
388 const SplitCategoriesProvider
& rSplitCategoriesProvider
)
390 vector
< vector
< ComplexCategory
> > aComplexCats
;
391 return lcl_getExplicitSimpleCategories( rSplitCategoriesProvider
, aComplexCats
);
394 static bool lcl_fillDateCategories( const uno::Reference
< data::XDataSequence
>& xDataSequence
, std::vector
< double >& rDateCategories
, bool bIsAutoDate
, ChartModel
& rModel
)
396 bool bOnlyDatesFound
= true;
397 bool bAnyDataFound
= false;
399 if( xDataSequence
.is() )
401 uno::Sequence
< uno::Any
> aValues
= xDataSequence
->getData();
402 sal_Int32 nCount
= aValues
.getLength();
403 rDateCategories
.reserve(nCount
);
404 Reference
< util::XNumberFormats
> xNumberFormats( rModel
.getNumberFormats() );
406 bool bOwnData
= false;
407 bool bOwnDataAnddAxisHasAnyFormat
= false;
408 bool bOwnDataAnddAxisHasDateFormat
= false;
409 rtl::Reference
< BaseCoordinateSystem
> xCooSysModel( ChartModelHelper::getFirstCoordinateSystem( &rModel
) );
410 if( xCooSysModel
.is() )
412 if( rModel
.hasInternalDataProvider() )
415 rtl::Reference
< Axis
> xAxisProps
= xCooSysModel
->getAxisByDimension2(0,0);
416 sal_Int32 nAxisNumberFormat
= 0;
417 if (xAxisProps
.is() && (xAxisProps
->getPropertyValue(CHART_UNONAME_NUMFMT
) >>= nAxisNumberFormat
))
419 bOwnDataAnddAxisHasAnyFormat
= true;
420 bOwnDataAnddAxisHasDateFormat
= DiagramHelper::isDateNumberFormat( nAxisNumberFormat
, xNumberFormats
);
425 for(sal_Int32 nN
=0;nN
<nCount
;nN
++)
427 bool bIsDate
= false;
431 bIsDate
= !bOwnDataAnddAxisHasAnyFormat
|| bOwnDataAnddAxisHasDateFormat
;
433 bIsDate
= DiagramHelper::isDateNumberFormat( xDataSequence
->getNumberFormatKeyByIndex( nN
), xNumberFormats
);
438 bool bContainsEmptyString
= false;
439 uno::Any aAny
= aValues
[nN
];
440 if( aAny
.hasValue() )
444 bool bContainsNan
= false;
445 if( (aAny
>>=aTest
) && aTest
.isEmpty() ) //empty String
446 bContainsEmptyString
= true;
447 else if( (aAny
>>=fTest
) && std::isnan(fTest
) )
450 if( !bContainsEmptyString
&& !bContainsNan
)
451 bAnyDataFound
= true;
454 if( bIsDate
&& (aAny
>>= aDate
) )
455 rDateCategories
.push_back( aDate
);
458 if( aAny
.hasValue() && !bContainsEmptyString
)//empty string does not count as non date value!
459 bOnlyDatesFound
=false;
460 rDateCategories
.push_back( std::numeric_limits
<double>::quiet_NaN() );
463 std::sort( rDateCategories
.begin(), rDateCategories
.end() );
466 return bAnyDataFound
&& bOnlyDatesFound
;
469 void ExplicitCategoriesProvider::init()
474 m_aComplexCats
.clear();//not one per index
475 m_aDateCategories
.clear();
477 if( m_xOriginalCategories
.is() )
479 if( !hasComplexCategories() )
483 if( ChartTypeHelper::isSupportingDateAxis( AxisHelper::getChartTypeByIndex( m_xCooSysModel
.get(), 0 ), 0 ) )
484 m_bIsDateAxis
= lcl_fillDateCategories( m_xOriginalCategories
->getValues(), m_aDateCategories
, m_bIsAutoDate
, mrModel
);
486 m_bIsDateAxis
= false;
491 m_bIsDateAxis
= false;
499 Sequence
< OUString
> const & ExplicitCategoriesProvider::getSimpleCategories()
501 if( !m_bIsExplicitCategoriesInited
)
504 m_aExplicitCategories
.realloc(0);
505 if( m_xOriginalCategories
.is() )
507 if( !hasComplexCategories() )
509 uno::Reference
< data::XDataSequence
> xDataSequence( m_xOriginalCategories
->getValues() );
510 if( xDataSequence
.is() )
511 ExplicitCategoriesProvider::convertCategoryAnysToText( m_aExplicitCategories
, xDataSequence
->getData(), mrModel
);
515 m_aExplicitCategories
= lcl_getExplicitSimpleCategories(
516 SplitCategoriesProvider_ForLabeledDataSequences( m_aSplitCategoriesList
, mrModel
), m_aComplexCats
);
519 if(!m_aExplicitCategories
.hasElements())
520 m_aExplicitCategories
= DiagramHelper::generateAutomaticCategoriesFromCooSys( m_xCooSysModel
.get() );
521 m_bIsExplicitCategoriesInited
= true;
523 return m_aExplicitCategories
;
526 const std::vector
<ComplexCategory
>* ExplicitCategoriesProvider::getCategoriesByLevel( sal_Int32 nLevel
)
529 sal_Int32 nMaxIndex
= m_aComplexCats
.size()-1;
530 if (nLevel
>= 0 && nLevel
<= nMaxIndex
)
531 return &m_aComplexCats
[nMaxIndex
-nLevel
];
535 OUString
ExplicitCategoriesProvider::getCategoryByIndex(
536 const rtl::Reference
< BaseCoordinateSystem
>& xCooSysModel
540 if( xCooSysModel
.is())
542 ExplicitCategoriesProvider
aExplicitCategoriesProvider( xCooSysModel
, rModel
);
543 Sequence
< OUString
> aCategories( aExplicitCategoriesProvider
.getSimpleCategories());
544 if( nIndex
< aCategories
.getLength())
545 return aCategories
[ nIndex
];
550 bool ExplicitCategoriesProvider::isDateAxis()
553 return m_bIsDateAxis
;
556 const std::vector
< double >& ExplicitCategoriesProvider::getDateCategories()
559 return m_aDateCategories
;
564 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */