LibreOffice Module chart2 (master) 1
BubbleDataInterpreter.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 <sal/config.h>
21
22#include <cstddef>
23
25#include <DataSeries.hxx>
26#include <DataSeriesHelper.hxx>
27#include <CommonConverters.hxx>
28#include <com/sun/star/util/XCloneable.hpp>
30
31using namespace ::com::sun::star;
32using namespace ::com::sun::star::chart2;
33
34using ::com::sun::star::uno::Reference;
35using ::com::sun::star::uno::Sequence;
36
37namespace chart
38{
39
41{
42}
43
45{
46}
47
48// ____ XDataInterpreter ____
51 const Sequence< beans::PropertyValue >& aArguments,
52 const std::vector< rtl::Reference< DataSeries > >& aSeriesToReUse )
53{
54 if( ! xSource.is())
55 return InterpretedData();
56
57 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aData = DataInterpreter::getDataSequences(xSource);
58
60 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aYValuesVector;
61 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aSizeValuesVector;
62
64 bool bHasCategories = HasCategories( aArguments, aData );
65 bool bUseCategoriesAsX = UseCategoriesAsX( aArguments );
66
67 sal_Int32 nDataSeqCount = aData.size();
68
69 bool bSetXValues = bHasCategories ? ( (nDataSeqCount-1) > 2 && (nDataSeqCount-1) % 2 != 0 )
70 :( nDataSeqCount > 2 && nDataSeqCount % 2 != 0 );
71
72 bool bCategoriesUsed = false;
73 bool bNextIsYValues = bHasCategories ? nDataSeqCount>2 : nDataSeqCount>1;
74 for( sal_Int32 nDataIdx = 0; nDataIdx < nDataSeqCount; ++nDataIdx )
75 {
76 try
77 {
78 if( bHasCategories && !bCategoriesUsed )
79 {
80 xCategories = aData[nDataIdx];
81 if( xCategories.is())
82 {
83 SetRole( xCategories->getValues(), "categories");
84 if( bUseCategoriesAsX )
85 {
86 bSetXValues = false;
87 bNextIsYValues = nDataSeqCount > 2;
88 }
89 }
90 bCategoriesUsed = true;
91 }
92 else if( !xValuesX.is() && bSetXValues )
93 {
94 xValuesX = aData[nDataIdx];
95 if( xValuesX.is())
96 SetRole( xValuesX->getValues(), "values-x");
97 }
98 else if( bNextIsYValues )
99 {
100 aYValuesVector.push_back( aData[nDataIdx] );
101 if( aData[nDataIdx].is())
102 SetRole( aData[nDataIdx]->getValues(), "values-y");
103 bNextIsYValues = false;
104 }
105 else if( !bNextIsYValues )
106 {
107 aSizeValuesVector.push_back( aData[nDataIdx] );
108 if( aData[nDataIdx].is())
109 SetRole( aData[nDataIdx]->getValues(), "values-size");
110 bNextIsYValues = (nDataSeqCount-(nDataIdx+1)) >= 2;//two or more left
111 }
112 }
113 catch( const uno::Exception & )
114 {
115 DBG_UNHANDLED_EXCEPTION("chart2");
116 }
117 }
118
119 // create DataSeries
120 std::size_t nSeriesIndex = 0;
121 std::vector< rtl::Reference< DataSeries > > aSeriesVec;
122 aSeriesVec.reserve( aSizeValuesVector.size());
123
124 Reference< data::XLabeledDataSequence > xClonedXValues = xValuesX;
125 Reference< util::XCloneable > xCloneableX( xValuesX, uno::UNO_QUERY );
126
127 for( size_t nN = 0; nN < aSizeValuesVector.size(); ++nN, ++nSeriesIndex )
128 {
129 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aNewData;
130 if( xValuesX.is() )
131 {
132 if( nN > 0 && xCloneableX.is() )
133 xClonedXValues.set( xCloneableX->createClone(), uno::UNO_QUERY );
134 aNewData.push_back( xClonedXValues );
135 }
136 if( aYValuesVector.size() > nN )
137 aNewData.push_back( aYValuesVector[nN] );
138 aNewData.push_back(aSizeValuesVector[nN]);
139
141 if( nSeriesIndex < aSeriesToReUse.size())
142 xSeries = aSeriesToReUse[nSeriesIndex];
143 else
144 xSeries = new DataSeries;
145 assert( xSeries.is() );
146 xSeries->setData( aNewData );
147
148 aSeriesVec.push_back( xSeries );
149 }
150
151 return { { aSeriesVec }, xCategories };
152}
153
155 const InterpretedData& aInterpretedData )
156{
157 InterpretedData aResult( aInterpretedData );
158
159 sal_Int32 i=0;
160 std::vector< rtl::Reference< DataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
161 const sal_Int32 nCount = aSeries.size();
162 for( ; i<nCount; ++i )
163 {
164 try
165 {
166 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aNewSequences;
167
169 DataSeriesHelper::getDataSequenceByRole( aSeries[i], "values-size" ));
171 DataSeriesHelper::getDataSequenceByRole( aSeries[i], "values-y" ));
173 DataSeriesHelper::getDataSequenceByRole( aSeries[i], "values-x" ));
174
175 if( ! xValuesX.is() ||
176 ! xValuesY.is() ||
177 ! xValuesSize.is() )
178 {
179 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aValueSeqVec(
181 aSeries[i]->getDataSequences2(), "values" ));
182 if( xValuesX.is())
183 aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesX ));
184 if( xValuesY.is())
185 aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesY ));
186 if( xValuesSize.is())
187 aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesSize ));
188
189 size_t nIndex = 0;
190
191 if( ! xValuesSize.is() &&
192 aValueSeqVec.size() > nIndex )
193 {
194 xValuesSize = aValueSeqVec[nIndex++];
195 if( xValuesSize.is())
196 SetRole( xValuesSize->getValues(), "values-size");
197 }
198
199 if( ! xValuesY.is() &&
200 aValueSeqVec.size() > nIndex )
201 {
202 xValuesY = aValueSeqVec[nIndex++];
203 if( xValuesY.is())
204 SetRole( xValuesY->getValues(), "values-y");
205 }
206
207 if( ! xValuesX.is() &&
208 aValueSeqVec.size() > nIndex )
209 {
210 xValuesX = aValueSeqVec[nIndex++];
211 if( xValuesX.is())
212 SetRole( xValuesY->getValues(), "values-x");
213 }
214 }
215 if( xValuesSize.is())
216 {
217 if( xValuesY.is() )
218 {
219 if( xValuesX.is() )
220 {
221 aNewSequences = { xValuesX, xValuesY, xValuesSize };
222 }
223 else
224 {
225 aNewSequences = { xValuesY, xValuesSize };
226 }
227 }
228 else
229 {
230 aNewSequences = { xValuesSize };
231 }
232 }
233
234 const std::vector< uno::Reference< data::XLabeledDataSequence > > & aSeqs = aSeries[i]->getDataSequences2();
235 if( aSeqs.size() != aNewSequences.size() )
236 {
237#if OSL_DEBUG_LEVEL > 0 && !defined NDEBUG
238 for( auto const & j : aSeqs )
239 {
240 assert( (j == xValuesY || j == xValuesX || j == xValuesSize) && "All sequences should be used" );
241 }
242#endif
243 aSeries[i]->setData( aNewSequences );
244 }
245 }
246 catch( const uno::Exception & )
247 {
248 DBG_UNHANDLED_EXCEPTION("chart2");
249 }
250 }
251
252 return aResult;
253}
254
256 const InterpretedData& aInterpretedData )
257{
258 const std::vector< rtl::Reference< DataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
259 for( rtl::Reference< DataSeries > const & dataSeries : aSeries )
260 {
261 try
262 {
263 if( dataSeries->getDataSequences2().size() != 3 )
264 return false;
265 }
266 catch( const uno::Exception & )
267 {
268 DBG_UNHANDLED_EXCEPTION("chart2");
269 }
270 }
271
272 return true;
273}
274
275} // namespace chart
276
277/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual bool isDataCompatible(const InterpretedData &aInterpretedData) override
parses the given data and states, if a reinterpretDataSeries() call can be done without data loss.
virtual InterpretedData reinterpretDataSeries(const InterpretedData &aInterpretedData) override
Re-interprets the data given in aInterpretedData while keeping the number of data series and the cate...
virtual InterpretedData interpretDataSource(const css::uno::Reference< css::chart2::data::XDataSource > &xSource, const css::uno::Sequence< css::beans::PropertyValue > &aArguments, const std::vector< rtl::Reference< ::chart::DataSeries > > &aSeriesToReUse) override
Interprets the given data.
static bool HasCategories(const css::uno::Sequence< css::beans::PropertyValue > &rArguments, const std::vector< css::uno::Reference< css::chart2::data::XLabeledDataSequence > > &rData)
static std::vector< css::uno::Reference< css::chart2::data::XLabeledDataSequence > > getDataSequences(const css::uno::Reference< css::chart2::data::XDataSource > &xSource)
static void SetRole(const css::uno::Reference< css::chart2::data::XDataSequence > &xSeq, const OUString &rRole)
static bool UseCategoriesAsX(const css::uno::Sequence< css::beans::PropertyValue > &rArguments)
int nCount
#define DBG_UNHANDLED_EXCEPTION(...)
Sequence< PropertyValue > aArguments
sal_Int32 nIndex
constexpr OUStringLiteral aData
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 std::vector< css::uno::Reference< css::chart2::data::XLabeledDataSequence > > getAllDataSequencesByRole(const css::uno::Sequence< css::uno::Reference< css::chart2::data::XLabeledDataSequence > > &aDataSequences, const OUString &aRole)
Retrieves all data sequences in the given data source that match the given role prefix.
std::vector< T > FlattenSequence(const std::vector< std::vector< T > > &aSeqSeq)
std::vector< std::vector< T > > -> std::vector< T >
OSQLColumns::const_iterator find(const OSQLColumns::const_iterator &first, const OSQLColumns::const_iterator &last, std::u16string_view _rVal, const ::comphelper::UStringMixEqual &_rCase)
int i
offers tooling to interpret different data sources in a structural and chart-type-dependent way.
std::vector< std::vector< rtl::Reference<::chart::DataSeries > > > Series