LibreOffice Module chart2 (master) 1
XYDataInterpreter.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
24#include "XYDataInterpreter.hxx"
25#include <DataSeries.hxx>
26#include <DataSeriesHelper.hxx>
27#include <CommonConverters.hxx>
28#include <com/sun/star/util/XCloneable.hpp>
30#include <sal/log.hxx>
31
32using namespace ::com::sun::star;
33using namespace ::com::sun::star::chart2;
34
35using ::com::sun::star::uno::Reference;
36using ::com::sun::star::uno::Sequence;
37
38namespace chart
39{
40
42{
43}
44
46{
47}
48
49// ____ XDataInterpreter ____
52 const Sequence< beans::PropertyValue >& aArguments,
53 const std::vector< rtl::Reference< DataSeries > >& aSeriesToReUse )
54{
55 if( ! xSource.is())
56 return InterpretedData();
57
58 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aData = DataInterpreter::getDataSequences(xSource);
59
61 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aSequencesVec;
62
64 bool bHasCategories = HasCategories( aArguments, aData );
65 bool bUseCategoriesAsX = UseCategoriesAsX( aArguments );
66
67 // parse data
68 bool bCategoriesUsed = false;
69 bool bSetXValues = aData.size()>1;
71 {
72 try
73 {
74 if( bHasCategories && ! bCategoriesUsed )
75 {
76 xCategories = labelData;
77 if( xCategories.is())
78 {
79 SetRole( xCategories->getValues(), "categories");
80 if( bUseCategoriesAsX )
81 bSetXValues = false;
82 }
83 bCategoriesUsed = true;
84 }
85 else if( !xValuesX.is() && bSetXValues )
86 {
87 xValuesX = labelData;
88 if( xValuesX.is())
89 SetRole( xValuesX->getValues(), "values-x");
90 }
91 else
92 {
93 aSequencesVec.push_back( labelData );
94 if( labelData.is())
95 SetRole( labelData->getValues(), "values-y");
96 }
97 }
98 catch( const uno::Exception & )
99 {
100 DBG_UNHANDLED_EXCEPTION("chart2");
101 }
102 }
103
104 // create DataSeries
105 std::vector< rtl::Reference< DataSeries > > aSeriesVec;
106 aSeriesVec.reserve( aSequencesVec.size());
107
108 Reference< data::XLabeledDataSequence > xClonedXValues = xValuesX;
109 Reference< util::XCloneable > xCloneable( xValuesX, uno::UNO_QUERY );
110
111 std::size_t nSeriesIndex = 0;
112 for (auto const& elem : aSequencesVec)
113 {
114 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aNewData;
115
116 if( nSeriesIndex && xCloneable.is() )
117 xClonedXValues.set( xCloneable->createClone(), uno::UNO_QUERY );
118 if( xValuesX.is() )
119 aNewData.push_back( xClonedXValues );
120
121 aNewData.push_back(elem);
122
124 if( nSeriesIndex < aSeriesToReUse.size())
125 xSeries = aSeriesToReUse[nSeriesIndex];
126 else
127 xSeries = new DataSeries;
128 assert( xSeries.is() );
129 xSeries->setData( aNewData );
130
131 aSeriesVec.push_back( xSeries );
132 ++nSeriesIndex;
133 }
134
135 return { { aSeriesVec }, xCategories };
136}
137
139 const InterpretedData& aInterpretedData )
140{
141 InterpretedData aResult( aInterpretedData );
142
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 )
147 {
148 try
149 {
150 std::vector< uno::Reference< data::XLabeledDataSequence > > aNewSequences;
151
152 // values-y
154 DataSeriesHelper::getDataSequenceByRole( aSeries[i], "values-y" ));
156 DataSeriesHelper::getDataSequenceByRole( aSeries[i], "values-x" ));
157 // re-use values-... as values-x/values-y
158 if( ! xValuesX.is() ||
159 ! xValuesY.is())
160 {
161 std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aValueSeqVec(
163 aSeries[i]->getDataSequences2(), "values" ));
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 ));
168
169 size_t nIndex = 0;
170 if( ! xValuesY.is() &&
171 aValueSeqVec.size() > nIndex )
172 {
173 xValuesY = aValueSeqVec[nIndex++];
174 if( xValuesY.is())
175 SetRole( xValuesY->getValues(), "values-y");
176 }
177
178 if( ! xValuesX.is() &&
179 aValueSeqVec.size() > nIndex )
180 {
181 xValuesX = aValueSeqVec[nIndex++];
182 if( xValuesX.is())
183 SetRole( xValuesY->getValues(), "values-x");
184 }
185 }
186 if( xValuesY.is())
187 {
188 if( xValuesX.is())
189 {
190 aNewSequences = { xValuesX, xValuesY };
191 }
192 else
193 {
194 aNewSequences = { xValuesY };
195 }
196 }
197
198 const std::vector< uno::Reference< data::XLabeledDataSequence > > & aSeqs = aSeries[i]->getDataSequences2();
199 if( aSeqs.size() != aNewSequences.size() )
200 {
201#ifdef DBG_UTIL
202 for( auto const & j : aSeqs )
203 {
204 SAL_WARN_IF((j == xValuesY || j == xValuesX), "chart2.template", "All sequences should be used" );
205 }
206#endif
207 aSeries[i]->setData( aNewSequences );
208 }
209 }
210 catch( const uno::Exception & )
211 {
212 DBG_UNHANDLED_EXCEPTION("chart2");
213 }
214 }
215
216 return aResult;
217}
218
219// criterion: all series must have exactly two data::XLabeledDataSequences
221 const InterpretedData& aInterpretedData )
222{
223 const std::vector< rtl::Reference< DataSeries > > aSeries = FlattenSequence( aInterpretedData.Series );
224 for( rtl::Reference< DataSeries > const & dataSeries : aSeries )
225 {
226 try
227 {
228 if( dataSeries->getDataSequences2().size() != 2 )
229 return false;
230 }
231 catch( const uno::Exception & )
232 {
233 DBG_UNHANDLED_EXCEPTION("chart2");
234 }
235 }
236
237 return true;
238}
239
240} // namespace chart
241
242/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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)
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.
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 ~XYDataInterpreter() override
virtual bool isDataCompatible(const InterpretedData &aInterpretedData) override
parses the given data and states, if a reinterpretDataSeries() call can be done without data loss.
int nCount
#define DBG_UNHANDLED_EXCEPTION(...)
Sequence< PropertyValue > aArguments
sal_Int32 nIndex
#define SAL_WARN_IF(condition, area, stream)
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