cid#1607171 Data race condition
[LibreOffice.git] / sc / source / ui / vba / vbachart.cxx
blobf915dc47e3438d868c0ff1986897279a25cdda6a
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 .
19 #include "vbachart.hxx"
20 #include <com/sun/star/beans/XPropertySet.hpp>
21 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
23 #include <com/sun/star/chart/XAxisXSupplier.hpp>
24 #include <com/sun/star/chart/XAxisYSupplier.hpp>
25 #include <com/sun/star/chart/XAxisZSupplier.hpp>
26 #include <com/sun/star/chart/XTwoAxisXSupplier.hpp>
27 #include <com/sun/star/chart/XTwoAxisYSupplier.hpp>
28 #include <com/sun/star/chart/XChartDataArray.hpp>
29 #include <com/sun/star/chart/ChartSymbolType.hpp>
30 #include <com/sun/star/chart/ChartSolidType.hpp>
31 #include <com/sun/star/chart/ChartDataRowSource.hpp>
32 #include <ooo/vba/excel/XlChartType.hpp>
33 #include <ooo/vba/excel/XlRowCol.hpp>
34 #include <ooo/vba/excel/XlAxisType.hpp>
35 #include <ooo/vba/excel/XlAxisGroup.hpp>
37 #include <basic/sberrors.hxx>
38 #include "vbachartobject.hxx"
39 #include "vbarange.hxx"
40 #include "vbacharttitle.hxx"
41 #include "vbaaxes.hxx"
42 #include <document.hxx>
43 #include <utility>
45 using namespace ::com::sun::star;
46 using namespace ::ooo::vba;
47 using namespace ::ooo::vba::excel::XlChartType;
48 using namespace ::ooo::vba::excel::XlRowCol;
49 using namespace ::ooo::vba::excel::XlAxisType;
50 using namespace ::ooo::vba::excel::XlAxisGroup;
52 constexpr OUStringLiteral CHART_NAME(u"Name");
53 // #TODO move this constant to vbaseries.[ch]xx ( when it exists )
54 constexpr OUStringLiteral DEFAULTSERIESPREFIX(u"Series");
55 constexpr OUString DATAROWSOURCE(u"DataRowSource"_ustr);
56 constexpr OUString UPDOWN(u"UpDown"_ustr);
57 constexpr OUString VOLUME(u"Volume"_ustr);
58 constexpr OUString LINES(u"Lines"_ustr);
59 constexpr OUString SPLINETYPE(u"SplineType"_ustr);
60 constexpr OUString SYMBOLTYPE(u"SymbolType"_ustr);
61 constexpr OUString DEEP(u"Deep"_ustr);
62 constexpr OUString SOLIDTYPE(u"SolidType"_ustr);
63 constexpr OUString VERTICAL(u"Vertical"_ustr);
64 constexpr OUString PERCENT(u"Percent"_ustr);
65 constexpr OUString STACKED(u"Stacked"_ustr);
66 constexpr OUString DIM3D(u"Dim3D"_ustr);
67 constexpr OUString HASMAINTITLE(u"HasMainTitle"_ustr);
68 constexpr OUString HASLEGEND(u"HasLegend"_ustr);
70 ScVbaChart::ScVbaChart( const css::uno::Reference< ov::XHelperInterface >& _xParent, const css::uno::Reference< css::uno::XComponentContext >& _xContext, const css::uno::Reference< css::lang::XComponent >& _xChartComponent, css::uno::Reference< css::table::XTableChart > _xTableChart ) : ChartImpl_BASE( _xParent, _xContext ), mxTableChart(std::move( _xTableChart ))
72 mxChartDocument.set( _xChartComponent, uno::UNO_QUERY_THROW ) ;
73 // #TODO is it possible that the XPropertySet interface is not set
74 // code in setPlotBy seems to indicate that this is possible? but
75 // additionally there is no check in most of the places where it is used
76 // ( and therefore could possibly be NULL )
77 // I'm going to let it throw for the moment ( npower )
78 mxDiagramPropertySet.set( mxChartDocument->getDiagram(), uno::UNO_QUERY_THROW );
79 mxChartPropertySet.set( _xChartComponent, uno::UNO_QUERY_THROW ) ;
82 OUString SAL_CALL
83 ScVbaChart::getName()
85 OUString sName;
86 uno::Reference< beans::XPropertySet > xProps( mxChartDocument, uno::UNO_QUERY_THROW );
87 try
89 xProps->getPropertyValue( CHART_NAME ) >>= sName;
91 catch( const uno::Exception & ) // swallow exceptions
94 return sName;
97 uno::Any SAL_CALL
98 ScVbaChart::SeriesCollection(const uno::Any&)
100 return uno::Any();
103 ::sal_Int32 SAL_CALL
104 ScVbaChart::getChartType()
106 sal_Int32 nChartType = -1;
109 OUString sDiagramType = mxChartDocument->getDiagram()->getDiagramType();
110 if ( sDiagramType == "com.sun.star.chart.AreaDiagram" )
112 if (is3D())
114 nChartType = getStackedType(xl3DAreaStacked, xl3DAreaStacked100, xl3DArea);
116 else
118 nChartType = getStackedType(xlAreaStacked, xlAreaStacked100, xlArea);
121 else if ( sDiagramType == "com.sun.star.chart.PieDiagram" )
123 if (is3D())
124 nChartType = xl3DPie;
125 else
126 nChartType = xlPie; /*TODO XlChartType xlPieExploded, XlChartType xlPieOfPie */
128 else if ( sDiagramType == "com.sun.star.chart.BarDiagram" )
130 sal_Int32 nSolidType = chart::ChartSolidType::RECTANGULAR_SOLID;
131 if (mxDiagramPropertySet->getPropertySetInfo()->hasPropertyByName(SOLIDTYPE))
132 { //in 2D diagrams 'SolidType' may not be set
133 if (is3D())
134 mxDiagramPropertySet->getPropertyValue(SOLIDTYPE) >>= nSolidType;
136 switch (nSolidType)
138 case chart::ChartSolidType::CONE:
139 nChartType = getSolidType(xlConeCol, xlConeColStacked, xlConeColStacked100, xlConeColClustered, xlConeBarStacked, xlConeBarStacked100, xlConeBarClustered);
140 break;
141 case chart::ChartSolidType::CYLINDER:
142 nChartType = getSolidType(xlCylinderCol, xlCylinderColStacked, xlCylinderColStacked100, xlCylinderColClustered, xlCylinderBarStacked, xlCylinderBarStacked100, xlCylinderBarClustered);
143 break;
144 case chart::ChartSolidType::PYRAMID:
145 nChartType = getSolidType(xlPyramidCol, xlPyramidColStacked, xlPyramidColStacked100, xlPyramidColClustered, xlPyramidBarStacked, xlPyramidBarStacked100, xlPyramidBarClustered);
146 break;
147 default: // RECTANGULAR_SOLID
148 if (is3D())
150 nChartType = getSolidType(xl3DColumn, xl3DColumnStacked, xl3DColumnStacked100, xl3DColumnClustered, xl3DBarStacked, xl3DBarStacked100, xl3DBarClustered);
152 else
154 nChartType = getSolidType(xlColumnClustered, xlColumnStacked, xlColumnStacked100, xlColumnClustered, xlBarStacked, xlBarStacked100, xlBarClustered);
156 break;
159 else if ( sDiagramType == "com.sun.star.chart.StockDiagram" )
161 bool bVolume = false;
162 mxDiagramPropertySet->getPropertyValue(VOLUME) >>= bVolume;
163 if (bVolume)
165 nChartType = getStockUpDownValue(xlStockVOHLC, xlStockVHLC);
167 else
169 nChartType = getStockUpDownValue(xlStockOHLC, xlStockHLC);
172 else if ( sDiagramType == "com.sun.star.chart.XYDiagram" )
174 bool bHasLines = false;
175 mxDiagramPropertySet->getPropertyValue(LINES) >>= bHasLines;
176 sal_Int32 nSplineType = 0;
177 mxDiagramPropertySet->getPropertyValue(SPLINETYPE) >>= nSplineType;
178 if (nSplineType == 1)
180 nChartType = getMarkerType(xlXYScatterSmooth, xlXYScatterSmoothNoMarkers);
182 else if (bHasLines)
184 nChartType = getMarkerType(xlXYScatterLines, xlXYScatterLinesNoMarkers);
186 else
188 nChartType = xlXYScatter;
191 else if ( sDiagramType == "com.sun.star.chart.LineDiagram" )
193 if (is3D())
195 nChartType = xl3DLine;
197 else if (hasMarkers())
199 nChartType = getStackedType(xlLineMarkersStacked, xlLineMarkersStacked100, xlLineMarkers);
201 else
203 nChartType = getStackedType(xlLineStacked, xlLineStacked100, xlLine);
206 else if ( sDiagramType == "com.sun.star.chart.DonutDiagram" )
208 nChartType = xlDoughnut; // TODO DoughnutExploded ??
210 else if ( sDiagramType == "com.sun.star.chart.NetDiagram" )
212 nChartType = getMarkerType(xlRadarMarkers, xlRadar);
215 catch ( const uno::Exception& )
217 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
219 return nChartType;
222 void SAL_CALL
223 ScVbaChart::setChartType( ::sal_Int32 _nChartType )
227 switch (_nChartType)
229 case xlColumnClustered:
230 case xlColumnStacked:
231 case xlColumnStacked100:
232 case xl3DColumnClustered:
233 case xl3DColumnStacked:
234 case xl3DColumnStacked100:
235 case xl3DColumn:
236 case xlBarClustered:
237 case xlBarStacked:
238 case xlBarStacked100:
239 case xl3DBarClustered:
240 case xl3DBarStacked:
241 case xl3DBarStacked100:
242 case xlConeColClustered:
243 case xlConeColStacked:
244 case xlConeColStacked100:
245 case xlConeBarClustered:
246 case xlConeBarStacked:
247 case xlConeBarStacked100:
248 case xlConeCol:
249 case xlPyramidColClustered:
250 case xlPyramidColStacked:
251 case xlPyramidColStacked100:
252 case xlPyramidBarClustered:
253 case xlPyramidBarStacked:
254 case xlPyramidBarStacked100:
255 case xlPyramidCol:
256 case xlCylinderColClustered:
257 case xlCylinderColStacked:
258 case xlCylinderColStacked100:
259 case xlCylinderBarClustered:
260 case xlCylinderBarStacked:
261 case xlCylinderBarStacked100:
262 case xlCylinderCol:
263 case xlSurface: // not possible
264 case xlSurfaceWireframe:
265 case xlSurfaceTopView:
266 case xlSurfaceTopViewWireframe:
267 setDiagram( u"com.sun.star.chart.BarDiagram"_ustr);
268 break;
269 case xlLine:
270 case xl3DLine:
271 case xlLineStacked:
272 case xlLineStacked100:
273 case xlLineMarkers:
274 case xlLineMarkersStacked:
275 case xlLineMarkersStacked100:
276 setDiagram( u"com.sun.star.chart.LineDiagram"_ustr);
277 break;
278 case xl3DArea:
279 case xlArea:
280 case xlAreaStacked:
281 case xlAreaStacked100:
282 case xl3DAreaStacked:
283 case xl3DAreaStacked100:
284 setDiagram( u"com.sun.star.chart.AreaDiagram"_ustr );
285 break;
286 case xlDoughnut:
287 case xlDoughnutExploded:
288 setDiagram( u"com.sun.star.chart.DonutDiagram"_ustr );
289 break;
290 case xlStockHLC:
291 case xlStockOHLC:
292 case xlStockVHLC:
293 case xlStockVOHLC:
294 setDiagram( u"com.sun.star.chart.StockDiagram"_ustr);
295 mxDiagramPropertySet->setPropertyValue( UPDOWN, uno::Any((_nChartType == xlStockOHLC) || (_nChartType == xlStockVOHLC)));
296 mxDiagramPropertySet->setPropertyValue( VOLUME, uno::Any((_nChartType == xlStockVHLC) || (_nChartType == xlStockVOHLC)));
297 break;
299 case xlPieOfPie: // not possible
300 case xlPieExploded: // SegmentOffset on ChartDataPointProperties -> get from XDiagram //How does Excel do this?
301 case xl3DPieExploded:
302 case xl3DPie:
303 case xlPie:
304 case xlBarOfPie: // not possible (Zoom pie)
305 setDiagram( u"com.sun.star.chart.PieDiagram"_ustr);
306 break;
308 case xlRadar:
309 case xlRadarMarkers:
310 case xlRadarFilled:
311 setDiagram( u"com.sun.star.chart.NetDiagram"_ustr);
312 break;
313 case xlXYScatter:
314 case xlBubble: // not possible
315 case xlBubble3DEffect: // not possible
316 case xlXYScatterLines:
317 case xlXYScatterLinesNoMarkers:
318 case xlXYScatterSmooth:
319 case xlXYScatterSmoothNoMarkers:
320 setDiagram( u"com.sun.star.chart.XYDiagram"_ustr);
321 switch(_nChartType)
323 case xlXYScatter:
324 case xlBubble: // not possible
325 case xlBubble3DEffect: // not possible
326 mxDiagramPropertySet->setPropertyValue(LINES, uno::Any( false ));
327 break;
328 case xlXYScatterLines:
329 case xlXYScatterLinesNoMarkers:
330 mxDiagramPropertySet->setPropertyValue(LINES, uno::Any( true ));
331 break;
332 case xlXYScatterSmooth:
333 case xlXYScatterSmoothNoMarkers:
334 mxDiagramPropertySet->setPropertyValue(SPLINETYPE, uno::Any( sal_Int32(1)));
335 break;
336 default:
337 break;
339 break;
340 default:
341 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_CONVERSION), OUString() );
344 switch (_nChartType)
346 case xlLineMarkers:
347 case xlLineMarkersStacked:
348 case xlLineMarkersStacked100:
349 case xlRadarMarkers:
350 case xlXYScatterLines:
351 case xlXYScatterSmooth:
352 case xlXYScatter:
353 case xlBubble: // not possible
354 case xlBubble3DEffect: // not possible
355 mxDiagramPropertySet->setPropertyValue(SYMBOLTYPE, uno::Any( chart::ChartSymbolType::AUTO));
356 break;
357 default:
358 if (mxDiagramPropertySet->getPropertySetInfo()->hasPropertyByName(SYMBOLTYPE))
360 mxDiagramPropertySet->setPropertyValue(SYMBOLTYPE, uno::Any(chart::ChartSymbolType::NONE));
362 break;
365 switch (_nChartType)
367 case xlConeCol:
368 case xlPyramidCol:
369 case xlCylinderCol:
370 case xl3DColumn:
371 case xlSurface: // not possible
372 case xlSurfaceWireframe:
373 case xlSurfaceTopView:
374 case xlSurfaceTopViewWireframe:
375 mxDiagramPropertySet->setPropertyValue(DEEP,uno::Any( true ));
376 break;
377 default:
378 if (mxDiagramPropertySet->getPropertySetInfo()->hasPropertyByName(DEEP))
380 mxDiagramPropertySet->setPropertyValue(DEEP, uno::Any( false));
382 break;
385 switch (_nChartType)
387 case xlConeColClustered:
388 case xlConeColStacked:
389 case xlConeColStacked100:
390 case xlConeBarClustered:
391 case xlConeBarStacked:
392 case xlConeBarStacked100:
393 case xlConeCol:
394 mxDiagramPropertySet->setPropertyValue(SOLIDTYPE, uno::Any(chart::ChartSolidType::CONE));
395 break;
396 case xlPyramidColClustered:
397 case xlPyramidColStacked:
398 case xlPyramidColStacked100:
399 case xlPyramidBarClustered:
400 case xlPyramidBarStacked:
401 case xlPyramidBarStacked100:
402 case xlPyramidCol:
403 mxDiagramPropertySet->setPropertyValue(SOLIDTYPE, uno::Any(chart::ChartSolidType::PYRAMID));
404 break;
405 case xlCylinderColClustered:
406 case xlCylinderColStacked:
407 case xlCylinderColStacked100:
408 case xlCylinderBarClustered:
409 case xlCylinderBarStacked:
410 case xlCylinderBarStacked100:
411 case xlCylinderCol:
412 mxDiagramPropertySet->setPropertyValue(SOLIDTYPE, uno::Any(chart::ChartSolidType::CYLINDER));
413 break;
414 default:
415 if (mxDiagramPropertySet->getPropertySetInfo()->hasPropertyByName(SOLIDTYPE))
417 mxDiagramPropertySet->setPropertyValue(SOLIDTYPE, uno::Any(chart::ChartSolidType::RECTANGULAR_SOLID));
419 break;
422 switch ( _nChartType)
424 case xlConeCol:
425 case xlConeColClustered:
426 case xlConeColStacked:
427 case xlConeColStacked100:
428 case xlPyramidColClustered:
429 case xlPyramidColStacked:
430 case xlPyramidColStacked100:
431 case xlCylinderColClustered:
432 case xlCylinderColStacked:
433 case xlCylinderColStacked100:
434 case xlColumnClustered:
435 case xlColumnStacked:
436 case xlColumnStacked100:
437 case xl3DColumnClustered:
438 case xl3DColumnStacked:
439 case xl3DColumnStacked100:
440 case xlSurface: // not possible
441 case xlSurfaceWireframe:
442 case xlSurfaceTopView:
443 case xlSurfaceTopViewWireframe:
444 mxDiagramPropertySet->setPropertyValue(VERTICAL, uno::Any( true));
445 break;
446 default:
447 if (mxDiagramPropertySet->getPropertySetInfo()->hasPropertyByName(VERTICAL))
449 mxDiagramPropertySet->setPropertyValue(VERTICAL, uno::Any(false));
451 break;
454 switch (_nChartType)
456 case xlColumnStacked:
457 case xl3DColumnStacked:
458 case xlBarStacked:
459 case xl3DBarStacked:
460 case xlLineStacked:
461 case xlLineMarkersStacked:
462 case xlAreaStacked:
463 case xl3DAreaStacked:
464 case xlCylinderColStacked:
465 case xlCylinderBarStacked:
466 case xlConeColStacked:
467 case xlConeBarStacked:
468 case xlPyramidColStacked:
469 case xlPyramidBarStacked:
470 mxDiagramPropertySet->setPropertyValue(PERCENT, uno::Any( false ));
471 mxDiagramPropertySet->setPropertyValue(STACKED, uno::Any( true ));
472 break;
473 case xlPyramidColStacked100:
474 case xlPyramidBarStacked100:
475 case xlConeColStacked100:
476 case xlConeBarStacked100:
477 case xlCylinderBarStacked100:
478 case xlCylinderColStacked100:
479 case xl3DAreaStacked100:
480 case xlLineMarkersStacked100:
481 case xlAreaStacked100:
482 case xlLineStacked100:
483 case xl3DBarStacked100:
484 case xlBarStacked100:
485 case xl3DColumnStacked100:
486 case xlColumnStacked100:
487 mxDiagramPropertySet->setPropertyValue(STACKED, uno::Any( true));
488 mxDiagramPropertySet->setPropertyValue(PERCENT, uno::Any( true ));
489 break;
490 default:
491 mxDiagramPropertySet->setPropertyValue(PERCENT, uno::Any( false));
492 mxDiagramPropertySet->setPropertyValue(STACKED, uno::Any( false));
493 break;
495 switch (_nChartType)
497 case xl3DArea:
498 case xl3DAreaStacked:
499 case xl3DAreaStacked100:
500 case xl3DBarClustered:
501 case xl3DBarStacked:
502 case xl3DBarStacked100:
503 case xl3DColumn:
504 case xl3DColumnClustered:
505 case xl3DColumnStacked:
506 case xl3DColumnStacked100:
507 case xl3DLine:
508 case xl3DPie:
509 case xl3DPieExploded:
510 case xlConeColClustered:
511 case xlConeColStacked:
512 case xlConeColStacked100:
513 case xlConeBarClustered:
514 case xlConeBarStacked:
515 case xlConeBarStacked100:
516 case xlConeCol:
517 case xlPyramidColClustered:
518 case xlPyramidColStacked:
519 case xlPyramidColStacked100:
520 case xlPyramidBarClustered:
521 case xlPyramidBarStacked:
522 case xlPyramidBarStacked100:
523 case xlPyramidCol:
524 case xlCylinderColClustered:
525 case xlCylinderColStacked:
526 case xlCylinderColStacked100:
527 case xlCylinderBarClustered:
528 case xlCylinderBarStacked:
529 case xlCylinderBarStacked100:
530 case xlCylinderCol:
531 mxDiagramPropertySet->setPropertyValue(DIM3D, uno::Any( true));
532 break;
533 default:
534 if (mxDiagramPropertySet->getPropertySetInfo()->hasPropertyByName(DIM3D))
536 mxDiagramPropertySet->setPropertyValue(DIM3D, uno::Any( false));
538 break;
541 catch ( const uno::Exception& )
543 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
547 void SAL_CALL
548 ScVbaChart::Activate()
550 // #TODO how are Chart sheets handled ( I know we don't even consider
551 // them in the worksheets/sheets collections ), but...???
552 // note: in vba for excel the parent of a Chart sheet is a workbook,
553 // e.g. 'ThisWorkbook'
554 uno::Reference< XHelperInterface > xParent( getParent() );
555 ScVbaChartObject* pChartObj = static_cast< ScVbaChartObject* >( xParent.get() );
556 if ( !pChartObj )
557 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), u"no ChartObject as parent"_ustr );
559 pChartObj->Activate();
563 void SAL_CALL
564 ScVbaChart::setSourceData( const css::uno::Reference< ::ooo::vba::excel::XRange >& _xCalcRange, const css::uno::Any& _aPlotBy )
568 table::CellRangeAddress aSingleRangeAddress;
570 uno::Reference< sheet::XCellRangeAddressable > xAddressable( _xCalcRange->getCellRange(), uno::UNO_QUERY_THROW );
571 aSingleRangeAddress = xAddressable->getRangeAddress();
573 mxTableChart->setRanges({ aSingleRangeAddress } );
575 bool bsetRowHeaders = false;
576 bool bsetColumnHeaders = false;
578 ScVbaRange* pRange = static_cast< ScVbaRange* >( _xCalcRange.get() );
579 if ( pRange )
581 ScDocument& rDoc = pRange->getScDocument();
582 bsetRowHeaders = rDoc.HasRowHeader( static_cast< SCCOL >( aSingleRangeAddress.StartColumn ), static_cast< SCROW >( aSingleRangeAddress.StartRow ), static_cast< SCCOL >( aSingleRangeAddress.EndColumn ), static_cast< SCROW >( aSingleRangeAddress.EndRow ), static_cast< SCTAB >( aSingleRangeAddress.Sheet ) );
583 bsetColumnHeaders = rDoc.HasColHeader( static_cast< SCCOL >( aSingleRangeAddress.StartColumn ), static_cast< SCROW >( aSingleRangeAddress.StartRow ), static_cast< SCCOL >( aSingleRangeAddress.EndColumn ), static_cast< SCROW >( aSingleRangeAddress.EndRow ), static_cast< SCTAB >( aSingleRangeAddress.Sheet ));
585 mxTableChart->setHasRowHeaders(bsetRowHeaders);
586 mxTableChart->setHasColumnHeaders(bsetColumnHeaders);
588 if ((!bsetColumnHeaders) || (!bsetRowHeaders))
590 uno::Reference< chart::XChartDataArray > xChartDataArray( mxChartDocument->getData(), uno::UNO_QUERY_THROW );
591 if (!bsetColumnHeaders)
593 xChartDataArray->setColumnDescriptions( getDefaultSeriesDescriptions(xChartDataArray->getColumnDescriptions().getLength() ));
595 if (!bsetRowHeaders)
597 xChartDataArray->setRowDescriptions(getDefaultSeriesDescriptions(xChartDataArray->getRowDescriptions().getLength() ));
601 if ( _aPlotBy.hasValue() )
603 sal_Int32 nVal = 0;
604 _aPlotBy >>= nVal;
605 setPlotBy( nVal );
607 else
609 sal_Int32 nRows = aSingleRangeAddress.EndRow - aSingleRangeAddress.StartRow;
610 sal_Int32 nCols = aSingleRangeAddress.EndColumn - aSingleRangeAddress.StartColumn;
611 // AutoDetect emulation
612 if ( nRows > nCols )
613 setPlotBy( xlColumns );
614 else if ( nRows <= nCols )
615 setPlotBy( xlRows );
618 catch (const uno::Exception&)
620 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
624 uno::Sequence< OUString >
625 ScVbaChart::getDefaultSeriesDescriptions( sal_Int32 _nCount )
627 uno::Sequence< OUString > sDescriptions ( _nCount );
628 std::generate_n(sDescriptions.getArray(), _nCount,
629 [i = 1]() mutable -> OUString { return DEFAULTSERIESPREFIX + OUString::number(i++); });
630 return sDescriptions;
633 void
634 ScVbaChart::setDefaultChartType()
636 setChartType( xlColumnClustered );
639 void
640 ScVbaChart::setPlotBy( ::sal_Int32 _nPlotBy )
644 if ( !mxDiagramPropertySet.is() )
645 setDefaultChartType();
646 switch (_nPlotBy)
648 case xlRows:
649 mxDiagramPropertySet->setPropertyValue( DATAROWSOURCE, uno::Any( chart::ChartDataRowSource_ROWS ) );
650 break;
651 case xlColumns:
652 mxDiagramPropertySet->setPropertyValue( DATAROWSOURCE, uno::Any( chart::ChartDataRowSource_COLUMNS) );
653 break;
654 default:
655 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
658 catch (const uno::Exception&)
660 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
664 ::sal_Int32 SAL_CALL
665 ScVbaChart::getPlotBy( )
669 chart::ChartDataRowSource aChartDataRowSource;
670 mxDiagramPropertySet->getPropertyValue(DATAROWSOURCE) >>= aChartDataRowSource;
671 if (aChartDataRowSource == chart::ChartDataRowSource_COLUMNS)
673 return xlColumns;
675 else
677 return xlRows;
680 catch (const uno::Exception&)
682 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
686 void
687 ScVbaChart::setDiagram( const OUString& _sDiagramType )
691 uno::Reference< lang::XMultiServiceFactory > xMSF( mxChartDocument, uno::UNO_QUERY_THROW );
692 uno::Reference< chart::XDiagram > xDiagram( xMSF->createInstance( _sDiagramType ), uno::UNO_QUERY_THROW );
693 mxChartDocument->setDiagram( xDiagram );
694 mxDiagramPropertySet.set( xDiagram, uno::UNO_QUERY_THROW );
696 catch ( const uno::Exception& )
698 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
702 // #TODO find out why we have Location/getLocation? there is afaik no
703 // Location property, just a Location function for the Chart object
704 sal_Int32 SAL_CALL
705 ScVbaChart::Location()
707 return getLocation();
710 sal_Int32 SAL_CALL
711 ScVbaChart::getLocation()
713 return -1;
716 void SAL_CALL
717 ScVbaChart::setLocation( ::sal_Int32 /*where*/, const css::uno::Any& /*Name*/ )
719 // Helper api just stubs out the code <shrug>
720 // #TODO come back and make sense out of this
721 // String sheetName = null;
723 // if ((name != null) && name instanceof String) {
724 // sheetName = (String) name;
725 // }
726 // XSpreadsheetDocument xShDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface( XSpreadsheetDocument.class,getXModel() );
727 // com.sun.star.sheet.XSpreadsheets xSheets = xShDoc.Sheets();
729 // switch (where) {
730 // case ClLocationType.clLocationAsObject_value: //{
732 // if (sheetName == null) {
733 // DebugHelper.writeInfo("Can't embed in Chart without knowing SheetName");
734 // return;
735 // }
737 // try {
738 // Any any = (Any) xSheets.getByName(sheetName);
739 // chartSheet = (XSpreadsheet) any.getObject();
741 // // chartSheet = (XSpreadsheet) xSheets.getByName( sheetName );
742 // } catch (NoSuchElementException e) {
743 // // TODO Auto-generated catch block
744 // e.printStackTrace();
746 // return;
747 // } catch (WrappedTargetException e) {
748 // // TODO Auto-generated catch block
749 // e.printStackTrace();
751 // return;
752 // } catch (java.lang.Exception e) {
753 // e.printStackTrace();
754 // }
756 // XTableChartsSupplier xTCS = (XTableChartsSupplier) UnoRuntime.queryInterface( XTableChartsSupplier.class, chartSheet);
757 // XTableCharts xTableCharts = xTCS.getCharts();
758 // XIndexAccess xIA = (XIndexAccess) UnoRuntime.queryInterface( XIndexAccess.class, xTableCharts);
759 // int numCharts = xIA.getCount();
760 // chartName = "Chart " + (numCharts + 1);
762 // //}
763 // break;
765 // case ClLocationType.clLocationAsNewSheet_value:
766 // case ClLocationType.clLocationAutomatic_value:default: //{
767 // chartName = "Chart 1"; // Since it's a new sheet, it's the first on it...
769 // XIndexAccess xSheetIA = (XIndexAccess) UnoRuntime.queryInterface( XIndexAccess.class, xSheets);
771 // short newSheetNum = (short) (xSheetIA.getCount() + 1);
773 // if (sheetName == null){
774 // sheetName = "ChartSheet " + newSheetNum; // Why not?
775 // }
776 // // DPK TODO : Probably should use Sheets to create this!
777 // xSheets.insertNewByName(sheetName, newSheetNum);
779 // try {
780 // chartSheet =
781 // (XSpreadsheet) xSheets.getByName(sheetName);
782 // } catch (NoSuchElementException e) {
783 // // TODO Auto-generated catch block
784 // e.printStackTrace();
786 // return;
787 // } catch (WrappedTargetException e) {
788 // // TODO Auto-generated catch block
789 // e.printStackTrace();
791 // return;
792 // }
794 // //}
795 // break;
796 // }
798 // // Last thing should be a call to createChartForReal(), one of them
799 // // should succeed.
800 // createChartForReal();
804 sal_Bool SAL_CALL
805 ScVbaChart::getHasTitle( )
807 bool bHasTitle = false;
810 mxChartPropertySet->getPropertyValue(HASMAINTITLE) >>= bHasTitle;
812 catch (const uno::Exception&)
814 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
816 return bHasTitle;
819 void SAL_CALL
820 ScVbaChart::setHasTitle( sal_Bool bTitle )
824 mxChartPropertySet->setPropertyValue(HASMAINTITLE, uno::Any( bTitle ));
826 catch (const uno::Exception&)
828 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
833 sal_Bool SAL_CALL
834 ScVbaChart::getHasLegend( )
836 bool bHasLegend = false;
839 mxChartPropertySet->getPropertyValue(HASLEGEND) >>= bHasLegend;
841 catch (const uno::Exception&)
843 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
845 return bHasLegend;
848 void SAL_CALL
849 ScVbaChart::setHasLegend( sal_Bool bLegend )
853 mxChartPropertySet->setPropertyValue(HASLEGEND, uno::Any(bLegend));
855 catch (const uno::Exception&)
857 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
861 uno::Reference< excel::XChartTitle > SAL_CALL
862 ScVbaChart::getChartTitle( )
864 uno::Reference< drawing::XShape > xTitleShape = mxChartDocument->getTitle();
865 // #TODO check parent
866 return new ScVbaChartTitle(this, mxContext, xTitleShape);
869 uno::Any SAL_CALL
870 ScVbaChart::Axes( const uno::Any& Type, const uno::Any& AxisGroup )
872 // mmm chart probably is the parent, #TODO check parent
873 uno::Reference< excel::XAxes > xAxes = new ScVbaAxes( this, mxContext, this );
874 if ( !Type.hasValue() )
875 return uno::Any( xAxes );
876 return xAxes->Item( Type, AxisGroup );
878 bool
879 ScVbaChart::is3D()
881 // #TODO perhaps provide limited Debughelper functionality
882 bool is3d = false;
883 mxDiagramPropertySet->getPropertyValue(DIM3D) >>= is3d;
884 return is3d;
887 sal_Int32
888 ScVbaChart::getStackedType( sal_Int32 _nStacked, sal_Int32 _n100PercentStacked, sal_Int32 _nUnStacked )
890 // #TODO perhaps provide limited Debughelper functionality
891 if (isStacked())
893 if (is100PercentStacked())
894 return _n100PercentStacked;
895 else
896 return _nStacked;
898 else
899 return _nUnStacked;
902 bool
903 ScVbaChart::isStacked()
905 // #TODO perhaps provide limited Debughelper functionality
906 bool bStacked = false;
907 mxDiagramPropertySet->getPropertyValue(STACKED) >>= bStacked;
908 return bStacked;
911 bool
912 ScVbaChart::is100PercentStacked()
914 // #TODO perhaps provide limited Debughelper functionality
915 bool b100Percent = false;
916 mxDiagramPropertySet->getPropertyValue(PERCENT) >>= b100Percent;
917 return b100Percent;
920 sal_Int32
921 ScVbaChart::getSolidType(sal_Int32 _nDeep, sal_Int32 _nVertiStacked, sal_Int32 _nVerti100PercentStacked, sal_Int32 _nVertiUnStacked, sal_Int32 _nHoriStacked, sal_Int32 _nHori100PercentStacked, sal_Int32 _nHoriUnStacked)
925 bool bIsVertical = true;
926 mxDiagramPropertySet->getPropertyValue(VERTICAL) >>= bIsVertical;
927 bool bIsDeep = false;
928 mxDiagramPropertySet->getPropertyValue(DEEP) >>= bIsDeep;
930 if (bIsDeep)
932 return _nDeep;
934 else
936 if (bIsVertical)
938 return getStackedType(_nVertiStacked, _nVerti100PercentStacked, _nVertiUnStacked);
940 else
942 return getStackedType(_nHoriStacked, _nHori100PercentStacked, _nHoriUnStacked);
946 catch (const uno::Exception&)
948 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
952 sal_Int32
953 ScVbaChart::getStockUpDownValue(sal_Int32 _nUpDown, sal_Int32 _nNotUpDown)
957 bool bUpDown = false;
958 mxDiagramPropertySet->getPropertyValue(UPDOWN) >>= bUpDown;
959 if (bUpDown)
961 return _nUpDown;
963 else
965 return _nNotUpDown;
968 catch (const uno::Exception&)
970 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
974 bool
975 ScVbaChart::hasMarkers()
977 bool bHasMarkers = false;
980 sal_Int32 nSymbol=0;
981 mxDiagramPropertySet->getPropertyValue(SYMBOLTYPE) >>= nSymbol;
982 bHasMarkers = nSymbol != chart::ChartSymbolType::NONE;
984 catch (const uno::Exception&)
986 throw script::BasicErrorException( OUString(), uno::Reference< uno::XInterface >(), sal_uInt32(ERRCODE_BASIC_METHOD_FAILED), OUString() );
988 return bHasMarkers;
991 sal_Int32
992 ScVbaChart::getMarkerType(sal_Int32 _nWithMarkers, sal_Int32 _nWithoutMarkers)
994 if (hasMarkers())
995 return _nWithMarkers;
996 return _nWithoutMarkers;
999 void
1000 ScVbaChart::assignDiagramAttributes()
1002 xAxisXSupplier.set( mxDiagramPropertySet, uno::UNO_QUERY_THROW );
1003 xAxisYSupplier.set( mxDiagramPropertySet, uno::UNO_QUERY_THROW );
1004 xAxisZSupplier.set( mxDiagramPropertySet, uno::UNO_QUERY_THROW );
1005 xTwoAxisXSupplier.set( mxDiagramPropertySet, uno::UNO_QUERY_THROW );
1006 xTwoAxisYSupplier.set( mxDiagramPropertySet, uno::UNO_QUERY_THROW );
1009 uno::Reference< beans::XPropertySet >
1010 ScVbaChart::getAxisPropertySet(sal_Int32 _nAxisType, sal_Int32 _nAxisGroup)
1012 assignDiagramAttributes();
1013 uno::Reference< beans::XPropertySet > xAxisProps;
1014 switch(_nAxisType)
1016 case xlCategory:
1017 if (_nAxisGroup == xlPrimary)
1019 xAxisProps = xAxisXSupplier->getXAxis();
1021 else if (_nAxisGroup == xlSecondary)
1023 xAxisProps = xTwoAxisXSupplier->getSecondaryXAxis();
1025 break;
1026 case xlSeriesAxis:
1027 xAxisProps = xAxisZSupplier->getZAxis();
1028 break;
1029 case xlValue:
1030 if (_nAxisGroup == xlPrimary)
1031 xAxisProps = xAxisYSupplier->getYAxis();
1032 else if (_nAxisGroup == xlSecondary)
1033 xAxisProps = xTwoAxisYSupplier->getSecondaryYAxis();
1034 break;
1035 default:
1036 return xAxisProps;
1038 return xAxisProps;
1041 OUString
1042 ScVbaChart::getServiceImplName()
1044 return u"ScVbaChart"_ustr;
1047 uno::Sequence< OUString >
1048 ScVbaChart::getServiceNames()
1050 static uno::Sequence< OUString > const aServiceNames
1052 u"ooo.vba.excel.Chart"_ustr
1054 return aServiceNames;
1057 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */