Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / chart2 / source / model / template / BubbleDataInterpreter.cxx
blob8157fdcfb0fe81c7cdfe17bc9751e15776de4ccf
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 <sal/config.h>
22 #include <cstddef>
24 #include "BubbleDataInterpreter.hxx"
25 #include <DataSeries.hxx>
26 #include <DataSeriesHelper.hxx>
27 #include <CommonConverters.hxx>
28 #include <com/sun/star/util/XCloneable.hpp>
29 #include <comphelper/diagnose_ex.hxx>
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::chart2;
34 using ::com::sun::star::uno::Reference;
35 using ::com::sun::star::uno::Sequence;
37 namespace chart
40 BubbleDataInterpreter::BubbleDataInterpreter()
44 BubbleDataInterpreter::~BubbleDataInterpreter()
48 // ____ XDataInterpreter ____
49 InterpretedData BubbleDataInterpreter::interpretDataSource(
50 const Reference< chart2::data::XDataSource >& xSource,
51 const Sequence< beans::PropertyValue >& aArguments,
52 const std::vector< rtl::Reference< DataSeries > >& aSeriesToReUse )
54 if( ! xSource.is())
55 return InterpretedData();
57 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aData = DataInterpreter::getDataSequences(xSource);
59 uno::Reference< chart2::data::XLabeledDataSequence > xValuesX;
60 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aYValuesVector;
61 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aSizeValuesVector;
63 uno::Reference< chart2::data::XLabeledDataSequence > xCategories;
64 bool bHasCategories = HasCategories( aArguments, aData );
65 bool bUseCategoriesAsX = UseCategoriesAsX( aArguments );
67 sal_Int32 nDataSeqCount = aData.size();
69 bool bSetXValues = bHasCategories ? ( (nDataSeqCount-1) > 2 && (nDataSeqCount-1) % 2 != 0 )
70 :( nDataSeqCount > 2 && nDataSeqCount % 2 != 0 );
72 bool bCategoriesUsed = false;
73 bool bNextIsYValues = bHasCategories ? nDataSeqCount>2 : nDataSeqCount>1;
74 for( sal_Int32 nDataIdx = 0; nDataIdx < nDataSeqCount; ++nDataIdx )
76 try
78 if( bHasCategories && !bCategoriesUsed )
80 xCategories = aData[nDataIdx];
81 if( xCategories.is())
83 SetRole( xCategories->getValues(), u"categories"_ustr);
84 if( bUseCategoriesAsX )
86 bSetXValues = false;
87 bNextIsYValues = nDataSeqCount > 2;
90 bCategoriesUsed = true;
92 else if( !xValuesX.is() && bSetXValues )
94 xValuesX = aData[nDataIdx];
95 if( xValuesX.is())
96 SetRole( xValuesX->getValues(), u"values-x"_ustr);
98 else if( bNextIsYValues )
100 aYValuesVector.push_back( aData[nDataIdx] );
101 if( aData[nDataIdx].is())
102 SetRole( aData[nDataIdx]->getValues(), u"values-y"_ustr);
103 bNextIsYValues = false;
105 else if( !bNextIsYValues )
107 aSizeValuesVector.push_back( aData[nDataIdx] );
108 if( aData[nDataIdx].is())
109 SetRole( aData[nDataIdx]->getValues(), u"values-size"_ustr);
110 bNextIsYValues = (nDataSeqCount-(nDataIdx+1)) >= 2;//two or more left
113 catch( const uno::Exception & )
115 DBG_UNHANDLED_EXCEPTION("chart2");
119 // create DataSeries
120 std::size_t nSeriesIndex = 0;
121 std::vector< rtl::Reference< DataSeries > > aSeriesVec;
122 aSeriesVec.reserve( aSizeValuesVector.size());
124 Reference< data::XLabeledDataSequence > xClonedXValues = xValuesX;
125 Reference< util::XCloneable > xCloneableX( xValuesX, uno::UNO_QUERY );
127 for( size_t nN = 0; nN < aSizeValuesVector.size(); ++nN, ++nSeriesIndex )
129 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aNewData;
130 if( xValuesX.is() )
132 if( nN > 0 && xCloneableX.is() )
133 xClonedXValues.set( xCloneableX->createClone(), uno::UNO_QUERY );
134 aNewData.push_back( xClonedXValues );
136 if( aYValuesVector.size() > nN )
137 aNewData.push_back( aYValuesVector[nN] );
138 aNewData.push_back(aSizeValuesVector[nN]);
140 rtl::Reference< DataSeries > xSeries;
141 if( nSeriesIndex < aSeriesToReUse.size())
142 xSeries = aSeriesToReUse[nSeriesIndex];
143 else
144 xSeries = new DataSeries;
145 assert( xSeries.is() );
146 xSeries->setData( aNewData );
148 aSeriesVec.push_back( xSeries );
151 return { { std::move(aSeriesVec) }, xCategories };
154 InterpretedData BubbleDataInterpreter::reinterpretDataSeries(
155 const InterpretedData& aInterpretedData )
157 InterpretedData aResult( aInterpretedData );
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 )
166 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aNewSequences;
168 uno::Reference< chart2::data::XLabeledDataSequence > xValuesSize(
169 DataSeriesHelper::getDataSequenceByRole( aSeries[i], u"values-size"_ustr ));
170 uno::Reference< chart2::data::XLabeledDataSequence > xValuesY(
171 DataSeriesHelper::getDataSequenceByRole( aSeries[i], u"values-y"_ustr ));
172 uno::Reference< chart2::data::XLabeledDataSequence > xValuesX(
173 DataSeriesHelper::getDataSequenceByRole( aSeries[i], u"values-x"_ustr ));
175 if( ! xValuesX.is() ||
176 ! xValuesY.is() ||
177 ! xValuesSize.is() )
179 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aValueSeqVec(
180 DataSeriesHelper::getAllDataSequencesByRole(
181 aSeries[i]->getDataSequences2(), u"values"_ustr ));
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 ));
189 size_t nIndex = 0;
191 if( ! xValuesSize.is() &&
192 aValueSeqVec.size() > nIndex )
194 xValuesSize = aValueSeqVec[nIndex++];
195 if( xValuesSize.is())
196 SetRole( xValuesSize->getValues(), u"values-size"_ustr);
199 if( ! xValuesY.is() &&
200 aValueSeqVec.size() > nIndex )
202 xValuesY = aValueSeqVec[nIndex++];
203 if( xValuesY.is())
204 SetRole( xValuesY->getValues(), u"values-y"_ustr);
207 if( ! xValuesX.is() &&
208 aValueSeqVec.size() > nIndex )
210 xValuesX = aValueSeqVec[nIndex++];
211 if( xValuesX.is())
212 SetRole( xValuesX->getValues(), u"values-x"_ustr);
215 if( xValuesSize.is())
217 if( xValuesY.is() )
219 if( xValuesX.is() )
221 aNewSequences = { xValuesX, xValuesY, xValuesSize };
223 else
225 aNewSequences = { xValuesY, xValuesSize };
228 else
230 aNewSequences = { xValuesSize };
234 const std::vector< uno::Reference< data::XLabeledDataSequence > > & aSeqs = aSeries[i]->getDataSequences2();
235 if( aSeqs.size() != aNewSequences.size() )
237 #if OSL_DEBUG_LEVEL > 0 && !defined NDEBUG
238 for( auto const & j : aSeqs )
240 assert( (j == xValuesY || j == xValuesX || j == xValuesSize) && "All sequences should be used" );
242 #endif
243 aSeries[i]->setData( aNewSequences );
246 catch( const uno::Exception & )
248 DBG_UNHANDLED_EXCEPTION("chart2");
252 return aResult;
255 bool BubbleDataInterpreter::isDataCompatible(
256 const InterpretedData& aInterpretedData )
258 const std::vector< rtl::Reference< DataSeries > > aSeries( FlattenSequence( aInterpretedData.Series ));
259 for( rtl::Reference< DataSeries > const & dataSeries : aSeries )
263 if( dataSeries->getDataSequences2().size() != 3 )
264 return false;
266 catch( const uno::Exception & )
268 DBG_UNHANDLED_EXCEPTION("chart2");
272 return true;
275 } // namespace chart
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */