Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / chart2 / source / model / template / XYDataInterpreter.cxx
blobd6daa2a5b1d9eb6055965cc44639c731f5cf2a83
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 "XYDataInterpreter.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>
30 #include <sal/log.hxx>
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::chart2;
35 using ::com::sun::star::uno::Reference;
36 using ::com::sun::star::uno::Sequence;
38 namespace chart
41 XYDataInterpreter::XYDataInterpreter()
45 XYDataInterpreter::~XYDataInterpreter()
49 // ____ XDataInterpreter ____
50 InterpretedData XYDataInterpreter::interpretDataSource(
51 const Reference< chart2::data::XDataSource >& xSource,
52 const Sequence< beans::PropertyValue >& aArguments,
53 const std::vector< rtl::Reference< DataSeries > >& aSeriesToReUse )
55 if( ! xSource.is())
56 return InterpretedData();
58 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aData = DataInterpreter::getDataSequences(xSource);
60 uno::Reference< chart2::data::XLabeledDataSequence > xValuesX;
61 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aSequencesVec;
63 uno::Reference< chart2::data::XLabeledDataSequence > xCategories;
64 bool bHasCategories = HasCategories( aArguments, aData );
65 bool bUseCategoriesAsX = UseCategoriesAsX( aArguments );
67 // parse data
68 bool bCategoriesUsed = false;
69 bool bSetXValues = aData.size()>1;
70 for( uno::Reference< chart2::data::XLabeledDataSequence > const & labelData : aData )
72 try
74 if( bHasCategories && ! bCategoriesUsed )
76 xCategories = labelData;
77 if( xCategories.is())
79 SetRole( xCategories->getValues(), u"categories"_ustr);
80 if( bUseCategoriesAsX )
81 bSetXValues = false;
83 bCategoriesUsed = true;
85 else if( !xValuesX.is() && bSetXValues )
87 xValuesX = labelData;
88 if( xValuesX.is())
89 SetRole( xValuesX->getValues(), u"values-x"_ustr);
91 else
93 aSequencesVec.push_back( labelData );
94 if( labelData.is())
95 SetRole( labelData->getValues(), u"values-y"_ustr);
98 catch( const uno::Exception & )
100 DBG_UNHANDLED_EXCEPTION("chart2");
104 // create DataSeries
105 std::vector< rtl::Reference< DataSeries > > aSeriesVec;
106 aSeriesVec.reserve( aSequencesVec.size());
108 Reference< data::XLabeledDataSequence > xClonedXValues = xValuesX;
109 Reference< util::XCloneable > xCloneable( xValuesX, uno::UNO_QUERY );
111 std::size_t nSeriesIndex = 0;
112 for (auto const& elem : aSequencesVec)
114 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aNewData;
116 if( nSeriesIndex && xCloneable.is() )
117 xClonedXValues.set( xCloneable->createClone(), uno::UNO_QUERY );
118 if( xValuesX.is() )
119 aNewData.push_back( xClonedXValues );
121 aNewData.push_back(elem);
123 rtl::Reference< DataSeries > xSeries;
124 if( nSeriesIndex < aSeriesToReUse.size())
125 xSeries = aSeriesToReUse[nSeriesIndex];
126 else
127 xSeries = new DataSeries;
128 assert( xSeries.is() );
129 xSeries->setData( aNewData );
131 aSeriesVec.push_back( xSeries );
132 ++nSeriesIndex;
135 return { { std::move(aSeriesVec) }, xCategories };
138 InterpretedData XYDataInterpreter::reinterpretDataSeries(
139 const InterpretedData& aInterpretedData )
141 InterpretedData aResult( aInterpretedData );
143 sal_Int32 i=0;
144 std::vector< rtl::Reference< DataSeries > > aSeries = FlattenSequence( aInterpretedData.Series );
145 const sal_Int32 nCount = aSeries.size();
146 for( ; i<nCount; ++i )
150 std::vector< uno::Reference< data::XLabeledDataSequence > > aNewSequences;
152 // values-y
153 uno::Reference< chart2::data::XLabeledDataSequence > xValuesY(
154 DataSeriesHelper::getDataSequenceByRole( aSeries[i], u"values-y"_ustr ));
155 uno::Reference< chart2::data::XLabeledDataSequence > xValuesX(
156 DataSeriesHelper::getDataSequenceByRole( aSeries[i], u"values-x"_ustr ));
157 // re-use values-... as values-x/values-y
158 if( ! xValuesX.is() ||
159 ! xValuesY.is())
161 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aValueSeqVec(
162 DataSeriesHelper::getAllDataSequencesByRole(
163 aSeries[i]->getDataSequences2(), u"values"_ustr ));
164 if( xValuesX.is())
165 aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesX ));
166 if( xValuesY.is())
167 aValueSeqVec.erase( find( aValueSeqVec.begin(), aValueSeqVec.end(), xValuesY ));
169 size_t nIndex = 0;
170 if( ! xValuesY.is() &&
171 aValueSeqVec.size() > nIndex )
173 xValuesY = aValueSeqVec[nIndex++];
174 if( xValuesY.is())
175 SetRole( xValuesY->getValues(), u"values-y"_ustr);
178 if( ! xValuesX.is() &&
179 aValueSeqVec.size() > nIndex )
181 xValuesX = aValueSeqVec[nIndex++];
182 if( xValuesX.is())
183 SetRole( xValuesX->getValues(), u"values-x"_ustr);
186 if( xValuesY.is())
188 if( xValuesX.is())
190 aNewSequences = { xValuesX, xValuesY };
192 else
194 aNewSequences = { xValuesY };
198 const std::vector< uno::Reference< data::XLabeledDataSequence > > & aSeqs = aSeries[i]->getDataSequences2();
199 if( aSeqs.size() != aNewSequences.size() )
201 #ifdef DBG_UTIL
202 for( auto const & j : aSeqs )
204 SAL_WARN_IF((j == xValuesY || j == xValuesX), "chart2.template", "All sequences should be used" );
206 #endif
207 aSeries[i]->setData( aNewSequences );
210 catch( const uno::Exception & )
212 DBG_UNHANDLED_EXCEPTION("chart2");
216 return aResult;
219 // criterion: all series must have exactly two data::XLabeledDataSequences
220 bool XYDataInterpreter::isDataCompatible(
221 const InterpretedData& aInterpretedData )
223 const std::vector< rtl::Reference< DataSeries > > aSeries = FlattenSequence( aInterpretedData.Series );
224 for( rtl::Reference< DataSeries > const & dataSeries : aSeries )
228 if( dataSeries->getDataSequences2().size() != 2 )
229 return false;
231 catch( const uno::Exception & )
233 DBG_UNHANDLED_EXCEPTION("chart2");
237 return true;
240 } // namespace chart
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */