summaryrefslogtreecommitdiff
path: root/sc/source/filter/excel/xichart.cxx
blob: 0e9f8fab2eeb70957543fab9cd907338034aab47 (plain)
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
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sc.hxx"

#include "xichart.hxx"

#include <algorithm>
#include <memory>

#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/drawing/Direction3D.hpp>
#include <com/sun/star/drawing/ProjectionMode.hpp>
#include <com/sun/star/drawing/ShadeMode.hpp>
#include <com/sun/star/drawing/XShape.hpp>
#include <com/sun/star/drawing/XDrawPageSupplier.hpp>
#include <com/sun/star/chart/ChartAxisArrangeOrderType.hpp>
#include <com/sun/star/chart/ChartAxisLabelPosition.hpp>
#include <com/sun/star/chart/ChartAxisMarkPosition.hpp>
#include <com/sun/star/chart/ChartAxisPosition.hpp>
#include <com/sun/star/chart/ChartLegendExpansion.hpp>
#include <com/sun/star/chart/TimeInterval.hpp>
#include <com/sun/star/chart/TimeUnit.hpp>
#include <com/sun/star/chart/XChartDocument.hpp>
#include <com/sun/star/chart/XDiagramPositioning.hpp>
#include <com/sun/star/chart2/XChartDocument.hpp>
#include <com/sun/star/chart2/XDiagram.hpp>
#include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
#include <com/sun/star/chart2/XChartTypeContainer.hpp>
#include <com/sun/star/chart2/XDataSeriesContainer.hpp>
#include <com/sun/star/chart2/XRegressionCurveContainer.hpp>
#include <com/sun/star/chart2/XTitled.hpp>
#include <com/sun/star/chart2/data/XDataProvider.hpp>
#include <com/sun/star/chart2/data/XDataReceiver.hpp>
#include <com/sun/star/chart2/data/XDataSink.hpp>
#include <com/sun/star/chart2/AxisType.hpp>
#include <com/sun/star/chart2/CurveStyle.hpp>
#include <com/sun/star/chart2/DataPointGeometry3D.hpp>
#include <com/sun/star/chart2/DataPointLabel.hpp>
#include <com/sun/star/chart2/LegendPosition.hpp>
#include <com/sun/star/chart2/StackingDirection.hpp>
#include <com/sun/star/chart2/TickmarkStyle.hpp>
#include <com/sun/star/chart2/RelativePosition.hpp>
#include <com/sun/star/chart2/RelativeSize.hpp>
#include <com/sun/star/chart/DataLabelPlacement.hpp>
#include <com/sun/star/chart/ErrorBarStyle.hpp>
#include <com/sun/star/chart/MissingValueTreatment.hpp>

#include <sfx2/objsh.hxx>
#include <svx/svdpage.hxx>
#include <svx/unoapi.hxx>

#include "document.hxx"
#include "drwlayer.hxx"
#include "rangeutl.hxx"
#include "tokenarray.hxx"
#include "token.hxx"
#include "compiler.hxx"
#include "reftokenhelper.hxx"
#include "chartlis.hxx"
#include "fprogressbar.hxx"
#include "xltracer.hxx"
#include "xistream.hxx"
#include "xiformula.hxx"
#include "xistyle.hxx"
#include "xipage.hxx"
#include "xiview.hxx"

using ::rtl::OUString;
using ::rtl::OUStringBuffer;
using ::com::sun::star::uno::Any;
using ::com::sun::star::uno::Reference;
using ::com::sun::star::uno::Sequence;
using ::com::sun::star::uno::UNO_QUERY;
using ::com::sun::star::uno::UNO_QUERY_THROW;
using ::com::sun::star::uno::UNO_SET_THROW;
using ::com::sun::star::uno::Exception;
using ::com::sun::star::beans::XPropertySet;
using ::com::sun::star::lang::XMultiServiceFactory;
using ::com::sun::star::frame::XModel;
using ::com::sun::star::util::XNumberFormatsSupplier;
using ::com::sun::star::drawing::XDrawPage;
using ::com::sun::star::drawing::XDrawPageSupplier;
using ::com::sun::star::drawing::XShape;

using ::com::sun::star::chart2::IncrementData;
using ::com::sun::star::chart2::RelativePosition;
using ::com::sun::star::chart2::RelativeSize;
using ::com::sun::star::chart2::ScaleData;
using ::com::sun::star::chart2::SubIncrement;
using ::com::sun::star::chart2::XAxis;
using ::com::sun::star::chart2::XChartDocument;
using ::com::sun::star::chart2::XChartType;
using ::com::sun::star::chart2::XChartTypeContainer;
using ::com::sun::star::chart2::XCoordinateSystem;
using ::com::sun::star::chart2::XCoordinateSystemContainer;
using ::com::sun::star::chart2::XDataSeries;
using ::com::sun::star::chart2::XDataSeriesContainer;
using ::com::sun::star::chart2::XDiagram;
using ::com::sun::star::chart2::XFormattedString;
using ::com::sun::star::chart2::XLegend;
using ::com::sun::star::chart2::XRegressionCurve;
using ::com::sun::star::chart2::XRegressionCurveContainer;
using ::com::sun::star::chart2::XScaling;
using ::com::sun::star::chart2::XTitle;
using ::com::sun::star::chart2::XTitled;

using ::com::sun::star::chart2::data::XDataProvider;
using ::com::sun::star::chart2::data::XDataReceiver;
using ::com::sun::star::chart2::data::XDataSequence;
using ::com::sun::star::chart2::data::XDataSink;
using ::com::sun::star::chart2::data::XLabeledDataSequence;

using ::formula::FormulaToken;
using ::formula::StackVar;
using ::boost::shared_ptr;
using ::std::pair;
using ::std::auto_ptr;

namespace cssc = ::com::sun::star::chart;
namespace cssc2 = ::com::sun::star::chart2;

// Helpers ====================================================================

namespace {

XclImpStream& operator>>( XclImpStream& rStrm, XclChRectangle& rRect )
{
    return rStrm >> rRect.mnX >> rRect.mnY >> rRect.mnWidth >> rRect.mnHeight;
}

inline void lclSetValueOrClearAny( Any& rAny, double fValue, bool bClear )
{
    if( bClear )
        rAny.clear();
    else
        rAny <<= fValue;
}

void lclSetExpValueOrClearAny( Any& rAny, double fValue, bool bLogScale, bool bClear )
{
    if( !bClear && bLogScale )
        fValue = pow( 10.0, fValue );
    lclSetValueOrClearAny( rAny, fValue, bClear );
}

double lclGetSerialDay( const XclImpRoot& rRoot, sal_uInt16 nValue, sal_uInt16 nTimeUnit )
{
    switch( nTimeUnit )
    {
        case EXC_CHDATERANGE_DAYS:
            return nValue;
        case EXC_CHDATERANGE_MONTHS:
            return rRoot.GetDoubleFromDateTime( Date( 1, static_cast< sal_uInt16 >( 1 + nValue % 12 ), static_cast< sal_uInt16 >( rRoot.GetBaseYear() + nValue / 12 ) ) );
        case EXC_CHDATERANGE_YEARS:
            return rRoot.GetDoubleFromDateTime( Date( 1, 1, static_cast< sal_uInt16 >( rRoot.GetBaseYear() + nValue ) ) );
        default:
            OSL_ENSURE( false, "lclGetSerialDay - unexpected time unit" );
    }
    return nValue;
}

void lclConvertTimeValue( const XclImpRoot& rRoot, Any& rAny, sal_uInt16 nValue, bool bAuto, sal_uInt16 nTimeUnit )
{
    if( bAuto )
        rAny.clear();
    else
        rAny <<= lclGetSerialDay( rRoot, nValue, nTimeUnit );
}

sal_Int32 lclGetApiTimeUnit( sal_uInt16 nTimeUnit )
{
    switch( nTimeUnit )
    {
        case EXC_CHDATERANGE_DAYS:      return cssc::TimeUnit::DAY;
        case EXC_CHDATERANGE_MONTHS:    return cssc::TimeUnit::MONTH;
        case EXC_CHDATERANGE_YEARS:     return cssc::TimeUnit::YEAR;
        default:                        OSL_ENSURE( false, "lclGetApiTimeUnit - unexpected time unit" );
    }
    return cssc::TimeUnit::DAY;
}

void lclConvertTimeInterval( Any& rInterval, sal_uInt16 nValue, bool bAuto, sal_uInt16 nTimeUnit )
{
    if( bAuto || (nValue == 0) )
        rInterval.clear();
    else
        rInterval <<= cssc::TimeInterval( nValue, lclGetApiTimeUnit( nTimeUnit ) );
}

} // namespace

// Common =====================================================================

/** Stores global data needed in various classes of the Chart import filter. */
struct XclImpChRootData : public XclChRootData
{
    XclImpChChart&      mrChartData;            /// The chart data object.

    inline explicit     XclImpChRootData( XclImpChChart& rChartData ) : mrChartData( rChartData ) {}
};

// ----------------------------------------------------------------------------

XclImpChRoot::XclImpChRoot( const XclImpRoot& rRoot, XclImpChChart& rChartData ) :
    XclImpRoot( rRoot ),
    mxChData( new XclImpChRootData( rChartData ) )
{
}

XclImpChRoot::~XclImpChRoot()
{
}

XclImpChChart& XclImpChRoot::GetChartData() const
{
    return mxChData->mrChartData;
}

const XclChTypeInfo& XclImpChRoot::GetChartTypeInfo( XclChTypeId eType ) const
{
    return mxChData->mxTypeInfoProv->GetTypeInfo( eType );
}

const XclChTypeInfo& XclImpChRoot::GetChartTypeInfo( sal_uInt16 nRecId ) const
{
    return mxChData->mxTypeInfoProv->GetTypeInfoFromRecId( nRecId );
}

const XclChFormatInfo& XclImpChRoot::GetFormatInfo( XclChObjectType eObjType ) const
{
    return mxChData->mxFmtInfoProv->GetFormatInfo( eObjType );
}

Color XclImpChRoot::GetFontAutoColor() const
{
    return GetPalette().GetColor( EXC_COLOR_CHWINDOWTEXT );
}

Color XclImpChRoot::GetSeriesLineAutoColor( sal_uInt16 nFormatIdx ) const
{
    return GetPalette().GetColor( XclChartHelper::GetSeriesLineAutoColorIdx( nFormatIdx ) );
}

Color XclImpChRoot::GetSeriesFillAutoColor( sal_uInt16 nFormatIdx ) const
{
    const XclImpPalette& rPal = GetPalette();
    Color aColor = rPal.GetColor( XclChartHelper::GetSeriesFillAutoColorIdx( nFormatIdx ) );
    sal_uInt8 nTrans = XclChartHelper::GetSeriesFillAutoTransp( nFormatIdx );
    return ScfTools::GetMixedColor( aColor, rPal.GetColor( EXC_COLOR_CHWINDOWBACK ), nTrans );
}

void XclImpChRoot::InitConversion( const Reference<XChartDocument>& xChartDoc, const Rectangle& rChartRect ) const
{
    // create formatting object tables
    mxChData->InitConversion( GetRoot(), xChartDoc, rChartRect );

    // lock the model to suppress any internal updates
    Reference< XModel > xModel( xChartDoc, UNO_QUERY );
    if( xModel.is() )
        xModel->lockControllers();

    SfxObjectShell* pDocShell = GetDocShell();
    Reference< XDataReceiver > xDataRec( xChartDoc, UNO_QUERY );
    if( pDocShell && xDataRec.is() )
    {
        // create and register a data provider
        Reference< XDataProvider > xDataProv(
            ScfApiHelper::CreateInstance( pDocShell, SERVICE_CHART2_DATAPROVIDER ), UNO_QUERY );
        if( xDataProv.is() )
            xDataRec->attachDataProvider( xDataProv );
        // attach the number formatter
        Reference< XNumberFormatsSupplier > xNumFmtSupp( pDocShell->GetModel(), UNO_QUERY );
        if( xNumFmtSupp.is() )
            xDataRec->attachNumberFormatsSupplier( xNumFmtSupp );
    }
}

void XclImpChRoot::FinishConversion( XclImpDffConverter& rDffConv ) const
{
    rDffConv.Progress( EXC_CHART_PROGRESS_SIZE );
    // unlock the model
    Reference< XModel > xModel( mxChData->mxChartDoc, UNO_QUERY );
    if( xModel.is() )
        xModel->unlockControllers();
    rDffConv.Progress( EXC_CHART_PROGRESS_SIZE );

    mxChData->FinishConversion();
}

Reference< XDataProvider > XclImpChRoot::GetDataProvider() const
{
    return mxChData->mxChartDoc->getDataProvider();
}

Reference< XShape > XclImpChRoot::GetTitleShape( const XclChTextKey& rTitleKey ) const
{
    return mxChData->GetTitleShape( rTitleKey );
}

sal_Int32 XclImpChRoot::CalcHmmFromChartX( sal_Int32 nPosX ) const
{
    return static_cast< sal_Int32 >( mxChData->mfUnitSizeX * nPosX + mxChData->mnBorderGapX + 0.5 );
}

sal_Int32 XclImpChRoot::CalcHmmFromChartY( sal_Int32 nPosY ) const
{
    return static_cast< sal_Int32 >( mxChData->mfUnitSizeY * nPosY + mxChData->mnBorderGapY + 0.5 );
}

::com::sun::star::awt::Rectangle XclImpChRoot::CalcHmmFromChartRect( const XclChRectangle& rRect ) const
{
    return ::com::sun::star::awt::Rectangle(
        CalcHmmFromChartX( rRect.mnX ),
        CalcHmmFromChartY( rRect.mnY ),
        CalcHmmFromChartX( rRect.mnWidth ),
        CalcHmmFromChartY( rRect.mnHeight ) );
}

double XclImpChRoot::CalcRelativeFromHmmX( sal_Int32 nPosX ) const
{
    return static_cast< double >( nPosX ) / mxChData->maChartRect.GetWidth();
}

double XclImpChRoot::CalcRelativeFromHmmY( sal_Int32 nPosY ) const
{
    return static_cast< double >( nPosY ) / mxChData->maChartRect.GetHeight();
}

double XclImpChRoot::CalcRelativeFromChartX( sal_Int32 nPosX ) const
{
    return CalcRelativeFromHmmX( CalcHmmFromChartX( nPosX ) );
}

double XclImpChRoot::CalcRelativeFromChartY( sal_Int32 nPosY ) const
{
    return CalcRelativeFromHmmY( CalcHmmFromChartY( nPosY ) );
}

void XclImpChRoot::ConvertLineFormat( ScfPropertySet& rPropSet,
        const XclChLineFormat& rLineFmt, XclChPropertyMode ePropMode ) const
{
    GetChartPropSetHelper().WriteLineProperties(
        rPropSet, *mxChData->mxLineDashTable, rLineFmt, ePropMode );
}

void XclImpChRoot::ConvertAreaFormat( ScfPropertySet& rPropSet,
        const XclChAreaFormat& rAreaFmt, XclChPropertyMode ePropMode ) const
{
    GetChartPropSetHelper().WriteAreaProperties( rPropSet, rAreaFmt, ePropMode );
}

void XclImpChRoot::ConvertEscherFormat( ScfPropertySet& rPropSet,
        const XclChEscherFormat& rEscherFmt, const XclChPicFormat& rPicFmt,
        XclChPropertyMode ePropMode ) const
{
    GetChartPropSetHelper().WriteEscherProperties( rPropSet,
        *mxChData->mxGradientTable, *mxChData->mxHatchTable, *mxChData->mxBitmapTable,
        rEscherFmt, rPicFmt, ePropMode );
}

void XclImpChRoot::ConvertFont( ScfPropertySet& rPropSet,
        sal_uInt16 nFontIdx, const Color* pFontColor ) const
{
    GetFontBuffer().WriteFontProperties( rPropSet, EXC_FONTPROPSET_CHART, nFontIdx, pFontColor );
}

void XclImpChRoot::ConvertPieRotation( ScfPropertySet& rPropSet, sal_uInt16 nAngle )
{
    sal_Int32 nApiRot = (450 - (nAngle % 360)) % 360;
    rPropSet.SetProperty( EXC_CHPROP_STARTINGANGLE, nApiRot );
}

// ----------------------------------------------------------------------------

XclImpChGroupBase::~XclImpChGroupBase()
{
}

void XclImpChGroupBase::ReadRecordGroup( XclImpStream& rStrm )
{
    // read contents of the header record
    ReadHeaderRecord( rStrm );

    // only read sub records, if the next record is a CHBEGIN
    if( rStrm.GetNextRecId() == EXC_ID_CHBEGIN )
    {
        // read the CHBEGIN record, may be used for special initial processing
        rStrm.StartNextRecord();
        ReadSubRecord( rStrm );

        // read the nested records
        bool bLoop = true;
        while( bLoop && rStrm.StartNextRecord() )
        {
            sal_uInt16 nRecId = rStrm.GetRecId();
            bLoop = nRecId != EXC_ID_CHEND;
            // skip unsupported nested blocks
            if( nRecId == EXC_ID_CHBEGIN )
                SkipBlock( rStrm );
            else
                ReadSubRecord( rStrm );
        }
    }
    /*  Returns with current CHEND record or unchanged stream, if no record
        group present. In every case another call to StartNextRecord() will go
        to next record of interest. */
}

void XclImpChGroupBase::SkipBlock( XclImpStream& rStrm )
{
    OSL_ENSURE( rStrm.GetRecId() == EXC_ID_CHBEGIN, "XclImpChGroupBase::SkipBlock - no CHBEGIN record" );
    // do nothing if current record is not CHBEGIN
    bool bLoop = rStrm.GetRecId() == EXC_ID_CHBEGIN;
    while( bLoop && rStrm.StartNextRecord() )
    {
        sal_uInt16 nRecId = rStrm.GetRecId();
        bLoop = nRecId != EXC_ID_CHEND;
        // skip nested record groups
        if( nRecId == EXC_ID_CHBEGIN )
            SkipBlock( rStrm );
    }
}

// Frame formatting ===========================================================

void XclImpChFramePos::ReadChFramePos( XclImpStream& rStrm )
{
    rStrm >> maData.mnTLMode >> maData.mnBRMode;
    /*  According to the spec, the upper 16 bits of all members in the
        rectangle are unused and may contain garbage. */
    maData.maRect.mnX = rStrm.ReadInt16(); rStrm.Ignore( 2 );
    maData.maRect.mnY = rStrm.ReadInt16(); rStrm.Ignore( 2 );
    maData.maRect.mnWidth = rStrm.ReadInt16(); rStrm.Ignore( 2 );
    maData.maRect.mnHeight = rStrm.ReadInt16(); rStrm.Ignore( 2 );
}

// ----------------------------------------------------------------------------

void XclImpChLineFormat::ReadChLineFormat( XclImpStream& rStrm )
{
    rStrm >> maData.maColor >> maData.mnPattern >> maData.mnWeight >> maData.mnFlags;

    const XclImpRoot& rRoot = rStrm.GetRoot();
    if( rRoot.GetBiff() == EXC_BIFF8 )
        // BIFF8: index into palette used instead of RGB data
        maData.maColor = rRoot.GetPalette().GetColor( rStrm.ReaduInt16() );
}

void XclImpChLineFormat::Convert( const XclImpChRoot& rRoot,
        ScfPropertySet& rPropSet, XclChObjectType eObjType, sal_uInt16 nFormatIdx ) const
{
    const XclChFormatInfo& rFmtInfo = rRoot.GetFormatInfo( eObjType );
    if( IsAuto() )
    {
        XclChLineFormat aLineFmt;
        aLineFmt.maColor = (eObjType == EXC_CHOBJTYPE_LINEARSERIES) ?
            rRoot.GetSeriesLineAutoColor( nFormatIdx ) :
            rRoot.GetPalette().GetColor( rFmtInfo.mnAutoLineColorIdx );
        aLineFmt.mnPattern = EXC_CHLINEFORMAT_SOLID;
        aLineFmt.mnWeight = rFmtInfo.mnAutoLineWeight;
        rRoot.ConvertLineFormat( rPropSet, aLineFmt, rFmtInfo.mePropMode );
    }
    else
    {
        rRoot.ConvertLineFormat( rPropSet, maData, rFmtInfo.mePropMode );
    }
}

// ----------------------------------------------------------------------------

void XclImpChAreaFormat::ReadChAreaFormat( XclImpStream& rStrm )
{
    rStrm >> maData.maPattColor >> maData.maBackColor >> maData.mnPattern >> maData.mnFlags;

    const XclImpRoot& rRoot = rStrm.GetRoot();
    if( rRoot.GetBiff() == EXC_BIFF8 )
    {
        // BIFF8: index into palette used instead of RGB data
        const XclImpPalette& rPal = rRoot.GetPalette();
        maData.maPattColor = rPal.GetColor( rStrm.ReaduInt16() );
        maData.maBackColor = rPal.GetColor( rStrm.ReaduInt16());
    }
}

void XclImpChAreaFormat::Convert( const XclImpChRoot& rRoot,
        ScfPropertySet& rPropSet, XclChObjectType eObjType, sal_uInt16 nFormatIdx ) const
{
    const XclChFormatInfo& rFmtInfo = rRoot.GetFormatInfo( eObjType );
    if( IsAuto() )
    {
        XclChAreaFormat aAreaFmt;
        aAreaFmt.maPattColor = (eObjType == EXC_CHOBJTYPE_FILLEDSERIES) ?
            rRoot.GetSeriesFillAutoColor( nFormatIdx ) :
            rRoot.GetPalette().GetColor( rFmtInfo.mnAutoPattColorIdx );
        aAreaFmt.mnPattern = EXC_PATT_SOLID;
        rRoot.ConvertAreaFormat( rPropSet, aAreaFmt, rFmtInfo.mePropMode );
    }
    else
    {
        rRoot.ConvertAreaFormat( rPropSet, maData, rFmtInfo.mePropMode );
    }
}

// ----------------------------------------------------------------------------

XclImpChEscherFormat::XclImpChEscherFormat( const XclImpRoot& rRoot )
{
    maData.mxItemSet.reset(
        new SfxItemSet( rRoot.GetDoc().GetDrawLayer()->GetItemPool() ) );
}

void XclImpChEscherFormat::ReadHeaderRecord( XclImpStream& rStrm )
{
    // read from stream - CHESCHERFORMAT uses own ID for record continuation
    XclImpDffPropSet aPropSet( rStrm.GetRoot() );
    rStrm.ResetRecord( true, rStrm.GetRecId() );
    rStrm >> aPropSet;
    // get the data
    aPropSet.FillToItemSet( *maData.mxItemSet );
    // get bitmap mode from DFF item set
    sal_uInt32 nType = aPropSet.GetPropertyValue( DFF_Prop_fillType, mso_fillSolid );
    maPicFmt.mnBmpMode = (nType == mso_fillPicture) ? EXC_CHPICFORMAT_STRETCH : EXC_CHPICFORMAT_STACK;
}

void XclImpChEscherFormat::ReadSubRecord( XclImpStream& rStrm )
{
    switch( rStrm.GetRecId() )
    {
        case EXC_ID_CHPICFORMAT:
            rStrm >> maPicFmt.mnBmpMode >> maPicFmt.mnFormat >> maPicFmt.mnFlags >> maPicFmt.mfScale;
        break;
    }
}

void XclImpChEscherFormat::Convert( const XclImpChRoot& rRoot,
        ScfPropertySet& rPropSet, XclChObjectType eObjType ) const
{
    const XclChFormatInfo& rFmtInfo = rRoot.GetFormatInfo( eObjType );
    rRoot.ConvertEscherFormat( rPropSet, maData, maPicFmt, rFmtInfo.mePropMode );
}

// ----------------------------------------------------------------------------

XclImpChFrameBase::XclImpChFrameBase( const XclChFormatInfo& rFmtInfo )
{
    if( rFmtInfo.mbCreateDefFrame ) switch( rFmtInfo.meDefFrameType )
    {
        case EXC_CHFRAMETYPE_AUTO:
            mxLineFmt.reset( new XclImpChLineFormat );
            if( rFmtInfo.mbIsFrame )
                mxAreaFmt.reset( new XclImpChAreaFormat );
        break;
        case EXC_CHFRAMETYPE_INVISIBLE:
        {
            XclChLineFormat aLineFmt;
            ::set_flag( aLineFmt.mnFlags, EXC_CHLINEFORMAT_AUTO, false );
            aLineFmt.mnPattern = EXC_CHLINEFORMAT_NONE;
            mxLineFmt.reset( new XclImpChLineFormat( aLineFmt ) );
            if( rFmtInfo.mbIsFrame )
            {
                XclChAreaFormat aAreaFmt;
                ::set_flag( aAreaFmt.mnFlags, EXC_CHAREAFORMAT_AUTO, false );
                aAreaFmt.mnPattern = EXC_PATT_NONE;
                mxAreaFmt.reset( new XclImpChAreaFormat( aAreaFmt ) );
            }
        }
        break;
        default:
            OSL_FAIL( "XclImpChFrameBase::XclImpChFrameBase - unknown frame type" );
    }
}

void XclImpChFrameBase::ReadSubRecord( XclImpStream& rStrm )
{
    switch( rStrm.GetRecId() )
    {
        case EXC_ID_CHLINEFORMAT:
            mxLineFmt.reset( new XclImpChLineFormat );
            mxLineFmt->ReadChLineFormat( rStrm );
        break;
        case EXC_ID_CHAREAFORMAT:
            mxAreaFmt.reset( new XclImpChAreaFormat );
            mxAreaFmt->ReadChAreaFormat( rStrm );
        break;
        case EXC_ID_CHESCHERFORMAT:
            mxEscherFmt.reset( new XclImpChEscherFormat( rStrm.GetRoot() ) );
            mxEscherFmt->ReadRecordGroup( rStrm );
        break;
    }
}

void XclImpChFrameBase::ConvertLineBase( const XclImpChRoot& rRoot,
        ScfPropertySet& rPropSet, XclChObjectType eObjType, sal_uInt16 nFormatIdx ) const
{
    if( mxLineFmt )
        mxLineFmt->Convert( rRoot, rPropSet, eObjType, nFormatIdx );
}

void XclImpChFrameBase::ConvertAreaBase( const XclImpChRoot& rRoot,
        ScfPropertySet& rPropSet, XclChObjectType eObjType, sal_uInt16 nFormatIdx ) const
{
    if( rRoot.GetFormatInfo( eObjType ).mbIsFrame )
    {
        // CHESCHERFORMAT overrides CHAREAFORMAT (even if it is auto)
        if( mxEscherFmt )
            mxEscherFmt->Convert( rRoot, rPropSet, eObjType );
        else if( mxAreaFmt )
            mxAreaFmt->Convert( rRoot, rPropSet, eObjType, nFormatIdx );
    }
}

void XclImpChFrameBase::ConvertFrameBase( const XclImpChRoot& rRoot,
        ScfPropertySet& rPropSet, XclChObjectType eObjType, sal_uInt16 nFormatIdx ) const
{
    ConvertLineBase( rRoot, rPropSet, eObjType, nFormatIdx );
    ConvertAreaBase( rRoot, rPropSet, eObjType, nFormatIdx );
}

// ----------------------------------------------------------------------------

XclImpChFrame::XclImpChFrame( const XclImpChRoot& rRoot, XclChObjectType eObjType ) :
    XclImpChFrameBase( rRoot.GetFormatInfo( eObjType ) ),
    XclImpChRoot( rRoot ),
    meObjType( eObjType )
{
}

void XclImpChFrame::ReadHeaderRecord( XclImpStream& rStrm )
{
    rStrm >> maData.mnFormat >> maData.mnFlags;
}

void XclImpChFrame::UpdateObjFrame( const XclObjLineData& rLineData, const XclObjFillData& rFillData )
{
    const XclImpPalette& rPal = GetPalette();

    if( rLineData.IsVisible() && (!mxLineFmt || !mxLineFmt->HasLine()) )
    {
        // line formatting
        XclChLineFormat aLineFmt;
        aLineFmt.maColor = rPal.GetColor( rLineData.mnColorIdx );
        switch( rLineData.mnStyle )
        {
            case EXC_OBJ_LINE_SOLID:        aLineFmt.mnPattern = EXC_CHLINEFORMAT_SOLID;        break;
            case EXC_OBJ_LINE_DASH:         aLineFmt.mnPattern = EXC_CHLINEFORMAT_DASH;         break;
            case EXC_OBJ_LINE_DOT:          aLineFmt.mnPattern = EXC_CHLINEFORMAT_DOT;          break;
            case EXC_OBJ_LINE_DASHDOT:      aLineFmt.mnPattern = EXC_CHLINEFORMAT_DASHDOT;      break;
            case EXC_OBJ_LINE_DASHDOTDOT:   aLineFmt.mnPattern = EXC_CHLINEFORMAT_DASHDOTDOT;   break;
            case EXC_OBJ_LINE_MEDTRANS:     aLineFmt.mnPattern = EXC_CHLINEFORMAT_MEDTRANS;     break;
            case EXC_OBJ_LINE_DARKTRANS:    aLineFmt.mnPattern = EXC_CHLINEFORMAT_DARKTRANS;    break;
            case EXC_OBJ_LINE_LIGHTTRANS:   aLineFmt.mnPattern = EXC_CHLINEFORMAT_LIGHTTRANS;   break;
            case EXC_OBJ_LINE_NONE:         aLineFmt.mnPattern = EXC_CHLINEFORMAT_NONE;         break;
            default:                        aLineFmt.mnPattern = EXC_CHLINEFORMAT_SOLID;
        }
        switch( rLineData.mnWidth )
        {
            case EXC_OBJ_LINE_HAIR:     aLineFmt.mnWeight = EXC_CHLINEFORMAT_HAIR;      break;
            case EXC_OBJ_LINE_THIN:     aLineFmt.mnWeight = EXC_CHLINEFORMAT_SINGLE;    break;
            case EXC_OBJ_LINE_MEDIUM:   aLineFmt.mnWeight = EXC_CHLINEFORMAT_DOUBLE;    break;
            case EXC_OBJ_LINE_THICK:    aLineFmt.mnWeight = EXC_CHLINEFORMAT_TRIPLE;    break;
            default:                    aLineFmt.mnWeight = EXC_CHLINEFORMAT_HAIR;
        }
        ::set_flag( aLineFmt.mnFlags, EXC_CHLINEFORMAT_AUTO, rLineData.IsAuto() );
        mxLineFmt.reset( new XclImpChLineFormat( aLineFmt ) );
    }

    if( rFillData.IsFilled() && (!mxAreaFmt || !mxAreaFmt->HasArea()) && !mxEscherFmt )
    {
        // area formatting
        XclChAreaFormat aAreaFmt;
        aAreaFmt.maPattColor = rPal.GetColor( rFillData.mnPattColorIdx );
        aAreaFmt.maBackColor = rPal.GetColor( rFillData.mnBackColorIdx );
        aAreaFmt.mnPattern = rFillData.mnPattern;
        ::set_flag( aAreaFmt.mnFlags, EXC_CHAREAFORMAT_AUTO, rFillData.IsAuto() );
        mxAreaFmt.reset( new XclImpChAreaFormat( aAreaFmt ) );
    }
}

void XclImpChFrame::Convert( ScfPropertySet& rPropSet ) const
{
    ConvertFrameBase( GetChRoot(), rPropSet, meObjType );
}

// Source links ===============================================================

namespace {

/** Creates a labeled data sequence object, adds link for series title if present. */
Reference< XLabeledDataSequence > lclCreateLabeledDataSequence(
        const XclImpChSourceLinkRef& xValueLink, const OUString& rValueRole,
        const XclImpChSourceLink* pTitleLink = 0 )
{
    // create data sequence for values and title
    Reference< XDataSequence > xValueSeq;
    if( xValueLink )
        xValueSeq = xValueLink->CreateDataSequence( rValueRole );
    Reference< XDataSequence > xTitleSeq;
    if( pTitleLink )
        xTitleSeq = pTitleLink->CreateDataSequence( EXC_CHPROP_ROLE_LABEL );

    // create the labeled data sequence, if values or title are present
    Reference< XLabeledDataSequence > xLabeledSeq;
    if( xValueSeq.is() || xTitleSeq.is() )
        xLabeledSeq.set( ScfApiHelper::CreateInstance( SERVICE_CHART2_LABELEDDATASEQ ), UNO_QUERY );
    if( xLabeledSeq.is() )
    {
        if( xValueSeq.is() )
            xLabeledSeq->setValues( xValueSeq );
        if( xTitleSeq.is() )
            xLabeledSeq->setLabel( xTitleSeq );
    }
    return xLabeledSeq;
}

} // namespace

// ----------------------------------------------------------------------------

XclImpChSourceLink::XclImpChSourceLink( const XclImpChRoot& rRoot ) :
    XclImpChRoot( rRoot )
{
}

XclImpChSourceLink::~XclImpChSourceLink()
{
}

void XclImpChSourceLink::ReadChSourceLink( XclImpStream& rStrm )
{
    rStrm   >> maData.mnDestType
            >> maData.mnLinkType
            >> maData.mnFlags
            >> maData.mnNumFmtIdx;

    mxTokenArray.reset();
    if( GetLinkType() == EXC_CHSRCLINK_WORKSHEET )
    {
        // read token array
        XclTokenArray aXclTokArr;
        rStrm >> aXclTokArr;

        // convert BIFF formula tokens to Calc token array
        if( const ScTokenArray* pTokens = GetFormulaCompiler().CreateFormula( EXC_FMLATYPE_CHART, aXclTokArr ) )
            mxTokenArray.reset( pTokens->Clone() );
    }

    // try to read a following CHSTRING record
    if( (rStrm.GetNextRecId() == EXC_ID_CHSTRING) && rStrm.StartNextRecord() )
    {
        mxString.reset( new XclImpString );
        rStrm.Ignore( 2 );
        mxString->Read( rStrm, EXC_STR_8BITLENGTH | EXC_STR_SEPARATEFORMATS );
    }
}

void XclImpChSourceLink::SetString( const String& rString )
{
    if( !mxString )
        mxString.reset( new XclImpString );
    mxString->SetText( rString );
}

void XclImpChSourceLink::SetTextFormats( const XclFormatRunVec& rFormats )
{
    if( mxString )
        mxString->SetFormats( rFormats );
}

sal_uInt16 XclImpChSourceLink::GetCellCount() const
{
    sal_uInt32 nCellCount = 0;
    if( mxTokenArray )
    {
        mxTokenArray->Reset();
        for( const FormulaToken* pToken = mxTokenArray->First(); pToken; pToken = mxTokenArray->Next() )
        {
            switch( pToken->GetType() )
            {
                case ::formula::svSingleRef:
                case ::formula::svExternalSingleRef:
                    // single cell
                    ++nCellCount;
                break;
                case ::formula::svDoubleRef:
                case ::formula::svExternalDoubleRef:
                {
                    // cell range
                    const ScComplexRefData& rComplexRef = static_cast< const ScToken* >( pToken )->GetDoubleRef();
                    const ScSingleRefData& rRef1 = rComplexRef.Ref1;
                    const ScSingleRefData& rRef2 = rComplexRef.Ref2;
                    sal_uInt32 nTabs = static_cast< sal_uInt32 >( rRef2.nTab - rRef1.nTab + 1 );
                    sal_uInt32 nCols = static_cast< sal_uInt32 >( rRef2.nCol - rRef1.nCol + 1 );
                    sal_uInt32 nRows = static_cast< sal_uInt32 >( rRef2.nRow - rRef1.nRow + 1 );
                    nCellCount += nCols * nRows * nTabs;
                }
                break;
                default: ;
            }
        }
    }
    return limit_cast< sal_uInt16 >( nCellCount );
}

void XclImpChSourceLink::ConvertNumFmt( ScfPropertySet& rPropSet, bool bPercent ) const
{
    bool bLinkToSource = ::get_flag( maData.mnFlags, EXC_CHSRCLINK_NUMFMT );
    sal_uInt32 nScNumFmt = bLinkToSource ? GetNumFmtBuffer().GetScFormat( maData.mnNumFmtIdx ) : NUMBERFORMAT_ENTRY_NOT_FOUND;
    OUString aPropName = bPercent ? EXC_CHPROP_PERCENTAGENUMFMT : EXC_CHPROP_NUMBERFORMAT;
    if( nScNumFmt != NUMBERFORMAT_ENTRY_NOT_FOUND )
        rPropSet.SetProperty( aPropName, static_cast< sal_Int32 >( nScNumFmt ) );
    else
        // restore 'link to source' at data point (series may contain manual number format)
        rPropSet.SetAnyProperty( aPropName, Any() );
}

Reference< XDataSequence > XclImpChSourceLink::CreateDataSequence( const OUString& rRole ) const
{
    Reference< XDataSequence > xDataSeq;
    Reference< XDataProvider > xDataProv = GetDataProvider();
    if( xDataProv.is() && mxTokenArray )
    {
        ScCompiler aComp( GetDocPtr(), ScAddress(), *mxTokenArray );
        aComp.SetGrammar( ::formula::FormulaGrammar::GRAM_ENGLISH );
        OUStringBuffer aRangeRep;
        aComp.CreateStringFromTokenArray( aRangeRep );
        try
        {
            xDataSeq = xDataProv->createDataSequenceByRangeRepresentation( aRangeRep.makeStringAndClear() );
            // set sequence role
            ScfPropertySet aSeqProp( xDataSeq );
            aSeqProp.SetProperty( EXC_CHPROP_ROLE, rRole );
        }
        catch( Exception& )
        {
//            OSL_FAIL( "XclImpChSourceLink::CreateDataSequence - cannot create data sequence" );
        }
    }
    else if( rRole == EXC_CHPROP_ROLE_LABEL && mxString && mxString->GetText().Len() )
    {
        try
        {
            OUString aString = OUString::createFromAscii("\"");
            xDataSeq = xDataProv->createDataSequenceByRangeRepresentation( aString + mxString->GetText() + aString );
            // set sequence role
            ScfPropertySet aSeqProp( xDataSeq );
            aSeqProp.SetProperty( EXC_CHPROP_ROLE, rRole );
        }
        catch( Exception& ) { }
    }
    return xDataSeq;
}

Sequence< Reference< XFormattedString > > XclImpChSourceLink::CreateStringSequence(
        const XclImpChRoot& rRoot, sal_uInt16 nLeadFontIdx, const Color& rLeadFontColor ) const
{
    ::std::vector< Reference< XFormattedString > > aStringVec;
    if( mxString )
    {
        for( XclImpStringIterator aIt( *mxString ); aIt.Is(); ++aIt )
        {
            Reference< XFormattedString > xFmtStr(
                ScfApiHelper::CreateInstance( SERVICE_CHART2_FORMATTEDSTRING ), UNO_QUERY );
            if( xFmtStr.is() )
            {
                // set text data
                xFmtStr->setString( aIt.GetPortionText() );

                // set font formatting and font color
                ScfPropertySet aStringProp( xFmtStr );
                sal_uInt16 nFontIdx = aIt.GetPortionFont();
                if( (nFontIdx == EXC_FONT_NOTFOUND) && (aIt.GetPortionIndex() == 0) )
                    // leading unformatted portion - use passed font settings
                    rRoot.ConvertFont( aStringProp, nLeadFontIdx, &rLeadFontColor );
                else
                    rRoot.ConvertFont( aStringProp, nFontIdx );

                // add string to vector of strings
                aStringVec.push_back( xFmtStr );
            }
        }
    }
    return ScfApiHelper::VectorToSequence( aStringVec );
}

void XclImpChSourceLink::FillSourceLink( ::std::vector< ScTokenRef >& rTokens ) const
{
    if( !mxTokenArray )
        // no links to fill.
        return;

    mxTokenArray->Reset();
    for (FormulaToken* p = mxTokenArray->First(); p; p = mxTokenArray->Next())
    {
        ScTokenRef pToken(static_cast<ScToken*>(p->Clone()));
        if (ScRefTokenHelper::isRef(pToken))
            // This is a reference token.  Store it.
            ScRefTokenHelper::join(rTokens, pToken);
    }
}

// Text =======================================================================

XclImpChFontBase::~XclImpChFontBase()
{
}

void XclImpChFontBase::ConvertFontBase( const XclImpChRoot& rRoot, ScfPropertySet& rPropSet ) const
{
    Color aFontColor = GetFontColor();
    rRoot.ConvertFont( rPropSet, GetFontIndex(), &aFontColor );
}

void XclImpChFontBase::ConvertRotationBase( const XclImpChRoot& rRoot, ScfPropertySet& rPropSet, bool bSupportsStacked ) const
{
    rRoot.GetChartPropSetHelper().WriteRotationProperties( rPropSet, GetRotation(), bSupportsStacked );
}

// ----------------------------------------------------------------------------

XclImpChFont::XclImpChFont() :
    mnFontIdx( EXC_FONT_NOTFOUND )
{
}

void XclImpChFont::ReadChFont( XclImpStream& rStrm )
{
    rStrm >> mnFontIdx;
}

// ----------------------------------------------------------------------------

XclImpChText::XclImpChText( const XclImpChRoot& rRoot ) :
    XclImpChRoot( rRoot )
{
}

void XclImpChText::ReadHeaderRecord( XclImpStream& rStrm )
{
    rStrm   >> maData.mnHAlign
            >> maData.mnVAlign
            >> maData.mnBackMode
            >> maData.maTextColor
            >> maData.maRect
            >> maData.mnFlags;

    if( GetBiff() == EXC_BIFF8 )
    {
        // BIFF8: index into palette used instead of RGB data
        maData.maTextColor = GetPalette().GetColor( rStrm.ReaduInt16() );
        // placement and rotation
        rStrm >> maData.mnFlags2 >> maData.mnRotation;
    }
    else
    {
        // BIFF2-BIFF7: get rotation from text orientation
        sal_uInt8 nOrient = ::extract_value< sal_uInt8 >( maData.mnFlags, 8, 3 );
        maData.mnRotation = XclTools::GetXclRotFromOrient( nOrient );
    }
}

void XclImpChText::ReadSubRecord( XclImpStream& rStrm )
{
    switch( rStrm.GetRecId() )
    {
        case EXC_ID_CHFRAMEPOS:
            mxFramePos.reset( new XclImpChFramePos );
            mxFramePos->ReadChFramePos( rStrm );
        break;
        case EXC_ID_CHFONT:
            mxFont.reset( new XclImpChFont );
            mxFont->ReadChFont( rStrm );
        break;
        case EXC_ID_CHFORMATRUNS:
            if( GetBiff() == EXC_BIFF8 )
                XclImpString::ReadFormats( rStrm, maFormats );
        break;
        case EXC_ID_CHSOURCELINK:
            mxSrcLink.reset( new XclImpChSourceLink( GetChRoot() ) );
            mxSrcLink->ReadChSourceLink( rStrm );
        break;
        case EXC_ID_CHFRAME:
            mxFrame.reset( new XclImpChFrame( GetChRoot(), EXC_CHOBJTYPE_TEXT ) );
            mxFrame->ReadRecordGroup( rStrm );
        break;
        case EXC_ID_CHOBJECTLINK:
            rStrm >> maObjLink.mnTarget >> maObjLink.maPointPos.mnSeriesIdx >> maObjLink.maPointPos.mnPointIdx;
        break;
        case EXC_ID_CHFRLABELPROPS:
            ReadChFrLabelProps( rStrm );
        break;
        case EXC_ID_CHEND:
            if( mxSrcLink && !maFormats.empty() )
                mxSrcLink->SetTextFormats( maFormats );
        break;
    }
}

sal_uInt16 XclImpChText::GetFontIndex() const
{
    return mxFont ? mxFont->GetFontIndex() : EXC_FONT_NOTFOUND;
}

Color XclImpChText::GetFontColor() const
{
    return ::get_flag( maData.mnFlags, EXC_CHTEXT_AUTOCOLOR ) ? GetFontAutoColor() : maData.maTextColor;
}

sal_uInt16 XclImpChText::GetRotation() const
{
    return maData.mnRotation;
}

void XclImpChText::SetString( const String& rString )
{
    if( !mxSrcLink )
        mxSrcLink.reset( new XclImpChSourceLink( GetChRoot() ) );
    mxSrcLink->SetString( rString );
}

void XclImpChText::UpdateText( const XclImpChText* pParentText )
{
    if( pParentText )
    {
        // update missing members
        if( !mxFrame )
            mxFrame = pParentText->mxFrame;
        if( !mxFont )
        {
            mxFont = pParentText->mxFont;
            // text color is taken from CHTEXT record, not from font in CHFONT
            ::set_flag( maData.mnFlags, EXC_CHTEXT_AUTOCOLOR, ::get_flag( pParentText->maData.mnFlags, EXC_CHTEXT_AUTOCOLOR ) );
            maData.maTextColor = pParentText->maData.maTextColor;
        }
    }
}

void XclImpChText::UpdateDataLabel( bool bCateg, bool bValue, bool bPercent )
{
    ::set_flag( maData.mnFlags, EXC_CHTEXT_SHOWCATEG,     bCateg );
    ::set_flag( maData.mnFlags, EXC_CHTEXT_SHOWVALUE,     bValue );
    ::set_flag( maData.mnFlags, EXC_CHTEXT_SHOWPERCENT,   bPercent );
    ::set_flag( maData.mnFlags, EXC_CHTEXT_SHOWCATEGPERC, bCateg && bPercent );
    ::set_flag( maData.mnFlags, EXC_CHTEXT_DELETED,       !bCateg && !bValue && !bPercent );
}

void XclImpChText::ConvertFont( ScfPropertySet& rPropSet ) const
{
    ConvertFontBase( GetChRoot(), rPropSet );
}

void XclImpChText::ConvertRotation( ScfPropertySet& rPropSet, bool bSupportsStacked ) const
{
    ConvertRotationBase( GetChRoot(), rPropSet, bSupportsStacked );
}

void XclImpChText::ConvertFrame( ScfPropertySet& rPropSet ) const
{
    if( mxFrame )
        mxFrame->Convert( rPropSet );
}

void XclImpChText::ConvertNumFmt( ScfPropertySet& rPropSet, bool bPercent ) const
{
    if( mxSrcLink )
        mxSrcLink->ConvertNumFmt( rPropSet, bPercent );
}

void XclImpChText::ConvertDataLabel( ScfPropertySet& rPropSet, const XclChTypeInfo& rTypeInfo ) const
{
    // existing CHFRLABELPROPS record wins over flags from CHTEXT
    sal_uInt16 nShowFlags = mxLabelProps ? mxLabelProps->mnFlags : maData.mnFlags;
    sal_uInt16 SHOWANYCATEG   = mxLabelProps ? EXC_CHFRLABELPROPS_SHOWCATEG : (EXC_CHTEXT_SHOWCATEGPERC | EXC_CHTEXT_SHOWCATEG);
    sal_uInt16 SHOWANYVALUE   = mxLabelProps ? EXC_CHFRLABELPROPS_SHOWVALUE : EXC_CHTEXT_SHOWVALUE;
    sal_uInt16 SHOWANYPERCENT = mxLabelProps ? EXC_CHFRLABELPROPS_SHOWPERCENT : (EXC_CHTEXT_SHOWPERCENT | EXC_CHTEXT_SHOWCATEGPERC);
    sal_uInt16 SHOWANYBUBBLE  = mxLabelProps ? EXC_CHFRLABELPROPS_SHOWBUBBLE : EXC_CHTEXT_SHOWBUBBLE;

    // get raw flags for label values
    bool bShowNone    = IsDeleted();
    bool bShowCateg   = !bShowNone && ::get_flag( nShowFlags, SHOWANYCATEG );
    bool bShowPercent = !bShowNone && ::get_flag( nShowFlags, SHOWANYPERCENT );
    bool bShowValue   = !bShowNone && ::get_flag( nShowFlags, SHOWANYVALUE );
    bool bShowBubble  = !bShowNone && ::get_flag( nShowFlags, SHOWANYBUBBLE );

    // adjust to Chart2 behaviour
    if( rTypeInfo.meTypeId == EXC_CHTYPEID_BUBBLES )
         bShowValue = bShowBubble;  // Chart2 bubble charts show bubble size if 'ShowValue' is set

    // other flags
    bool bShowAny = bShowValue || bShowPercent || bShowCateg;
    bool bShowSymbol = bShowAny && ::get_flag( maData.mnFlags, EXC_CHTEXT_SHOWSYMBOL );

    // create API struct for label values, set API label separator
    cssc2::DataPointLabel aPointLabel( bShowValue, bShowPercent, bShowCateg, bShowSymbol );
    rPropSet.SetProperty( EXC_CHPROP_LABEL, aPointLabel );
    String aSep = mxLabelProps ? mxLabelProps->maSeparator : String( sal_Unicode( '\n' ) );
    if( aSep.Len() == 0 )
        aSep = CREATE_STRING( "; " );
    rPropSet.SetStringProperty( EXC_CHPROP_LABELSEPARATOR, aSep );

    // text properties of attached label
    if( bShowAny )
    {
        ConvertFont( rPropSet );
        ConvertRotation( rPropSet, false );
        // label placement
        using namespace cssc::DataLabelPlacement;
        sal_Int32 nPlacement = rTypeInfo.mnDefaultLabelPos;
        switch( ::extract_value< sal_uInt16 >( maData.mnFlags2, 0, 4 ) )
        {
            case EXC_CHTEXT_POS_DEFAULT:    nPlacement = rTypeInfo.mnDefaultLabelPos;   break;
            case EXC_CHTEXT_POS_OUTSIDE:    nPlacement = OUTSIDE;                       break;
            case EXC_CHTEXT_POS_INSIDE:     nPlacement = INSIDE;                        break;
            case EXC_CHTEXT_POS_CENTER:     nPlacement = CENTER;                        break;
            case EXC_CHTEXT_POS_AXIS:       nPlacement = NEAR_ORIGIN;                   break;
            case EXC_CHTEXT_POS_ABOVE:      nPlacement = TOP;                           break;
            case EXC_CHTEXT_POS_BELOW:      nPlacement = BOTTOM;                        break;
            case EXC_CHTEXT_POS_LEFT:       nPlacement = LEFT;                          break;
            case EXC_CHTEXT_POS_RIGHT:      nPlacement = RIGHT;                         break;
            case EXC_CHTEXT_POS_AUTO:       nPlacement = AVOID_OVERLAP;                 break;
        }
        rPropSet.SetProperty( EXC_CHPROP_LABELPLACEMENT, nPlacement );
        // label number format (percentage format wins over value format)
        if( bShowPercent || bShowValue )
            ConvertNumFmt( rPropSet, bShowPercent );
    }
}

Reference< XTitle > XclImpChText::CreateTitle() const
{
    Reference< XTitle > xTitle;
    if( mxSrcLink && mxSrcLink->HasString() )
    {
        // create the formatted strings
        Sequence< Reference< XFormattedString > > aStringSeq(
            mxSrcLink->CreateStringSequence( GetChRoot(), GetFontIndex(), GetFontColor() ) );
        if( aStringSeq.hasElements() )
        {
            // create the title object
            xTitle.set( ScfApiHelper::CreateInstance( SERVICE_CHART2_TITLE ), UNO_QUERY );
            if( xTitle.is() )
            {
                // set the formatted strings
                xTitle->setText( aStringSeq );
                // more title formatting properties
                ScfPropertySet aTitleProp( xTitle );
                ConvertFrame( aTitleProp );
                ConvertRotation( aTitleProp, true );
            }
        }
    }
    return xTitle;
}

void XclImpChText::ConvertTitlePosition( const XclChTextKey& rTitleKey ) const
{
    if( !mxFramePos ) return;

    const XclChFramePos& rPosData = mxFramePos->GetFramePosData();
    OSL_ENSURE( (rPosData.mnTLMode == EXC_CHFRAMEPOS_PARENT) && (rPosData.mnBRMode == EXC_CHFRAMEPOS_PARENT),
        "XclImpChText::ConvertTitlePosition - unexpected frame position mode" );

    /*  Check if title is moved manually. To get the actual position of the
        title, we do some kind of hack and use the values from the CHTEXT
        record, effectively ignoring the contents of the CHFRAMEPOS record
        which contains the position relative to the default title position
        (according to the spec, the CHFRAMEPOS supersedes the CHTEXT record).
        Especially when it comes to axis titles, things would become very
        complicated here, because the relative title position is stored in a
        measurement unit that is dependent on the size of the inner plot area,
        the interpretation of the X and Y coordinate is dependent on the
        direction of the axis, and in 3D charts, and the title default
        positions are dependent on the 3D view settings (rotation, elevation,
        and perspective). Thus, it is easier to assume that the creator has
        written out the correct absolute position and size of the title in the
        CHTEXT record. This is assured by checking that the shape size stored
        in the CHTEXT record is non-zero. */
    if( (rPosData.mnTLMode == EXC_CHFRAMEPOS_PARENT) &&
        ((rPosData.maRect.mnX != 0) || (rPosData.maRect.mnY != 0)) &&
        (maData.maRect.mnWidth > 0) && (maData.maRect.mnHeight > 0) ) try
    {
        Reference< XShape > xTitleShape( GetTitleShape( rTitleKey ), UNO_SET_THROW );
        // the call to XShape.getSize() may recalc the chart view
        ::com::sun::star::awt::Size aTitleSize = xTitleShape->getSize();
        // rotated titles need special handling...
        sal_Int32 nScRot = XclTools::GetScRotation( GetRotation(), 0 );
        double fRad = nScRot * F_PI18000;
        double fSin = fabs( sin( fRad ) );
        double fCos = fabs( cos( fRad ) );
        ::com::sun::star::awt::Size aBoundSize(
            static_cast< sal_Int32 >( fCos * aTitleSize.Width + fSin * aTitleSize.Height + 0.5 ),
            static_cast< sal_Int32 >( fSin * aTitleSize.Width + fCos * aTitleSize.Height + 0.5 ) );
        // calculate the title position from the values in the CHTEXT record
        ::com::sun::star::awt::Point aTitlePos(
            CalcHmmFromChartX( maData.maRect.mnX ),
            CalcHmmFromChartY( maData.maRect.mnY ) );
        // add part of height to X direction, if title is rotated down (clockwise)
        if( nScRot > 18000 )
            aTitlePos.X += static_cast< sal_Int32 >( fSin * aTitleSize.Height + 0.5 );
        // add part of width to Y direction, if title is rotated up (counterclockwise)
        else if( nScRot > 0 )
            aTitlePos.Y += static_cast< sal_Int32 >( fSin * aTitleSize.Width + 0.5 );
        // set the resulting position at the title shape
        xTitleShape->setPosition( aTitlePos );
    }
    catch( Exception& )
    {
    }
}

void XclImpChText::ReadChFrLabelProps( XclImpStream& rStrm )
{
    if( GetBiff() == EXC_BIFF8 )
    {
        mxLabelProps.reset( new XclChFrLabelProps );
        sal_uInt16 nSepLen;
        rStrm.Ignore( 12 );
        rStrm >> mxLabelProps->mnFlags >> nSepLen;
        if( nSepLen > 0 )
            mxLabelProps->maSeparator = rStrm.ReadUniString( nSepLen );
    }
}

namespace {

void lclUpdateText( XclImpChTextRef& rxText, const XclImpChText* xDefText )
{
    if( rxText )
        rxText->UpdateText( xDefText );
    else
    {
        XclImpChTextRef xNew(new XclImpChText(*xDefText));
        rxText = xNew;
    }
}

void lclFinalizeTitle( XclImpChTextRef& rxTitle, const XclImpChText* pDefText, const String& rAutoTitle )
{
    /*  Do not update a title, if it is not visible (if rxTitle is null).
        Existing reference indicates enabled title. */
    if( rxTitle )
    {
        if( !rxTitle->HasString() )
            rxTitle->SetString( rAutoTitle );
        if( rxTitle->HasString() )
            rxTitle->UpdateText(pDefText);
        else
            rxTitle.reset();
    }
}

} // namespace

// Data series ================================================================

void XclImpChMarkerFormat::ReadChMarkerFormat( XclImpStream& rStrm )
{
    rStrm >> maData.maLineColor >> maData.maFillColor >> maData.mnMarkerType >> maData.mnFlags;

    const XclImpRoot& rRoot = rStrm.GetRoot();
    if( rRoot.GetBiff() == EXC_BIFF8 )
    {
        // BIFF8: index into palette used instead of RGB data
        const XclImpPalette& rPal = rRoot.GetPalette();
        maData.maLineColor = rPal.GetColor( rStrm.ReaduInt16() );
        maData.maFillColor = rPal.GetColor( rStrm.ReaduInt16() );
        // marker size
        rStrm >> maData.mnMarkerSize;
    }
}

void XclImpChMarkerFormat::Convert( const XclImpChRoot& rRoot,
        ScfPropertySet& rPropSet, sal_uInt16 nFormatIdx, sal_Int16 nLineWeight ) const
{
    if( IsAuto() )
    {
        XclChMarkerFormat aMarkerFmt;
        // line and fill color of the symbol are equal to series line color
        //! TODO: Excel sets no fill color for specific symbols (e.g. cross)
        aMarkerFmt.maLineColor = aMarkerFmt.maFillColor = rRoot.GetSeriesLineAutoColor( nFormatIdx );
        switch( nLineWeight )
        {
            case EXC_CHLINEFORMAT_HAIR:     aMarkerFmt.mnMarkerSize = EXC_CHMARKERFORMAT_HAIRSIZE;      break;
            case EXC_CHLINEFORMAT_SINGLE:   aMarkerFmt.mnMarkerSize = EXC_CHMARKERFORMAT_SINGLESIZE;    break;
            case EXC_CHLINEFORMAT_DOUBLE:   aMarkerFmt.mnMarkerSize = EXC_CHMARKERFORMAT_DOUBLESIZE;    break;
            case EXC_CHLINEFORMAT_TRIPLE:   aMarkerFmt.mnMarkerSize = EXC_CHMARKERFORMAT_TRIPLESIZE;    break;
            default:                        aMarkerFmt.mnMarkerSize = EXC_CHMARKERFORMAT_SINGLESIZE;
        }
        aMarkerFmt.mnMarkerType = XclChartHelper::GetAutoMarkerType( nFormatIdx );
        rRoot.GetChartPropSetHelper().WriteMarkerProperties( rPropSet, aMarkerFmt );
    }
    else
    {
        rRoot.GetChartPropSetHelper().WriteMarkerProperties( rPropSet, maData );
    }
}

void XclImpChMarkerFormat::ConvertColor( const XclImpChRoot& rRoot,
        ScfPropertySet& rPropSet, sal_uInt16 nFormatIdx ) const
{
    Color aLineColor = IsAuto() ? rRoot.GetSeriesLineAutoColor( nFormatIdx ) : maData.maFillColor;
    rPropSet.SetColorProperty( EXC_CHPROP_COLOR, aLineColor );
}

// ----------------------------------------------------------------------------

XclImpChPieFormat::XclImpChPieFormat() :
    mnPieDist( 0 )
{
}

void XclImpChPieFormat::ReadChPieFormat( XclImpStream& rStrm )
{
    rStrm >> mnPieDist;
}

void XclImpChPieFormat::Convert( ScfPropertySet& rPropSet ) const
{
    double fApiDist = ::std::min< double >( mnPieDist / 100.0, 1.0 );
    rPropSet.SetProperty( EXC_CHPROP_OFFSET, fApiDist );
}

// ----------------------------------------------------------------------------

XclImpChSeriesFormat::XclImpChSeriesFormat() :
    mnFlags( 0 )
{
}

void XclImpChSeriesFormat::ReadChSeriesFormat( XclImpStream& rStrm )
{
    rStrm >> mnFlags;
}

// ----------------------------------------------------------------------------

void XclImpCh3dDataFormat::ReadCh3dDataFormat( XclImpStream& rStrm )
{
    rStrm >> maData.mnBase >> maData.mnTop;
}

void XclImpCh3dDataFormat::Convert( ScfPropertySet& rPropSet ) const
{
    using namespace ::com::sun::star::chart2::DataPointGeometry3D;
    sal_Int32 nApiType = (maData.mnBase == EXC_CH3DDATAFORMAT_RECT) ?
        ((maData.mnTop == EXC_CH3DDATAFORMAT_STRAIGHT) ? CUBOID : PYRAMID) :
        ((maData.mnTop == EXC_CH3DDATAFORMAT_STRAIGHT) ? CYLINDER : CONE);
    rPropSet.SetProperty( EXC_CHPROP_GEOMETRY3D, nApiType );
}

// ----------------------------------------------------------------------------

XclImpChAttachedLabel::XclImpChAttachedLabel( const XclImpChRoot& rRoot ) :
    XclImpChRoot( rRoot ),
    mnFlags( 0 )
{
}

void XclImpChAttachedLabel::ReadChAttachedLabel( XclImpStream& rStrm )
{
    rStrm >> mnFlags;
}

XclImpChTextRef XclImpChAttachedLabel::CreateDataLabel( const XclImpChText* pParent ) const
{
    const sal_uInt16 EXC_CHATTLABEL_SHOWANYVALUE = EXC_CHATTLABEL_SHOWVALUE;
    const sal_uInt16 EXC_CHATTLABEL_SHOWANYPERCENT = EXC_CHATTLABEL_SHOWPERCENT | EXC_CHATTLABEL_SHOWCATEGPERC;
    const sal_uInt16 EXC_CHATTLABEL_SHOWANYCATEG = EXC_CHATTLABEL_SHOWCATEG | EXC_CHATTLABEL_SHOWCATEGPERC;

    XclImpChTextRef xLabel( pParent ? new XclImpChText( *pParent ) : new XclImpChText( GetChRoot() ) );
    xLabel->UpdateDataLabel(
        ::get_flag( mnFlags, EXC_CHATTLABEL_SHOWANYCATEG ),
        ::get_flag( mnFlags, EXC_CHATTLABEL_SHOWANYVALUE ),
        ::get_flag( mnFlags, EXC_CHATTLABEL_SHOWANYPERCENT ) );
    return xLabel;
}

// ----------------------------------------------------------------------------

XclImpChDataFormat::XclImpChDataFormat( const XclImpChRoot& rRoot ) :
    XclImpChRoot( rRoot )
{
}

void XclImpChDataFormat::ReadHeaderRecord( XclImpStream& rStrm )
{
    rStrm   >> maData.maPointPos.mnPointIdx
            >> maData.maPointPos.mnSeriesIdx
            >> maData.mnFormatIdx
            >> maData.mnFlags;
}

void XclImpChDataFormat::ReadSubRecord( XclImpStream& rStrm )
{
    switch( rStrm.GetRecId() )
    {
        case EXC_ID_CHMARKERFORMAT:
            mxMarkerFmt.reset( new XclImpChMarkerFormat );
            mxMarkerFmt->ReadChMarkerFormat( rStrm );
        break;
        case EXC_ID_CHPIEFORMAT:
            mxPieFmt.reset( new XclImpChPieFormat );
            mxPieFmt->ReadChPieFormat( rStrm );
        break;
        case EXC_ID_CHSERIESFORMAT:
            mxSeriesFmt.reset( new XclImpChSeriesFormat );
            mxSeriesFmt->ReadChSeriesFormat( rStrm );
        break;
        case EXC_ID_CH3DDATAFORMAT:
            mx3dDataFmt.reset( new XclImpCh3dDataFormat );
            mx3dDataFmt->ReadCh3dDataFormat( rStrm );
        break;
        case EXC_ID_CHATTACHEDLABEL:
            mxAttLabel.reset( new XclImpChAttachedLabel( GetChRoot() ) );
            mxAttLabel->ReadChAttachedLabel( rStrm );
        break;
        default:
            XclImpChFrameBase::ReadSubRecord( rStrm );
    }
}

void XclImpChDataFormat::SetPointPos( const XclChDataPointPos& rPointPos, sal_uInt16 nFormatIdx )
{
    maData.maPointPos = rPointPos;
    maData.mnFormatIdx = nFormatIdx;
}

void XclImpChDataFormat::UpdateGroupFormat( const XclChExtTypeInfo& rTypeInfo )
{
    // remove formats not used for the current chart type
    RemoveUnusedFormats( rTypeInfo );
}

void XclImpChDataFormat::UpdateSeriesFormat( const XclChExtTypeInfo& rTypeInfo, const XclImpChDataFormat* pGroupFmt )
{
    // update missing formats from passed chart type group format
    if( pGroupFmt )
    {
        if( !mxLineFmt )
            mxLineFmt = pGroupFmt->mxLineFmt;
        if( !mxAreaFmt && !mxEscherFmt )
        {
            mxAreaFmt = pGroupFmt->mxAreaFmt;
            mxEscherFmt = pGroupFmt->mxEscherFmt;
        }
        if( !mxMarkerFmt )
            mxMarkerFmt = pGroupFmt->mxMarkerFmt;
        if( !mxPieFmt )
            mxPieFmt = pGroupFmt->mxPieFmt;
        if( !mxSeriesFmt )
            mxSeriesFmt = pGroupFmt->mxSeriesFmt;
        if( !mx3dDataFmt )
            mx3dDataFmt = pGroupFmt->mx3dDataFmt;
        if( !mxAttLabel )
            mxAttLabel = pGroupFmt->mxAttLabel;
    }

    /*  Create missing but required formats. Existing line, area, and marker
        format objects are needed to create automatic series formatting. */
    if( !mxLineFmt )
        mxLineFmt.reset( new XclImpChLineFormat );
    if( !mxAreaFmt && !mxEscherFmt )
        mxAreaFmt.reset( new XclImpChAreaFormat );
    if( !mxMarkerFmt )
        mxMarkerFmt.reset( new XclImpChMarkerFormat );

    // remove formats not used for the current chart type
    RemoveUnusedFormats( rTypeInfo );
    // update data label
    UpdateDataLabel( pGroupFmt );
}

void XclImpChDataFormat::UpdatePointFormat( const XclChExtTypeInfo& rTypeInfo, const XclImpChDataFormat* pSeriesFmt )
{
    // remove formats if they are automatic in this and in the passed series format
    if( pSeriesFmt )
    {
        if( IsAutoLine() && pSeriesFmt->IsAutoLine() )
            mxLineFmt.reset();
        if( IsAutoArea() && pSeriesFmt->IsAutoArea() )
            mxAreaFmt.reset();
        if( IsAutoMarker() && pSeriesFmt->IsAutoMarker() )
            mxMarkerFmt.reset();
        mxSeriesFmt.reset();
    }

    // Excel ignores 3D bar format for single data points
    mx3dDataFmt.reset();
    // remove point line formats for linear chart types, TODO: implement in OOChart
    if( !rTypeInfo.IsSeriesFrameFormat() )
        mxLineFmt.reset();

    // remove formats not used for the current chart type
    RemoveUnusedFormats( rTypeInfo );
    // update data label
    UpdateDataLabel( pSeriesFmt );
}

void XclImpChDataFormat::UpdateTrendLineFormat()
{
    if( !mxLineFmt )
        mxLineFmt.reset( new XclImpChLineFormat );
    mxAreaFmt.reset();
    mxEscherFmt.reset();
    mxMarkerFmt.reset();
    mxPieFmt.reset();
    mxSeriesFmt.reset();
    mx3dDataFmt.reset();
    mxAttLabel.reset();
    // update data label
    UpdateDataLabel( 0 );
}

void XclImpChDataFormat::Convert( ScfPropertySet& rPropSet, const XclChExtTypeInfo& rTypeInfo ) const
{
    // line and area format
    ConvertFrameBase( GetChRoot(), rPropSet, rTypeInfo.GetSeriesObjectType(), maData.mnFormatIdx );
#if EXC_CHART2_3DBAR_HAIRLINES_ONLY
    // #i83151# only hair lines in 3D charts with filled data points
    if( rTypeInfo.mb3dChart && rTypeInfo.IsSeriesFrameFormat() && mxLineFmt && mxLineFmt->HasLine() )
        rPropSet.SetProperty< sal_Int32 >( CREATE_OUSTRING( "BorderWidth" ), 0 );
#endif

    // other formatting
    if( mxMarkerFmt )
        mxMarkerFmt->Convert( GetChRoot(), rPropSet, maData.mnFormatIdx, GetLineWeight() );
    if( mxPieFmt )
        mxPieFmt->Convert( rPropSet );
    if( mx3dDataFmt )
        mx3dDataFmt->Convert( rPropSet );
    if( mxLabel )
        mxLabel->ConvertDataLabel( rPropSet, rTypeInfo );

    // 3D settings
    rPropSet.SetProperty< sal_Int16 >( EXC_CHPROP_PERCENTDIAGONAL, 0 );

    /*  Special case: set marker color as line color, if series line is not
        visible. This makes the color visible in the marker area.
        TODO: remove this if OOChart supports own colors in markers. */
    if( !rTypeInfo.IsSeriesFrameFormat() && !HasLine() && mxMarkerFmt )
        mxMarkerFmt->ConvertColor( GetChRoot(), rPropSet, maData.mnFormatIdx );
}

void XclImpChDataFormat::ConvertLine( ScfPropertySet& rPropSet, XclChObjectType eObjType ) const
{
    ConvertLineBase( GetChRoot(), rPropSet, eObjType );
}

void XclImpChDataFormat::ConvertArea( ScfPropertySet& rPropSet, sal_uInt16 nFormatIdx ) const
{
    ConvertAreaBase( GetChRoot(), rPropSet, EXC_CHOBJTYPE_FILLEDSERIES, nFormatIdx );
}

void XclImpChDataFormat::RemoveUnusedFormats( const XclChExtTypeInfo& rTypeInfo )
{
    // data point marker only in linear 2D charts
    if( rTypeInfo.IsSeriesFrameFormat() )
        mxMarkerFmt.reset();
    // pie format only in pie/donut charts
    if( rTypeInfo.meTypeCateg != EXC_CHTYPECATEG_PIE )
        mxPieFmt.reset();
    // 3D format only in 3D bar charts
    if( !rTypeInfo.mb3dChart || (rTypeInfo.meTypeCateg != EXC_CHTYPECATEG_BAR) )
        mx3dDataFmt.reset();
}

void XclImpChDataFormat::UpdateDataLabel( const XclImpChDataFormat* pParentFmt )
{
    /*  CHTEXT groups linked to data labels override existing CHATTACHEDLABEL
        records. Only if there is a CHATTACHEDLABEL record without a CHTEXT
        group, the contents of the CHATTACHEDLABEL record are used. In this
        case a new CHTEXT group is created and filled with the settings from
        the CHATTACHEDLABEL record. */
    const XclImpChText* pDefText = NULL;
    if (pParentFmt)
        pDefText = pParentFmt->GetDataLabel();
    if (!pDefText)
        pDefText = GetChartData().GetDefaultText( EXC_CHTEXTTYPE_DATALABEL );
    if (mxLabel)
        mxLabel->UpdateText(pDefText);
    else if (mxAttLabel)
        mxLabel = mxAttLabel->CreateDataLabel( pDefText );
}

// ----------------------------------------------------------------------------

XclImpChSerTrendLine::XclImpChSerTrendLine( const XclImpChRoot& rRoot ) :
    XclImpChRoot( rRoot )
{
}

void XclImpChSerTrendLine::ReadChSerTrendLine( XclImpStream& rStrm )
{
    rStrm   >> maData.mnLineType
            >> maData.mnOrder
            >> maData.mfIntercept
            >> maData.mnShowEquation
            >> maData.mnShowRSquared
            >> maData.mfForecastFor
            >> maData.mfForecastBack;
}

Reference< XRegressionCurve > XclImpChSerTrendLine::CreateRegressionCurve() const
{
    // trend line type
    OUString aService;
    switch( maData.mnLineType )
    {
        case EXC_CHSERTREND_POLYNOMIAL:
            // TODO: only linear trend lines are supported by OOChart (#i20819#)
            if( maData.mnOrder == 1 )
                aService = SERVICE_CHART2_LINEARREGCURVE;
        break;
        case EXC_CHSERTREND_EXPONENTIAL:
            aService = SERVICE_CHART2_EXPREGCURVE;
        break;
        case EXC_CHSERTREND_LOGARITHMIC:
            aService = SERVICE_CHART2_LOGREGCURVE;
        break;
        case EXC_CHSERTREND_POWER:
            aService = SERVICE_CHART2_POTREGCURVE;
        break;
    }
    Reference< XRegressionCurve > xRegCurve;
    if( aService.getLength() > 0 )
        xRegCurve.set( ScfApiHelper::CreateInstance( aService ), UNO_QUERY );

    // trend line formatting
    if( xRegCurve.is() && mxDataFmt )
    {
        ScfPropertySet aPropSet( xRegCurve );
        mxDataFmt->ConvertLine( aPropSet, EXC_CHOBJTYPE_TRENDLINE );

        // #i83100# show equation and correlation coefficient
        ScfPropertySet aLabelProp( xRegCurve->getEquationProperties() );
        aLabelProp.SetBoolProperty( EXC_CHPROP_SHOWEQUATION, maData.mnShowEquation != 0 );
        aLabelProp.SetBoolProperty( EXC_CHPROP_SHOWCORRELATION, maData.mnShowRSquared != 0 );

        // #i83100# formatting of the equation text box
        if (const XclImpChText* pLabel = mxDataFmt->GetDataLabel())
        {
            pLabel->ConvertFont( aLabelProp );
            pLabel->ConvertFrame( aLabelProp );
            pLabel->ConvertNumFmt( aLabelProp, false );
        }
    }

    // missing features
    // #i20819# polynomial trend lines
    // #i66819# moving average trend lines
    // #i5085# manual trend line size
    // #i34093# manual crossing point

    return xRegCurve;
}

// ----------------------------------------------------------------------------

XclImpChSerErrorBar::XclImpChSerErrorBar( const XclImpChRoot& rRoot ) :
    XclImpChRoot( rRoot )
{
}

void XclImpChSerErrorBar::ReadChSerErrorBar( XclImpStream& rStrm )
{
    rStrm >> maData.mnBarType >> maData.mnSourceType >> maData.mnLineEnd;
    rStrm.Ignore( 1 );
    rStrm >> maData.mfValue >> maData.mnValueCount;
}

void XclImpChSerErrorBar::SetSeriesData( XclImpChSourceLinkRef xValueLink, XclImpChDataFormatRef xDataFmt )
{
    mxValueLink = xValueLink;
    mxDataFmt = xDataFmt;
}

Reference< XLabeledDataSequence > XclImpChSerErrorBar::CreateValueSequence() const
{
    return lclCreateLabeledDataSequence( mxValueLink, XclChartHelper::GetErrorBarValuesRole( maData.mnBarType ) );
}

Reference< XPropertySet > XclImpChSerErrorBar::CreateErrorBar( const XclImpChSerErrorBar* pPosBar, const XclImpChSerErrorBar* pNegBar )
{
    Reference< XPropertySet > xErrorBar;

    if( const XclImpChSerErrorBar* pPrimaryBar = pPosBar ? pPosBar : pNegBar )
    {
        xErrorBar.set( ScfApiHelper::CreateInstance( SERVICE_CHART2_ERRORBAR ), UNO_QUERY );
        ScfPropertySet aBarProp( xErrorBar );

        // plus/minus bars visible?
        aBarProp.SetBoolProperty( EXC_CHPROP_SHOWPOSITIVEERROR, pPosBar != 0 );
        aBarProp.SetBoolProperty( EXC_CHPROP_SHOWNEGATIVEERROR, pNegBar != 0 );

        // type of displayed error
        switch( pPrimaryBar->maData.mnSourceType )
        {
            case EXC_CHSERERR_PERCENT:
                aBarProp.SetProperty( EXC_CHPROP_ERRORBARSTYLE, cssc::ErrorBarStyle::RELATIVE );
                aBarProp.SetProperty( EXC_CHPROP_POSITIVEERROR, pPrimaryBar->maData.mfValue );
                aBarProp.SetProperty( EXC_CHPROP_NEGATIVEERROR, pPrimaryBar->maData.mfValue );
            break;
            case EXC_CHSERERR_FIXED:
                aBarProp.SetProperty( EXC_CHPROP_ERRORBARSTYLE, cssc::ErrorBarStyle::ABSOLUTE );
                aBarProp.SetProperty( EXC_CHPROP_POSITIVEERROR, pPrimaryBar->maData.mfValue );
                aBarProp.SetProperty( EXC_CHPROP_NEGATIVEERROR, pPrimaryBar->maData.mfValue );
            break;
            case EXC_CHSERERR_STDDEV:
                aBarProp.SetProperty( EXC_CHPROP_ERRORBARSTYLE, cssc::ErrorBarStyle::STANDARD_DEVIATION );
                aBarProp.SetProperty( EXC_CHPROP_WEIGHT, pPrimaryBar->maData.mfValue );
            break;
            case EXC_CHSERERR_STDERR:
                aBarProp.SetProperty( EXC_CHPROP_ERRORBARSTYLE, cssc::ErrorBarStyle::STANDARD_ERROR );
            break;
            case EXC_CHSERERR_CUSTOM:
            {
                aBarProp.SetProperty( EXC_CHPROP_ERRORBARSTYLE, cssc::ErrorBarStyle::FROM_DATA );
                // attach data sequences to erorr bar
                Reference< XDataSink > xDataSink( xErrorBar, UNO_QUERY );
                if( xDataSink.is() )
                {
                    // create vector of all value sequences
                    ::std::vector< Reference< XLabeledDataSequence > > aLabeledSeqVec;
                    // add positive values
                    if( pPosBar )
                    {
                        Reference< XLabeledDataSequence > xValueSeq = pPosBar->CreateValueSequence();
                        if( xValueSeq.is() )
                            aLabeledSeqVec.push_back( xValueSeq );
                    }
                    // add negative values
                    if( pNegBar )
                    {
                        Reference< XLabeledDataSequence > xValueSeq = pNegBar->CreateValueSequence();
                        if( xValueSeq.is() )
                            aLabeledSeqVec.push_back( xValueSeq );
                    }
                    // attach labeled data sequences to series
                    if( aLabeledSeqVec.empty() )
                        xErrorBar.clear();
                    else
                        xDataSink->setData( ScfApiHelper::VectorToSequence( aLabeledSeqVec ) );
                }
            }
            break;
            default:
                xErrorBar.clear();
        }

        // error bar formatting
        if( pPrimaryBar->mxDataFmt && xErrorBar.is() )
            pPrimaryBar->mxDataFmt->ConvertLine( aBarProp, EXC_CHOBJTYPE_ERRORBAR );
    }

    return xErrorBar;
}

// ----------------------------------------------------------------------------

XclImpChSeries::XclImpChSeries( const XclImpChRoot& rRoot, sal_uInt16 nSeriesIdx ) :
    XclImpChRoot( rRoot ),
    mnGroupIdx( EXC_CHSERGROUP_NONE ),
    mnSeriesIdx( nSeriesIdx ),
    mnParentIdx( EXC_CHSERIES_INVALID )
{
}

void XclImpChSeries::ReadHeaderRecord( XclImpStream& rStrm )
{
    rStrm >> maData.mnCategType >> maData.mnValueType >> maData.mnCategCount >> maData.mnValueCount;
    if( GetBiff() == EXC_BIFF8 )
        rStrm >> maData.mnBubbleType >> maData.mnBubbleCount;
}

void XclImpChSeries::ReadSubRecord( XclImpStream& rStrm )
{
    switch( rStrm.GetRecId() )
    {
        case EXC_ID_CHSOURCELINK:
            ReadChSourceLink( rStrm );
        break;
        case EXC_ID_CHDATAFORMAT:
            ReadChDataFormat( rStrm );
        break;
        case EXC_ID_CHSERGROUP:
            rStrm >> mnGroupIdx;
        break;
        case EXC_ID_CHSERPARENT:
            ReadChSerParent( rStrm );
        break;
        case EXC_ID_CHSERTRENDLINE:
            ReadChSerTrendLine( rStrm );
        break;
        case EXC_ID_CHSERERRORBAR:
            ReadChSerErrorBar( rStrm );
        break;
    }
}

void XclImpChSeries::SetDataFormat( XclImpChDataFormatRef xDataFmt )
{
    if( xDataFmt )
    {
        XclImpChDataFormatRef* pxDataFmt = GetDataFormatRef( xDataFmt->GetPointPos().mnPointIdx );
        // do not overwrite existing data format
        if( pxDataFmt && !*pxDataFmt )
        {
            *pxDataFmt = xDataFmt;
            // #i51639# register series format index at chart type group
            if( (pxDataFmt == &mxSeriesFmt) && !HasParentSeries() )
                if( XclImpChTypeGroup* pTypeGroup = GetChartData().GetTypeGroup( mnGroupIdx ).get() )
                    pTypeGroup->SetUsedFormatIndex( xDataFmt->GetFormatIdx() );
        }
    }
}

void XclImpChSeries::SetDataLabel( XclImpChTextRef xLabel )
{
    if( xLabel )
    {
        XclImpChTextRef* pxLabel = GetDataLabelRef( xLabel->GetPointPos().mnPointIdx );
        if( pxLabel && !*pxLabel )
            *pxLabel = xLabel;
    }
}

void XclImpChSeries::AddChildSeries( const XclImpChSeries& rSeries )
{
    OSL_ENSURE( !HasParentSeries(), "XclImpChSeries::AddChildSeries - not allowed for child series" );

    /*  In Excel, trend lines and error bars are stored as own series. In Calc,
        these are properties of the parent series. This function adds the
        settings of the passed series to this series. */
    maTrendLines.insert( maTrendLines.end(), rSeries.maTrendLines.begin(), rSeries.maTrendLines.end() );
    maErrorBars.insert( rSeries.maErrorBars.begin(), rSeries.maErrorBars.end() );
}

void XclImpChSeries::FinalizeDataFormats()
{
    if( HasParentSeries() )
    {
        // *** series is a child series, e.g. trend line or error bar ***

        // create missing series format
        if( !mxSeriesFmt )
            mxSeriesFmt = CreateDataFormat( EXC_CHDATAFORMAT_ALLPOINTS, 0 );

        if( mxSeriesFmt )
        {
            // #i83100# set text label format, e.g. for trend line equations
            XclImpChTextRef xLabel;
            XclImpChTextMap::iterator itr = maLabels.find(EXC_CHDATAFORMAT_ALLPOINTS);
            if (itr != maLabels.end())
                xLabel = itr->second;
            mxSeriesFmt->SetDataLabel(xLabel);
            // create missing automatic formats
            mxSeriesFmt->UpdateTrendLineFormat();
        }

        // copy series formatting to child objects
        for( XclImpChSerTrendLineList::iterator aLIt = maTrendLines.begin(), aLEnd = maTrendLines.end(); aLIt != aLEnd; ++aLIt )
            (*aLIt)->SetDataFormat( mxSeriesFmt );
        for( XclImpChSerErrorBarMap::iterator aMIt = maErrorBars.begin(), aMEnd = maErrorBars.end(); aMIt != aMEnd; ++aMIt )
            aMIt->second->SetSeriesData( mxValueLink, mxSeriesFmt );
    }
    else if( XclImpChTypeGroup* pTypeGroup = GetChartData().GetTypeGroup( mnGroupIdx ).get() )
    {
        // *** series is a regular data series ***

        // create missing series format
        if( !mxSeriesFmt )
        {
            // #i51639# use a new unused format index to create series default format
            sal_uInt16 nFormatIdx = pTypeGroup->PopUnusedFormatIndex();
            mxSeriesFmt = CreateDataFormat( EXC_CHDATAFORMAT_ALLPOINTS, nFormatIdx );
        }

        // set text labels to data formats
        for( XclImpChTextMap::iterator aTIt = maLabels.begin(), aTEnd = maLabels.end(); aTIt != aTEnd; ++aTIt )
        {
            if( XclImpChDataFormatRef* pxDataFmt = GetDataFormatRef( aTIt->first ) )
            {
                if( !*pxDataFmt )
                    *pxDataFmt = CreateDataFormat( aTIt->first, EXC_CHDATAFORMAT_DEFAULT );
                (*pxDataFmt)->SetDataLabel( aTIt->second );
            }
        }

        // update series format (copy missing formatting from group default format)
        if( mxSeriesFmt )
            mxSeriesFmt->UpdateSeriesFormat( pTypeGroup->GetTypeInfo(), pTypeGroup->GetGroupFormat().get() );

        // update data point formats (removes unchanged automatic formatting)
        for( XclImpChDataFormatMap::iterator aFIt = maPointFmts.begin(), aFEnd = maPointFmts.end(); aFIt != aFEnd; ++aFIt )
            aFIt->second->UpdatePointFormat( pTypeGroup->GetTypeInfo(), mxSeriesFmt.get() );
    }
}

namespace {

/** Returns the property set of the specified data point. */
ScfPropertySet lclGetPointPropSet( Reference< XDataSeries > xDataSeries, sal_uInt16 nPointIdx )
{
    ScfPropertySet aPropSet;
    try
    {
        aPropSet.Set( xDataSeries->getDataPointByIndex( static_cast< sal_Int32 >( nPointIdx ) ) );
    }
    catch( Exception& )
    {
        OSL_FAIL( "lclGetPointPropSet - no data point property set" );
    }
    return aPropSet;
}

} // namespace

Reference< XLabeledDataSequence > XclImpChSeries::CreateValueSequence( const OUString& rValueRole ) const
{
    return lclCreateLabeledDataSequence( mxValueLink, rValueRole, mxTitleLink.get() );
}

Reference< XLabeledDataSequence > XclImpChSeries::CreateCategSequence( const OUString& rCategRole ) const
{
    return lclCreateLabeledDataSequence( mxCategLink, rCategRole );
}

Reference< XDataSeries > XclImpChSeries::CreateDataSeries() const
{
    Reference< XDataSeries > xDataSeries;
    if( const XclImpChTypeGroup* pTypeGroup = GetChartData().GetTypeGroup( mnGroupIdx ).get() )
    {
        const XclChExtTypeInfo& rTypeInfo = pTypeGroup->GetTypeInfo();

        // create the data series object
        xDataSeries.set( ScfApiHelper::CreateInstance( SERVICE_CHART2_DATASERIES ), UNO_QUERY );

        // attach data and title sequences to series
        Reference< XDataSink > xDataSink( xDataSeries, UNO_QUERY );
        if( xDataSink.is() )
        {
            // create vector of all value sequences
            ::std::vector< Reference< XLabeledDataSequence > > aLabeledSeqVec;
            // add Y values
            Reference< XLabeledDataSequence > xYValueSeq =
                CreateValueSequence( EXC_CHPROP_ROLE_YVALUES );
            if( xYValueSeq.is() )
                aLabeledSeqVec.push_back( xYValueSeq );
            // add X values
            if( !rTypeInfo.mbCategoryAxis )
            {
                Reference< XLabeledDataSequence > xXValueSeq =
                    CreateCategSequence( EXC_CHPROP_ROLE_XVALUES );
                if( xXValueSeq.is() )
                    aLabeledSeqVec.push_back( xXValueSeq );
                // add size values of bubble charts
                if( rTypeInfo.meTypeId == EXC_CHTYPEID_BUBBLES )
                {
                    Reference< XLabeledDataSequence > xSizeValueSeq =
                        lclCreateLabeledDataSequence( mxBubbleLink, EXC_CHPROP_ROLE_SIZEVALUES, mxTitleLink.get() );
                    if( xSizeValueSeq.is() )
                        aLabeledSeqVec.push_back( xSizeValueSeq );
                }
            }
            // attach labeled data sequences to series
            if( !aLabeledSeqVec.empty() )
                xDataSink->setData( ScfApiHelper::VectorToSequence( aLabeledSeqVec ) );
        }

        // series formatting
        ScfPropertySet aSeriesProp( xDataSeries );
        if( mxSeriesFmt )
            mxSeriesFmt->Convert( aSeriesProp, rTypeInfo );

        // trend lines
        ConvertTrendLines( xDataSeries );

        // error bars
        Reference< XPropertySet > xErrorBarX = CreateErrorBar( EXC_CHSERERR_XPLUS, EXC_CHSERERR_XMINUS );
        if( xErrorBarX.is() )
            aSeriesProp.SetProperty( EXC_CHPROP_ERRORBARX, xErrorBarX );
        Reference< XPropertySet > xErrorBarY = CreateErrorBar( EXC_CHSERERR_YPLUS, EXC_CHSERERR_YMINUS );
        if( xErrorBarY.is() )
            aSeriesProp.SetProperty( EXC_CHPROP_ERRORBARY, xErrorBarY );

        // own area formatting for every data point (TODO: varying line color not supported)
        bool bVarPointFmt = pTypeGroup->HasVarPointFormat() && rTypeInfo.IsSeriesFrameFormat();
#if EXC_CHART2_VARYCOLORSBY_PROP
        aSeriesProp.SetBoolProperty( EXC_CHPROP_VARYCOLORSBY, bVarPointFmt );
#else
        aSeriesProp.SetBoolProperty( EXC_CHPROP_VARYCOLORSBY, rTypeInfo.meTypeCateg == EXC_CHTYPECATEG_PIE );
#endif
        // #i91271# always set area formatting for every point in pie/doughnut charts
        if( mxSeriesFmt && ((bVarPointFmt && mxSeriesFmt->IsAutoArea()) || (rTypeInfo.meTypeCateg == EXC_CHTYPECATEG_PIE)) )
        {
            for( sal_uInt16 nPointIdx = 0, nPointCount = mxValueLink->GetCellCount(); nPointIdx < nPointCount; ++nPointIdx )
            {
                ScfPropertySet aPointProp = lclGetPointPropSet( xDataSeries, nPointIdx );
                mxSeriesFmt->ConvertArea( aPointProp, bVarPointFmt ? nPointIdx : mnSeriesIdx );
            }
        }

        // data point formatting
        for( XclImpChDataFormatMap::const_iterator aIt = maPointFmts.begin(), aEnd = maPointFmts.end(); aIt != aEnd; ++aIt )
        {
            ScfPropertySet aPointProp = lclGetPointPropSet( xDataSeries, aIt->first );
            aIt->second->Convert( aPointProp, rTypeInfo );
        }
    }
    return xDataSeries;
}

void XclImpChSeries::FillAllSourceLinks( ::std::vector< ScTokenRef >& rTokens ) const
{
    if( mxValueLink )
        mxValueLink->FillSourceLink( rTokens );
    if( mxCategLink )
        mxCategLink->FillSourceLink( rTokens );
    if( mxTitleLink )
        mxTitleLink->FillSourceLink( rTokens );
    if( mxBubbleLink )
        mxBubbleLink->FillSourceLink( rTokens );
}

void XclImpChSeries::ReadChSourceLink( XclImpStream& rStrm )
{
    XclImpChSourceLinkRef xSrcLink( new XclImpChSourceLink( GetChRoot() ) );
    xSrcLink->ReadChSourceLink( rStrm );
    switch( xSrcLink->GetDestType() )
    {
        case EXC_CHSRCLINK_TITLE:       mxTitleLink = xSrcLink;     break;
        case EXC_CHSRCLINK_VALUES:      mxValueLink = xSrcLink;     break;
        case EXC_CHSRCLINK_CATEGORY:    mxCategLink = xSrcLink;     break;
        case EXC_CHSRCLINK_BUBBLES:     mxBubbleLink = xSrcLink;    break;
    }
}

void XclImpChSeries::ReadChDataFormat( XclImpStream& rStrm )
{
    // #i51639# chart stores all data formats and assigns them later to the series
    GetChartData().ReadChDataFormat( rStrm );
}

void XclImpChSeries::ReadChSerParent( XclImpStream& rStrm )
{
    rStrm >> mnParentIdx;
    // index to parent series is 1-based, convert it to 0-based
    if( mnParentIdx > 0 )
        --mnParentIdx;
    else
        mnParentIdx = EXC_CHSERIES_INVALID;
}

void XclImpChSeries::ReadChSerTrendLine( XclImpStream& rStrm )
{
    XclImpChSerTrendLineRef xTrendLine( new XclImpChSerTrendLine( GetChRoot() ) );
    xTrendLine->ReadChSerTrendLine( rStrm );
    maTrendLines.push_back( xTrendLine );
}

void XclImpChSeries::ReadChSerErrorBar( XclImpStream& rStrm )
{
    SAL_WNODEPRECATED_DECLARATIONS_PUSH
    auto_ptr<XclImpChSerErrorBar> pErrorBar(new XclImpChSerErrorBar(GetChRoot()));
    SAL_WNODEPRECATED_DECLARATIONS_POP
    pErrorBar->ReadChSerErrorBar(rStrm);
    sal_uInt8 nBarType = pErrorBar->GetBarType();
    maErrorBars.insert(nBarType, pErrorBar);
}

XclImpChDataFormatRef XclImpChSeries::CreateDataFormat( sal_uInt16 nPointIdx, sal_uInt16 nFormatIdx )
{
    XclImpChDataFormatRef xDataFmt( new XclImpChDataFormat( GetChRoot() ) );
    xDataFmt->SetPointPos( XclChDataPointPos( mnSeriesIdx, nPointIdx ), nFormatIdx );
    return xDataFmt;
}

XclImpChDataFormatRef* XclImpChSeries::GetDataFormatRef( sal_uInt16 nPointIdx )
{
    if( nPointIdx == EXC_CHDATAFORMAT_ALLPOINTS )
        return &mxSeriesFmt;

    if (nPointIdx < EXC_CHDATAFORMAT_MAXPOINTCOUNT)
    {
        XclImpChDataFormatMap::iterator itr = maPointFmts.lower_bound(nPointIdx);
        if (itr == maPointFmts.end() || maPointFmts.key_comp()(nPointIdx, itr->first))
        {
            // No object exists at this point index position.  Insert a new one.
            XclImpChDataFormatRef p(new XclImpChDataFormat(GetChRoot()));
            itr = maPointFmts.insert(itr, XclImpChDataFormatMap::value_type(nPointIdx, p));
        }
        return &itr->second;
    }
    return 0;
}

XclImpChTextRef* XclImpChSeries::GetDataLabelRef( sal_uInt16 nPointIdx )
{
    if ((nPointIdx == EXC_CHDATAFORMAT_ALLPOINTS) || (nPointIdx < EXC_CHDATAFORMAT_MAXPOINTCOUNT))
    {
        XclImpChTextMap::iterator itr = maLabels.lower_bound(nPointIdx);
        if (itr == maLabels.end() || maLabels.key_comp()(nPointIdx, itr->first))
        {
            // No object exists at this point index position.  Insert a new one.
            XclImpChTextRef p(new XclImpChText(GetChRoot()));
            itr = maLabels.insert(itr, XclImpChTextMap::value_type(nPointIdx, p));
        }
        return &itr->second;
    }
    return 0;
}

void XclImpChSeries::ConvertTrendLines( Reference< XDataSeries > xDataSeries ) const
{
    Reference< XRegressionCurveContainer > xRegCurveCont( xDataSeries, UNO_QUERY );
    if( xRegCurveCont.is() )
    {
        for( XclImpChSerTrendLineList::const_iterator aIt = maTrendLines.begin(), aEnd = maTrendLines.end(); aIt != aEnd; ++aIt )
        {
            try
            {
                Reference< XRegressionCurve > xRegCurve = (*aIt)->CreateRegressionCurve();
                if( xRegCurve.is() )
                    xRegCurveCont->addRegressionCurve( xRegCurve );
            }
            catch( Exception& )
            {
                OSL_FAIL( "XclImpChSeries::ConvertTrendLines - cannot add regression curve" );
            }
        }
    }
}

Reference< XPropertySet > XclImpChSeries::CreateErrorBar( sal_uInt8 nPosBarId, sal_uInt8 nNegBarId ) const
{
    XclImpChSerErrorBarMap::const_iterator itrPosBar = maErrorBars.find(nPosBarId);
    XclImpChSerErrorBarMap::const_iterator itrNegBar = maErrorBars.find(nNegBarId);
    XclImpChSerErrorBarMap::const_iterator itrEnd = maErrorBars.end();
    if (itrPosBar == itrEnd || itrNegBar == itrEnd)
        return Reference<XPropertySet>();

    return XclImpChSerErrorBar::CreateErrorBar(itrPosBar->second, itrNegBar->second);
}

// Chart type groups ==========================================================

XclImpChType::XclImpChType( const XclImpChRoot& rRoot ) :
    XclImpChRoot( rRoot ),
    mnRecId( EXC_ID_CHUNKNOWN ),
    maTypeInfo( rRoot.GetChartTypeInfo( EXC_CHTYPEID_UNKNOWN ) )
{
}

void XclImpChType::ReadChType( XclImpStream& rStrm )
{
    sal_uInt16 nRecId = rStrm.GetRecId();
    bool bKnownType = true;

    switch( nRecId )
    {
        case EXC_ID_CHBAR:
            rStrm >> maData.mnOverlap >> maData.mnGap >> maData.mnFlags;
        break;

        case EXC_ID_CHLINE:
        case EXC_ID_CHAREA:
        case EXC_ID_CHRADARLINE:
        case EXC_ID_CHRADARAREA:
            rStrm >> maData.mnFlags;
        break;

        case EXC_ID_CHPIE:
            rStrm >> maData.mnRotation >> maData.mnPieHole;
            if( GetBiff() == EXC_BIFF8 )
                rStrm >> maData.mnFlags;
            else
                maData.mnFlags = 0;
        break;

        case EXC_ID_CHPIEEXT:
            maData.mnRotation = 0;
            maData.mnPieHole = 0;
            maData.mnFlags = 0;
        break;

        case EXC_ID_CHSCATTER:
            if( GetBiff() == EXC_BIFF8 )
                rStrm >> maData.mnBubbleSize >> maData.mnBubbleType >> maData.mnFlags;
            else
                maData.mnFlags = 0;
        break;

        case EXC_ID_CHSURFACE:
            rStrm >> maData.mnFlags;
        break;

        default:
            bKnownType = false;
    }

    if( bKnownType )
        mnRecId = nRecId;
}

void XclImpChType::Finalize( bool bStockChart )
{
    switch( mnRecId )
    {
        case EXC_ID_CHLINE:
            maTypeInfo = GetChartTypeInfo( bStockChart ?
                EXC_CHTYPEID_STOCK : EXC_CHTYPEID_LINE );
        break;
        case EXC_ID_CHBAR:
            maTypeInfo = GetChartTypeInfo( ::get_flagvalue(
                maData.mnFlags, EXC_CHBAR_HORIZONTAL,
                EXC_CHTYPEID_HORBAR, EXC_CHTYPEID_BAR ) );
        break;
        case EXC_ID_CHPIE:
            maTypeInfo = GetChartTypeInfo( (maData.mnPieHole > 0) ?
                EXC_CHTYPEID_DONUT : EXC_CHTYPEID_PIE );
        break;
        case EXC_ID_CHSCATTER:
            maTypeInfo = GetChartTypeInfo( ::get_flagvalue(
                maData.mnFlags, EXC_CHSCATTER_BUBBLES,
                EXC_CHTYPEID_BUBBLES, EXC_CHTYPEID_SCATTER ) );
        break;
        default:
            maTypeInfo = GetChartTypeInfo( mnRecId );
    }

    switch( maTypeInfo.meTypeId )
    {
        case EXC_CHTYPEID_PIEEXT:
        case EXC_CHTYPEID_BUBBLES:
        case EXC_CHTYPEID_SURFACE:
        case EXC_CHTYPEID_UNKNOWN:
            GetTracer().TraceChartUnKnownType();
        break;
        default:;
    }
}

bool XclImpChType::IsStacked() const
{
    bool bStacked = false;
    if( maTypeInfo.mbSupportsStacking ) switch( maTypeInfo.meTypeCateg )
    {
        case EXC_CHTYPECATEG_LINE:
            bStacked =
                ::get_flag( maData.mnFlags, EXC_CHLINE_STACKED ) &&
                !::get_flag( maData.mnFlags, EXC_CHLINE_PERCENT );
        break;
        case EXC_CHTYPECATEG_BAR:
            bStacked =
                ::get_flag( maData.mnFlags, EXC_CHBAR_STACKED ) &&
                !::get_flag( maData.mnFlags, EXC_CHBAR_PERCENT );
        break;
        default:;
    }
    return bStacked;
}

bool XclImpChType::IsPercent() const
{
    bool bPercent = false;
    if( maTypeInfo.mbSupportsStacking ) switch( maTypeInfo.meTypeCateg )
    {
        case EXC_CHTYPECATEG_LINE:
            bPercent =
                ::get_flag( maData.mnFlags, EXC_CHLINE_STACKED ) &&
                ::get_flag( maData.mnFlags, EXC_CHLINE_PERCENT );
        break;
        case EXC_CHTYPECATEG_BAR:
            bPercent =
                ::get_flag( maData.mnFlags, EXC_CHBAR_STACKED ) &&
                ::get_flag( maData.mnFlags, EXC_CHBAR_PERCENT );
        break;
        default:;
    }
    return bPercent;
}

bool XclImpChType::HasCategoryLabels() const
{
    // radar charts disable category labels in chart type, not in CHTICK of X axis
    return (maTypeInfo.meTypeCateg != EXC_CHTYPECATEG_RADAR) || ::get_flag( maData.mnFlags, EXC_CHRADAR_AXISLABELS );
}

Reference< XCoordinateSystem > XclImpChType::CreateCoordSystem( bool b3dChart ) const
{
    // service name
    OUString aCoordSysService;
    if( maTypeInfo.mbPolarCoordSystem )
    {
        if( b3dChart )
            aCoordSysService = SERVICE_CHART2_POLARCOORDSYS3D;
        else
            aCoordSysService = SERVICE_CHART2_POLARCOORDSYS2D;
    }
    else
    {
        if( b3dChart )
            aCoordSysService = SERVICE_CHART2_CARTESIANCOORDSYS3D;
        else
            aCoordSysService = SERVICE_CHART2_CARTESIANCOORDSYS2D;
    }

    // create the coordinate system object
    Reference< XCoordinateSystem > xCoordSystem( ScfApiHelper::CreateInstance( aCoordSysService ), UNO_QUERY );

    // swap X and Y axis
    if( maTypeInfo.mbSwappedAxesSet )
    {
        ScfPropertySet aCoordSysProp( xCoordSystem );
        aCoordSysProp.SetBoolProperty( EXC_CHPROP_SWAPXANDYAXIS, true );
    }

    return xCoordSystem;
}

Reference< XChartType > XclImpChType::CreateChartType( Reference< XDiagram > xDiagram, bool b3dChart ) const
{
    OUString aService = OUString::createFromAscii( maTypeInfo.mpcServiceName );
    Reference< XChartType > xChartType( ScfApiHelper::CreateInstance( aService ), UNO_QUERY );

    // additional properties
    switch( maTypeInfo.meTypeCateg )
    {
        case EXC_CHTYPECATEG_BAR:
        {
            ScfPropertySet aTypeProp( xChartType );
            Sequence< sal_Int32 > aInt32Seq( 2 );
            aInt32Seq[ 0 ] = aInt32Seq[ 1 ] = -maData.mnOverlap;
            aTypeProp.SetProperty( EXC_CHPROP_OVERLAPSEQ, aInt32Seq );
            aInt32Seq[ 0 ] = aInt32Seq[ 1 ] = maData.mnGap;
            aTypeProp.SetProperty( EXC_CHPROP_GAPWIDTHSEQ, aInt32Seq );
        }
        break;
        case EXC_CHTYPECATEG_PIE:
        {
            ScfPropertySet aTypeProp( xChartType );
            aTypeProp.SetBoolProperty( EXC_CHPROP_USERINGS, maTypeInfo.meTypeId == EXC_CHTYPEID_DONUT );
            /*  #i85166# starting angle of first pie slice. 3D pie charts use Y
                rotation setting in view3D element. Of-pie charts do not
                support pie rotation. */
            if( !b3dChart && (maTypeInfo.meTypeId != EXC_CHTYPEID_PIEEXT) )
            {
                ScfPropertySet aDiaProp( xDiagram );
                XclImpChRoot::ConvertPieRotation( aDiaProp, maData.mnRotation );
            }
        }
        break;
        default:;
    }

    return xChartType;
}

// ----------------------------------------------------------------------------

void XclImpChChart3d::ReadChChart3d( XclImpStream& rStrm )
{
    rStrm   >> maData.mnRotation
            >> maData.mnElevation
            >> maData.mnEyeDist
            >> maData.mnRelHeight
            >> maData.mnRelDepth
            >> maData.mnDepthGap
            >> maData.mnFlags;
}

void XclImpChChart3d::Convert( ScfPropertySet& rPropSet, bool b3dWallChart ) const
{
    namespace cssd = ::com::sun::star::drawing;

//    #i104057# do not assert this, written by broken external generators
//    OSL_ENSURE( ::get_flag( maData.mnFlags, EXC_CHCHART3D_HASWALLS ) == b3dWallChart, "XclImpChChart3d::Convert - wrong wall flag" );

    sal_Int32 nRotationY = 0;
    sal_Int32 nRotationX = 0;
    sal_Int32 nPerspective = 15;
    bool bRightAngled = false;
    cssd::ProjectionMode eProjMode = cssd::ProjectionMode_PERSPECTIVE;
    Color aAmbientColor, aLightColor;

    if( b3dWallChart )
    {
        // Y rotation (Excel [0..359], Chart2 [-179,180])
        nRotationY = maData.mnRotation % 360;
        if( nRotationY > 180 ) nRotationY -= 360;
        // X rotation a.k.a. elevation (Excel [-90..90], Chart2 [-179,180])
        nRotationX = limit_cast< sal_Int32, sal_Int32 >( maData.mnElevation, -90, 90 );
        // perspective (Excel and Chart2 [0,100])
        nPerspective = limit_cast< sal_Int32, sal_Int32 >( maData.mnEyeDist, 0, 100 );
        // right-angled axes
        bRightAngled = !::get_flag( maData.mnFlags, EXC_CHCHART3D_REAL3D );
        // projection mode (parallel axes, if right-angled, #i90360# or if perspective is at 0%)
        bool bParallel = bRightAngled || (nPerspective == 0);
        eProjMode = bParallel ? cssd::ProjectionMode_PARALLEL : cssd::ProjectionMode_PERSPECTIVE;
        // ambient color (Gray 20%)
        aAmbientColor.SetColor( RGB_COLORDATA( 204, 204, 204 ) );
        // light color (Gray 60%)
        aLightColor.SetColor( RGB_COLORDATA( 102, 102, 102 ) );
    }
    else
    {
        // Y rotation not used in pie charts, but 'first pie slice angle'
        nRotationY = 0;
        XclImpChRoot::ConvertPieRotation( rPropSet, maData.mnRotation );
        // X rotation a.k.a. elevation (map Excel [10..80] to Chart2 [-80,-10])
        nRotationX = limit_cast< sal_Int32, sal_Int32 >( maData.mnElevation, 10, 80 ) - 90;
        // perspective (Excel and Chart2 [0,100])
        nPerspective = limit_cast< sal_Int32, sal_Int32 >( maData.mnEyeDist, 0, 100 );
        // no right-angled axes in pie charts, but parallel projection
        bRightAngled = false;
        eProjMode = cssd::ProjectionMode_PARALLEL;
        // ambient color (Gray 30%)
        aAmbientColor.SetColor( RGB_COLORDATA( 179, 179, 179 ) );
        // light color (Gray 70%)
        aLightColor.SetColor( RGB_COLORDATA( 76, 76, 76 ) );
    }

    // properties
    rPropSet.SetProperty( EXC_CHPROP_ROTATIONVERTICAL, nRotationY );
    rPropSet.SetProperty( EXC_CHPROP_ROTATIONHORIZONTAL, nRotationX );
    rPropSet.SetProperty( EXC_CHPROP_PERSPECTIVE, nPerspective );
    rPropSet.SetBoolProperty( EXC_CHPROP_RIGHTANGLEDAXES, bRightAngled );
    rPropSet.SetProperty( EXC_CHPROP_D3DSCENEPERSPECTIVE, eProjMode );

    // light settings
    rPropSet.SetProperty( EXC_CHPROP_D3DSCENESHADEMODE, cssd::ShadeMode_FLAT );
    rPropSet.SetColorProperty( EXC_CHPROP_D3DSCENEAMBIENTCOLOR, aAmbientColor );
    rPropSet.SetBoolProperty( EXC_CHPROP_D3DSCENELIGHTON1, false );
    rPropSet.SetBoolProperty( EXC_CHPROP_D3DSCENELIGHTON2, true );
    rPropSet.SetColorProperty( EXC_CHPROP_D3DSCENELIGHTCOLOR2, aLightColor );
    rPropSet.SetProperty( EXC_CHPROP_D3DSCENELIGHTDIR2, cssd::Direction3D( 0.2, 0.4, 1.0 ) );
}

// ----------------------------------------------------------------------------

XclImpChLegend::XclImpChLegend( const XclImpChRoot& rRoot ) :
    XclImpChRoot( rRoot )
{
}

void XclImpChLegend::ReadHeaderRecord( XclImpStream& rStrm )
{
    rStrm >> maData.maRect >> maData.mnDockMode >> maData.mnSpacing >> maData.mnFlags;

    // trace unsupported features
    if( GetTracer().IsEnabled() )
    {
        if( maData.mnDockMode == EXC_CHLEGEND_NOTDOCKED )
            GetTracer().TraceChartLegendPosition();
        if( ::get_flag( maData.mnFlags, EXC_CHLEGEND_DATATABLE ) )
            GetTracer().TraceChartDataTable();
    }
}

void XclImpChLegend::ReadSubRecord( XclImpStream& rStrm )
{
    switch( rStrm.GetRecId() )
    {
        case EXC_ID_CHFRAMEPOS:
            mxFramePos.reset( new XclImpChFramePos );
            mxFramePos->ReadChFramePos( rStrm );
        break;
        case EXC_ID_CHTEXT:
            mxText.reset( new XclImpChText( GetChRoot() ) );
            mxText->ReadRecordGroup( rStrm );
        break;
        case EXC_ID_CHFRAME:
            mxFrame.reset( new XclImpChFrame( GetChRoot(), EXC_CHOBJTYPE_LEGEND ) );
            mxFrame->ReadRecordGroup( rStrm );
        break;
    }
}

void XclImpChLegend::Finalize()
{
    // legend default formatting differs in OOChart and Excel, missing frame means automatic
    if( !mxFrame )
        mxFrame.reset( new XclImpChFrame( GetChRoot(), EXC_CHOBJTYPE_LEGEND ) );
    // Update text formatting. If mxText is empty, the passed default text is used.
    lclUpdateText( mxText, GetChartData().GetDefaultText( EXC_CHTEXTTYPE_LEGEND ) );
}

Reference< XLegend > XclImpChLegend::CreateLegend() const
{
    Reference< XLegend > xLegend( ScfApiHelper::CreateInstance( SERVICE_CHART2_LEGEND ), UNO_QUERY );
    if( xLegend.is() )
    {
        ScfPropertySet aLegendProp( xLegend );
        aLegendProp.SetBoolProperty( EXC_CHPROP_SHOW, true );

        // frame properties
        if( mxFrame )
            mxFrame->Convert( aLegendProp );
        // text properties
        if( mxText )
            mxText->ConvertFont( aLegendProp );

        /*  Legend position and size. Default positions are used only if the
            plot area is positioned automatically (Excel sets the plot area to
            manual mode, if the legend is moved or resized). With manual plot
            areas, Excel ignores the value in maData.mnDockMode completely. */
        cssc2::LegendPosition eApiPos = cssc2::LegendPosition_CUSTOM;
        cssc::ChartLegendExpansion eApiExpand = cssc::ChartLegendExpansion_CUSTOM;
        if( !GetChartData().IsManualPlotArea() ) switch( maData.mnDockMode )
        {
            case EXC_CHLEGEND_LEFT:
                eApiPos = cssc2::LegendPosition_LINE_START;
                eApiExpand = cssc::ChartLegendExpansion_HIGH;
            break;
            case EXC_CHLEGEND_RIGHT:
            // top-right not supported
            case EXC_CHLEGEND_CORNER:
                eApiPos = cssc2::LegendPosition_LINE_END;
                eApiExpand = cssc::ChartLegendExpansion_HIGH;
            break;
            case EXC_CHLEGEND_TOP:
                eApiPos = cssc2::LegendPosition_PAGE_START;
                eApiExpand = cssc::ChartLegendExpansion_WIDE;
            break;
            case EXC_CHLEGEND_BOTTOM:
                eApiPos = cssc2::LegendPosition_PAGE_END;
                eApiExpand = cssc::ChartLegendExpansion_WIDE;
            break;
        }

        // no automatic position/size: try to find the correct position and size
        if( eApiPos == cssc2::LegendPosition_CUSTOM )
        {
            const XclChFramePos* pFramePos = mxFramePos ? &mxFramePos->GetFramePosData() : 0;

            /*  Legend position. Only the settings from the CHFRAMEPOS record
                are used by Excel, the position in the CHLEGEND record will be
                ignored. */
            if( pFramePos )
            {
                RelativePosition aRelPos(
                    CalcRelativeFromChartX( pFramePos->maRect.mnX ),
                    CalcRelativeFromChartY( pFramePos->maRect.mnY ),
                    ::com::sun::star::drawing::Alignment_TOP_LEFT );
                aLegendProp.SetProperty( EXC_CHPROP_RELATIVEPOSITION, aRelPos );
            }
            else
            {
                // no manual position/size found, just go for the default
                eApiPos = cssc2::LegendPosition_LINE_END;
            }

            /*  Legend size. The member mnBRMode specifies whether size is
                automatic or changes manually. Manual size is given in points,
                not in chart units. */
            if( pFramePos && (pFramePos->mnBRMode == EXC_CHFRAMEPOS_ABSSIZE_POINTS) &&
                (pFramePos->maRect.mnWidth > 0) && (pFramePos->maRect.mnHeight > 0) )
            {
                eApiExpand = cssc::ChartLegendExpansion_CUSTOM;
                sal_Int32 nWidthHmm = static_cast< sal_Int32 >( pFramePos->maRect.mnWidth / EXC_POINTS_PER_HMM );
                sal_Int32 nHeightHmm = static_cast< sal_Int32 >( pFramePos->maRect.mnHeight / EXC_POINTS_PER_HMM );
                RelativeSize aRelSize( CalcRelativeFromHmmX( nWidthHmm ), CalcRelativeFromHmmY( nHeightHmm ) );
                aLegendProp.SetProperty( EXC_CHPROP_RELATIVESIZE, aRelSize );
            }
            else
            {
                // automatic size: determine entry direction from flags
                eApiExpand = ::get_flagvalue( maData.mnFlags, EXC_CHLEGEND_STACKED,
                    cssc::ChartLegendExpansion_HIGH, cssc::ChartLegendExpansion_WIDE );
            }
        }
        aLegendProp.SetProperty( EXC_CHPROP_ANCHORPOSITION, eApiPos );
        aLegendProp.SetProperty( EXC_CHPROP_EXPANSION, eApiExpand );
    }
    return xLegend;
}

// ----------------------------------------------------------------------------

XclImpChDropBar::XclImpChDropBar( sal_uInt16 nDropBar ) :
    mnDropBar( nDropBar ),
    mnBarDist( 0 )
{
}

void XclImpChDropBar::ReadHeaderRecord( XclImpStream& rStrm )
{
    rStrm >> mnBarDist;
}

void XclImpChDropBar::Convert( const XclImpChRoot& rRoot, ScfPropertySet& rPropSet ) const
{
    XclChObjectType eObjType = EXC_CHOBJTYPE_BACKGROUND;
    switch( mnDropBar )
    {
        case EXC_CHDROPBAR_UP:      eObjType = EXC_CHOBJTYPE_WHITEDROPBAR;  break;
        case EXC_CHDROPBAR_DOWN:    eObjType = EXC_CHOBJTYPE_BLACKDROPBAR;  break;
    }
    ConvertFrameBase( rRoot, rPropSet, eObjType );
}

// ----------------------------------------------------------------------------

XclImpChTypeGroup::XclImpChTypeGroup( const XclImpChRoot& rRoot ) :
    XclImpChRoot( rRoot ),
    maType( rRoot ),
    maTypeInfo( maType.GetTypeInfo() )
{
    // Initialize unused format indexes set. At this time, all formats are unused.
    for( sal_uInt16 nFormatIdx = 0; nFormatIdx <= EXC_CHSERIES_MAXSERIES; ++nFormatIdx )
        maUnusedFormats.insert( maUnusedFormats.end(), nFormatIdx );
}

void XclImpChTypeGroup::ReadHeaderRecord( XclImpStream& rStrm )
{
    rStrm.Ignore( 16 );
    rStrm >> maData.mnFlags >> maData.mnGroupIdx;
}

void XclImpChTypeGroup::ReadSubRecord( XclImpStream& rStrm )
{
    switch( rStrm.GetRecId() )
    {
        case EXC_ID_CHCHART3D:
            mxChart3d.reset( new XclImpChChart3d );
            mxChart3d->ReadChChart3d( rStrm );
        break;
        case EXC_ID_CHLEGEND:
            mxLegend.reset( new XclImpChLegend( GetChRoot() ) );
            mxLegend->ReadRecordGroup( rStrm );
        break;
        case EXC_ID_CHDEFAULTTEXT:
            GetChartData().ReadChDefaultText( rStrm );
        break;
        case EXC_ID_CHDROPBAR:
            ReadChDropBar( rStrm );
        break;
        case EXC_ID_CHCHARTLINE:
            ReadChChartLine( rStrm );
        break;
        case EXC_ID_CHDATAFORMAT:
            ReadChDataFormat( rStrm );
        break;
        default:
            maType.ReadChType( rStrm );
    }
}

void XclImpChTypeGroup::Finalize()
{
    // check and set valid chart type
    bool bStockChart =
        (maType.GetRecId() == EXC_ID_CHLINE) &&         // must be a line chart
        !mxChart3d &&                                   // must be a 2d chart
        HasHiLoLine() &&                                // must contain hi-lo lines
        (maSeries.size() == static_cast<XclImpChSeriesVec::size_type>(HasDropBars() ? 4 : 3));   // correct series count
    maType.Finalize( bStockChart );

    // extended type info
    maTypeInfo.Set( maType.GetTypeInfo(), mxChart3d, false );

    // reverse series order for some unstacked 2D chart types
    if( maTypeInfo.mbReverseSeries && !Is3dChart() && !maType.IsStacked() && !maType.IsPercent() )
        ::std::reverse( maSeries.begin(), maSeries.end() );

    // update chart type group format, may depend on chart type finalized above
    if( mxGroupFmt )
        mxGroupFmt->UpdateGroupFormat( maTypeInfo );
}

void XclImpChTypeGroup::AddSeries( XclImpChSeriesRef xSeries )
{
    if( xSeries )
        maSeries.push_back( xSeries );
    // store first inserted series separately, series order may be reversed later
    if( !mxFirstSeries )
        mxFirstSeries = xSeries;
}

void XclImpChTypeGroup::SetUsedFormatIndex( sal_uInt16 nFormatIdx )
{
    maUnusedFormats.erase( nFormatIdx );
}

sal_uInt16 XclImpChTypeGroup::PopUnusedFormatIndex()
{
    OSL_ENSURE( !maUnusedFormats.empty(), "XclImpChTypeGroup::PopUnusedFormatIndex - no more format indexes available" );
    sal_uInt16 nFormatIdx = maUnusedFormats.empty() ? 0 : *maUnusedFormats.begin();
    SetUsedFormatIndex( nFormatIdx );
    return nFormatIdx;
}

bool XclImpChTypeGroup::HasVarPointFormat() const
{
    return ::get_flag( maData.mnFlags, EXC_CHTYPEGROUP_VARIEDCOLORS ) &&
        ((maTypeInfo.meVarPointMode == EXC_CHVARPOINT_MULTI) ||         // multiple series allowed
            ((maTypeInfo.meVarPointMode == EXC_CHVARPOINT_SINGLE) &&    // or exactly 1 series?
                (maSeries.size() == 1)));
}

bool XclImpChTypeGroup::HasConnectorLines() const
{
    // existence of connector lines (only in stacked bar charts)
    if ( !(maType.IsStacked() || maType.IsPercent()) || (maTypeInfo.meTypeCateg != EXC_CHTYPECATEG_BAR) )
        return false;
    XclImpChLineFormatMap::const_iterator xConLine = maChartLines.find( EXC_CHCHARTLINE_CONNECT );
    return ( xConLine != maChartLines.end() && xConLine->second->HasLine() );
}

const String& XclImpChTypeGroup::GetSingleSeriesTitle() const
{
    // no automatic title for series with trendlines or error bars
    // pie charts always show an automatic title, even if more series exist
    return (mxFirstSeries && !mxFirstSeries->HasChildSeries() && (maTypeInfo.mbSingleSeriesVis || (maSeries.size() == 1))) ?
        mxFirstSeries->GetTitle() : String::EmptyString();
}

void XclImpChTypeGroup::ConvertChart3d( ScfPropertySet& rPropSet ) const
{
    if( mxChart3d )
        mxChart3d->Convert( rPropSet, Is3dWallChart() );
}

Reference< XCoordinateSystem > XclImpChTypeGroup::CreateCoordSystem() const
{
    return maType.CreateCoordSystem( Is3dChart() );
}

Reference< XChartType > XclImpChTypeGroup::CreateChartType( Reference< XDiagram > xDiagram, sal_Int32 nApiAxesSetIdx ) const
{
    OSL_ENSURE( IsValidGroup(), "XclImpChTypeGroup::CreateChartType - type group without series" );

    // create the chart type object
    Reference< XChartType > xChartType = maType.CreateChartType( xDiagram, Is3dChart() );

    // bar chart connector lines
    if( HasConnectorLines() )
    {
        ScfPropertySet aDiaProp( xDiagram );
        aDiaProp.SetBoolProperty( EXC_CHPROP_CONNECTBARS, true );
    }

    /*  Stock chart needs special processing. Create one 'big' series with
        data sequences of different roles. */
    if( maTypeInfo.meTypeId == EXC_CHTYPEID_STOCK )
        CreateStockSeries( xChartType, nApiAxesSetIdx );
    else
        CreateDataSeries( xChartType, nApiAxesSetIdx );

    return xChartType;
}

Reference< XLabeledDataSequence > XclImpChTypeGroup::CreateCategSequence() const
{
    Reference< XLabeledDataSequence > xLabeledSeq;
    // create category sequence from first visible series
    if( mxFirstSeries )
        xLabeledSeq = mxFirstSeries->CreateCategSequence( EXC_CHPROP_ROLE_CATEG );
    return xLabeledSeq;
}

void XclImpChTypeGroup::ReadChDropBar( XclImpStream& rStrm )
{
    if (maDropBars.find(EXC_CHDROPBAR_UP) == maDropBars.end())
    {
        SAL_WNODEPRECATED_DECLARATIONS_PUSH
        auto_ptr<XclImpChDropBar> p(new XclImpChDropBar(EXC_CHDROPBAR_UP));
        SAL_WNODEPRECATED_DECLARATIONS_POP
        p->ReadRecordGroup(rStrm);
        maDropBars.insert(EXC_CHDROPBAR_UP, p);
    }
    else if(maDropBars.find(EXC_CHDROPBAR_DOWN) == maDropBars.end())
    {
        SAL_WNODEPRECATED_DECLARATIONS_PUSH
        auto_ptr<XclImpChDropBar> p(new XclImpChDropBar(EXC_CHDROPBAR_DOWN));
        SAL_WNODEPRECATED_DECLARATIONS_POP
        p->ReadRecordGroup(rStrm);
        maDropBars.insert(EXC_CHDROPBAR_DOWN, p);
    }
}

void XclImpChTypeGroup::ReadChChartLine( XclImpStream& rStrm )
{
    sal_uInt16 nLineId = rStrm.ReaduInt16();
    if( (rStrm.GetNextRecId() == EXC_ID_CHLINEFORMAT) && rStrm.StartNextRecord() )
    {
        XclImpChLineFormat xLineFmt;
        xLineFmt.ReadChLineFormat( rStrm );
        maChartLines[ nLineId ] = xLineFmt;
    }
}

void XclImpChTypeGroup::ReadChDataFormat( XclImpStream& rStrm )
{
    // global series and data point format
    XclImpChDataFormatRef xDataFmt( new XclImpChDataFormat( GetChRoot() ) );
    xDataFmt->ReadRecordGroup( rStrm );
    const XclChDataPointPos& rPos = xDataFmt->GetPointPos();
    if( (rPos.mnSeriesIdx == 0) && (rPos.mnPointIdx == 0) &&
            (xDataFmt->GetFormatIdx() == EXC_CHDATAFORMAT_DEFAULT) )
        mxGroupFmt = xDataFmt;
}


void XclImpChTypeGroup::InsertDataSeries( Reference< XChartType > xChartType,
        Reference< XDataSeries > xSeries, sal_Int32 nApiAxesSetIdx ) const
{
    Reference< XDataSeriesContainer > xSeriesCont( xChartType, UNO_QUERY );
    if( xSeriesCont.is() && xSeries.is() )
    {
        // series stacking mode
        cssc2::StackingDirection eStacking = cssc2::StackingDirection_NO_STACKING;
        // stacked overrides deep-3d
        if( maType.IsStacked() || maType.IsPercent() )
            eStacking = cssc2::StackingDirection_Y_STACKING;
        else if( Is3dDeepChart() )
            eStacking = cssc2::StackingDirection_Z_STACKING;

        // additional series properties
        ScfPropertySet aSeriesProp( xSeries );
        aSeriesProp.SetProperty( EXC_CHPROP_STACKINGDIR, eStacking );
        aSeriesProp.SetProperty( EXC_CHPROP_ATTAXISINDEX, nApiAxesSetIdx );

        // insert series into container
        try
        {
            xSeriesCont->addDataSeries( xSeries );
        }
        catch( Exception& )
        {
            OSL_FAIL( "XclImpChTypeGroup::InsertDataSeries - cannot add data series" );
        }
    }
}

void XclImpChTypeGroup::CreateDataSeries( Reference< XChartType > xChartType, sal_Int32 nApiAxesSetIdx ) const
{
    bool bSpline = false;
    for( XclImpChSeriesVec::const_iterator aIt = maSeries.begin(), aEnd = maSeries.end(); aIt != aEnd; ++aIt )
    {
        Reference< XDataSeries > xDataSeries = (*aIt)->CreateDataSeries();
        InsertDataSeries( xChartType, xDataSeries, nApiAxesSetIdx );
        bSpline |= (*aIt)->HasSpline();
    }
    // spline - TODO: set at single series (#i66858#)
    if( bSpline && !maTypeInfo.IsSeriesFrameFormat() && (maTypeInfo.meTypeCateg != EXC_CHTYPECATEG_RADAR) )
    {
        ScfPropertySet aTypeProp( xChartType );
        aTypeProp.SetProperty( EXC_CHPROP_CURVESTYLE, ::com::sun::star::chart2::CurveStyle_CUBIC_SPLINES );
    }
}

void XclImpChTypeGroup::CreateStockSeries( Reference< XChartType > xChartType, sal_Int32 nApiAxesSetIdx ) const
{
    // create the data series object
    Reference< XDataSeries > xDataSeries( ScfApiHelper::CreateInstance( SERVICE_CHART2_DATASERIES ), UNO_QUERY );
    Reference< XDataSink > xDataSink( xDataSeries, UNO_QUERY );
    if( xDataSink.is() )
    {
        // create a list of data sequences from all series
        ::std::vector< Reference< XLabeledDataSequence > > aLabeledSeqVec;
        OSL_ENSURE( maSeries.size() >= 3, "XclImpChTypeGroup::CreateChartType - missing stock series" );
        int nRoleIdx = (maSeries.size() == 3) ? 1 : 0;
        for( XclImpChSeriesVec::const_iterator aIt = maSeries.begin(), aEnd = maSeries.end();
                (nRoleIdx < 4) && (aIt != aEnd); ++nRoleIdx, ++aIt )
        {
            // create a data sequence with a specific role
            OUString aRole;
            switch( nRoleIdx )
            {
                case 0: aRole = EXC_CHPROP_ROLE_OPENVALUES;     break;
                case 1: aRole = EXC_CHPROP_ROLE_HIGHVALUES;     break;
                case 2: aRole = EXC_CHPROP_ROLE_LOWVALUES;      break;
                case 3: aRole = EXC_CHPROP_ROLE_CLOSEVALUES;    break;
            }
            Reference< XLabeledDataSequence > xDataSeq = (*aIt)->CreateValueSequence( aRole );
            if( xDataSeq.is() )
                aLabeledSeqVec.push_back( xDataSeq );
        }

        // attach labeled data sequences to series and insert series into chart type
        xDataSink->setData( ScfApiHelper::VectorToSequence( aLabeledSeqVec ) );

        // formatting of special stock chart elements
        ScfPropertySet aTypeProp( xChartType );
        aTypeProp.SetBoolProperty( EXC_CHPROP_JAPANESE, HasDropBars() );
        aTypeProp.SetBoolProperty( EXC_CHPROP_SHOWFIRST, HasDropBars() );
        aTypeProp.SetBoolProperty( EXC_CHPROP_SHOWHIGHLOW, true );
        // hi-lo line format
        XclImpChLineFormatMap::const_iterator xHiLoLine = maChartLines.find( EXC_CHCHARTLINE_HILO );
        if ( xHiLoLine != maChartLines.end() )
        {
            ScfPropertySet aSeriesProp( xDataSeries );
            xHiLoLine->second->Convert( GetChRoot(), aSeriesProp, EXC_CHOBJTYPE_HILOLINE );
        }
        // white dropbar format
        XclImpChDropBarMap::const_iterator itr = maDropBars.find(EXC_CHDROPBAR_UP);
        Reference<XPropertySet> xWhitePropSet;
        if (itr != maDropBars.end() && aTypeProp.GetProperty(xWhitePropSet, EXC_CHPROP_WHITEDAY))
        {
            ScfPropertySet aBarProp( xWhitePropSet );
            itr->second->Convert(GetChRoot(), aBarProp);
        }
        // black dropbar format
        itr = maDropBars.find(EXC_CHDROPBAR_DOWN);
        Reference<XPropertySet> xBlackPropSet;
        if (itr != maDropBars.end() && aTypeProp.GetProperty(xBlackPropSet, EXC_CHPROP_BLACKDAY))
        {
            ScfPropertySet aBarProp( xBlackPropSet );
            itr->second->Convert(GetChRoot(), aBarProp);
        }

        // insert the series into the chart type object
        InsertDataSeries( xChartType, xDataSeries, nApiAxesSetIdx );
    }
}

// Axes =======================================================================

XclImpChLabelRange::XclImpChLabelRange( const XclImpChRoot& rRoot ) :
    XclImpChRoot( rRoot )
{
}

void XclImpChLabelRange::ReadChLabelRange( XclImpStream& rStrm )
{
    rStrm >> maLabelData.mnCross >> maLabelData.mnLabelFreq >> maLabelData.mnTickFreq >> maLabelData.mnFlags;
}

void XclImpChLabelRange::ReadChDateRange( XclImpStream& rStrm )
{
    rStrm   >> maDateData.mnMinDate
            >> maDateData.mnMaxDate
            >> maDateData.mnMajorStep
            >> maDateData.mnMajorUnit
            >> maDateData.mnMinorStep
            >> maDateData.mnMinorUnit
            >> maDateData.mnBaseUnit
            >> maDateData.mnCross
            >> maDateData.mnFlags;
}

void XclImpChLabelRange::Convert( ScfPropertySet& rPropSet, ScaleData& rScaleData, bool bMirrorOrient ) const
{
    // automatic axis type detection
    rScaleData.AutoDateAxis = ::get_flag( maDateData.mnFlags, EXC_CHDATERANGE_AUTODATE );

    // the flag EXC_CHDATERANGE_DATEAXIS specifies whether this is a date axis
    if( ::get_flag( maDateData.mnFlags, EXC_CHDATERANGE_DATEAXIS ) )
    {
        /*  Chart2 requires axis type CATEGORY for automatic category/date axis
            (even if it is a date axis currently). */
        rScaleData.AxisType = rScaleData.AutoDateAxis ? cssc2::AxisType::CATEGORY : cssc2::AxisType::DATE;
        rScaleData.Scaling.set( ScfApiHelper::CreateInstance( SERVICE_CHART2_LINEARSCALING ), UNO_QUERY );
        /*  Min/max values depend on base time unit, they specify the number of
            days, months, or years starting from null date. */
        lclConvertTimeValue( GetRoot(), rScaleData.Minimum, maDateData.mnMinDate, ::get_flag( maDateData.mnFlags, EXC_CHDATERANGE_AUTOMIN ), maDateData.mnBaseUnit );
        lclConvertTimeValue( GetRoot(), rScaleData.Maximum, maDateData.mnMaxDate, ::get_flag( maDateData.mnFlags, EXC_CHDATERANGE_AUTOMAX ), maDateData.mnBaseUnit );
        // increment
        cssc::TimeIncrement& rTimeIncrement = rScaleData.TimeIncrement;
        lclConvertTimeInterval( rTimeIncrement.MajorTimeInterval, maDateData.mnMajorStep, ::get_flag( maDateData.mnFlags, EXC_CHDATERANGE_AUTOMAJOR ), maDateData.mnMajorUnit );
        lclConvertTimeInterval( rTimeIncrement.MinorTimeInterval, maDateData.mnMinorStep, ::get_flag( maDateData.mnFlags, EXC_CHDATERANGE_AUTOMINOR ), maDateData.mnMinorUnit );
        // base unit
        if( ::get_flag( maDateData.mnFlags, EXC_CHDATERANGE_AUTOBASE ) )
            rTimeIncrement.TimeResolution.clear();
        else
            rTimeIncrement.TimeResolution <<= lclGetApiTimeUnit( maDateData.mnBaseUnit );
    }
    else
    {
        // do not overlap text unless all labels are visible
        rPropSet.SetBoolProperty( EXC_CHPROP_TEXTOVERLAP, maLabelData.mnLabelFreq == 1 );
        // do not break text into several lines unless all labels are visible
        rPropSet.SetBoolProperty( EXC_CHPROP_TEXTBREAK, maLabelData.mnLabelFreq == 1 );
        // do not stagger labels in two lines
        rPropSet.SetProperty( EXC_CHPROP_ARRANGEORDER, cssc::ChartAxisArrangeOrderType_SIDE_BY_SIDE );
    }

    // reverse order
    bool bReverse = ::get_flag( maLabelData.mnFlags, EXC_CHLABELRANGE_REVERSE ) != bMirrorOrient;
    rScaleData.Orientation = bReverse ? cssc2::AxisOrientation_REVERSE : cssc2::AxisOrientation_MATHEMATICAL;

    //! TODO #i58731# show n-th category
}

void XclImpChLabelRange::ConvertAxisPosition( ScfPropertySet& rPropSet, bool b3dChart ) const
{
    /*  Crossing mode (max-cross flag overrides other crossing settings). Excel
        does not move the Y axis in 3D charts, regardless of actual settings.
        But: the Y axis has to be moved to "end", if the X axis is mirrored,
        to keep it at the left end of the chart. */
    bool bMaxCross = ::get_flag( maLabelData.mnFlags, b3dChart ? EXC_CHLABELRANGE_REVERSE : EXC_CHLABELRANGE_MAXCROSS );
    cssc::ChartAxisPosition eAxisPos = bMaxCross ? cssc::ChartAxisPosition_END : cssc::ChartAxisPosition_VALUE;
    rPropSet.SetProperty( EXC_CHPROP_CROSSOVERPOSITION, eAxisPos );

    // crossing position (depending on axis type text/date)
    if( ::get_flag( maDateData.mnFlags, EXC_CHDATERANGE_DATEAXIS ) )
    {
        bool bAutoCross = ::get_flag( maDateData.mnFlags, EXC_CHDATERANGE_AUTOCROSS );
        /*  Crossing position value depends on base time unit, it specifies the
            number of days, months, or years from null date. Note that Excel
            2007/2010 write broken BIFF8 files, they always stores the number
            of days cregardless of the base time unit (and they are reading it
            the same way, thus wrongly displaying files written by Excel
            97-2003). This filter sticks to the correct behaviour of Excel
            97-2003. */
        double fCrossingPos = bAutoCross ? 1.0 : lclGetSerialDay( GetRoot(), maDateData.mnCross, maDateData.mnBaseUnit );
        rPropSet.SetProperty( EXC_CHPROP_CROSSOVERVALUE, fCrossingPos );
    }
    else
    {
        double fCrossingPos = b3dChart ? 1.0 : maLabelData.mnCross;
        rPropSet.SetProperty( EXC_CHPROP_CROSSOVERVALUE, fCrossingPos );
    }
}

// ----------------------------------------------------------------------------

XclImpChValueRange::XclImpChValueRange( const XclImpChRoot& rRoot ) :
    XclImpChRoot( rRoot )
{
}

void XclImpChValueRange::ReadChValueRange( XclImpStream& rStrm )
{
    rStrm   >> maData.mfMin
            >> maData.mfMax
            >> maData.mfMajorStep
            >> maData.mfMinorStep
            >> maData.mfCross
            >> maData.mnFlags;
}

void XclImpChValueRange::Convert( ScaleData& rScaleData, bool bMirrorOrient ) const
{
    // scaling algorithm
    bool bLogScale = ::get_flag( maData.mnFlags, EXC_CHVALUERANGE_LOGSCALE );
    OUString aScalingService = bLogScale ? SERVICE_CHART2_LOGSCALING : SERVICE_CHART2_LINEARSCALING;
    rScaleData.Scaling.set( ScfApiHelper::CreateInstance( aScalingService ), UNO_QUERY );

    // min/max
    lclSetExpValueOrClearAny( rScaleData.Minimum, maData.mfMin, bLogScale, ::get_flag( maData.mnFlags, EXC_CHVALUERANGE_AUTOMIN ) );
    lclSetExpValueOrClearAny( rScaleData.Maximum, maData.mfMax, bLogScale, ::get_flag( maData.mnFlags, EXC_CHVALUERANGE_AUTOMAX ) );

    // increment
    bool bAutoMajor = ::get_flag( maData.mnFlags, EXC_CHVALUERANGE_AUTOMAJOR );
    bool bAutoMinor = ::get_flag( maData.mnFlags, EXC_CHVALUERANGE_AUTOMINOR );
    // major increment
    IncrementData& rIncrementData = rScaleData.IncrementData;
    lclSetValueOrClearAny( rIncrementData.Distance, maData.mfMajorStep, bAutoMajor );
    // minor increment
    Sequence< SubIncrement >& rSubIncrementSeq = rIncrementData.SubIncrements;
    rSubIncrementSeq.realloc( 1 );
    Any& rIntervalCount = rSubIncrementSeq[ 0 ].IntervalCount;
    rIntervalCount.clear();
    if( bLogScale )
    {
        if( !bAutoMinor )
            rIntervalCount <<= sal_Int32( 9 );
    }
    else
    {
        if( !bAutoMajor && !bAutoMinor && (0.0 < maData.mfMinorStep) && (maData.mfMinorStep <= maData.mfMajorStep) )
        {
            double fCount = maData.mfMajorStep / maData.mfMinorStep + 0.5;
            if( (1.0 <= fCount) && (fCount < 1001.0) )
                rIntervalCount <<= static_cast< sal_Int32 >( fCount );
        }
    }

    // reverse order
    bool bReverse = ::get_flag( maData.mnFlags, EXC_CHVALUERANGE_REVERSE ) != bMirrorOrient;
    rScaleData.Orientation = bReverse ? cssc2::AxisOrientation_REVERSE : cssc2::AxisOrientation_MATHEMATICAL;
}

void XclImpChValueRange::ConvertAxisPosition( ScfPropertySet& rPropSet ) const
{
    bool bMaxCross = ::get_flag( maData.mnFlags, EXC_CHVALUERANGE_MAXCROSS );
    bool bAutoCross = ::get_flag( maData.mnFlags, EXC_CHVALUERANGE_AUTOCROSS );
    bool bLogScale = ::get_flag( maData.mnFlags, EXC_CHVALUERANGE_LOGSCALE );

    // crossing mode (max-cross flag overrides other crossing settings)
    cssc::ChartAxisPosition eAxisPos = bMaxCross ? cssc::ChartAxisPosition_END : cssc::ChartAxisPosition_VALUE;
    rPropSet.SetProperty( EXC_CHPROP_CROSSOVERPOSITION, eAxisPos );

    // crossing position
    double fCrossingPos = bAutoCross ? 0.0 : maData.mfCross;
    if( bLogScale ) fCrossingPos = pow( 10.0, fCrossingPos );
    rPropSet.SetProperty( EXC_CHPROP_CROSSOVERVALUE, fCrossingPos );
}

// ----------------------------------------------------------------------------

namespace {

sal_Int32 lclGetApiTickmarks( sal_uInt8 nXclTickPos )
{
    using namespace ::com::sun::star::chart2::TickmarkStyle;
    sal_Int32 nApiTickmarks = NONE;
    ::set_flag( nApiTickmarks, INNER, ::get_flag( nXclTickPos, EXC_CHTICK_INSIDE ) );
    ::set_flag( nApiTickmarks, OUTER, ::get_flag( nXclTickPos, EXC_CHTICK_OUTSIDE ) );
    return nApiTickmarks;
}

cssc::ChartAxisLabelPosition lclGetApiLabelPosition( sal_Int8 nXclLabelPos )
{
    using namespace ::com::sun::star::chart;
    switch( nXclLabelPos )
    {
        case EXC_CHTICK_LOW:    return ChartAxisLabelPosition_OUTSIDE_START;
        case EXC_CHTICK_HIGH:   return ChartAxisLabelPosition_OUTSIDE_END;
        case EXC_CHTICK_NEXT:   return ChartAxisLabelPosition_NEAR_AXIS;
    }
    return ChartAxisLabelPosition_NEAR_AXIS;
}

} // namespace

XclImpChTick::XclImpChTick( const XclImpChRoot& rRoot ) :
    XclImpChRoot( rRoot )
{
}

void XclImpChTick::ReadChTick( XclImpStream& rStrm )
{
    rStrm   >> maData.mnMajor
            >> maData.mnMinor
            >> maData.mnLabelPos
            >> maData.mnBackMode;
    rStrm.Ignore( 16 );
    rStrm   >> maData.maTextColor
            >> maData.mnFlags;

    if( GetBiff() == EXC_BIFF8 )
    {
        // BIFF8: index into palette used instead of RGB data
        maData.maTextColor = GetPalette().GetColor( rStrm.ReaduInt16() );
        // rotation
        rStrm >> maData.mnRotation;
    }
    else
    {
        // BIFF2-BIFF7: get rotation from text orientation
        sal_uInt8 nOrient = ::extract_value< sal_uInt8 >( maData.mnFlags, 2, 3 );
        maData.mnRotation = XclTools::GetXclRotFromOrient( nOrient );
    }
}

Color XclImpChTick::GetFontColor() const
{
    return ::get_flag( maData.mnFlags, EXC_CHTICK_AUTOCOLOR ) ? GetFontAutoColor() : maData.maTextColor;
}

sal_uInt16 XclImpChTick::GetRotation() const
{
    return ::get_flag( maData.mnFlags, EXC_CHTICK_AUTOROT ) ? EXC_CHART_AUTOROTATION : maData.mnRotation;
}

void XclImpChTick::Convert( ScfPropertySet& rPropSet ) const
{
    rPropSet.SetProperty( EXC_CHPROP_MAJORTICKS, lclGetApiTickmarks( maData.mnMajor ) );
    rPropSet.SetProperty( EXC_CHPROP_MINORTICKS, lclGetApiTickmarks( maData.mnMinor ) );
    rPropSet.SetProperty( EXC_CHPROP_LABELPOSITION, lclGetApiLabelPosition( maData.mnLabelPos ) );
    rPropSet.SetProperty( EXC_CHPROP_MARKPOSITION, cssc::ChartAxisMarkPosition_AT_AXIS );
}

// ----------------------------------------------------------------------------

XclImpChAxis::XclImpChAxis( const XclImpChRoot& rRoot, sal_uInt16 nAxisType ) :
    XclImpChRoot( rRoot ),
    mnNumFmtIdx( EXC_FORMAT_NOTFOUND )
{
    maData.mnType = nAxisType;
}

void XclImpChAxis::ReadHeaderRecord( XclImpStream& rStrm )
{
    rStrm >> maData.mnType;
}

void XclImpChAxis::ReadSubRecord( XclImpStream& rStrm )
{
    switch( rStrm.GetRecId() )
    {
        case EXC_ID_CHLABELRANGE:
            mxLabelRange.reset( new XclImpChLabelRange( GetChRoot() ) );
            mxLabelRange->ReadChLabelRange( rStrm );
        break;
        case EXC_ID_CHDATERANGE:
            if( !mxLabelRange )
                mxLabelRange.reset( new XclImpChLabelRange( GetChRoot() ) );
            mxLabelRange->ReadChDateRange( rStrm );
        break;
        case EXC_ID_CHVALUERANGE:
            mxValueRange.reset( new XclImpChValueRange( GetChRoot() ) );
            mxValueRange->ReadChValueRange( rStrm );
        break;
        case EXC_ID_CHFORMAT:
            rStrm >> mnNumFmtIdx;
        break;
        case EXC_ID_CHTICK:
            mxTick.reset( new XclImpChTick( GetChRoot() ) );
            mxTick->ReadChTick( rStrm );
        break;
        case EXC_ID_CHFONT:
            mxFont.reset( new XclImpChFont );
            mxFont->ReadChFont( rStrm );
        break;
        case EXC_ID_CHAXISLINE:
            ReadChAxisLine( rStrm );
        break;
    }
}

void XclImpChAxis::Finalize()
{
    // add default scaling, needed e.g. to adjust rotation direction of pie and radar charts
    if( !mxLabelRange )
        mxLabelRange.reset( new XclImpChLabelRange( GetChRoot() ) );
    if( !mxValueRange )
        mxValueRange.reset( new XclImpChValueRange( GetChRoot() ) );
    // remove invisible grid lines completely
    if( mxMajorGrid && !mxMajorGrid->HasLine() )
        mxMajorGrid.reset();
    if( mxMinorGrid && !mxMinorGrid->HasLine() )
        mxMinorGrid.reset();
    // default tick settings different in OOChart and Excel
    if( !mxTick )
        mxTick.reset( new XclImpChTick( GetChRoot() ) );
    // #i4140# different default axis line color
    if( !mxAxisLine )
    {
        XclChLineFormat aLineFmt;
        // set "show axis" flag, default if line format record is missing
        ::set_flag( aLineFmt.mnFlags, EXC_CHLINEFORMAT_SHOWAXIS );
        mxAxisLine.reset( new XclImpChLineFormat( aLineFmt ) );
    }
    // add wall/floor frame for 3d charts
    if( !mxWallFrame )
        CreateWallFrame();
}

sal_uInt16 XclImpChAxis::GetFontIndex() const
{
    return mxFont ? mxFont->GetFontIndex() : EXC_FONT_NOTFOUND;
}

Color XclImpChAxis::GetFontColor() const
{
    return mxTick ? mxTick->GetFontColor() : GetFontAutoColor();
}

sal_uInt16 XclImpChAxis::GetRotation() const
{
    return mxTick ? mxTick->GetRotation() : EXC_CHART_AUTOROTATION;
}

Reference< XAxis > XclImpChAxis::CreateAxis( const XclImpChTypeGroup& rTypeGroup, const XclImpChAxis* pCrossingAxis ) const
{
    // create the axis object (always)
    Reference< XAxis > xAxis( ScfApiHelper::CreateInstance( SERVICE_CHART2_AXIS ), UNO_QUERY );
    if( xAxis.is() )
    {
        ScfPropertySet aAxisProp( xAxis );
        // #i58688# axis enabled
        aAxisProp.SetBoolProperty( EXC_CHPROP_SHOW, IsActivated() );

        // axis line properties
        if( mxAxisLine )
            mxAxisLine->Convert( GetChRoot(), aAxisProp, EXC_CHOBJTYPE_AXISLINE );
        // axis ticks properties
        if( mxTick )
            mxTick->Convert( aAxisProp );

        // axis caption text --------------------------------------------------

        // radar charts disable their category labels via chart type, not via axis
        bool bHasLabels = HasLabels() &&
            ((GetAxisType() != EXC_CHAXIS_X) || rTypeGroup.HasCategoryLabels());
        aAxisProp.SetBoolProperty( EXC_CHPROP_DISPLAYLABELS, bHasLabels );
        if( bHasLabels )
        {
            // font settings from CHFONT record or from default text
            if( mxFont )
                ConvertFontBase( GetChRoot(), aAxisProp );
            else if( const XclImpChText* pDefText = GetChartData().GetDefaultText( EXC_CHTEXTTYPE_AXISLABEL ) )
                pDefText->ConvertFont( aAxisProp );
            // label text rotation
            ConvertRotationBase( GetChRoot(), aAxisProp, true );
            // number format
            sal_uInt32 nScNumFmt = GetNumFmtBuffer().GetScFormat( mnNumFmtIdx );
            if( nScNumFmt != NUMBERFORMAT_ENTRY_NOT_FOUND )
                aAxisProp.SetProperty( EXC_CHPROP_NUMBERFORMAT, static_cast< sal_Int32 >( nScNumFmt ) );
        }

        // axis scaling and increment -----------------------------------------

        const XclChExtTypeInfo& rTypeInfo = rTypeGroup.GetTypeInfo();
        ScaleData aScaleData = xAxis->getScaleData();
        // set axis type
        switch( GetAxisType() )
        {
            case EXC_CHAXIS_X:
                if( rTypeInfo.mbCategoryAxis )
                {
                    aScaleData.AxisType = cssc2::AxisType::CATEGORY;
                    aScaleData.Categories = rTypeGroup.CreateCategSequence();
                }
                else
                    aScaleData.AxisType = cssc2::AxisType::REALNUMBER;
            break;
            case EXC_CHAXIS_Y:
                aScaleData.AxisType = rTypeGroup.IsPercent() ?
                    cssc2::AxisType::PERCENT : cssc2::AxisType::REALNUMBER;
            break;
            case EXC_CHAXIS_Z:
                aScaleData.AxisType = cssc2::AxisType::SERIES;
            break;
        }
        // axis scaling settings, dependent on axis type
        switch( aScaleData.AxisType )
        {
            case cssc2::AxisType::CATEGORY:
            case cssc2::AxisType::SERIES:
                OSL_ENSURE( mxLabelRange, "Missing Label Range" );
                // #i71684# radar charts have reversed rotation direction
                if (mxLabelRange)
                    mxLabelRange->Convert( aAxisProp, aScaleData, rTypeInfo.meTypeCateg == EXC_CHTYPECATEG_RADAR );
            break;
            case cssc2::AxisType::REALNUMBER:
            case cssc2::AxisType::PERCENT:
                // #i85167# pie/donut charts have reversed rotation direction (at Y axis!)
                mxValueRange->Convert( aScaleData, rTypeInfo.meTypeCateg == EXC_CHTYPECATEG_PIE );
            break;
            default:
                OSL_FAIL( "XclImpChAxis::CreateAxis - unknown axis type" );
        }

        /*  Do not set a value to the Origin member anymore (will be done via
            new axis properties 'CrossoverPosition' and 'CrossoverValue'). */
        aScaleData.Origin.clear();

        // write back
        xAxis->setScaleData( aScaleData );

        // grid ---------------------------------------------------------------

        // main grid
        ScfPropertySet aGridProp( xAxis->getGridProperties() );
        aGridProp.SetBoolProperty( EXC_CHPROP_SHOW, HasMajorGrid() );
        if( mxMajorGrid )
            mxMajorGrid->Convert( GetChRoot(), aGridProp, EXC_CHOBJTYPE_GRIDLINE );
        // sub grid
        Sequence< Reference< XPropertySet > > aSubGridPropSeq = xAxis->getSubGridProperties();
        if( aSubGridPropSeq.hasElements() )
        {
            ScfPropertySet aSubGridProp( aSubGridPropSeq[ 0 ] );
            aSubGridProp.SetBoolProperty( EXC_CHPROP_SHOW, HasMinorGrid() );
            if( mxMinorGrid )
                mxMinorGrid->Convert( GetChRoot(), aSubGridProp, EXC_CHOBJTYPE_GRIDLINE );
        }

        // position of crossing axis ------------------------------------------

        if( pCrossingAxis )
            pCrossingAxis->ConvertAxisPosition( aAxisProp, rTypeGroup );
    }
    return xAxis;
}

void XclImpChAxis::ConvertWall( ScfPropertySet& rPropSet ) const
{
    if( mxWallFrame )
        mxWallFrame->Convert( rPropSet );
}

void XclImpChAxis::ConvertAxisPosition( ScfPropertySet& rPropSet, const XclImpChTypeGroup& rTypeGroup ) const
{
    if( ((GetAxisType() == EXC_CHAXIS_X) && rTypeGroup.GetTypeInfo().mbCategoryAxis) || (GetAxisType() == EXC_CHAXIS_Z) )
    {
        OSL_ENSURE( mxLabelRange, "Missing Label Range" );
        if (mxLabelRange)
            mxLabelRange->ConvertAxisPosition( rPropSet, rTypeGroup.Is3dChart() );
    }
    else
        mxValueRange->ConvertAxisPosition( rPropSet );
}

void XclImpChAxis::ReadChAxisLine( XclImpStream& rStrm )
{
    XclImpChLineFormatRef* pxLineFmt = 0;
    bool bWallFrame = false;
    switch( rStrm.ReaduInt16() )
    {
        case EXC_CHAXISLINE_AXISLINE:   pxLineFmt = &mxAxisLine;    break;
        case EXC_CHAXISLINE_MAJORGRID:  pxLineFmt = &mxMajorGrid;   break;
        case EXC_CHAXISLINE_MINORGRID:  pxLineFmt = &mxMinorGrid;   break;
        case EXC_CHAXISLINE_WALLS:      bWallFrame = true;          break;
    }
    if( bWallFrame )
        CreateWallFrame();

    bool bLoop = pxLineFmt || bWallFrame;
    while( bLoop )
    {
        sal_uInt16 nRecId = rStrm.GetNextRecId();
        bLoop = ((nRecId == EXC_ID_CHLINEFORMAT) ||
                 (nRecId == EXC_ID_CHAREAFORMAT) ||
                 (nRecId == EXC_ID_CHESCHERFORMAT))
                 && rStrm.StartNextRecord();
        if( bLoop )
        {
            if( pxLineFmt && (nRecId == EXC_ID_CHLINEFORMAT) )
            {
                pxLineFmt->reset( new XclImpChLineFormat );
                (*pxLineFmt)->ReadChLineFormat( rStrm );
            }
            else if( bWallFrame && mxWallFrame )
            {
                mxWallFrame->ReadSubRecord( rStrm );
            }
        }
    }
}

void XclImpChAxis::CreateWallFrame()
{
    switch( GetAxisType() )
    {
        case EXC_CHAXIS_X:
            mxWallFrame.reset( new XclImpChFrame( GetChRoot(), EXC_CHOBJTYPE_WALL3D ) );
        break;
        case EXC_CHAXIS_Y:
            mxWallFrame.reset( new XclImpChFrame( GetChRoot(), EXC_CHOBJTYPE_FLOOR3D ) );
        break;
        default:
            mxWallFrame.reset();
    }
}

// ----------------------------------------------------------------------------

XclImpChAxesSet::XclImpChAxesSet( const XclImpChRoot& rRoot, sal_uInt16 nAxesSetId ) :
    XclImpChRoot( rRoot )
{
    maData.mnAxesSetId = nAxesSetId;
}

void XclImpChAxesSet::ReadHeaderRecord( XclImpStream& rStrm )
{
    rStrm >> maData.mnAxesSetId >> maData.maRect;
}

void XclImpChAxesSet::ReadSubRecord( XclImpStream& rStrm )
{
    switch( rStrm.GetRecId() )
    {
        case EXC_ID_CHFRAMEPOS:
            mxFramePos.reset( new XclImpChFramePos );
            mxFramePos->ReadChFramePos( rStrm );
        break;
        case EXC_ID_CHAXIS:
            ReadChAxis( rStrm );
        break;
        case EXC_ID_CHTEXT:
            ReadChText( rStrm );
        break;
        case EXC_ID_CHPLOTFRAME:
            ReadChPlotFrame( rStrm );
        break;
        case EXC_ID_CHTYPEGROUP:
            ReadChTypeGroup( rStrm );
        break;
    }
}

void XclImpChAxesSet::Finalize()
{
    if( IsValidAxesSet() )
    {
        // finalize chart type groups, erase empty groups without series
        XclImpChTypeGroupMap aValidGroups;
        for( XclImpChTypeGroupMap::const_iterator aIt = maTypeGroups.begin(), aEnd = maTypeGroups.end(); aIt != aEnd; ++aIt )
        {
            XclImpChTypeGroupRef xTypeGroup = aIt->second;
            xTypeGroup->Finalize();
            if( xTypeGroup->IsValidGroup() )
                aValidGroups.insert(
                    XclImpChTypeGroupMap::value_type(aIt->first, xTypeGroup));
        }
        maTypeGroups.swap( aValidGroups );
    }

    // invalid chart type groups are deleted now, check again with IsValidAxesSet()
    if( IsValidAxesSet() )
    {
        // always create missing axis objects
        if( !mxXAxis )
            mxXAxis.reset( new XclImpChAxis( GetChRoot(), EXC_CHAXIS_X ) );
        if( !mxYAxis )
            mxYAxis.reset( new XclImpChAxis( GetChRoot(), EXC_CHAXIS_Y ) );
        if( !mxZAxis && GetFirstTypeGroup()->Is3dDeepChart() )
            mxZAxis.reset( new XclImpChAxis( GetChRoot(), EXC_CHAXIS_Z ) );

        // finalize axes
        if( mxXAxis ) mxXAxis->Finalize();
        if( mxYAxis ) mxYAxis->Finalize();
        if( mxZAxis ) mxZAxis->Finalize();

        // finalize axis titles
        const XclImpChText* pDefText = GetChartData().GetDefaultText( EXC_CHTEXTTYPE_AXISTITLE );
        String aAutoTitle = CREATE_STRING( "Axis Title" );
        lclFinalizeTitle( mxXAxisTitle, pDefText, aAutoTitle );
        lclFinalizeTitle( mxYAxisTitle, pDefText, aAutoTitle );
        lclFinalizeTitle( mxZAxisTitle, pDefText, aAutoTitle );

        // #i47745# missing plot frame -> invisible border and area
        if( !mxPlotFrame )
            mxPlotFrame.reset( new XclImpChFrame( GetChRoot(), EXC_CHOBJTYPE_PLOTFRAME ) );
    }
}

XclImpChTypeGroupRef XclImpChAxesSet::GetTypeGroup( sal_uInt16 nGroupIdx ) const
{
    XclImpChTypeGroupMap::const_iterator itr = maTypeGroups.find(nGroupIdx);
    return itr == maTypeGroups.end() ? XclImpChTypeGroupRef() : itr->second;
}

XclImpChTypeGroupRef XclImpChAxesSet::GetFirstTypeGroup() const
{
    XclImpChTypeGroupRef xTypeGroup;
    if( !maTypeGroups.empty() )
        xTypeGroup = maTypeGroups.begin()->second;
    return xTypeGroup;
}

XclImpChLegendRef XclImpChAxesSet::GetLegend() const
{
    XclImpChLegendRef xLegend;
    for( XclImpChTypeGroupMap::const_iterator aIt = maTypeGroups.begin(), aEnd = maTypeGroups.end(); !xLegend && (aIt != aEnd); ++aIt )
        xLegend = aIt->second->GetLegend();
    return xLegend;
}

const String& XclImpChAxesSet::GetSingleSeriesTitle() const
{
    return (maTypeGroups.size() == 1) ? maTypeGroups.begin()->second->GetSingleSeriesTitle() : String::EmptyString();
}

void XclImpChAxesSet::Convert( Reference< XDiagram > xDiagram ) const
{
    if( IsValidAxesSet() && xDiagram.is() )
    {
        // diagram background formatting
        if( GetAxesSetId() == EXC_CHAXESSET_PRIMARY )
            ConvertBackground( xDiagram );

        // create the coordinate system, this inserts all chart types and series
        Reference< XCoordinateSystem > xCoordSystem = CreateCoordSystem( xDiagram );
        if( xCoordSystem.is() )
        {
            // insert coordinate system, if not already done
            try
            {
                Reference< XCoordinateSystemContainer > xCoordSystemCont( xDiagram, UNO_QUERY_THROW );
                Sequence< Reference< XCoordinateSystem > > aCoordSystems = xCoordSystemCont->getCoordinateSystems();
                if( aCoordSystems.getLength() == 0 )
                    xCoordSystemCont->addCoordinateSystem( xCoordSystem );
            }
            catch( Exception& )
            {
                OSL_FAIL( "XclImpChAxesSet::Convert - cannot insert coordinate system" );
            }

            // create the axes with grids and axis titles and insert them into the diagram
            ConvertAxis( mxXAxis, mxXAxisTitle, xCoordSystem, mxYAxis.get() );
            ConvertAxis( mxYAxis, mxYAxisTitle, xCoordSystem, mxXAxis.get() );
            ConvertAxis( mxZAxis, mxZAxisTitle, xCoordSystem, 0 );
        }
    }
}

void XclImpChAxesSet::ConvertTitlePositions() const
{
    if( mxXAxisTitle )
        mxXAxisTitle->ConvertTitlePosition( XclChTextKey( EXC_CHTEXTTYPE_AXISTITLE, maData.mnAxesSetId, EXC_CHAXIS_X ) );
    if( mxYAxisTitle )
        mxYAxisTitle->ConvertTitlePosition( XclChTextKey( EXC_CHTEXTTYPE_AXISTITLE, maData.mnAxesSetId, EXC_CHAXIS_Y ) );
    if( mxZAxisTitle )
        mxZAxisTitle->ConvertTitlePosition( XclChTextKey( EXC_CHTEXTTYPE_AXISTITLE, maData.mnAxesSetId, EXC_CHAXIS_Z ) );
}

void XclImpChAxesSet::ReadChAxis( XclImpStream& rStrm )
{
    XclImpChAxisRef xAxis( new XclImpChAxis( GetChRoot() ) );
    xAxis->ReadRecordGroup( rStrm );

    switch( xAxis->GetAxisType() )
    {
        case EXC_CHAXIS_X:  mxXAxis = xAxis;    break;
        case EXC_CHAXIS_Y:  mxYAxis = xAxis;    break;
        case EXC_CHAXIS_Z:  mxZAxis = xAxis;    break;
    }
}

void XclImpChAxesSet::ReadChText( XclImpStream& rStrm )
{
    XclImpChTextRef xText( new XclImpChText( GetChRoot() ) );
    xText->ReadRecordGroup( rStrm );

    switch( xText->GetLinkTarget() )
    {
        case EXC_CHOBJLINK_XAXIS:   mxXAxisTitle = xText;   break;
        case EXC_CHOBJLINK_YAXIS:   mxYAxisTitle = xText;   break;
        case EXC_CHOBJLINK_ZAXIS:   mxZAxisTitle = xText;   break;
    }
}

void XclImpChAxesSet::ReadChPlotFrame( XclImpStream& rStrm )
{
    if( (rStrm.GetNextRecId() == EXC_ID_CHFRAME) && rStrm.StartNextRecord() )
    {
        mxPlotFrame.reset( new XclImpChFrame( GetChRoot(), EXC_CHOBJTYPE_PLOTFRAME ) );
        mxPlotFrame->ReadRecordGroup( rStrm );
    }
}

void XclImpChAxesSet::ReadChTypeGroup( XclImpStream& rStrm )
{
    XclImpChTypeGroupRef xTypeGroup( new XclImpChTypeGroup( GetChRoot() ) );
    xTypeGroup->ReadRecordGroup( rStrm );
    sal_uInt16 nGroupIdx = xTypeGroup->GetGroupIdx();
    XclImpChTypeGroupMap::iterator itr = maTypeGroups.lower_bound(nGroupIdx);
    if (itr != maTypeGroups.end() && !maTypeGroups.key_comp()(nGroupIdx, itr->first))
        // Overwrite the existing element.
        itr->second = xTypeGroup;
    else
        maTypeGroups.insert(
            itr, XclImpChTypeGroupMap::value_type(nGroupIdx, xTypeGroup));
}

Reference< XCoordinateSystem > XclImpChAxesSet::CreateCoordSystem( Reference< XDiagram > xDiagram ) const
{
    Reference< XCoordinateSystem > xCoordSystem;

    /*  Try to get existing ccordinate system. For now, all series from primary
        and secondary axes sets are inserted into one coordinate system. Later,
        this should be changed to use one coordinate system for each axes set. */
    Reference< XCoordinateSystemContainer > xCoordSystemCont( xDiagram, UNO_QUERY );
    if( xCoordSystemCont.is() )
    {
        Sequence< Reference< XCoordinateSystem > > aCoordSystems = xCoordSystemCont->getCoordinateSystems();
        OSL_ENSURE( aCoordSystems.getLength() <= 1, "XclImpChAxesSet::CreateCoordSystem - too many existing coordinate systems" );
        if( aCoordSystems.getLength() > 0 )
            xCoordSystem = aCoordSystems[ 0 ];
    }

    // create the coordinate system according to the first chart type
    if( !xCoordSystem.is() )
    {
        XclImpChTypeGroupRef xTypeGroup = GetFirstTypeGroup();
        if( xTypeGroup )
        {
            xCoordSystem = xTypeGroup->CreateCoordSystem();
            // convert 3d chart settings
            ScfPropertySet aDiaProp( xDiagram );
            xTypeGroup->ConvertChart3d( aDiaProp );
        }
    }

    /*  Create XChartType objects for all chart type groups. Each group will
        add its series to the data provider attached to the chart document. */
    Reference< XChartTypeContainer > xChartTypeCont( xCoordSystem, UNO_QUERY );
    if( xChartTypeCont.is() )
    {
        sal_Int32 nApiAxesSetIdx = GetApiAxesSetIndex();
        for( XclImpChTypeGroupMap::const_iterator aIt = maTypeGroups.begin(), aEnd = maTypeGroups.end(); aIt != aEnd; ++aIt )
        {
            try
            {
                Reference< XChartType > xChartType = aIt->second->CreateChartType( xDiagram, nApiAxesSetIdx );
                if( xChartType.is() )
                    xChartTypeCont->addChartType( xChartType );
            }
            catch( Exception& )
            {
                OSL_FAIL( "XclImpChAxesSet::CreateCoordSystem - cannot add chart type" );
            }
        }
    }

    return xCoordSystem;
}

void XclImpChAxesSet::ConvertAxis(
        XclImpChAxisRef xChAxis, XclImpChTextRef xChAxisTitle,
        Reference< XCoordinateSystem > xCoordSystem, const XclImpChAxis* pCrossingAxis ) const
{
    if( xChAxis )
    {
        // create and attach the axis object
        Reference< XAxis > xAxis = CreateAxis( *xChAxis, pCrossingAxis );
        if( xAxis.is() )
        {
            // create and attach the axis title
            if( xChAxisTitle ) try
            {
                Reference< XTitled > xTitled( xAxis, UNO_QUERY_THROW );
                Reference< XTitle > xTitle( xChAxisTitle->CreateTitle(), UNO_SET_THROW );
                xTitled->setTitleObject( xTitle );
            }
            catch( Exception& )
            {
                OSL_FAIL( "XclImpChAxesSet::ConvertAxis - cannot set axis title" );
            }

            // insert axis into coordinate system
            try
            {
                sal_Int32 nApiAxisDim = xChAxis->GetApiAxisDimension();
                sal_Int32 nApiAxesSetIdx = GetApiAxesSetIndex();
                xCoordSystem->setAxisByDimension( nApiAxisDim, xAxis, nApiAxesSetIdx );
            }
            catch( Exception& )
            {
                OSL_FAIL( "XclImpChAxesSet::ConvertAxis - cannot set axis" );
            }
        }
    }
}

Reference< XAxis > XclImpChAxesSet::CreateAxis( const XclImpChAxis& rChAxis, const XclImpChAxis* pCrossingAxis ) const
{
    Reference< XAxis > xAxis;
    if( const XclImpChTypeGroup* pTypeGroup = GetFirstTypeGroup().get() )
        xAxis = rChAxis.CreateAxis( *pTypeGroup, pCrossingAxis );
    return xAxis;
}

void XclImpChAxesSet::ConvertBackground( Reference< XDiagram > xDiagram ) const
{
    XclImpChTypeGroupRef xTypeGroup = GetFirstTypeGroup();
    if( xTypeGroup && xTypeGroup->Is3dWallChart() )
    {
        // wall/floor formatting (3D charts)
        if( mxXAxis )
        {
            ScfPropertySet aWallProp( xDiagram->getWall() );
            mxXAxis->ConvertWall( aWallProp );
        }
        if( mxYAxis )
        {
            ScfPropertySet aFloorProp( xDiagram->getFloor() );
            mxYAxis->ConvertWall( aFloorProp );
        }
    }
    else if( mxPlotFrame )
    {
        // diagram background formatting
        ScfPropertySet aWallProp( xDiagram->getWall() );
        mxPlotFrame->Convert( aWallProp );
    }
}

// The chart object ===========================================================

XclImpChChart::XclImpChChart( const XclImpRoot& rRoot ) :
    XclImpChRoot( rRoot, *this )
{
    mxPrimAxesSet.reset( new XclImpChAxesSet( GetChRoot(), EXC_CHAXESSET_PRIMARY ) );
    mxSecnAxesSet.reset( new XclImpChAxesSet( GetChRoot(), EXC_CHAXESSET_SECONDARY ) );
}

XclImpChChart::~XclImpChChart()
{
}

void XclImpChChart::ReadHeaderRecord( XclImpStream& rStrm )
{
    // coordinates are stored as 16.16 fixed point
    rStrm >> maRect;
}

void XclImpChChart::ReadSubRecord( XclImpStream& rStrm )
{
    switch( rStrm.GetRecId() )
    {
        case EXC_ID_CHFRAME:
            mxFrame.reset( new XclImpChFrame( GetChRoot(), EXC_CHOBJTYPE_BACKGROUND ) );
            mxFrame->ReadRecordGroup( rStrm );
        break;
        case EXC_ID_CHSERIES:
            ReadChSeries( rStrm );
        break;
        case EXC_ID_CHPROPERTIES:
            ReadChProperties( rStrm );
        break;
        case EXC_ID_CHDEFAULTTEXT:
            ReadChDefaultText( rStrm );
        break;
        case EXC_ID_CHAXESSET:
            ReadChAxesSet( rStrm );
        break;
        case EXC_ID_CHTEXT:
            ReadChText( rStrm );
        break;
        case EXC_ID_CHEND:
            Finalize();     // finalize the entire chart object
        break;
    }
}

void XclImpChChart::ReadChDefaultText( XclImpStream& rStrm )
{
    sal_uInt16 nTextId = rStrm.ReaduInt16();
    if( (rStrm.GetNextRecId() == EXC_ID_CHTEXT) && rStrm.StartNextRecord() )
    {
        SAL_WNODEPRECATED_DECLARATIONS_PUSH
        auto_ptr<XclImpChText> pText(new XclImpChText(GetChRoot()));
        SAL_WNODEPRECATED_DECLARATIONS_POP
        pText->ReadRecordGroup(rStrm);
        maDefTexts.insert(nTextId, pText);
    }
}

void XclImpChChart::ReadChDataFormat( XclImpStream& rStrm )
{
    XclImpChDataFormatRef xDataFmt( new XclImpChDataFormat( GetChRoot() ) );
    xDataFmt->ReadRecordGroup( rStrm );
    if( xDataFmt->GetPointPos().mnSeriesIdx <= EXC_CHSERIES_MAXSERIES )
    {
        const XclChDataPointPos& rPos = xDataFmt->GetPointPos();
        XclImpChDataFormatMap::iterator itr = maDataFmts.lower_bound(rPos);
        if (itr == maDataFmts.end() || maDataFmts.key_comp()(rPos, itr->first))
            // No element exists for this data point.  Insert it.
            maDataFmts.insert(
                itr, XclImpChDataFormatMap::value_type(rPos, xDataFmt));

        /*  Do not overwrite existing data format group, Excel always uses the
            first data format group occuring in any CHSERIES group. */
    }
}

void XclImpChChart::UpdateObjFrame( const XclObjLineData& rLineData, const XclObjFillData& rFillData )
{
    if( !mxFrame )
        mxFrame.reset( new XclImpChFrame( GetChRoot(), EXC_CHOBJTYPE_BACKGROUND ) );
    mxFrame->UpdateObjFrame( rLineData, rFillData );
}

XclImpChTypeGroupRef XclImpChChart::GetTypeGroup( sal_uInt16 nGroupIdx ) const
{
    XclImpChTypeGroupRef xTypeGroup = mxPrimAxesSet->GetTypeGroup( nGroupIdx );
    if( !xTypeGroup ) xTypeGroup = mxSecnAxesSet->GetTypeGroup( nGroupIdx );
    if( !xTypeGroup ) xTypeGroup = mxPrimAxesSet->GetFirstTypeGroup();
    return xTypeGroup;
}

const XclImpChText* XclImpChChart::GetDefaultText( XclChTextType eTextType ) const
{
    sal_uInt16 nDefTextId = EXC_CHDEFTEXT_GLOBAL;
    bool bBiff8 = GetBiff() == EXC_BIFF8;
    switch( eTextType )
    {
        case EXC_CHTEXTTYPE_TITLE:      nDefTextId = EXC_CHDEFTEXT_GLOBAL;                                  break;
        case EXC_CHTEXTTYPE_LEGEND:     nDefTextId = EXC_CHDEFTEXT_GLOBAL;                                  break;
        case EXC_CHTEXTTYPE_AXISTITLE:  nDefTextId = bBiff8 ? EXC_CHDEFTEXT_AXESSET : EXC_CHDEFTEXT_GLOBAL; break;
        case EXC_CHTEXTTYPE_AXISLABEL:  nDefTextId = bBiff8 ? EXC_CHDEFTEXT_AXESSET : EXC_CHDEFTEXT_GLOBAL; break;
        case EXC_CHTEXTTYPE_DATALABEL:  nDefTextId = bBiff8 ? EXC_CHDEFTEXT_AXESSET : EXC_CHDEFTEXT_GLOBAL; break;
    }

    XclImpChTextMap::const_iterator itr = maDefTexts.find(nDefTextId);
    return itr == maDefTexts.end() ? NULL : itr->second;
}

bool XclImpChChart::IsManualPlotArea() const
{
    // there is no real automatic mode in BIFF5 charts
    return (GetBiff() <= EXC_BIFF5) || ::get_flag( maProps.mnFlags, EXC_CHPROPS_USEMANPLOTAREA );
}

void XclImpChChart::Convert( const Reference<XChartDocument>& xChartDoc,
        XclImpDffConverter& rDffConv, const OUString& rObjName, const Rectangle& rChartRect ) const
{
    // initialize conversion (locks the model to suppress any internal updates)
    InitConversion( xChartDoc, rChartRect );

    // chart frame formatting
    if( mxFrame )
    {
        ScfPropertySet aFrameProp( xChartDoc->getPageBackground() );
        mxFrame->Convert( aFrameProp );
    }

    // chart title
    if( mxTitle ) try
    {
        Reference< XTitled > xTitled( xChartDoc, UNO_QUERY_THROW );
        Reference< XTitle > xTitle( mxTitle->CreateTitle(), UNO_SET_THROW );
        xTitled->setTitleObject( xTitle );
    }
    catch( Exception& )
    {
    }

    /*  Create the diagram object and attach it to the chart document. Currently,
        one diagram is used to carry all coordinate systems and data series. */
    Reference< XDiagram > xDiagram = CreateDiagram();
    xChartDoc->setFirstDiagram( xDiagram );

    // coordinate systems and chart types, convert axis settings
    mxPrimAxesSet->Convert( xDiagram );
    mxSecnAxesSet->Convert( xDiagram );

    // legend
    if( xDiagram.is() && mxLegend )
        xDiagram->setLegend( mxLegend->CreateLegend() );

    /*  Following all conversions needing the old Chart1 API that involves full
        initialization of the chart view. */
    Reference< cssc::XChartDocument > xChart1Doc( xChartDoc, UNO_QUERY );
    if( xChart1Doc.is() )
    {
        Reference< cssc::XDiagram > xDiagram1 = xChart1Doc->getDiagram();

        /*  Set the 'IncludeHiddenCells' property via the old API as only this
            ensures that the data provider and all created sequences get this
            flag correctly. */
        ScfPropertySet aDiaProp( xDiagram1 );
        bool bShowVisCells = ::get_flag( maProps.mnFlags, EXC_CHPROPS_SHOWVISIBLEONLY );
        aDiaProp.SetBoolProperty( EXC_CHPROP_INCLUDEHIDDENCELLS, !bShowVisCells );

        // plot area position and size (there is no real automatic mode in BIFF5 charts)
        XclImpChFramePosRef xPlotAreaPos = mxPrimAxesSet->GetPlotAreaFramePos();
        if( IsManualPlotArea() && xPlotAreaPos ) try
        {
            const XclChFramePos& rFramePos = xPlotAreaPos->GetFramePosData();
            if( (rFramePos.mnTLMode == EXC_CHFRAMEPOS_PARENT) && (rFramePos.mnBRMode == EXC_CHFRAMEPOS_PARENT) )
            {
                Reference< cssc::XDiagramPositioning > xPositioning( xDiagram1, UNO_QUERY_THROW );
                ::com::sun::star::awt::Rectangle aDiagramRect = CalcHmmFromChartRect( rFramePos.maRect );
                // for pie charts, always set inner plot area size to exclude the data labels as Excel does
                const XclImpChTypeGroup* pFirstTypeGroup = mxPrimAxesSet->GetFirstTypeGroup().get();
                if( pFirstTypeGroup && (pFirstTypeGroup->GetTypeInfo().meTypeCateg == EXC_CHTYPECATEG_PIE) )
                    xPositioning->setDiagramPositionExcludingAxes( aDiagramRect );
                else if( pFirstTypeGroup && pFirstTypeGroup->Is3dChart() )
                    xPositioning->setDiagramPositionIncludingAxesAndAxisTitles( aDiagramRect );
                else
                    xPositioning->setDiagramPositionIncludingAxes( aDiagramRect );
            }
        }
        catch( Exception& )
        {
        }

        // positions of all title objects
        if( mxTitle )
            mxTitle->ConvertTitlePosition( XclChTextKey( EXC_CHTEXTTYPE_TITLE ) );
        mxPrimAxesSet->ConvertTitlePositions();
        mxSecnAxesSet->ConvertTitlePositions();
    }

    // unlock the model
    FinishConversion( rDffConv );

    // start listening to this chart
    ScDocument& rDoc = GetRoot().GetDoc();
    if( ScChartListenerCollection* pChartCollection = rDoc.GetChartListenerCollection() )
    {
        SAL_WNODEPRECATED_DECLARATIONS_PUSH
        ::std::auto_ptr< ::std::vector< ScTokenRef > > xRefTokens( new ::std::vector< ScTokenRef > );
        SAL_WNODEPRECATED_DECLARATIONS_POP
        for( XclImpChSeriesVec::const_iterator aIt = maSeries.begin(), aEnd = maSeries.end(); aIt != aEnd; ++aIt )
            (*aIt)->FillAllSourceLinks( *xRefTokens );
        if( !xRefTokens->empty() )
        {
            SAL_WNODEPRECATED_DECLARATIONS_PUSH
            ::std::auto_ptr< ScChartListener > xListener( new ScChartListener( rObjName, &rDoc, xRefTokens.release() ) );
            SAL_WNODEPRECATED_DECLARATIONS_POP
            xListener->SetUsed( true );
            xListener->StartListeningTo();
            pChartCollection->Insert( xListener.release() );
        }
    }
}

void XclImpChChart::ReadChSeries( XclImpStream& rStrm )
{
    sal_uInt16 nNewSeriesIdx = static_cast< sal_uInt16 >( maSeries.size() );
    XclImpChSeriesRef xSeries( new XclImpChSeries( GetChRoot(), nNewSeriesIdx ) );
    xSeries->ReadRecordGroup( rStrm );
    maSeries.push_back( xSeries );
}

void XclImpChChart::ReadChProperties( XclImpStream& rStrm )
{
    rStrm >> maProps.mnFlags >> maProps.mnEmptyMode;
}

void XclImpChChart::ReadChAxesSet( XclImpStream& rStrm )
{
    XclImpChAxesSetRef xAxesSet( new XclImpChAxesSet( GetChRoot(), EXC_CHAXESSET_NONE ) );
    xAxesSet->ReadRecordGroup( rStrm );
    switch( xAxesSet->GetAxesSetId() )
    {
        case EXC_CHAXESSET_PRIMARY:     mxPrimAxesSet = xAxesSet;   break;
        case EXC_CHAXESSET_SECONDARY:   mxSecnAxesSet = xAxesSet;   break;
    }
}

void XclImpChChart::ReadChText( XclImpStream& rStrm )
{
    XclImpChTextRef xText( new XclImpChText( GetChRoot() ) );
    xText->ReadRecordGroup( rStrm );
    switch( xText->GetLinkTarget() )
    {
        case EXC_CHOBJLINK_TITLE:
            mxTitle = xText;
        break;
        case EXC_CHOBJLINK_DATA:
        {
            sal_uInt16 nSeriesIdx = xText->GetPointPos().mnSeriesIdx;
            if( nSeriesIdx < maSeries.size() )
                maSeries[ nSeriesIdx ]->SetDataLabel( xText );
        }
        break;
    }
}

void XclImpChChart::Finalize()
{
    // finalize series (must be done first)
    FinalizeSeries();
    // #i49218# legend may be attached to primary or secondary axes set
    mxLegend = mxPrimAxesSet->GetLegend();
    if( !mxLegend )
        mxLegend = mxSecnAxesSet->GetLegend();
    if( mxLegend )
        mxLegend->Finalize();
    // axes sets, updates chart type group default formats -> must be called before FinalizeDataFormats()
    mxPrimAxesSet->Finalize();
    mxSecnAxesSet->Finalize();
    // formatting of all series
    FinalizeDataFormats();
    // #i47745# missing frame -> invisible border and area
    if( !mxFrame )
        mxFrame.reset( new XclImpChFrame( GetChRoot(), EXC_CHOBJTYPE_BACKGROUND ) );
    // chart title
    FinalizeTitle();
}

void XclImpChChart::FinalizeSeries()
{
    for( XclImpChSeriesVec::iterator aSIt = maSeries.begin(), aSEnd = maSeries.end(); aSIt != aSEnd; ++aSIt )
    {
        XclImpChSeriesRef xSeries = *aSIt;
        if( xSeries->HasParentSeries() )
        {
            /*  Process child series (trend lines and error bars). Data of
                child series will be set at the connected parent series. */
            if( xSeries->GetParentIdx() < maSeries.size() )
                maSeries[ xSeries->GetParentIdx() ]->AddChildSeries( *xSeries );
        }
        else
        {
            // insert the series into the related chart type group
            if( XclImpChTypeGroup* pTypeGroup = GetTypeGroup( xSeries->GetGroupIdx() ).get() )
                pTypeGroup->AddSeries( xSeries );
        }
    }
}

void XclImpChChart::FinalizeDataFormats()
{
    /*  #i51639# (part 1): CHDATAFORMAT groups are part of CHSERIES groups.
        Each CHDATAFORMAT group specifies the series and data point it is
        assigned to. This makes it possible to have a data format that is
        related to another series, e.g. a CHDATAFORMAT group for series 2 is
        part of a CHSERIES group that describes series 1. Therefore the chart
        itself has collected all CHDATAFORMAT groups to be able to store data
        format groups for series that have not been imported at that time. This
        loop finally assigns these groups to the related series. */
    for( XclImpChDataFormatMap::const_iterator aMIt = maDataFmts.begin(), aMEnd = maDataFmts.end(); aMIt != aMEnd; ++aMIt )
    {
        sal_uInt16 nSeriesIdx = aMIt->first.mnSeriesIdx;
        if( nSeriesIdx < maSeries.size() )
            maSeries[ nSeriesIdx ]->SetDataFormat( aMIt->second );
    }

    /*  #i51639# (part 2): Finalize data formats of all series. This adds for
        example missing CHDATAFORMAT groups for entire series that are needed
        for automatic colors of lines and areas. */
    for( XclImpChSeriesVec::iterator aVIt = maSeries.begin(), aVEnd = maSeries.end(); aVIt != aVEnd; ++aVIt )
        (*aVIt)->FinalizeDataFormats();
}

void XclImpChChart::FinalizeTitle()
{
    // special handling for auto-generated title
    String aAutoTitle;
    if( !mxTitle || (!mxTitle->IsDeleted() && !mxTitle->HasString()) )
    {
        // automatic title from first series name (if there are no series on secondary axes set)
        if( !mxSecnAxesSet->IsValidAxesSet() )
            aAutoTitle = mxPrimAxesSet->GetSingleSeriesTitle();
        if( mxTitle || (aAutoTitle.Len() > 0) )
        {
            if( !mxTitle )
                mxTitle.reset( new XclImpChText( GetChRoot() ) );
            if( aAutoTitle.Len() == 0 )
                aAutoTitle = CREATE_STRING( "Chart Title" );
        }
    }

    // will reset mxTitle, if it does not contain a string and no auto title exists
    lclFinalizeTitle( mxTitle, GetDefaultText( EXC_CHTEXTTYPE_TITLE ), aAutoTitle );
}

Reference< XDiagram > XclImpChChart::CreateDiagram() const
{
    // create a diagram object
    Reference< XDiagram > xDiagram( ScfApiHelper::CreateInstance( SERVICE_CHART2_DIAGRAM ), UNO_QUERY );

    // convert global chart settings
    ScfPropertySet aDiaProp( xDiagram );

    // treatment of missing values
    using namespace cssc::MissingValueTreatment;
    sal_Int32 nMissingValues = LEAVE_GAP;
    switch( maProps.mnEmptyMode )
    {
        case EXC_CHPROPS_EMPTY_SKIP:        nMissingValues = LEAVE_GAP; break;
        case EXC_CHPROPS_EMPTY_ZERO:        nMissingValues = USE_ZERO;  break;
        case EXC_CHPROPS_EMPTY_INTERPOLATE: nMissingValues = CONTINUE;  break;
    }
    aDiaProp.SetProperty( EXC_CHPROP_MISSINGVALUETREATMENT, nMissingValues );

    return xDiagram;
}

// ----------------------------------------------------------------------------

XclImpChartDrawing::XclImpChartDrawing( const XclImpRoot& rRoot, bool bOwnTab ) :
    XclImpDrawing( rRoot, bOwnTab ), // sheet charts may contain OLE objects
    mnScTab( rRoot.GetCurrScTab() ),
    mbOwnTab( bOwnTab )
{
}

void XclImpChartDrawing::ConvertObjects( XclImpDffConverter& rDffConv,
        const Reference< XModel >& rxModel, const Rectangle& rChartRect )
{
    maChartRect = rChartRect;   // needed in CalcAnchorRect() callback

    SdrModel* pSdrModel = 0;
    SdrPage* pSdrPage = 0;
    if( mbOwnTab )
    {
        // chart sheet: insert all shapes into the sheet, not into the chart object
        pSdrModel = GetDoc().GetDrawLayer();
        pSdrPage = GetSdrPage( mnScTab );
    }
    else
    {
        // embedded chart object: insert all shapes into the chart
        try
        {
            Reference< XDrawPageSupplier > xDrawPageSupp( rxModel, UNO_QUERY_THROW );
            Reference< XDrawPage > xDrawPage( xDrawPageSupp->getDrawPage(), UNO_SET_THROW );
            pSdrPage = ::GetSdrPageFromXDrawPage( xDrawPage );
            pSdrModel = pSdrPage ? pSdrPage->GetModel() : 0;
        }
        catch( Exception& )
        {
        }
    }

    if( pSdrModel && pSdrPage )
        ImplConvertObjects( rDffConv, *pSdrModel, *pSdrPage );
}

Rectangle XclImpChartDrawing::CalcAnchorRect( const XclObjAnchor& rAnchor, bool bDffAnchor ) const
{
    /*  In objects with DFF client anchor, the position of the shape is stored
        in the cell address components of the client anchor. In old BIFF3-BIFF5
        objects, the position is stored in the offset components of the anchor. */
    Rectangle aRect(
        static_cast< long >( static_cast< double >( bDffAnchor ? rAnchor.maFirst.mnCol : rAnchor.mnLX ) / EXC_CHART_TOTALUNITS * maChartRect.GetWidth()  + 0.5 ),
        static_cast< long >( static_cast< double >( bDffAnchor ? rAnchor.maFirst.mnRow : rAnchor.mnTY ) / EXC_CHART_TOTALUNITS * maChartRect.GetHeight() + 0.5 ),
        static_cast< long >( static_cast< double >( bDffAnchor ? rAnchor.maLast.mnCol  : rAnchor.mnRX ) / EXC_CHART_TOTALUNITS * maChartRect.GetWidth()  + 0.5 ),
        static_cast< long >( static_cast< double >( bDffAnchor ? rAnchor.maLast.mnRow  : rAnchor.mnBY ) / EXC_CHART_TOTALUNITS * maChartRect.GetHeight() + 0.5 ) );
    aRect.Justify();
    // move shapes into chart area for sheet charts
    if( mbOwnTab )
        aRect.Move( maChartRect.Left(), maChartRect.Top() );
    return aRect;
}

void XclImpChartDrawing::OnObjectInserted( const XclImpDrawObjBase& )
{
}

// ----------------------------------------------------------------------------

XclImpChart::XclImpChart( const XclImpRoot& rRoot, bool bOwnTab ) :
    XclImpRoot( rRoot ),
    mbOwnTab( bOwnTab ),
    mbIsPivotChart( false )
{
}

XclImpChart::~XclImpChart()
{
}

void XclImpChart::ReadChartSubStream( XclImpStream& rStrm )
{
    XclImpPageSettings& rPageSett = GetPageSettings();
    XclImpTabViewSettings& rTabViewSett = GetTabViewSettings();

    bool bLoop = true;
    while( bLoop && rStrm.StartNextRecord() )
    {
        // page settings - only for charts in entire sheet
        if( mbOwnTab ) switch( rStrm.GetRecId() )
        {
            case EXC_ID_HORPAGEBREAKS:
            case EXC_ID_VERPAGEBREAKS:  rPageSett.ReadPageBreaks( rStrm );      break;
            case EXC_ID_HEADER:
            case EXC_ID_FOOTER:         rPageSett.ReadHeaderFooter( rStrm );    break;
            case EXC_ID_LEFTMARGIN:
            case EXC_ID_RIGHTMARGIN:
            case EXC_ID_TOPMARGIN:
            case EXC_ID_BOTTOMMARGIN:   rPageSett.ReadMargin( rStrm );          break;
            case EXC_ID_PRINTHEADERS:   rPageSett.ReadPrintHeaders( rStrm );    break;
            case EXC_ID_PRINTGRIDLINES: rPageSett.ReadPrintGridLines( rStrm );  break;
            case EXC_ID_HCENTER:
            case EXC_ID_VCENTER:        rPageSett.ReadCenter( rStrm );          break;
            case EXC_ID_SETUP:          rPageSett.ReadSetup( rStrm );           break;
            case EXC_ID8_IMGDATA:       rPageSett.ReadImgData( rStrm );         break;

            case EXC_ID_WINDOW2:        rTabViewSett.ReadWindow2( rStrm, true );break;
            case EXC_ID_SCL:            rTabViewSett.ReadScl( rStrm );          break;

            case EXC_ID_SHEETEXT: //0x0862
            {
                // FIXME: do not need to pass palette, XclImpTabVieSettings is derived from root
                XclImpPalette& rPal = GetPalette();
                rTabViewSett.ReadTabBgColor( rStrm,  rPal);
            }
            break;

            case EXC_ID_CODENAME:       ReadCodeName( rStrm, false );           break;
        }

        // common records
        switch( rStrm.GetRecId() )
        {
            case EXC_ID_EOF:            bLoop = false;                          break;

            // #i31882# ignore embedded chart objects
            case EXC_ID2_BOF:
            case EXC_ID3_BOF:
            case EXC_ID4_BOF:
            case EXC_ID5_BOF:           XclTools::SkipSubStream( rStrm );       break;

            case EXC_ID_CHCHART:        ReadChChart( rStrm );                   break;

            case EXC_ID8_CHPIVOTREF:
                GetTracer().TracePivotChartExists();
                mbIsPivotChart = true;
            break;

            // BIFF specific records
            default: switch( GetBiff() )
            {
                case EXC_BIFF5: switch( rStrm.GetRecId() )
                {
                    case EXC_ID_OBJ:        GetChartDrawing().ReadObj( rStrm );         break;
                }
                break;
                case EXC_BIFF8: switch( rStrm.GetRecId() )
                {
                    case EXC_ID_MSODRAWING: GetChartDrawing().ReadMsoDrawing( rStrm );  break;
                    // #i61786# weird documents: OBJ without MSODRAWING -> read in BIFF5 format
                    case EXC_ID_OBJ:        GetChartDrawing().ReadObj( rStrm );         break;
                }
                break;
                default:;
            }
        }
    }
}

void XclImpChart::UpdateObjFrame( const XclObjLineData& rLineData, const XclObjFillData& rFillData )
{
    if( !mxChartData )
        mxChartData.reset( new XclImpChChart( GetRoot() ) );
    mxChartData->UpdateObjFrame( rLineData, rFillData );
}

sal_Size XclImpChart::GetProgressSize() const
{
    return
        (mxChartData ? mxChartData->GetProgressSize() : 0) +
        (mxChartDrawing ? mxChartDrawing->GetProgressSize() : 0);
}

void XclImpChart::Convert( Reference< XModel > xModel, XclImpDffConverter& rDffConv, const OUString& rObjName, const Rectangle& rChartRect ) const
{
    Reference< XChartDocument > xChartDoc( xModel, UNO_QUERY );
    if( xChartDoc.is() )
    {
        if( mxChartData )
            mxChartData->Convert( xChartDoc, rDffConv, rObjName, rChartRect );
        if( mxChartDrawing )
            mxChartDrawing->ConvertObjects( rDffConv, xModel, rChartRect );
    }
}

XclImpChartDrawing& XclImpChart::GetChartDrawing()
{
    if( !mxChartDrawing )
        mxChartDrawing.reset( new XclImpChartDrawing( GetRoot(), mbOwnTab ) );
    return *mxChartDrawing;
}

void XclImpChart::ReadChChart( XclImpStream& rStrm )
{
    mxChartData.reset( new XclImpChChart( GetRoot() ) );
    mxChartData->ReadRecordGroup( rStrm );
}

// ============================================================================

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */