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 :
45 : // ============================================================================
46 :
47 21 : ExcelChartConverter::ExcelChartConverter( const WorkbookHelper& rHelper ) :
48 21 : WorkbookHelper( rHelper )
49 : {
50 21 : }
51 :
52 42 : ExcelChartConverter::~ExcelChartConverter()
53 : {
54 42 : }
55 :
56 2 : void ExcelChartConverter::createDataProvider( const Reference< XChartDocument >& rxChartDoc )
57 : {
58 : try
59 : {
60 2 : Reference< XDataReceiver > xDataRec( rxChartDoc, UNO_QUERY_THROW );
61 2 : Reference< XDataProvider > xDataProv( getBaseFilter().getModelFactory()->createInstance(
62 4 : "com.sun.star.chart2.data.DataProvider" ), UNO_QUERY_THROW );
63 4 : xDataRec->attachDataProvider( xDataProv );
64 : }
65 0 : catch( Exception& )
66 : {
67 : }
68 2 : }
69 :
70 6 : Reference< XDataSequence > ExcelChartConverter::createDataSequence(
71 : const Reference< XDataProvider >& rxDataProvider, const DataSequenceModel& rDataSeq )
72 : {
73 6 : Reference< XDataSequence > xDataSeq;
74 6 : if (!rxDataProvider.is())
75 0 : return xDataSeq;
76 :
77 12 : Reference<XSheetDataProvider> xSheetProvider(rxDataProvider, UNO_QUERY);
78 6 : if (!xSheetProvider.is())
79 0 : return xDataSeq;
80 :
81 6 : if (!rDataSeq.maFormula.isEmpty())
82 : {
83 : // parse the formula string, create a token sequence
84 6 : FormulaParser& rParser = getFormulaParser();
85 6 : CellAddress aBaseAddr( getCurrentSheetIndex(), 0, 0 );
86 6 : ApiTokenSequence aTokens = rParser.importFormula( aBaseAddr, rDataSeq.maFormula );
87 :
88 : try
89 : {
90 : // create the data sequence
91 6 : xDataSeq = xSheetProvider->createDataSequenceByFormulaTokens(aTokens);
92 : }
93 0 : catch (Exception&)
94 : {
95 : OSL_FAIL( "ExcelChartConverter::createDataSequence - cannot create data sequence" );
96 6 : }
97 : }
98 0 : else if (!rDataSeq.maData.empty())
99 : {
100 : // create a single-row array from constant source data
101 0 : Matrix< Any > aMatrix( rDataSeq.maData.size(), 1 );
102 0 : Matrix< Any >::iterator aMIt = aMatrix.begin();
103 : // TODO: how to handle missing values in the map?
104 0 : for( DataSequenceModel::AnyMap::const_iterator aDIt = rDataSeq.maData.begin(), aDEnd = rDataSeq.maData.end(); aDIt != aDEnd; ++aDIt, ++aMIt )
105 0 : *aMIt = aDIt->second;
106 0 : OUString aRangeRep = FormulaProcessorBase::generateApiArray( aMatrix );
107 :
108 0 : if (!aRangeRep.isEmpty())
109 : {
110 : try
111 : {
112 : // create the data sequence
113 0 : xDataSeq = rxDataProvider->createDataSequenceByRangeRepresentation( aRangeRep );
114 : }
115 0 : catch (Exception&)
116 : {
117 : OSL_FAIL( "ExcelChartConverter::createDataSequence - cannot create data sequence" );
118 : }
119 0 : }
120 : }
121 6 : return xDataSeq;
122 : }
123 :
124 : // ============================================================================
125 :
126 : } // namespace xls
127 15 : } // namespace oox
128 :
129 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|