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 "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
;
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
)
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
)
78 if( bHasCategories
&& !bCategoriesUsed
)
80 xCategories
= aData
[nDataIdx
];
83 SetRole( xCategories
->getValues(), u
"categories"_ustr
);
84 if( bUseCategoriesAsX
)
87 bNextIsYValues
= nDataSeqCount
> 2;
90 bCategoriesUsed
= true;
92 else if( !xValuesX
.is() && bSetXValues
)
94 xValuesX
= aData
[nDataIdx
];
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");
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
;
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
];
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
);
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() ||
179 std::vector
< uno::Reference
< chart2::data::XLabeledDataSequence
> > aValueSeqVec(
180 DataSeriesHelper::getAllDataSequencesByRole(
181 aSeries
[i
]->getDataSequences2(), u
"values"_ustr
));
183 aValueSeqVec
.erase( find( aValueSeqVec
.begin(), aValueSeqVec
.end(), xValuesX
));
185 aValueSeqVec
.erase( find( aValueSeqVec
.begin(), aValueSeqVec
.end(), xValuesY
));
186 if( xValuesSize
.is())
187 aValueSeqVec
.erase( find( aValueSeqVec
.begin(), aValueSeqVec
.end(), xValuesSize
));
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
++];
204 SetRole( xValuesY
->getValues(), u
"values-y"_ustr
);
207 if( ! xValuesX
.is() &&
208 aValueSeqVec
.size() > nIndex
)
210 xValuesX
= aValueSeqVec
[nIndex
++];
212 SetRole( xValuesX
->getValues(), u
"values-x"_ustr
);
215 if( xValuesSize
.is())
221 aNewSequences
= { xValuesX
, xValuesY
, xValuesSize
};
225 aNewSequences
= { xValuesY
, xValuesSize
};
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" );
243 aSeries
[i
]->setData( aNewSequences
);
246 catch( const uno::Exception
& )
248 DBG_UNHANDLED_EXCEPTION("chart2");
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 )
266 catch( const uno::Exception
& )
268 DBG_UNHANDLED_EXCEPTION("chart2");
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */