Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / tools / ExplicitCategoriesProvider.cxx
blobb6652e25f11d6a94f84d99a3a3e7a9a3fa43d8e4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
33 namespace chart
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;
40 using std::vector;
42 ExplicitCategoriesProvider::ExplicitCategoriesProvider( const Reference< chart2::XCoordinateSystem >& xCooSysModel
43 , ChartModel& rModel )
44 : m_bDirty(true)
45 , m_xCooSysModel( xCooSysModel )
46 , mrModel(rModel)
47 , m_xOriginalCategories()
48 , m_bIsExplicitCategoriesInited(false)
49 , m_bIsDateAxis(false)
50 , m_bIsAutoDate(false)
52 try
54 if( xCooSysModel.is() )
56 uno::Reference< XAxis > xAxis( xCooSysModel->getAxisByDimension(0,0) );
57 if( xAxis.is() )
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;
103 bool bDummy;
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;
110 else
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();
136 return nullptr;
139 bool ExplicitCategoriesProvider::hasComplexCategories() const
141 return m_aSplitCategoriesList.getLength() > 1;
144 sal_Int32 ExplicitCategoriesProvider::getCategoryLevelCount() const
146 sal_Int32 nCount = m_aSplitCategoriesList.getLength();
147 if(!nCount)
148 nCount = 1;
149 return nCount;
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();
167 if(!nCount)
168 return;
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 );
180 Color nLabelColor;
181 bool bColorChanged = false;
183 NumberFormatterWrapper aNumberFormatterWrapper( rModel.getNumberFormatsSupplier() );
185 for(sal_Int32 nN=0;nN<nCount;nN++)
187 OUString aText;
188 uno::Any aAny = rInAnys[nN];
189 if( aAny.hasValue() )
191 double fDouble = 0;
192 if( aAny>>=fDouble )
194 if( !::rtl::math::isNan(fDouble) )
195 aText = aNumberFormatterWrapper.getFormattedString(
196 nAxisNumberFormat, fDouble, nLabelColor, bColorChanged );
198 else
200 aAny>>=aText;
203 rOutTexts[nN] = aText;
207 SplitCategoriesProvider::~SplitCategoriesProvider()
211 class SplitCategoriesProvider_ForLabeledDataSequences : public SplitCategoriesProvider
213 public:
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 )
220 , mrModel( rModel )
223 virtual sal_Int32 getLevelCount() const override;
224 virtual uno::Sequence< OUString > getStringsForLevel( sal_Int32 nIndex ) const override;
226 private:
227 const css::uno::Sequence< css::uno::Reference<
228 css::chart2::data::XLabeledDataSequence> >& m_rSplitCategoriesList;
230 ChartModel& mrModel;
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 );
247 return aRet;
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();
257 OUString aPrevious;
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 );
265 nCurrentCount=1;
266 aPrevious = aCurrent;
268 else
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())
275 ++nCurrentCount;
276 else
278 aResult.emplace_back(aPrevious,nCurrentCount );
279 nCurrentCount=1;
280 aPrevious = aCurrent;
284 if( nCurrentCount )
285 aResult.emplace_back(aPrevious,nCurrentCount );
287 return aResult;
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;
295 return nCount;
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;
309 if(nL>0)
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;
345 while( nCount-- )
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() )
364 if(!aText.isEmpty())
365 aText.append(" ");
366 aText.append(aAddText);
370 aRet[nN]=aText.makeStringAndClear();
373 return aRet;
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() )
403 bOwnData = true;
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;
417 if( bIsAutoDate )
419 if( bOwnData )
420 bIsDate = !bOwnDataAnddAxisHasAnyFormat || bOwnDataAnddAxisHasDateFormat;
421 else
422 bIsDate = DiagramHelper::isDateNumberFormat( xDataSequence->getNumberFormatKeyByIndex( nN ), xNumberFormats );
424 else
425 bIsDate = true;
427 bool bContainsEmptyString = false;
428 uno::Any aAny = aValues[nN];
429 if( aAny.hasValue() )
431 OUString aTest;
432 double fTest = 0;
433 bool bContainsNan = false;
434 if( (aAny>>=aTest) && aTest.isEmpty() ) //empty String
435 bContainsEmptyString = true;
436 else if( (aAny>>=fTest) && ::rtl::math::isNan(fTest) )
437 bContainsNan = true;
439 if( !bContainsEmptyString && !bContainsNan )
440 bAnyDataFound = true;
442 double aDate( 1.0 );
443 if( bIsDate && (aAny >>= aDate) )
444 rDateCategories.push_back( aDate );
445 else
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()
461 if( m_bDirty )
463 m_aComplexCats.clear();//not one per index
464 m_aDateCategories.clear();
466 if( m_xOriginalCategories.is() )
468 if( !hasComplexCategories() )
470 if(m_bIsDateAxis)
472 if( ChartTypeHelper::isSupportingDateAxis( AxisHelper::getChartTypeByIndex( m_xCooSysModel, 0 ), 0 ) )
473 m_bIsDateAxis = lcl_fillDateCategories( m_xOriginalCategories->getValues(), m_aDateCategories, m_bIsAutoDate, mrModel );
474 else
475 m_bIsDateAxis = false;
478 else
480 m_bIsDateAxis = false;
483 else
484 m_bIsDateAxis=false;
485 m_bDirty = false;
489 Sequence< OUString > const & ExplicitCategoriesProvider::getSimpleCategories()
491 if( !m_bIsExplicitCategoriesInited )
493 init();
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 );
503 else
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 )
518 init();
519 sal_Int32 nMaxIndex = m_aComplexCats.size()-1;
520 if (nLevel >= 0 && nLevel <= nMaxIndex)
521 return &m_aComplexCats[nMaxIndex-nLevel];
522 return nullptr;
525 OUString ExplicitCategoriesProvider::getCategoryByIndex(
526 const Reference< XCoordinateSystem >& xCooSysModel
527 , ChartModel& rModel
528 , sal_Int32 nIndex )
530 if( xCooSysModel.is())
532 ExplicitCategoriesProvider aExplicitCategoriesProvider( xCooSysModel, rModel );
533 Sequence< OUString > aCategories( aExplicitCategoriesProvider.getSimpleCategories());
534 if( nIndex < aCategories.getLength())
535 return aCategories[ nIndex ];
537 return OUString();
540 bool ExplicitCategoriesProvider::isDateAxis()
542 init();
543 return m_bIsDateAxis;
546 const std::vector< double >& ExplicitCategoriesProvider::getDateCategories()
548 init();
549 return m_aDateCategories;
552 } //namespace chart
554 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */