Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / oox / source / drawingml / chart / chartconverter.cxx
blob739092d72b028e5ea15f7eca926761893d04e001
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <oox/drawingml/chart/chartconverter.hxx>
22 #include <com/sun/star/chart2/XChartDocument.hpp>
23 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
24 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
25 #include <drawingml/chart/chartspaceconverter.hxx>
26 #include <drawingml/chart/chartspacemodel.hxx>
27 #include <oox/helper/containerhelper.hxx>
28 #include <oox/core/xmlfilterbase.hxx>
29 #include <osl/diagnose.h>
31 using ::oox::drawingml::chart::DataSequenceModel;
32 using ::com::sun::star::uno::Any;
33 namespace oox {
34 namespace drawingml {
35 namespace chart {
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::chart2;
39 using namespace ::com::sun::star::chart2::data;
40 using namespace ::com::sun::star::drawing;
41 using namespace ::com::sun::star::uno;
43 using ::oox::core::XmlFilterBase;
45 static const sal_Unicode API_TOKEN_ARRAY_OPEN = '{';
46 static const sal_Unicode API_TOKEN_ARRAY_CLOSE = '}';
47 static const sal_Unicode API_TOKEN_ARRAY_COLSEP = ';';
49 // Code similar to oox/source/xls/formulabase.cxx
50 static OUString lclGenerateApiString( const OUString& rString )
52 OUString aRetString = rString;
53 sal_Int32 nQuotePos = aRetString.getLength();
54 while( (nQuotePos = aRetString.lastIndexOf( '"', nQuotePos )) >= 0 )
55 aRetString = aRetString.replaceAt( nQuotePos, 1, "\"\"" );
56 return "\"" + aRetString + "\"";
59 static OUString lclGenerateApiArray(const std::vector<Any>& rRow, sal_Int32 nStart, sal_Int32 nCount)
61 OSL_ENSURE( !rRow.empty(), "ChartConverter::lclGenerateApiArray - missing matrix values" );
62 OUStringBuffer aBuffer;
63 aBuffer.append( API_TOKEN_ARRAY_OPEN );
64 for (auto aBeg = rRow.begin() + nStart, aIt = aBeg, aEnd = aBeg + nCount; aIt != aEnd; ++aIt)
66 double fValue = 0.0;
67 OUString aString;
68 if( aIt != aBeg )
69 aBuffer.append( API_TOKEN_ARRAY_COLSEP );
70 if( *aIt >>= fValue )
71 aBuffer.append( fValue );
72 else if( *aIt >>= aString )
73 aBuffer.append( lclGenerateApiString( aString ) );
74 else
75 aBuffer.append( "\"\"" );
77 aBuffer.append( API_TOKEN_ARRAY_CLOSE );
78 return aBuffer.makeStringAndClear();
81 ChartConverter::ChartConverter()
85 ChartConverter::~ChartConverter()
89 void ChartConverter::convertFromModel( XmlFilterBase& rFilter,
90 ChartSpaceModel& rChartModel, const Reference< XChartDocument >& rxChartDoc,
91 const Reference< XShapes >& rxExternalPage, const awt::Point& rChartPos, const awt::Size& rChartSize )
93 OSL_ENSURE( rxChartDoc.is(), "ChartConverter::convertFromModel - missing chart document" );
94 if( rxChartDoc.is() )
96 Reference< data::XDataReceiver > xDataReceiver( rxChartDoc, uno::UNO_QUERY_THROW );
97 Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( rFilter.getModel(), uno::UNO_QUERY );
98 if (xNumberFormatsSupplier.is())
99 xDataReceiver->attachNumberFormatsSupplier( xNumberFormatsSupplier );
101 ConverterRoot aConvBase( rFilter, *this, rChartModel, rxChartDoc, rChartSize );
102 ChartSpaceConverter aSpaceConv( aConvBase, rChartModel );
103 aSpaceConv.convertFromModel( rxExternalPage, rChartPos );
107 void ChartConverter::createDataProvider( const Reference< XChartDocument >& rxChartDoc )
111 if( !rxChartDoc->hasInternalDataProvider() )
112 rxChartDoc->createInternalDataProvider( false );
114 catch( Exception& )
119 Reference< XDataSequence > ChartConverter::createDataSequence(
120 const Reference< XDataProvider >& rxDataProvider, const DataSequenceModel& rDataSeq,
121 const OUString& rRole )
123 Reference< XDataSequence > xDataSeq;
124 if( rxDataProvider.is() )
126 OUString aRangeRep;
127 if( !rDataSeq.maData.empty() || (rRole == "values-y" && rDataSeq.mnPointCount > 0) ) try
129 // create a single-row array from constant source data
130 // (multiple levels in the case of complex categories)
131 std::vector<Any> aRow(rDataSeq.mnLevelCount * rDataSeq.mnPointCount);
132 for (auto const& elem : rDataSeq.maData)
133 aRow.at(elem.first) = elem.second;
135 for (sal_Int32 i = rDataSeq.mnLevelCount-1; i >= 0; i--)
137 aRangeRep = lclGenerateApiArray( aRow, i * rDataSeq.mnPointCount, rDataSeq.mnPointCount);
139 if (!aRangeRep.isEmpty())
141 // create or add a new level to the data sequence
142 xDataSeq = rxDataProvider->createDataSequenceByValueArray(rRole, aRangeRep);
143 if (i == 0)
144 return xDataSeq;
148 catch( Exception& )
150 OSL_FAIL( "ChartConverter::createDataSequence - cannot create data sequence" );
154 return nullptr;
157 } // namespace chart
158 } // namespace drawingml
159 } // namespace oox
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */