1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <DiagramHelper.hxx>
#include <DataSeriesHelper.hxx>
#include <AxisHelper.hxx>
#include <ChartTypeHelper.hxx>
#include <ChartModel.hxx>
#include <ChartModelHelper.hxx>
#include <ExplicitCategoriesProvider.hxx>
#include <servicenames_charttypes.hxx>
#include <RelativePositionHelper.hxx>
#include <ControllerLockGuard.hxx>
#include <NumberFormatterWrapper.hxx>
#include <unonames.hxx>
#include <com/sun/star/chart/MissingValueTreatment.hpp>
#include <com/sun/star/chart/XDiagramPositioning.hpp>
#include <com/sun/star/chart2/XAnyDescriptionAccess.hpp>
#include <com/sun/star/chart2/XTitled.hpp>
#include <com/sun/star/chart2/XChartTypeContainer.hpp>
#include <com/sun/star/chart2/XChartTypeTemplate.hpp>
#include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
#include <com/sun/star/chart2/XDataSeriesContainer.hpp>
#include <com/sun/star/chart2/AxisType.hpp>
#include <com/sun/star/chart2/DataPointGeometry3D.hpp>
#include <com/sun/star/chart2/RelativePosition.hpp>
#include <com/sun/star/chart2/RelativeSize.hpp>
#include <com/sun/star/chart2/StackingDirection.hpp>
#include <com/sun/star/util/CloseVetoException.hpp>
#include <com/sun/star/util/NumberFormat.hpp>
#include <com/sun/star/util/XNumberFormatsSupplier.hpp>
#include <unotools/saveopt.hxx>
#include <rtl/math.hxx>
#include <svl/zforlist.hxx>
#include <vcl/svapp.hxx>
#include <vcl/settings.hxx>
#include <comphelper/sequence.hxx>
#include <tools/diagnose_ex.h>
#include <sal/log.hxx>
using namespace ::com::sun::star;
using namespace ::com::sun::star::chart2;
using namespace ::std;
using ::com::sun::star::uno::Reference;
using ::com::sun::star::uno::Sequence;
using ::com::sun::star::uno::Any;
using ::com::sun::star::chart2::XAnyDescriptionAccess;
namespace chart
{
DiagramHelper::tTemplateWithServiceName
DiagramHelper::getTemplateForDiagram(
const Reference< XDiagram > & xDiagram,
const Reference< lang::XMultiServiceFactory > & xChartTypeManager )
{
DiagramHelper::tTemplateWithServiceName aResult;
if( ! (xChartTypeManager.is() && xDiagram.is()))
return aResult;
Sequence< OUString > aServiceNames( xChartTypeManager->getAvailableServiceNames());
const sal_Int32 nLength = aServiceNames.getLength();
bool bTemplateFound = false;
for( sal_Int32 i = 0; ! bTemplateFound && i < nLength; ++i )
{
try
{
Reference< XChartTypeTemplate > xTempl(
xChartTypeManager->createInstance( aServiceNames[ i ] ), uno::UNO_QUERY_THROW );
if (xTempl.is() && xTempl->matchesTemplate(xDiagram, true))
{
aResult.first = xTempl;
aResult.second = aServiceNames[ i ];
bTemplateFound = true;
}
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
}
return aResult;
}
void DiagramHelper::setVertical(
const Reference< XDiagram > & xDiagram,
bool bVertical /* = true */ )
{
try
{
Reference< XCoordinateSystemContainer > xCnt( xDiagram, uno::UNO_QUERY );
if (!xCnt.is())
return;
Sequence< Reference<XCoordinateSystem> > aCooSys = xCnt->getCoordinateSystems();
uno::Any aValue;
aValue <<= bVertical;
for( sal_Int32 i=0; i<aCooSys.getLength(); ++i )
{
uno::Reference< XCoordinateSystem > xCooSys( aCooSys[i] );
Reference< beans::XPropertySet > xProp( xCooSys, uno::UNO_QUERY );
bool bChanged = false;
if (xProp.is())
{
bool bOldSwap = false;
if( !(xProp->getPropertyValue("SwapXAndYAxis") >>= bOldSwap)
|| bVertical != bOldSwap )
bChanged = true;
if( bChanged )
xProp->setPropertyValue("SwapXAndYAxis", aValue);
}
if (!xCooSys.is())
continue;
const sal_Int32 nDimensionCount = xCooSys->getDimension();
sal_Int32 nDimIndex = 0;
for (nDimIndex=0; nDimIndex < nDimensionCount; ++nDimIndex)
{
const sal_Int32 nMaximumScaleIndex = xCooSys->getMaximumAxisIndexByDimension(nDimIndex);
for (sal_Int32 nI = 0; nI <= nMaximumScaleIndex; ++nI)
{
Reference<chart2::XAxis> xAxis = xCooSys->getAxisByDimension(nDimIndex,nI);
if (!xAxis.is())
continue;
//adapt title rotation only when axis swapping has changed
if (!bChanged)
continue;
Reference< XTitled > xTitled( xAxis, uno::UNO_QUERY );
if (!xTitled.is())
continue;
Reference< beans::XPropertySet > xTitleProps( xTitled->getTitleObject(), uno::UNO_QUERY );
if (!xTitleProps.is())
continue;
double fAngleDegree = 0.0;
xTitleProps->getPropertyValue("TextRotation") >>= fAngleDegree;
if (fAngleDegree != 0.0 &&
!rtl::math::approxEqual(fAngleDegree, 90.0))
continue;
double fNewAngleDegree = 0.0;
if( !bVertical && nDimIndex == 1 )
fNewAngleDegree = 90.0;
else if( bVertical && nDimIndex == 0 )
fNewAngleDegree = 90.0;
xTitleProps->setPropertyValue("TextRotation", uno::Any(fNewAngleDegree));
}
}
}
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
}
bool DiagramHelper::getVertical( const uno::Reference< chart2::XDiagram > & xDiagram,
bool& rbFound, bool& rbAmbiguous )
{
bool bValue = false;
rbFound = false;
rbAmbiguous = false;
Reference< XCoordinateSystemContainer > xCnt( xDiagram, uno::UNO_QUERY );
if (!xCnt.is())
return false;
Sequence< Reference<XCoordinateSystem> > aCooSys = xCnt->getCoordinateSystems();
for (sal_Int32 i = 0; i < aCooSys.getLength(); ++i)
{
Reference<beans::XPropertySet> xProp(aCooSys[i], uno::UNO_QUERY);
if (!xProp.is())
continue;
bool bCurrent = false;
if (xProp->getPropertyValue("SwapXAndYAxis") >>= bCurrent)
{
if (!rbFound)
{
bValue = bCurrent;
rbFound = true;
}
else if (bCurrent != bValue)
{
// ambiguous -> choose always first found
rbAmbiguous = true;
}
}
}
return bValue;
}
void DiagramHelper::setStackMode(
const Reference< XDiagram > & xDiagram,
StackMode eStackMode
)
{
try
{
bool bValueFound = false;
bool bIsAmbiguous = false;
StackMode eOldStackMode = DiagramHelper::getStackMode( xDiagram, bValueFound, bIsAmbiguous );
if( eStackMode == eOldStackMode && !bIsAmbiguous )
return;
StackingDirection eNewDirection = StackingDirection_NO_STACKING;
if( eStackMode == StackMode::YStacked || eStackMode == StackMode::YStackedPercent )
eNewDirection = StackingDirection_Y_STACKING;
else if( eStackMode == StackMode::ZStacked )
eNewDirection = StackingDirection_Z_STACKING;
uno::Any aNewDirection( eNewDirection );
bool bPercent = false;
if( eStackMode == StackMode::YStackedPercent )
bPercent = true;
//iterate through all coordinate systems
uno::Reference< XCoordinateSystemContainer > xCooSysContainer( xDiagram, uno::UNO_QUERY );
if( !xCooSysContainer.is() )
return;
uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysList( xCooSysContainer->getCoordinateSystems() );
for( sal_Int32 nCS = 0; nCS < aCooSysList.getLength(); ++nCS )
{
uno::Reference< XCoordinateSystem > xCooSys( aCooSysList[nCS] );
//set correct percent stacking
const sal_Int32 nMaximumScaleIndex = xCooSys->getMaximumAxisIndexByDimension(1);
for(sal_Int32 nI=0; nI<=nMaximumScaleIndex; ++nI)
{
Reference< chart2::XAxis > xAxis( xCooSys->getAxisByDimension( 1,nI ));
if( xAxis.is())
{
chart2::ScaleData aScaleData = xAxis->getScaleData();
if( (aScaleData.AxisType==AxisType::PERCENT) != bPercent )
{
if( bPercent )
aScaleData.AxisType = AxisType::PERCENT;
else
aScaleData.AxisType = AxisType::REALNUMBER;
xAxis->setScaleData( aScaleData );
}
}
}
//iterate through all chart types in the current coordinate system
uno::Reference< XChartTypeContainer > xChartTypeContainer( xCooSys, uno::UNO_QUERY );
if( !xChartTypeContainer.is() )
continue;
uno::Sequence< uno::Reference< XChartType > > aChartTypeList( xChartTypeContainer->getChartTypes() );
if (!aChartTypeList.hasElements())
continue;
uno::Reference< XChartType > xChartType( aChartTypeList[0] );
//iterate through all series in this chart type
uno::Reference< XDataSeriesContainer > xDataSeriesContainer( xChartType, uno::UNO_QUERY );
OSL_ASSERT( xDataSeriesContainer.is());
if( !xDataSeriesContainer.is() )
continue;
uno::Sequence< uno::Reference< XDataSeries > > aSeriesList( xDataSeriesContainer->getDataSeries() );
for( sal_Int32 nS = 0; nS < aSeriesList.getLength(); ++nS )
{
Reference< beans::XPropertySet > xProp( aSeriesList[nS], uno::UNO_QUERY );
if(xProp.is())
xProp->setPropertyValue( "StackingDirection", aNewDirection );
}
}
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
}
StackMode DiagramHelper::getStackMode( const Reference< XDiagram > & xDiagram, bool& rbFound, bool& rbAmbiguous )
{
rbFound=false;
rbAmbiguous=false;
StackMode eGlobalStackMode = StackMode::NONE;
//iterate through all coordinate systems
uno::Reference< XCoordinateSystemContainer > xCooSysContainer( xDiagram, uno::UNO_QUERY );
if( !xCooSysContainer.is() )
return eGlobalStackMode;
uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysList( xCooSysContainer->getCoordinateSystems() );
for( sal_Int32 nCS = 0; nCS < aCooSysList.getLength(); ++nCS )
{
uno::Reference< XCoordinateSystem > xCooSys( aCooSysList[nCS] );
//iterate through all chart types in the current coordinate system
uno::Reference< XChartTypeContainer > xChartTypeContainer( xCooSys, uno::UNO_QUERY );
if( !xChartTypeContainer.is() )
continue;
uno::Sequence< uno::Reference< XChartType > > aChartTypeList( xChartTypeContainer->getChartTypes() );
for( sal_Int32 nT = 0; nT < aChartTypeList.getLength(); ++nT )
{
uno::Reference< XChartType > xChartType( aChartTypeList[nT] );
StackMode eLocalStackMode = DiagramHelper::getStackModeFromChartType(
xChartType, rbFound, rbAmbiguous, xCooSys );
if( rbFound && eLocalStackMode != eGlobalStackMode && nT>0 )
{
rbAmbiguous = true;
return eGlobalStackMode;
}
eGlobalStackMode = eLocalStackMode;
}
}
return eGlobalStackMode;
}
StackMode DiagramHelper::getStackModeFromChartType(
const Reference< XChartType > & xChartType,
bool& rbFound, bool& rbAmbiguous,
const Reference< XCoordinateSystem > & xCorrespondingCoordinateSystem )
{
StackMode eStackMode = StackMode::NONE;
rbFound = false;
rbAmbiguous = false;
try
{
Reference< XDataSeriesContainer > xDSCnt( xChartType, uno::UNO_QUERY_THROW );
Sequence< Reference< chart2::XDataSeries > > aSeries( xDSCnt->getDataSeries());
chart2::StackingDirection eCommonDirection = chart2::StackingDirection_NO_STACKING;
bool bDirectionInitialized = false;
// first series is irrelevant for stacking, start with second, unless
// there is only one series
const sal_Int32 nSeriesCount = aSeries.getLength();
sal_Int32 i = (nSeriesCount == 1) ? 0: 1;
for( ; i<nSeriesCount; ++i )
{
rbFound = true;
Reference< beans::XPropertySet > xProp( aSeries[i], uno::UNO_QUERY_THROW );
chart2::StackingDirection eCurrentDirection = eCommonDirection;
// property is not MAYBEVOID
bool bSuccess = ( xProp->getPropertyValue( "StackingDirection" ) >>= eCurrentDirection );
OSL_ASSERT( bSuccess );
if( ! bDirectionInitialized )
{
eCommonDirection = eCurrentDirection;
bDirectionInitialized = true;
}
else
{
if( eCommonDirection != eCurrentDirection )
{
rbAmbiguous = true;
break;
}
}
}
if( rbFound )
{
if( eCommonDirection == chart2::StackingDirection_Z_STACKING )
eStackMode = StackMode::ZStacked;
else if( eCommonDirection == chart2::StackingDirection_Y_STACKING )
{
eStackMode = StackMode::YStacked;
// percent stacking
if( xCorrespondingCoordinateSystem.is() )
{
if( 1 < xCorrespondingCoordinateSystem->getDimension() )
{
sal_Int32 nAxisIndex = 0;
if( nSeriesCount )
nAxisIndex = DataSeriesHelper::getAttachedAxisIndex(aSeries[0]);
Reference< chart2::XAxis > xAxis(
xCorrespondingCoordinateSystem->getAxisByDimension( 1,nAxisIndex ));
if( xAxis.is())
{
chart2::ScaleData aScaleData = xAxis->getScaleData();
if( aScaleData.AxisType==chart2::AxisType::PERCENT )
eStackMode = StackMode::YStackedPercent;
}
}
}
}
}
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
return eStackMode;
}
sal_Int32 DiagramHelper::getDimension( const Reference< XDiagram > & xDiagram )
{
// -1: not yet set
sal_Int32 nResult = -1;
try
{
Reference< XCoordinateSystemContainer > xCooSysCnt( xDiagram, uno::UNO_QUERY );
if( xCooSysCnt.is() )
{
Sequence< Reference< XCoordinateSystem > > aCooSysSeq(
xCooSysCnt->getCoordinateSystems());
for( sal_Int32 i=0; i<aCooSysSeq.getLength(); ++i )
{
Reference< XCoordinateSystem > xCooSys( aCooSysSeq[i] );
if(xCooSys.is())
{
nResult = xCooSys->getDimension();
break;
}
}
}
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
return nResult;
}
void DiagramHelper::setDimension(
const Reference< XDiagram > & xDiagram,
sal_Int32 nNewDimensionCount )
{
if( ! xDiagram.is())
return;
if( DiagramHelper::getDimension( xDiagram ) == nNewDimensionCount )
return;
try
{
bool rbFound = false;
bool rbAmbiguous = true;
StackMode eStackMode = DiagramHelper::getStackMode( xDiagram, rbFound, rbAmbiguous );
bool bIsSupportingOnlyDeepStackingFor3D=false;
//change all coordinate systems:
Reference< XCoordinateSystemContainer > xCooSysContainer( xDiagram, uno::UNO_QUERY_THROW );
Sequence< Reference< XCoordinateSystem > > aCooSysList( xCooSysContainer->getCoordinateSystems() );
for( sal_Int32 nCS = 0; nCS < aCooSysList.getLength(); ++nCS )
{
Reference< XCoordinateSystem > xOldCooSys( aCooSysList[nCS], uno::UNO_QUERY );
Reference< XCoordinateSystem > xNewCooSys;
Reference< XChartTypeContainer > xChartTypeContainer( xOldCooSys, uno::UNO_QUERY );
if( !xChartTypeContainer.is() )
continue;
Sequence< Reference< XChartType > > aChartTypeList( xChartTypeContainer->getChartTypes() );
for( sal_Int32 nT = 0; nT < aChartTypeList.getLength(); ++nT )
{
Reference< XChartType > xChartType( aChartTypeList[nT], uno::UNO_QUERY );
bIsSupportingOnlyDeepStackingFor3D = ChartTypeHelper::isSupportingOnlyDeepStackingFor3D( xChartType );
if(!xNewCooSys.is())
{
xNewCooSys = xChartType->createCoordinateSystem( nNewDimensionCount );
break;
}
//@todo make sure that all following charttypes are also capable of the new dimension
//otherwise separate them in a different group
//BM: might be done in replaceCoordinateSystem()
}
// replace the old coordinate system at all places where it was used
DiagramHelper::replaceCoordinateSystem( xDiagram, xOldCooSys, xNewCooSys );
}
//correct stack mode if necessary
if( nNewDimensionCount==3 && eStackMode != StackMode::ZStacked && bIsSupportingOnlyDeepStackingFor3D )
DiagramHelper::setStackMode( xDiagram, StackMode::ZStacked );
else if( nNewDimensionCount==2 && eStackMode == StackMode::ZStacked )
DiagramHelper::setStackMode( xDiagram, StackMode::NONE );
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
}
void DiagramHelper::replaceCoordinateSystem(
const Reference< XDiagram > & xDiagram,
const Reference< XCoordinateSystem > & xCooSysToReplace,
const Reference< XCoordinateSystem > & xReplacement )
{
OSL_ASSERT( xDiagram.is());
if( ! xDiagram.is())
return;
// update the coordinate-system container
Reference< XCoordinateSystemContainer > xCont( xDiagram, uno::UNO_QUERY );
if( xCont.is())
{
try
{
Reference< chart2::data::XLabeledDataSequence > xCategories = DiagramHelper::getCategoriesFromDiagram( xDiagram );
// move chart types of xCooSysToReplace to xReplacement
Reference< XChartTypeContainer > xCTCntCooSys( xCooSysToReplace, uno::UNO_QUERY_THROW );
Reference< XChartTypeContainer > xCTCntReplacement( xReplacement, uno::UNO_QUERY_THROW );
xCTCntReplacement->setChartTypes( xCTCntCooSys->getChartTypes());
xCont->removeCoordinateSystem( xCooSysToReplace );
xCont->addCoordinateSystem( xReplacement );
if( xCategories.is() )
DiagramHelper::setCategoriesToDiagram( xCategories, xDiagram );
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
}
}
bool DiagramHelper::isSeriesAttachedToMainAxis(
const uno::Reference< chart2::XDataSeries >& xDataSeries )
{
sal_Int32 nAxisIndex = DataSeriesHelper::getAttachedAxisIndex(xDataSeries);
return (nAxisIndex==0);
}
bool DiagramHelper::attachSeriesToAxis( bool bAttachToMainAxis
, const uno::Reference< chart2::XDataSeries >& xDataSeries
, const uno::Reference< chart2::XDiagram >& xDiagram
, const uno::Reference< uno::XComponentContext > & xContext
, bool bAdaptAxes )
{
bool bChanged = false;
//set property at axis
Reference< beans::XPropertySet > xProp( xDataSeries, uno::UNO_QUERY_THROW );
sal_Int32 nNewAxisIndex = bAttachToMainAxis ? 0 : 1;
sal_Int32 nOldAxisIndex = DataSeriesHelper::getAttachedAxisIndex(xDataSeries);
uno::Reference< chart2::XAxis > xOldAxis( DiagramHelper::getAttachedAxis( xDataSeries, xDiagram ) );
if( nOldAxisIndex != nNewAxisIndex )
{
try
{
xProp->setPropertyValue( "AttachedAxisIndex", uno::Any( nNewAxisIndex ) );
bChanged = true;
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
}
if( bChanged && xDiagram.is() )
{
uno::Reference< XAxis > xAxis( AxisHelper::getAxis( 1, bAttachToMainAxis, xDiagram ) );
if(!xAxis.is()) //create an axis if necessary
xAxis = AxisHelper::createAxis( 1, bAttachToMainAxis, xDiagram, xContext );
if( bAdaptAxes )
{
AxisHelper::makeAxisVisible( xAxis );
AxisHelper::hideAxisIfNoDataIsAttached( xOldAxis, xDiagram );
}
}
return bChanged;
}
uno::Reference< XAxis > DiagramHelper::getAttachedAxis(
const uno::Reference< XDataSeries >& xSeries,
const uno::Reference< XDiagram >& xDiagram )
{
return AxisHelper::getAxis( 1, DiagramHelper::isSeriesAttachedToMainAxis( xSeries ), xDiagram );
}
uno::Reference< XChartType > DiagramHelper::getChartTypeOfSeries(
const uno::Reference< chart2::XDiagram >& xDiagram
, const uno::Reference< XDataSeries >& xGivenDataSeries )
{
if( !xGivenDataSeries.is() )
return nullptr;
if(!xDiagram.is())
return nullptr;
//iterate through the model to find the given xSeries
//the found parent indicates the charttype
//iterate through all coordinate systems
uno::Reference< XCoordinateSystemContainer > xCooSysContainer( xDiagram, uno::UNO_QUERY );
if( !xCooSysContainer.is())
return nullptr;
uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysList( xCooSysContainer->getCoordinateSystems() );
for( sal_Int32 nCS = 0; nCS < aCooSysList.getLength(); ++nCS )
{
uno::Reference< XCoordinateSystem > xCooSys( aCooSysList[nCS] );
//iterate through all chart types in the current coordinate system
uno::Reference< XChartTypeContainer > xChartTypeContainer( xCooSys, uno::UNO_QUERY );
OSL_ASSERT( xChartTypeContainer.is());
if( !xChartTypeContainer.is() )
continue;
uno::Sequence< uno::Reference< XChartType > > aChartTypeList( xChartTypeContainer->getChartTypes() );
for( sal_Int32 nT = 0; nT < aChartTypeList.getLength(); ++nT )
{
uno::Reference< XChartType > xChartType( aChartTypeList[nT] );
//iterate through all series in this chart type
uno::Reference< XDataSeriesContainer > xDataSeriesContainer( xChartType, uno::UNO_QUERY );
OSL_ASSERT( xDataSeriesContainer.is());
if( !xDataSeriesContainer.is() )
continue;
uno::Sequence< uno::Reference< XDataSeries > > aSeriesList( xDataSeriesContainer->getDataSeries() );
for( sal_Int32 nS = 0; nS < aSeriesList.getLength(); ++nS )
{
if( xGivenDataSeries==aSeriesList[nS] )
return xChartType;
}
}
}
return nullptr;
}
std::vector< Reference< XDataSeries > >
DiagramHelper::getDataSeriesFromDiagram(
const Reference< XDiagram > & xDiagram )
{
std::vector< Reference< XDataSeries > > aResult;
try
{
Reference< XCoordinateSystemContainer > xCooSysCnt(
xDiagram, uno::UNO_QUERY_THROW );
Sequence< Reference< XCoordinateSystem > > aCooSysSeq(
xCooSysCnt->getCoordinateSystems());
for( sal_Int32 i=0; i<aCooSysSeq.getLength(); ++i )
{
Reference< XChartTypeContainer > xCTCnt( aCooSysSeq[i], uno::UNO_QUERY_THROW );
Sequence< Reference< XChartType > > aChartTypeSeq( xCTCnt->getChartTypes());
for( sal_Int32 j=0; j<aChartTypeSeq.getLength(); ++j )
{
Reference< XDataSeriesContainer > xDSCnt( aChartTypeSeq[j], uno::UNO_QUERY_THROW );
Sequence< Reference< XDataSeries > > aSeriesSeq( xDSCnt->getDataSeries() );
std::copy( aSeriesSeq.begin(), aSeriesSeq.end(),
std::back_inserter( aResult ));
}
}
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
return aResult;
}
Sequence< Sequence< Reference< XDataSeries > > >
DiagramHelper::getDataSeriesGroups( const Reference< XDiagram > & xDiagram )
{
vector< Sequence< Reference< XDataSeries > > > aResult;
//iterate through all coordinate systems
Reference< XCoordinateSystemContainer > xCooSysContainer( xDiagram, uno::UNO_QUERY );
if( xCooSysContainer.is() )
{
Sequence< Reference< XCoordinateSystem > > aCooSysList( xCooSysContainer->getCoordinateSystems() );
for( sal_Int32 nCS = 0; nCS < aCooSysList.getLength(); ++nCS )
{
//iterate through all chart types in the current coordinate system
Reference< XChartTypeContainer > xChartTypeContainer( aCooSysList[nCS], uno::UNO_QUERY );
if( !xChartTypeContainer.is() )
continue;
Sequence< Reference< XChartType > > aChartTypeList( xChartTypeContainer->getChartTypes() );
for( sal_Int32 nT = 0; nT < aChartTypeList.getLength(); ++nT )
{
Reference< XDataSeriesContainer > xDataSeriesContainer( aChartTypeList[nT], uno::UNO_QUERY );
if( !xDataSeriesContainer.is() )
continue;
aResult.push_back( xDataSeriesContainer->getDataSeries() );
}
}
}
return comphelper::containerToSequence( aResult );
}
Reference< XChartType >
DiagramHelper::getChartTypeByIndex( const Reference< XDiagram >& xDiagram, sal_Int32 nIndex )
{
Reference< XChartType > xChartType;
//iterate through all coordinate systems
Reference< XCoordinateSystemContainer > xCooSysContainer( xDiagram, uno::UNO_QUERY );
if( ! xCooSysContainer.is())
return xChartType;
Sequence< Reference< XCoordinateSystem > > aCooSysList( xCooSysContainer->getCoordinateSystems() );
sal_Int32 nTypesSoFar = 0;
for( sal_Int32 nCS = 0; nCS < aCooSysList.getLength(); ++nCS )
{
Reference< XChartTypeContainer > xChartTypeContainer( aCooSysList[nCS], uno::UNO_QUERY );
if( !xChartTypeContainer.is() )
continue;
Sequence< Reference< XChartType > > aChartTypeList( xChartTypeContainer->getChartTypes() );
if( nIndex >= 0 && nIndex < (nTypesSoFar + aChartTypeList.getLength()) )
{
xChartType.set( aChartTypeList[nIndex - nTypesSoFar] );
break;
}
nTypesSoFar += aChartTypeList.getLength();
}
return xChartType;
}
namespace
{
std::vector< Reference< XAxis > > lcl_getAxisHoldingCategoriesFromDiagram(
const Reference< XDiagram > & xDiagram )
{
std::vector< Reference< XAxis > > aRet;
// return first x-axis as fall-back
Reference< XAxis > xFallBack;
try
{
Reference< XCoordinateSystemContainer > xCooSysCnt(
xDiagram, uno::UNO_QUERY_THROW );
Sequence< Reference< XCoordinateSystem > > aCooSysSeq(
xCooSysCnt->getCoordinateSystems());
for( sal_Int32 i=0; i<aCooSysSeq.getLength(); ++i )
{
Reference< XCoordinateSystem > xCooSys( aCooSysSeq[i] );
OSL_ASSERT( xCooSys.is());
for( sal_Int32 nN = xCooSys->getDimension(); nN--; )
{
const sal_Int32 nMaximumScaleIndex = xCooSys->getMaximumAxisIndexByDimension(nN);
for(sal_Int32 nI=0; nI<=nMaximumScaleIndex; ++nI)
{
Reference< XAxis > xAxis = xCooSys->getAxisByDimension( nN,nI );
OSL_ASSERT( xAxis.is());
if( xAxis.is())
{
ScaleData aScaleData = xAxis->getScaleData();
if( aScaleData.Categories.is() || (aScaleData.AxisType == AxisType::CATEGORY) )
{
aRet.push_back(xAxis);
}
if( (nN == 0) && !xFallBack.is())
xFallBack.set( xAxis );
}
}
}
}
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2" );
}
if( aRet.empty() )
aRet.push_back(xFallBack);
return aRet;
}
} // anonymous namespace
bool DiagramHelper::isCategoryDiagram(
const Reference< XDiagram >& xDiagram )
{
try
{
Reference< XCoordinateSystemContainer > xCooSysCnt(
xDiagram, uno::UNO_QUERY_THROW );
Sequence< Reference< XCoordinateSystem > > aCooSysSeq(
xCooSysCnt->getCoordinateSystems());
for( sal_Int32 i=0; i<aCooSysSeq.getLength(); ++i )
{
Reference< XCoordinateSystem > xCooSys( aCooSysSeq[i] );
OSL_ASSERT( xCooSys.is());
for( sal_Int32 nN = xCooSys->getDimension(); nN--; )
{
const sal_Int32 nMaximumScaleIndex = xCooSys->getMaximumAxisIndexByDimension(nN);
for(sal_Int32 nI=0; nI<=nMaximumScaleIndex; ++nI)
{
Reference< XAxis > xAxis = xCooSys->getAxisByDimension( nN,nI );
OSL_ASSERT( xAxis.is());
if( xAxis.is())
{
ScaleData aScaleData = xAxis->getScaleData();
if( aScaleData.AxisType == AxisType::CATEGORY || aScaleData.AxisType == AxisType::DATE )
return true;
}
}
}
}
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
return false;
}
void DiagramHelper::setCategoriesToDiagram(
const Reference< chart2::data::XLabeledDataSequence >& xCategories,
const Reference< XDiagram >& xDiagram,
bool bSetAxisType /* = false */,
bool bCategoryAxis /* = true */ )
{
std::vector< Reference< chart2::XAxis > > aCatAxes(
lcl_getAxisHoldingCategoriesFromDiagram( xDiagram ));
for (const Reference< chart2::XAxis >& xCatAxis : aCatAxes)
{
if( xCatAxis.is())
{
ScaleData aScaleData( xCatAxis->getScaleData());
aScaleData.Categories = xCategories;
if( bSetAxisType )
{
if( bCategoryAxis )
aScaleData.AxisType = AxisType::CATEGORY;
else if( aScaleData.AxisType == AxisType::CATEGORY || aScaleData.AxisType == AxisType::DATE )
aScaleData.AxisType = AxisType::REALNUMBER;
}
xCatAxis->setScaleData( aScaleData );
}
}
}
Reference< data::XLabeledDataSequence >
DiagramHelper::getCategoriesFromDiagram(
const Reference< XDiagram > & xDiagram )
{
Reference< data::XLabeledDataSequence > xResult;
try
{
std::vector< Reference< chart2::XAxis > > aCatAxes(
lcl_getAxisHoldingCategoriesFromDiagram( xDiagram ));
//search for first categories
if (!aCatAxes.empty())
{
Reference< chart2::XAxis > xCatAxis(aCatAxes[0]);
if( xCatAxis.is())
{
ScaleData aScaleData( xCatAxis->getScaleData());
if( aScaleData.Categories.is() )
{
xResult.set( aScaleData.Categories );
uno::Reference<beans::XPropertySet> xProp(aScaleData.Categories->getValues(), uno::UNO_QUERY );
if( xProp.is() )
{
try
{
xProp->setPropertyValue( "Role", uno::Any( OUString("categories") ) );
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
}
}
}
}
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
return xResult;
}
static void lcl_generateAutomaticCategoriesFromChartType(
Sequence< OUString >& rRet,
const Reference< XChartType >& xChartType )
{
if(!xChartType.is())
return;
OUString aMainSeq( xChartType->getRoleOfSequenceForSeriesLabel() );
Reference< XDataSeriesContainer > xSeriesCnt( xChartType, uno::UNO_QUERY );
if( xSeriesCnt.is() )
{
Sequence< Reference< XDataSeries > > aSeriesSeq( xSeriesCnt->getDataSeries() );
for( sal_Int32 nS = 0; nS < aSeriesSeq.getLength(); nS++ )
{
Reference< data::XDataSource > xDataSource( aSeriesSeq[nS], uno::UNO_QUERY );
if( !xDataSource.is() )
continue;
Reference< chart2::data::XLabeledDataSequence > xLabeledSeq(
::chart::DataSeriesHelper::getDataSequenceByRole( xDataSource, aMainSeq ));
if( !xLabeledSeq.is() )
continue;
Reference< chart2::data::XDataSequence > xValueSeq( xLabeledSeq->getValues() );
if( !xValueSeq.is() )
continue;
rRet = xValueSeq->generateLabel( chart2::data::LabelOrigin_LONG_SIDE );
if( rRet.hasElements() )
return;
}
}
}
Sequence< OUString > DiagramHelper::generateAutomaticCategoriesFromCooSys( const Reference< XCoordinateSystem > & xCooSys )
{
Sequence< OUString > aRet;
Reference< XChartTypeContainer > xTypeCntr( xCooSys, uno::UNO_QUERY );
if( xTypeCntr.is() )
{
Sequence< Reference< XChartType > > aChartTypes( xTypeCntr->getChartTypes() );
for( sal_Int32 nN=0; nN<aChartTypes.getLength(); nN++ )
{
lcl_generateAutomaticCategoriesFromChartType( aRet, aChartTypes[nN] );
if( aRet.hasElements() )
return aRet;
}
}
return aRet;
}
Sequence< OUString > DiagramHelper::getExplicitSimpleCategories(
ChartModel& rModel )
{
uno::Reference< chart2::XCoordinateSystem > xCooSys( ChartModelHelper::getFirstCoordinateSystem( rModel ) );
ExplicitCategoriesProvider aExplicitCategoriesProvider( xCooSys, rModel );
return aExplicitCategoriesProvider.getSimpleCategories();
}
namespace
{
void lcl_switchToDateCategories( const Reference< XChartDocument >& xChartDoc, const Reference< XAxis >& xAxis )
{
if( !xAxis.is() )
return;
if( !xChartDoc.is() )
return;
ScaleData aScale( xAxis->getScaleData() );
if( xChartDoc->hasInternalDataProvider() )
{
//remove all content the is not of type double and remove multiple level
Reference< XAnyDescriptionAccess > xDataAccess( xChartDoc->getDataProvider(), uno::UNO_QUERY );
if( xDataAccess.is() )
{
Sequence< Sequence< Any > > aAnyCategories( xDataAccess->getAnyRowDescriptions() );
double fTest = 0.0;
double fNan = 0.0;
::rtl::math::setNan( & fNan );
sal_Int32 nN = aAnyCategories.getLength();
for( ; nN--; )
{
Sequence< Any >& rCat = aAnyCategories[nN];
if( rCat.getLength() > 1 )
rCat.realloc(1);
if( rCat.getLength() == 1 )
{
Any& rAny = rCat[0];
if( !(rAny>>=fTest) )
{
rAny <<= fNan;
}
}
}
xDataAccess->setAnyRowDescriptions( aAnyCategories );
}
//check the numberformat at the axis
Reference< beans::XPropertySet > xAxisProps( xAxis, uno::UNO_QUERY );
Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( xChartDoc, uno::UNO_QUERY );
if( xAxisProps.is() && xNumberFormatsSupplier.is() )
{
sal_Int32 nNumberFormat = -1;
xAxisProps->getPropertyValue(CHART_UNONAME_NUMFMT) >>= nNumberFormat;
Reference< util::XNumberFormats > xNumberFormats( xNumberFormatsSupplier->getNumberFormats() );
if( xNumberFormats.is() )
{
Reference< beans::XPropertySet > xKeyProps;
try
{
xKeyProps = xNumberFormats->getByKey( nNumberFormat );
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
sal_Int32 nType = util::NumberFormat::UNDEFINED;
if( xKeyProps.is() )
xKeyProps->getPropertyValue( "Type" ) >>= nType;
if( !( nType & util::NumberFormat::DATE ) )
{
//set a date format to the axis
const LocaleDataWrapper& rLocaleDataWrapper = Application::GetSettings().GetLocaleDataWrapper();
Sequence<sal_Int32> aKeySeq = xNumberFormats->queryKeys( util::NumberFormat::DATE, rLocaleDataWrapper.getLanguageTag().getLocale(), true/*bCreate*/ );
if( aKeySeq.hasElements() )
{
xAxisProps->setPropertyValue(CHART_UNONAME_NUMFMT, uno::Any(aKeySeq[0]));
}
}
}
}
}
if( aScale.AxisType != chart2::AxisType::DATE )
AxisHelper::removeExplicitScaling( aScale );
aScale.AxisType = chart2::AxisType::DATE;
xAxis->setScaleData( aScale );
}
void lcl_switchToTextCategories( const Reference< XChartDocument >& xChartDoc, const Reference< XAxis >& xAxis )
{
if( !xAxis.is() )
return;
if( !xChartDoc.is() )
return;
ScaleData aScale( xAxis->getScaleData() );
if( aScale.AxisType != chart2::AxisType::CATEGORY )
AxisHelper::removeExplicitScaling( aScale );
//todo migrate dates to text?
aScale.AxisType = chart2::AxisType::CATEGORY;
aScale.AutoDateAxis = false;
xAxis->setScaleData( aScale );
}
}
void DiagramHelper::switchToDateCategories( const Reference< XChartDocument >& xChartDoc )
{
if(xChartDoc.is())
{
ControllerLockGuardUNO aCtrlLockGuard( xChartDoc );
Reference< chart2::XCoordinateSystem > xCooSys( ChartModelHelper::getFirstCoordinateSystem( xChartDoc ) );
if( xCooSys.is() )
{
Reference< XAxis > xAxis( xCooSys->getAxisByDimension(0,0) );
lcl_switchToDateCategories( xChartDoc, xAxis );
}
}
}
void DiagramHelper::switchToTextCategories( const Reference< XChartDocument >& xChartDoc )
{
if(xChartDoc.is())
{
ControllerLockGuardUNO aCtrlLockGuard( xChartDoc );
Reference< chart2::XCoordinateSystem > xCooSys( ChartModelHelper::getFirstCoordinateSystem( xChartDoc ) );
if( xCooSys.is() )
{
Reference< XAxis > xAxis( xCooSys->getAxisByDimension(0,0) );
lcl_switchToTextCategories( xChartDoc, xAxis );
}
}
}
bool DiagramHelper::isSupportingDateAxis( const Reference< chart2::XDiagram >& xDiagram )
{
return ::chart::ChartTypeHelper::isSupportingDateAxis(
DiagramHelper::getChartTypeByIndex( xDiagram, 0 ), 0 );
}
bool DiagramHelper::isDateNumberFormat( sal_Int32 nNumberFormat, const Reference< util::XNumberFormats >& xNumberFormats )
{
bool bIsDate = false;
if( !xNumberFormats.is() )
return bIsDate;
Reference< beans::XPropertySet > xKeyProps = xNumberFormats->getByKey( nNumberFormat );
if( xKeyProps.is() )
{
sal_Int32 nType = util::NumberFormat::UNDEFINED;
xKeyProps->getPropertyValue( "Type" ) >>= nType;
bIsDate = nType & util::NumberFormat::DATE;
}
return bIsDate;
}
sal_Int32 DiagramHelper::getDateNumberFormat( const Reference< util::XNumberFormatsSupplier >& xNumberFormatsSupplier )
{
sal_Int32 nRet=-1;
//try to get a date format with full year display
const LanguageTag& rLanguageTag = Application::GetSettings().GetLanguageTag();
NumberFormatterWrapper aNumberFormatterWrapper( xNumberFormatsSupplier );
SvNumberFormatter* pNumFormatter = aNumberFormatterWrapper.getSvNumberFormatter();
if( pNumFormatter )
{
nRet = pNumFormatter->GetFormatIndex( NF_DATE_SYS_DDMMYYYY, rLanguageTag.getLanguageType() );
}
else
{
Reference< util::XNumberFormats > xNumberFormats( xNumberFormatsSupplier->getNumberFormats() );
if( xNumberFormats.is() )
{
Sequence<sal_Int32> aKeySeq = xNumberFormats->queryKeys( util::NumberFormat::DATE,
rLanguageTag.getLocale(), true/*bCreate */);
if( aKeySeq.hasElements() )
{
nRet = aKeySeq[0];
}
}
}
return nRet;
}
sal_Int32 DiagramHelper::getDateTimeInputNumberFormat( const Reference< util::XNumberFormatsSupplier >& xNumberFormatsSupplier, double fNumber )
{
sal_Int32 nRet = 0;
// Get the most detailed date/time format according to fNumber.
NumberFormatterWrapper aNumberFormatterWrapper( xNumberFormatsSupplier );
SvNumberFormatter* pNumFormatter = aNumberFormatterWrapper.getSvNumberFormatter();
if (!pNumFormatter)
SAL_WARN("chart2", "DiagramHelper::getDateTimeInputNumberFormat - no SvNumberFormatter");
else
{
SvNumFormatType nType;
// Obtain best matching date, time or datetime format.
nRet = pNumFormatter->GuessDateTimeFormat( nType, fNumber, LANGUAGE_SYSTEM);
// Obtain the corresponding edit format.
nRet = pNumFormatter->GetEditFormat( fNumber, nRet, nType, LANGUAGE_SYSTEM, nullptr);
}
return nRet;
}
sal_Int32 DiagramHelper::getPercentNumberFormat( const Reference< util::XNumberFormatsSupplier >& xNumberFormatsSupplier )
{
sal_Int32 nRet=-1;
const LanguageTag& rLanguageTag = Application::GetSettings().GetLanguageTag();
NumberFormatterWrapper aNumberFormatterWrapper( xNumberFormatsSupplier );
SvNumberFormatter* pNumFormatter = aNumberFormatterWrapper.getSvNumberFormatter();
if( pNumFormatter )
{
nRet = pNumFormatter->GetFormatIndex( NF_PERCENT_INT, rLanguageTag.getLanguageType() );
}
else
{
Reference< util::XNumberFormats > xNumberFormats( xNumberFormatsSupplier->getNumberFormats() );
if( xNumberFormats.is() )
{
Sequence<sal_Int32> aKeySeq = xNumberFormats->queryKeys( util::NumberFormat::PERCENT,
rLanguageTag.getLocale(), true/*bCreate*/ );
if( aKeySeq.hasElements() )
{
// This *assumes* the sequence is sorted as in
// NfIndexTableOffset and the first format is the integer 0%
// format by chance... which usually is the case, but... anyway,
// we usually also have a number formatter so don't reach here.
nRet = aKeySeq[0];
}
}
}
return nRet;
}
Sequence< Reference< XChartType > >
DiagramHelper::getChartTypesFromDiagram(
const Reference< XDiagram > & xDiagram )
{
std::vector< Reference< XChartType > > aResult;
if(xDiagram.is())
{
try
{
Reference< XCoordinateSystemContainer > xCooSysCnt(
xDiagram, uno::UNO_QUERY_THROW );
Sequence< Reference< XCoordinateSystem > > aCooSysSeq(
xCooSysCnt->getCoordinateSystems());
for( sal_Int32 i=0; i<aCooSysSeq.getLength(); ++i )
{
Reference< XChartTypeContainer > xCTCnt( aCooSysSeq[i], uno::UNO_QUERY_THROW );
Sequence< Reference< XChartType > > aChartTypeSeq( xCTCnt->getChartTypes());
std::copy( aChartTypeSeq.begin(), aChartTypeSeq.end(),
std::back_inserter( aResult ));
}
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
}
return comphelper::containerToSequence( aResult );
}
bool DiagramHelper::areChartTypesCompatible( const Reference< ::chart2::XChartType >& xFirstType,
const Reference< ::chart2::XChartType >& xSecondType )
{
if( !xFirstType.is() || !xSecondType.is() )
return false;
auto aFirstRoles( comphelper::sequenceToContainer<std::vector< OUString >>( xFirstType->getSupportedMandatoryRoles() ) );
auto aSecondRoles( comphelper::sequenceToContainer<std::vector< OUString >>( xSecondType->getSupportedMandatoryRoles() ) );
std::sort( aFirstRoles.begin(), aFirstRoles.end() );
std::sort( aSecondRoles.begin(), aSecondRoles.end() );
return ( aFirstRoles == aSecondRoles );
}
namespace
{
/**
* This method implements the logic of checking if a series can be moved
* forward/backward. Depending on the "bDoMove" parameter the series will
* be moved (bDoMove = true) or the function just will test if the
* series can be moved without doing the move (bDoMove = false).
*
* @param xDiagram
* Reference to the diagram that contains the series.
*
* @param xGivenDataSeries
* Reference to the series that should moved or tested for moving.
*
* @param bForward
* Direction in which the series should be moved or tested for moving.
*
* @param bDoMove
* Should this function really move the series (true) or just test if it is
* possible (false).
*
*
* @returns
* in case of bDoMove == true
* - True : if the move was done
* - False : the move failed
* in case of bDoMove == false
* - True : the series can be moved
* - False : the series can not be moved
*
*/
bool lcl_moveSeriesOrCheckIfMoveIsAllowed(
const Reference< XDiagram >& xDiagram,
const Reference< XDataSeries >& xGivenDataSeries,
bool bForward,
bool bDoMove )
{
bool bMovedOrMoveAllowed = false;
try
{
uno::Reference< XCoordinateSystemContainer > xCooSysContainer( xDiagram, uno::UNO_QUERY );
if( xGivenDataSeries.is() && xCooSysContainer.is() )
{
//find position of series.
bool bFound = false;
uno::Sequence< uno::Reference< XCoordinateSystem > > aCooSysList( xCooSysContainer->getCoordinateSystems() );
for( sal_Int32 nCS = 0; !bFound && nCS < aCooSysList.getLength(); ++nCS )
{
uno::Reference< XCoordinateSystem > xCooSys( aCooSysList[nCS] );
//iterate through all chart types in the current coordinate system
uno::Reference< XChartTypeContainer > xChartTypeContainer( xCooSys, uno::UNO_QUERY );
OSL_ASSERT( xChartTypeContainer.is());
if( !xChartTypeContainer.is() )
continue;
uno::Sequence< uno::Reference< XChartType > > aChartTypeList( xChartTypeContainer->getChartTypes() );
uno::Reference< XChartType > xFormerChartType;
for( sal_Int32 nT = 0; !bFound && nT < aChartTypeList.getLength(); ++nT )
{
uno::Reference< XChartType > xCurrentChartType( aChartTypeList[nT] );
//iterate through all series in this chart type
uno::Reference< XDataSeriesContainer > xDataSeriesContainer( xCurrentChartType, uno::UNO_QUERY );
OSL_ASSERT( xDataSeriesContainer.is());
if( !xDataSeriesContainer.is() )
continue;
uno::Sequence< uno::Reference< XDataSeries > > aSeriesList( xDataSeriesContainer->getDataSeries() );
for( sal_Int32 nS = 0; !bFound && nS < aSeriesList.getLength(); ++nS )
{
// We found the series we are interested in!
if( xGivenDataSeries==aSeriesList[nS] )
{
sal_Int32 nOldSeriesIndex = nS;
bFound = true;
try
{
sal_Int32 nNewSeriesIndex = nS;
if( bForward )
nNewSeriesIndex--;
else
nNewSeriesIndex++;
if( nNewSeriesIndex >= 0 && nNewSeriesIndex < aSeriesList.getLength() )
{
//move series in the same charttype
bMovedOrMoveAllowed = true;
if( bDoMove )
{
aSeriesList[ nOldSeriesIndex ] = aSeriesList[ nNewSeriesIndex ];
aSeriesList[ nNewSeriesIndex ] = xGivenDataSeries;
xDataSeriesContainer->setDataSeries( aSeriesList );
}
}
else if( nNewSeriesIndex<0 )
{
//exchange series with former charttype
if( xFormerChartType.is() && DiagramHelper::areChartTypesCompatible( xFormerChartType, xCurrentChartType ) )
{
bMovedOrMoveAllowed = true;
if( bDoMove )
{
uno::Reference< XDataSeriesContainer > xOtherDataSeriesContainer( xFormerChartType, uno::UNO_QUERY );
if( xOtherDataSeriesContainer.is() )
{
uno::Sequence< uno::Reference< XDataSeries > > aOtherSeriesList( xOtherDataSeriesContainer->getDataSeries() );
sal_Int32 nOtherSeriesIndex = aOtherSeriesList.getLength()-1;
if( nOtherSeriesIndex >= 0 && nOtherSeriesIndex < aOtherSeriesList.getLength() )
{
uno::Reference< XDataSeries > xExchangeSeries( aOtherSeriesList[nOtherSeriesIndex] );
aOtherSeriesList[nOtherSeriesIndex] = xGivenDataSeries;
xOtherDataSeriesContainer->setDataSeries(aOtherSeriesList);
aSeriesList[nOldSeriesIndex]=xExchangeSeries;
xDataSeriesContainer->setDataSeries(aSeriesList);
}
}
}
}
}
else if( nT+1 < aChartTypeList.getLength() )
{
//exchange series with next charttype
uno::Reference< XChartType > xOtherChartType( aChartTypeList[nT+1] );
if( xOtherChartType.is() && DiagramHelper::areChartTypesCompatible( xOtherChartType, xCurrentChartType ) )
{
bMovedOrMoveAllowed = true;
if( bDoMove )
{
uno::Reference< XDataSeriesContainer > xOtherDataSeriesContainer( xOtherChartType, uno::UNO_QUERY );
if( xOtherDataSeriesContainer.is() )
{
uno::Sequence< uno::Reference< XDataSeries > > aOtherSeriesList( xOtherDataSeriesContainer->getDataSeries() );
if( aOtherSeriesList.hasElements() )
{
uno::Reference< XDataSeries > xExchangeSeries( aOtherSeriesList[0] );
aOtherSeriesList[0] = xGivenDataSeries;
xOtherDataSeriesContainer->setDataSeries(aOtherSeriesList);
aSeriesList[nOldSeriesIndex]=xExchangeSeries;
xDataSeriesContainer->setDataSeries(aSeriesList);
}
}
}
}
}
}
catch( const util::CloseVetoException& )
{
}
catch( const uno::RuntimeException& )
{
}
}
}
xFormerChartType = xCurrentChartType;
}
}
}
}
catch( const util::CloseVetoException& )
{
}
catch( const uno::RuntimeException& )
{
}
return bMovedOrMoveAllowed;
}
} // anonymous namespace
bool DiagramHelper::isSeriesMoveable(
const Reference< XDiagram >& xDiagram,
const Reference< XDataSeries >& xGivenDataSeries,
bool bForward )
{
const bool bDoMove = false;
bool bIsMoveable = lcl_moveSeriesOrCheckIfMoveIsAllowed(
xDiagram, xGivenDataSeries, bForward, bDoMove );
return bIsMoveable;
}
bool DiagramHelper::moveSeries( const Reference< XDiagram >& xDiagram, const Reference< XDataSeries >& xGivenDataSeries, bool bForward )
{
const bool bDoMove = true;
bool bMoved = lcl_moveSeriesOrCheckIfMoveIsAllowed(
xDiagram, xGivenDataSeries, bForward, bDoMove );
return bMoved;
}
bool DiagramHelper::isSupportingFloorAndWall( const Reference<
chart2::XDiagram >& xDiagram )
{
//pies and donuts currently do not support this because of wrong files from older versions
//todo: allow this in future again, if fileversion are available for ole objects (metastream)
//thus the wrong bottom can be removed on import
Sequence< Reference< chart2::XChartType > > aTypes(
::chart::DiagramHelper::getChartTypesFromDiagram( xDiagram ) );
for( sal_Int32 nN = 0; nN < aTypes.getLength(); nN++ )
{
Reference< chart2::XChartType > xType( aTypes[nN] );
if( xType.is() && xType->getChartType().match(CHART2_SERVICE_NAME_CHARTTYPE_PIE) )
return false;
if( xType.is() && xType->getChartType().match(CHART2_SERVICE_NAME_CHARTTYPE_NET) )
return false;
if( xType.is() && xType->getChartType().match(CHART2_SERVICE_NAME_CHARTTYPE_FILLED_NET) )
return false;
}
return true;
}
bool DiagramHelper::isPieOrDonutChart( const css::uno::Reference< css::chart2::XDiagram >& xDiagram )
{
uno::Reference< chart2::XChartType > xChartType( DiagramHelper::getChartTypeByIndex(
xDiagram, 0 ) );
if( xChartType .is() )
{
OUString aChartType = xChartType->getChartType();
if( aChartType == CHART2_SERVICE_NAME_CHARTTYPE_PIE )
return true;
}
return false;
}
sal_Int32 DiagramHelper::getGeometry3D(
const uno::Reference< chart2::XDiagram > & xDiagram,
bool& rbFound, bool& rbAmbiguous )
{
sal_Int32 nCommonGeom( DataPointGeometry3D::CUBOID );
rbFound = false;
rbAmbiguous = false;
std::vector< Reference< chart2::XDataSeries > > aSeriesVec(
DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
if( aSeriesVec.empty())
rbAmbiguous = true;
for (auto const& series : aSeriesVec)
{
try
{
sal_Int32 nGeom = 0;
Reference< beans::XPropertySet > xProp(series, uno::UNO_QUERY_THROW);
if( xProp->getPropertyValue( "Geometry3D") >>= nGeom )
{
if( ! rbFound )
{
// first series
nCommonGeom = nGeom;
rbFound = true;
}
// further series: compare for uniqueness
else if( nCommonGeom != nGeom )
{
rbAmbiguous = true;
break;
}
}
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
}
return nCommonGeom;
}
void DiagramHelper::setGeometry3D(
const Reference< chart2::XDiagram > & xDiagram,
sal_Int32 nNewGeometry )
{
std::vector< Reference< chart2::XDataSeries > > aSeriesVec(
DiagramHelper::getDataSeriesFromDiagram( xDiagram ));
for (auto const& series : aSeriesVec)
{
DataSeriesHelper::setPropertyAlsoToAllAttributedDataPoints(
series, "Geometry3D", uno::Any( nNewGeometry ));
}
}
sal_Int32 DiagramHelper::getCorrectedMissingValueTreatment(
const Reference< chart2::XDiagram > & xDiagram,
const Reference< chart2::XChartType >& xChartType )
{
sal_Int32 nResult = css::chart::MissingValueTreatment::LEAVE_GAP;
uno::Sequence < sal_Int32 > aAvailableMissingValueTreatments(
ChartTypeHelper::getSupportedMissingValueTreatments( xChartType ) );
uno::Reference< beans::XPropertySet > xDiaProp( xDiagram, uno::UNO_QUERY );
if( xDiaProp.is() && (xDiaProp->getPropertyValue( "MissingValueTreatment" ) >>= nResult) )
{
//ensure that the set value is supported by this charttype
for( sal_Int32 nN = 0; nN < aAvailableMissingValueTreatments.getLength(); nN++ )
if( aAvailableMissingValueTreatments[nN] == nResult )
return nResult; //ok
}
//otherwise use the first supported one
if( aAvailableMissingValueTreatments.hasElements() )
{
nResult = aAvailableMissingValueTreatments[0];
return nResult;
}
return nResult;
}
DiagramPositioningMode DiagramHelper::getDiagramPositioningMode( const uno::Reference<
chart2::XDiagram > & xDiagram )
{
DiagramPositioningMode eMode = DiagramPositioningMode_AUTO;
uno::Reference< beans::XPropertySet > xDiaProps( xDiagram, uno::UNO_QUERY );
if( xDiaProps.is() )
{
RelativePosition aRelPos;
RelativeSize aRelSize;
if( (xDiaProps->getPropertyValue("RelativePosition") >>= aRelPos ) &&
(xDiaProps->getPropertyValue("RelativeSize") >>= aRelSize ) )
{
bool bPosSizeExcludeAxes=false;
xDiaProps->getPropertyValue("PosSizeExcludeAxes") >>= bPosSizeExcludeAxes;
if( bPosSizeExcludeAxes )
eMode = DiagramPositioningMode_EXCLUDING;
else
eMode = DiagramPositioningMode_INCLUDING;
}
}
return eMode;
}
static void lcl_ensureRange0to1( double& rValue )
{
if(rValue<0.0)
rValue=0.0;
if(rValue>1.0)
rValue=1.0;
}
bool DiagramHelper::setDiagramPositioning( const uno::Reference< frame::XModel >& xChartModel,
const awt::Rectangle& rPosRect /*100th mm*/ )
{
ControllerLockGuardUNO aCtrlLockGuard( xChartModel );
bool bChanged = false;
awt::Size aPageSize( ChartModelHelper::getPageSize(xChartModel) );
uno::Reference< beans::XPropertySet > xDiaProps( ChartModelHelper::findDiagram( xChartModel ), uno::UNO_QUERY );
if( !xDiaProps.is() )
return bChanged;
RelativePosition aOldPos;
RelativeSize aOldSize;
xDiaProps->getPropertyValue("RelativePosition" ) >>= aOldPos;
xDiaProps->getPropertyValue("RelativeSize" ) >>= aOldSize;
RelativePosition aNewPos;
aNewPos.Anchor = drawing::Alignment_TOP_LEFT;
aNewPos.Primary = double(rPosRect.X)/double(aPageSize.Width);
aNewPos.Secondary = double(rPosRect.Y)/double(aPageSize.Height);
chart2::RelativeSize aNewSize;
aNewSize.Primary = double(rPosRect.Width)/double(aPageSize.Width);
aNewSize.Secondary = double(rPosRect.Height)/double(aPageSize.Height);
lcl_ensureRange0to1( aNewPos.Primary );
lcl_ensureRange0to1( aNewPos.Secondary );
lcl_ensureRange0to1( aNewSize.Primary );
lcl_ensureRange0to1( aNewSize.Secondary );
if( (aNewPos.Primary + aNewSize.Primary) > 1.0 )
aNewPos.Primary = 1.0 - aNewSize.Primary;
if( (aNewPos.Secondary + aNewSize.Secondary) > 1.0 )
aNewPos.Secondary = 1.0 - aNewSize.Secondary;
xDiaProps->setPropertyValue( "RelativePosition", uno::Any(aNewPos) );
xDiaProps->setPropertyValue( "RelativeSize", uno::Any(aNewSize) );
bChanged = (aOldPos.Anchor!=aNewPos.Anchor) ||
(aOldPos.Primary!=aNewPos.Primary) ||
(aOldPos.Secondary!=aNewPos.Secondary) ||
(aOldSize.Primary!=aNewSize.Primary) ||
(aOldSize.Secondary!=aNewSize.Secondary);
return bChanged;
}
awt::Rectangle DiagramHelper::getDiagramRectangleFromModel( const uno::Reference< frame::XModel >& xChartModel )
{
awt::Rectangle aRet(-1,-1,-1,-1);
uno::Reference< beans::XPropertySet > xDiaProps( ChartModelHelper::findDiagram( xChartModel ), uno::UNO_QUERY );
if( !xDiaProps.is() )
return aRet;
awt::Size aPageSize( ChartModelHelper::getPageSize(xChartModel) );
RelativePosition aRelPos;
RelativeSize aRelSize;
xDiaProps->getPropertyValue("RelativePosition" ) >>= aRelPos;
xDiaProps->getPropertyValue("RelativeSize" ) >>= aRelSize;
awt::Size aAbsSize(
static_cast< sal_Int32 >( aRelSize.Primary * aPageSize.Width ),
static_cast< sal_Int32 >( aRelSize.Secondary * aPageSize.Height ));
awt::Point aAbsPos(
static_cast< sal_Int32 >( aRelPos.Primary * aPageSize.Width ),
static_cast< sal_Int32 >( aRelPos.Secondary * aPageSize.Height ));
awt::Point aAbsPosLeftTop = RelativePositionHelper::getUpperLeftCornerOfAnchoredObject( aAbsPos, aAbsSize, aRelPos.Anchor );
aRet = awt::Rectangle(aAbsPosLeftTop.X, aAbsPosLeftTop.Y, aAbsSize.Width, aAbsSize.Height );
return aRet;
}
bool DiagramHelper::switchDiagramPositioningToExcludingPositioning(
ChartModel& rModel, bool bResetModifiedState, bool bConvertAlsoFromAutoPositioning )
{
//return true if something was changed
const SvtSaveOptions::ODFDefaultVersion nCurrentODFVersion( SvtSaveOptions().GetODFDefaultVersion() );
if( nCurrentODFVersion > SvtSaveOptions::ODFVER_012 )
{
uno::Reference< css::chart::XDiagramPositioning > xDiagramPositioning( rModel.getFirstDiagram(), uno::UNO_QUERY );
if( xDiagramPositioning.is() && ( bConvertAlsoFromAutoPositioning || !xDiagramPositioning->isAutomaticDiagramPositioning() )
&& !xDiagramPositioning->isExcludingDiagramPositioning() )
{
ControllerLockGuard aCtrlLockGuard( rModel );
bool bModelWasModified = rModel.isModified();
xDiagramPositioning->setDiagramPositionExcludingAxes( xDiagramPositioning->calculateDiagramPositionExcludingAxes() );
if(bResetModifiedState && !bModelWasModified )
rModel.setModified(false);
return true;
}
}
return false;
}
} // namespace chart
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|