LibreOffice Module chart2 (master) 1
DataSourceHelper.cxx
Go to the documentation of this file.
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 <DataSourceHelper.hxx>
21#include <ChartModel.hxx>
22#include <ChartModelHelper.hxx>
23#include <ChartTypeManager.hxx>
24#include <DiagramHelper.hxx>
25#include <Diagram.hxx>
26#include <DataSeries.hxx>
27#include <DataSeriesHelper.hxx>
28#include <DataSource.hxx>
32#include <unonames.hxx>
33
34#include <com/sun/star/chart2/data/XDataSource.hpp>
35#include <com/sun/star/chart2/data/XLabeledDataSequence.hpp>
36
37#include <com/sun/star/chart/ChartDataRowSource.hpp>
38#include <com/sun/star/chart/ErrorBarStyle.hpp>
40
41namespace chart
42{
43using namespace ::com::sun::star;
44using namespace ::com::sun::star::chart2;
45using ::com::sun::star::uno::Reference;
46using ::com::sun::star::uno::Sequence;
47
48namespace
49{
50void lcl_addRanges( std::vector< OUString > & rOutResult,
51 const uno::Reference< data::XLabeledDataSequence > & xLabeledSeq )
52{
53 if( ! xLabeledSeq.is())
54 return;
55 uno::Reference< data::XDataSequence > xSeq( xLabeledSeq->getLabel());
56 if( xSeq.is())
57 rOutResult.push_back( xSeq->getSourceRangeRepresentation());
58 xSeq.set( xLabeledSeq->getValues());
59 if( xSeq.is())
60 rOutResult.push_back( xSeq->getSourceRangeRepresentation());
61}
62
63void lcl_addDataSourceRanges(
64 std::vector< OUString > & rOutResult,
65 const uno::Reference< data::XDataSource > & xDataSource )
66{
67 if( xDataSource.is() )
68 {
69 const auto aDataSequences(xDataSource->getDataSequences());
70 for (const auto& rDataSequence : aDataSequences)
71 lcl_addRanges(rOutResult, rDataSequence);
72 }
73}
74
75void lcl_addErrorBarRanges(
76 std::vector< OUString > & rOutResult,
77 const rtl::Reference< DataSeries > & xDataSeries )
78{
79 if( !xDataSeries.is())
80 return;
81
82 try
83 {
84 uno::Reference< beans::XPropertySet > xErrorBarProp;
85 if( ( xDataSeries->getPropertyValue( CHART_UNONAME_ERRORBAR_Y) >>= xErrorBarProp ) &&
86 xErrorBarProp.is())
87 {
88 sal_Int32 eStyle = css::chart::ErrorBarStyle::NONE;
89 if( ( xErrorBarProp->getPropertyValue( "ErrorBarStyle") >>= eStyle ) &&
90 eStyle == css::chart::ErrorBarStyle::FROM_DATA )
91 {
92 uno::Reference< data::XDataSource > xErrorBarDataSource( xErrorBarProp, uno::UNO_QUERY );
93 if( xErrorBarDataSource.is() )
94 lcl_addDataSourceRanges( rOutResult, xErrorBarDataSource );
95 }
96 }
97
98 if( ( xDataSeries->getPropertyValue(CHART_UNONAME_ERRORBAR_X) >>= xErrorBarProp ) && xErrorBarProp.is())
99 {
100 sal_Int32 eStyle = css::chart::ErrorBarStyle::NONE;
101 if( ( xErrorBarProp->getPropertyValue("ErrorBarStyle") >>= eStyle ) &&
102 eStyle == css::chart::ErrorBarStyle::FROM_DATA )
103 {
104 uno::Reference< data::XDataSource > xErrorBarDataSource( xErrorBarProp, uno::UNO_QUERY );
105 if( xErrorBarDataSource.is() )
106 lcl_addDataSourceRanges( rOutResult, xErrorBarDataSource );
107 }
108 }
109 }
110 catch( const uno::Exception & )
111 {
112 DBG_UNHANDLED_EXCEPTION("chart2");
113 }
114}
115
116} // anonymous namespace
117
119{
120 return new ::chart::CachedDataSequence();
121}
122
124{
125 return new ::chart::CachedDataSequence( rSingleText );
126}
127
131{
132 return new ::chart::LabeledDataSequence( xValues, xLabels );
133}
134
136 const Reference< chart2::data::XDataSequence >& xValues )
137{
138 return new ::chart::LabeledDataSequence( xValues );
139}
140
142{
143 return new ::chart::LabeledDataSequence;
144}
145
147 bool bUseColumns, bool bFirstCellAsLabel, bool bHasCategories )
148{
149 css::chart::ChartDataRowSource eRowSource = css::chart::ChartDataRowSource_ROWS;
150 if( bUseColumns )
151 eRowSource = css::chart::ChartDataRowSource_COLUMNS;
152
153 return
154 {
155 { "DataRowSource", -1, uno::Any( eRowSource), beans::PropertyState_DIRECT_VALUE },
156 { "FirstCellAsLabel", -1, uno::Any( bFirstCellAsLabel ), beans::PropertyState_DIRECT_VALUE },
157 { "HasCategories", -1, uno::Any( bHasCategories ), beans::PropertyState_DIRECT_VALUE }
158 };
159}
160
162 const OUString & rRangeRepresentation,
163 const uno::Sequence< sal_Int32 >& rSequenceMapping,
164 bool bUseColumns, bool bFirstCellAsLabel, bool bHasCategories )
165{
166 uno::Sequence< beans::PropertyValue > aArguments( createArguments( bUseColumns, bFirstCellAsLabel, bHasCategories ));
167 aArguments.realloc( aArguments.getLength() + 1 );
168 aArguments.getArray()[aArguments.getLength() - 1] =
169 beans::PropertyValue( "CellRangeRepresentation"
170 , -1, uno::Any( rRangeRepresentation )
171 , beans::PropertyState_DIRECT_VALUE );
172 if( rSequenceMapping.hasElements() )
173 {
174 aArguments.realloc( aArguments.getLength() + 1 );
175 aArguments.getArray()[aArguments.getLength() - 1] =
176 beans::PropertyValue( "SequenceMapping"
177 , -1, uno::Any( rSequenceMapping )
178 , beans::PropertyState_DIRECT_VALUE );
179 }
180 return aArguments;
181}
182
184 , OUString & rRangeRepresentation, uno::Sequence< sal_Int32 >& rSequenceMapping
185 , bool& bUseColumns, bool& bFirstCellAsLabel, bool& bHasCategories )
186{
187 for(const beans::PropertyValue& rProperty : rArguments)
188 {
189 if ( rProperty.Name == "DataRowSource" )
190 {
191 css::chart::ChartDataRowSource eRowSource;
192 if( rProperty.Value >>= eRowSource )
193 bUseColumns = (eRowSource==css::chart::ChartDataRowSource_COLUMNS);
194 }
195 else if ( rProperty.Name == "FirstCellAsLabel" )
196 {
197 rProperty.Value >>= bFirstCellAsLabel;
198 }
199 else if ( rProperty.Name == "HasCategories" )
200 {
201 rProperty.Value >>= bHasCategories;
202 }
203 else if ( rProperty.Name == "CellRangeRepresentation" )
204 {
205 rProperty.Value >>= rRangeRepresentation;
206 }
207 else if ( rProperty.Name == "SequenceMapping" )
208 {
209 rProperty.Value >>= rSequenceMapping;
210 }
211 }
212}
213
215 const rtl::Reference< ChartModel >& xChartDoc )
216{
217 std::vector< Reference< chart2::data::XLabeledDataSequence > > aResultVector;
218
219 //categories are always the first sequence
220 rtl::Reference< Diagram > xDiagram( xChartDoc->getFirstChartDiagram());
221
223 if (xDiagram)
224 xCategories = xDiagram->getCategories();
225 if( xCategories.is() )
226 aResultVector.push_back( xCategories );
227
228 std::vector< rtl::Reference< DataSeries > > aSeriesVector;
229 if (xDiagram)
230 aSeriesVector = xDiagram->getDataSeries();
232 DataSeriesHelper::getDataSource( aSeriesVector );
233 const Sequence< Reference< chart2::data::XLabeledDataSequence > > aDataSequences( xSeriesSource->getDataSequences() );
234
235 //the first x-values is always the next sequence //todo ... other x-values get lost for old format
237 DataSeriesHelper::getDataSequenceByRole( xSeriesSource, "values-x" ) );
238 if( xXValues.is() )
239 aResultVector.push_back( xXValues );
240
241 //add all other sequences now without x-values
242 for( Reference< chart2::data::XLabeledDataSequence > const & labeledData : aDataSequences )
243 {
244 OUString aRole = DataSeriesHelper::getRole(labeledData);
245 if( aRole != "values-x" )
246 aResultVector.push_back( labeledData );
247 }
248
249 return new DataSource( aResultVector );
250}
251
253 const rtl::Reference< Diagram > & xDiagram )
254{
255 std::vector< OUString > aResult;
256
257 if( xDiagram.is())
258 {
259 uno::Reference< data::XLabeledDataSequence > xCategories( xDiagram->getCategories() );
260 if( xCategories.is() )
261 lcl_addRanges( aResult, xCategories );
262
263 std::vector< rtl::Reference< DataSeries > > aSeriesVector( xDiagram->getDataSeries() );
264 for (auto const& series : aSeriesVector)
265 {
266 lcl_addDataSourceRanges( aResult, series );
267 lcl_addErrorBarRanges( aResult, series );
268 }
269 }
270
271 return comphelper::containerToSequence( aResult );
272}
273
275{
276 rtl::Reference< Diagram > xDiagram( xChartModel->getFirstChartDiagram() );
277 return getUsedDataRanges( xDiagram );
278}
279
281 ChartModel& rModel )
282{
283 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aResult;
284
285 rtl::Reference< Diagram > xDiagram = rModel.getFirstChartDiagram();
286 uno::Reference< chart2::data::XLabeledDataSequence > xCategories( xDiagram->getCategories() );
287 if( xCategories.is() )
288 aResult.push_back( xCategories );
289
290 std::vector< rtl::Reference< DataSeries > > aSeriesVector = ChartModelHelper::getDataSeries( &rModel );
291 for (auto const& series : aSeriesVector)
292 {
293 const std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > & aDataSequences( series->getDataSequences2() );
294 aResult.insert( aResult.end(), aDataSequences.begin(), aDataSequences.end() );
295 }
296
297 return new DataSource( aResult );
298}
299
301 const rtl::Reference<::chart::ChartModel>& xChartModel
302 , OUString& rOutRangeString
303 , css::uno::Sequence< sal_Int32 >& rSequenceMapping
304 , bool& rOutUseColumns
305 , bool& rOutFirstCellAsLabel
306 , bool& rOutHasCategories )
307{
308 bool bSomethingDetected = false;
309
310 if( !xChartModel.is() )
311 return bSomethingDetected;
312 uno::Reference< data::XDataProvider > xDataProvider( xChartModel->getDataProvider() );
313 if( !xDataProvider.is() )
314 return bSomethingDetected;
315
316 try
317 {
319 xDataProvider->detectArguments( pressUsedDataIntoRectangularFormat( xChartModel ) ),
320 rOutRangeString, rSequenceMapping, rOutUseColumns, rOutFirstCellAsLabel, rOutHasCategories );
321 bSomethingDetected = !rOutRangeString.isEmpty();
322
323 rtl::Reference<Diagram> xDiagram = xChartModel->getFirstChartDiagram();
325 if (xDiagram)
326 xCategories = xDiagram->getCategories();
327 rOutHasCategories = xCategories.is();
328 }
329 catch( uno::Exception & )
330 {
331 DBG_UNHANDLED_EXCEPTION("chart2");
332 }
333 return bSomethingDetected;
334}
335
337 const rtl::Reference<::chart::ChartModel>& xChartDocument )
338{
339 bool bHasDataRowSource = false;
340 bool bHasFirstCellAsLabel = false;
341 bool bHasCellRangeRepresentation = false;
342
343 uno::Reference< data::XDataProvider > xDataProvider( xChartDocument->getDataProvider() );
344 if( !xDataProvider.is() )
345 return false;
346
347 try
348 {
350 xDataProvider->detectArguments( pressUsedDataIntoRectangularFormat( xChartDocument )));
351 for(const beans::PropertyValue& rProperty : aArguments)
352 {
353 if ( rProperty.Name == "DataRowSource" )
354 {
355 bHasDataRowSource =
356 (rProperty.Value.hasValue() && rProperty.Value.isExtractableTo(
358 }
359 else if ( rProperty.Name == "FirstCellAsLabel" )
360 {
361 bHasFirstCellAsLabel =
362 (rProperty.Value.hasValue() && rProperty.Value.isExtractableTo(cppu::UnoType<bool>::get()));
363 }
364 else if ( rProperty.Name == "CellRangeRepresentation" )
365 {
366 OUString aRange;
367 bHasCellRangeRepresentation =
368 (rProperty.Value.hasValue() && (rProperty.Value >>= aRange) && !aRange.isEmpty());
369 }
370 }
371 }
372 catch( const uno::Exception & )
373 {
374 DBG_UNHANDLED_EXCEPTION("chart2");
375 }
376
377 return (bHasCellRangeRepresentation && bHasDataRowSource && bHasFirstCellAsLabel);
378}
379
381 const rtl::Reference<::chart::ChartModel>& xChartModel
382 , const css::uno::Sequence< sal_Int32 >& rSequenceMapping
383 , bool bUseColumns , bool bFirstCellAsLabel, bool bUseCategories )
384{
385 uno::Reference< data::XDataProvider > xDataProvider( xChartModel->getDataProvider() );
386 if( !xDataProvider.is() )
387 return;
388 rtl::Reference< Diagram > xDiagram( xChartModel->getFirstChartDiagram() );
389 if( !xDiagram.is() )
390 return;
391 rtl::Reference< ::chart::ChartTypeManager > xChartTypeManager = xChartModel->getTypeManager();
392 if( !xChartTypeManager.is() )
393 return;
394
395 OUString aRangeString;
396 bool bDummy;
398 readArguments( xDataProvider->detectArguments( pressUsedDataIntoRectangularFormat( xChartModel )),
399 aRangeString, aDummy, bDummy, bDummy, bDummy );
400
402 createArguments( aRangeString, rSequenceMapping, bUseColumns, bFirstCellAsLabel, bUseCategories ) );
403
404 uno::Reference< chart2::data::XDataSource > xDataSource( xDataProvider->createDataSource(
405 aArguments ) );
406 if( !xDataSource.is() )
407 return;
408
409 ControllerLockGuardUNO aCtrlLockGuard( xChartModel );
410 xDiagram->setDiagramData( xDataSource, aArguments );
411}
412
415{
416 Sequence< OUString > aResult;
417 if( xLSeq.is())
418 {
419 Reference< data::XDataSequence > xLabel( xLSeq->getLabel());
420 Reference< data::XDataSequence > xValues( xLSeq->getValues());
421
422 if( xLabel.is())
423 {
424 if( xValues.is())
425 {
426 aResult = { xLabel->getSourceRangeRepresentation(),
427 xValues->getSourceRangeRepresentation() };
428 }
429 else
430 {
431 aResult = { xLabel->getSourceRangeRepresentation() };
432 }
433 }
434 else if( xValues.is())
435 {
436 aResult = { xValues->getSourceRangeRepresentation() };
437 }
438 }
439 return aResult;
440}
441
444{
445 OUString aResult;
446 if( xLSeq.is() )
447 {
448 Reference< data::XDataSequence > xValues( xLSeq->getValues() );
449 if( xValues.is() )
450 aResult = xValues->getSourceRangeRepresentation();
451 }
452 return aResult;
453}
454
456{
457 std::vector< OUString > aResult;
458 if( xSource.is())
459 {
460 const Sequence< Reference< data::XLabeledDataSequence > > aLSeqSeq( xSource->getDataSequences());
461 for( Reference< data::XLabeledDataSequence > const & labeledData : aLSeqSeq )
462 {
463 Reference< data::XDataSequence > xLabel( labeledData->getLabel());
464 Reference< data::XDataSequence > xValues( labeledData->getValues());
465
466 if( xLabel.is())
467 aResult.push_back( xLabel->getSourceRangeRepresentation());
468 if( xValues.is())
469 aResult.push_back( xValues->getSourceRangeRepresentation());
470 }
471 }
472 return comphelper::containerToSequence( aResult );
473}
474
475} //namespace chart
476
477/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static std::vector< rtl::Reference< ::chart::DataSeries > > getDataSeries(const rtl::Reference< ::chart::ChartModel > &xModel)
This guard calls lockControllers at the given Model in the CTOR and unlockControllers in the DTOR.
static SAL_DLLPRIVATE css::uno::Sequence< OUString > getUsedDataRanges(const rtl::Reference< ::chart::Diagram > &xDiagram)
static bool detectRangeSegmentation(const rtl::Reference<::chart::ChartModel > &xChartModel, OUString &rOutRangeString, css::uno::Sequence< sal_Int32 > &rSequenceMapping, bool &rOutUseColumns, bool &rOutFirstCellAsLabel, bool &rOutHasCategories)
static SAL_DLLPRIVATE css::uno::Sequence< OUString > getRangesFromLabeledDataSequence(const css::uno::Reference< css::chart2::data::XLabeledDataSequence > &xLSeq)
static void setRangeSegmentation(const rtl::Reference<::chart::ChartModel > &xChartModel, const css::uno::Sequence< sal_Int32 > &rSequenceMapping, bool bUseColumns, bool bFirstCellAsLabel, bool bUseCategories)
static SAL_DLLPRIVATE css::uno::Sequence< OUString > getRangesFromDataSource(const css::uno::Reference< css::chart2::data::XDataSource > &xSource)
static rtl::Reference< ::chart::DataSource > getUsedData(ChartModel &rModel)
static css::uno::Reference< css::chart2::data::XDataSequence > createCachedDataSequence()
static css::uno::Sequence< css::beans::PropertyValue > createArguments(bool bUseColumns, bool bFirstCellAsLabel, bool bHasCategories)
static bool allArgumentsForRectRangeDetected(const rtl::Reference<::chart::ChartModel > &xChartDocument)
Returns true, if all arguments necessary for getting all data by a rectangular region are returned by...
static SAL_DLLPRIVATE OUString getRangeFromValues(const css::uno::Reference< css::chart2::data::XLabeledDataSequence > &xLSeq)
static rtl::Reference< LabeledDataSequence > createLabeledDataSequence()
static rtl::Reference< ::chart::DataSource > pressUsedDataIntoRectangularFormat(const rtl::Reference< ::chart::ChartModel > &xChartDoc)
static SAL_DLLPRIVATE void readArguments(const css::uno::Sequence< css::beans::PropertyValue > &rArguments, OUString &rRangeRepresentation, css::uno::Sequence< sal_Int32 > &rSequenceMapping, bool &bUseColumns, bool &bFirstCellAsLabel, bool &bHasCategories)
#define DBG_UNHANDLED_EXCEPTION(...)
Sequence< PropertyValue > aArguments
OOO_DLLPUBLIC_CHARTTOOLS rtl::Reference< ::chart::DataSource > getDataSource(const std::vector< rtl::Reference< ::chart::DataSeries > > &aSeries)
Retrieves all data sequences found in the given data series and puts them into a data source.
OOO_DLLPUBLIC_CHARTTOOLS css::uno::Reference< css::chart2::data::XLabeledDataSequence > getDataSequenceByRole(const css::uno::Reference< css::chart2::data::XDataSource > &xSource, const OUString &aRole, bool bMatchPrefix=false)
Retrieves the data sequence in the given data source that matches the given role.
OOO_DLLPUBLIC_CHARTTOOLS OUString getRole(const css::uno::Reference< css::chart2::data::XLabeledDataSequence > &xLabeledDataSequence)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
constexpr OUStringLiteral CHART_UNONAME_ERRORBAR_Y
Definition: unonames.hxx:23
constexpr OUStringLiteral CHART_UNONAME_ERRORBAR_X
Definition: unonames.hxx:22