Update ooo320-m1
[ooovba.git] / oox / source / xls / excelchartconverter.cxx
blob52e327699f0d340cef8240c2a1f860b17249a90d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: excelchartconverter.cxx,v $
11 * $Revision: 1.3 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 #include "oox/xls/excelchartconverter.hxx"
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/chart2/data/XDataProvider.hpp>
35 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
36 #include "oox/drawingml/chart/datasourcemodel.hxx"
37 #include "oox/xls/formulaparser.hxx"
39 using ::rtl::OUString;
40 using ::com::sun::star::uno::Any;
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::uno::Exception;
43 using ::com::sun::star::uno::UNO_QUERY_THROW;
44 using ::com::sun::star::lang::XMultiServiceFactory;
45 using ::com::sun::star::table::CellAddress;
46 using ::com::sun::star::chart2::XChartDocument;
47 using ::com::sun::star::chart2::data::XDataProvider;
48 using ::com::sun::star::chart2::data::XDataReceiver;
49 using ::com::sun::star::chart2::data::XDataSequence;
50 using ::oox::drawingml::chart::DataSequenceModel;
52 namespace oox {
53 namespace xls {
55 // ============================================================================
57 ExcelChartConverter::ExcelChartConverter( const WorkbookHelper& rHelper ) :
58 WorkbookHelper( rHelper )
62 ExcelChartConverter::~ExcelChartConverter()
66 void ExcelChartConverter::createDataProvider( const Reference< XChartDocument >& rxChartDoc )
68 try
70 Reference< XDataReceiver > xDataRec( rxChartDoc, UNO_QUERY_THROW );
71 Reference< XMultiServiceFactory > xFactory( getDocument(), UNO_QUERY_THROW );
72 Reference< XDataProvider > xDataProv( xFactory->createInstance(
73 CREATE_OUSTRING( "com.sun.star.chart2.data.DataProvider" ) ), UNO_QUERY_THROW );
74 xDataRec->attachDataProvider( xDataProv );
76 catch( Exception& )
81 Reference< XDataSequence > ExcelChartConverter::createDataSequence(
82 const Reference< XDataProvider >& rxDataProvider, const DataSequenceModel& rDataSeq )
84 Reference< XDataSequence > xDataSeq;
85 if( rxDataProvider.is() )
87 OUString aRangeRep;
88 if( rDataSeq.maFormula.getLength() > 0 )
90 // parse the formula string, create a token sequence
91 FormulaParser& rParser = getFormulaParser();
92 TokensFormulaContext aContext( true, true );
93 aContext.setBaseAddress( CellAddress( getCurrentSheetIndex(), 0, 0 ) );
94 rParser.importFormula( aContext, rDataSeq.maFormula );
96 // create a range list from the token sequence
97 ApiCellRangeList aRanges;
98 rParser.extractCellRangeList( aRanges, aContext.getTokens(), false );
99 aRangeRep = rParser.generateApiRangeListString( aRanges );
101 else if( !rDataSeq.maData.empty() )
103 // create a single-row array from constant source data
104 Matrix< Any > aMatrix( rDataSeq.maData.size(), 1 );
105 Matrix< Any >::iterator aMIt = aMatrix.begin();
106 // TODO: how to handle missing values in the map?
107 for( DataSequenceModel::AnyMap::const_iterator aDIt = rDataSeq.maData.begin(), aDEnd = rDataSeq.maData.end(); aDIt != aDEnd; ++aDIt, ++aMIt )
108 *aMIt = aDIt->second;
109 aRangeRep = FormulaProcessorBase::generateApiArray( aMatrix );
112 if( aRangeRep.getLength() > 0 ) try
114 // create the data sequence
115 xDataSeq = rxDataProvider->createDataSequenceByRangeRepresentation( aRangeRep );
117 catch( Exception& )
119 OSL_ENSURE( false, "ExcelChartConverter::createDataSequence - cannot create data sequence" );
122 return xDataSeq;
125 // ============================================================================
127 } // namespace xls
128 } // namespace oox