Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "excelchartconverter.hxx"
21 :
22 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 : #include <com/sun/star/chart2/data/XDataProvider.hpp>
24 : #include <com/sun/star/chart2/data/XDataReceiver.hpp>
25 : #include <com/sun/star/chart2/data/XSheetDataProvider.hpp>
26 :
27 : #include "oox/core/filterbase.hxx"
28 : #include "oox/drawingml/chart/datasourcemodel.hxx"
29 : #include "oox/helper/containerhelper.hxx"
30 : #include "formulaparser.hxx"
31 :
32 : namespace oox {
33 : namespace xls {
34 :
35 : // ============================================================================
36 :
37 : using namespace ::com::sun::star::chart2;
38 : using namespace ::com::sun::star::chart2::data;
39 : using namespace ::com::sun::star::lang;
40 : using namespace ::com::sun::star::table;
41 : using namespace ::com::sun::star::uno;
42 :
43 : using ::oox::drawingml::chart::DataSequenceModel;
44 : using ::rtl::OUString;
45 :
46 : // ============================================================================
47 :
48 22 : ExcelChartConverter::ExcelChartConverter( const WorkbookHelper& rHelper ) :
49 22 : WorkbookHelper( rHelper )
50 : {
51 22 : }
52 :
53 44 : ExcelChartConverter::~ExcelChartConverter()
54 : {
55 44 : }
56 :
57 0 : void ExcelChartConverter::createDataProvider( const Reference< XChartDocument >& rxChartDoc )
58 : {
59 : try
60 : {
61 0 : Reference< XDataReceiver > xDataRec( rxChartDoc, UNO_QUERY_THROW );
62 0 : Reference< XDataProvider > xDataProv( getBaseFilter().getModelFactory()->createInstance(
63 0 : "com.sun.star.chart2.data.DataProvider" ), UNO_QUERY_THROW );
64 0 : xDataRec->attachDataProvider( xDataProv );
65 : }
66 0 : catch( Exception& )
67 : {
68 : }
69 0 : }
70 :
71 0 : Reference< XDataSequence > ExcelChartConverter::createDataSequence(
72 : const Reference< XDataProvider >& rxDataProvider, const DataSequenceModel& rDataSeq )
73 : {
74 0 : Reference< XDataSequence > xDataSeq;
75 0 : if (!rxDataProvider.is())
76 : return xDataSeq;
77 :
78 0 : Reference<XSheetDataProvider> xSheetProvider(rxDataProvider, UNO_QUERY);
79 0 : if (!xSheetProvider.is())
80 : return xDataSeq;
81 :
82 0 : if (!rDataSeq.maFormula.isEmpty())
83 : {
84 : // parse the formula string, create a token sequence
85 0 : FormulaParser& rParser = getFormulaParser();
86 0 : CellAddress aBaseAddr( getCurrentSheetIndex(), 0, 0 );
87 0 : ApiTokenSequence aTokens = rParser.importFormula( aBaseAddr, rDataSeq.maFormula );
88 :
89 : try
90 : {
91 : // create the data sequence
92 0 : xDataSeq = xSheetProvider->createDataSequenceByFormulaTokens(aTokens);
93 : }
94 0 : catch (Exception&)
95 : {
96 : OSL_FAIL( "ExcelChartConverter::createDataSequence - cannot create data sequence" );
97 0 : }
98 : }
99 0 : else if (!rDataSeq.maData.empty())
100 : {
101 : // create a single-row array from constant source data
102 0 : Matrix< Any > aMatrix( rDataSeq.maData.size(), 1 );
103 0 : Matrix< Any >::iterator aMIt = aMatrix.begin();
104 : // TODO: how to handle missing values in the map?
105 0 : for( DataSequenceModel::AnyMap::const_iterator aDIt = rDataSeq.maData.begin(), aDEnd = rDataSeq.maData.end(); aDIt != aDEnd; ++aDIt, ++aMIt )
106 0 : *aMIt = aDIt->second;
107 0 : OUString aRangeRep = FormulaProcessorBase::generateApiArray( aMatrix );
108 :
109 0 : if (!aRangeRep.isEmpty())
110 : {
111 : try
112 : {
113 : // create the data sequence
114 0 : xDataSeq = rxDataProvider->createDataSequenceByRangeRepresentation( aRangeRep );
115 : }
116 0 : catch (Exception&)
117 : {
118 : OSL_FAIL( "ExcelChartConverter::createDataSequence - cannot create data sequence" );
119 : }
120 0 : }
121 : }
122 0 : return xDataSeq;
123 : }
124 :
125 : // ============================================================================
126 :
127 : } // namespace xls
128 24 : } // namespace oox
129 :
130 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|