1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
;
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
)
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
);
68 bool bCategoriesUsed
= false;
69 bool bSetXValues
= aData
.size()>1;
70 for( uno::Reference
< chart2::data::XLabeledDataSequence
> const & labelData
: aData
)
74 if( bHasCategories
&& ! bCategoriesUsed
)
76 xCategories
= labelData
;
79 SetRole( xCategories
->getValues(), u
"categories"_ustr
);
80 if( bUseCategoriesAsX
)
83 bCategoriesUsed
= true;
85 else if( !xValuesX
.is() && bSetXValues
)
89 SetRole( xValuesX
->getValues(), u
"values-x"_ustr
);
93 aSequencesVec
.push_back( labelData
);
95 SetRole( labelData
->getValues(), u
"values-y"_ustr
);
98 catch( const uno::Exception
& )
100 DBG_UNHANDLED_EXCEPTION("chart2");
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
);
119 aNewData
.push_back( xClonedXValues
);
121 aNewData
.push_back(elem
);
123 rtl::Reference
< DataSeries
> xSeries
;
124 if( nSeriesIndex
< aSeriesToReUse
.size())
125 xSeries
= aSeriesToReUse
[nSeriesIndex
];
127 xSeries
= new DataSeries
;
128 assert( xSeries
.is() );
129 xSeries
->setData( aNewData
);
131 aSeriesVec
.push_back( xSeries
);
135 return { { std::move(aSeriesVec
) }, xCategories
};
138 InterpretedData
XYDataInterpreter::reinterpretDataSeries(
139 const InterpretedData
& aInterpretedData
)
141 InterpretedData
aResult( aInterpretedData
);
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
;
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() ||
161 std::vector
< uno::Reference
< chart2::data::XLabeledDataSequence
> > aValueSeqVec(
162 DataSeriesHelper::getAllDataSequencesByRole(
163 aSeries
[i
]->getDataSequences2(), u
"values"_ustr
));
165 aValueSeqVec
.erase( find( aValueSeqVec
.begin(), aValueSeqVec
.end(), xValuesX
));
167 aValueSeqVec
.erase( find( aValueSeqVec
.begin(), aValueSeqVec
.end(), xValuesY
));
170 if( ! xValuesY
.is() &&
171 aValueSeqVec
.size() > nIndex
)
173 xValuesY
= aValueSeqVec
[nIndex
++];
175 SetRole( xValuesY
->getValues(), u
"values-y"_ustr
);
178 if( ! xValuesX
.is() &&
179 aValueSeqVec
.size() > nIndex
)
181 xValuesX
= aValueSeqVec
[nIndex
++];
183 SetRole( xValuesX
->getValues(), u
"values-x"_ustr
);
190 aNewSequences
= { xValuesX
, xValuesY
};
194 aNewSequences
= { xValuesY
};
198 const std::vector
< uno::Reference
< data::XLabeledDataSequence
> > & aSeqs
= aSeries
[i
]->getDataSequences2();
199 if( aSeqs
.size() != aNewSequences
.size() )
202 for( auto const & j
: aSeqs
)
204 SAL_WARN_IF((j
== xValuesY
|| j
== xValuesX
), "chart2.template", "All sequences should be used" );
207 aSeries
[i
]->setData( aNewSequences
);
210 catch( const uno::Exception
& )
212 DBG_UNHANDLED_EXCEPTION("chart2");
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 )
231 catch( const uno::Exception
& )
233 DBG_UNHANDLED_EXCEPTION("chart2");
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */