1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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::drawingml::chart
{
35 using namespace ::com::sun::star
;
36 using namespace ::com::sun::star::chart2
;
37 using namespace ::com::sun::star::chart2::data
;
38 using namespace ::com::sun::star::drawing
;
39 using namespace ::com::sun::star::uno
;
41 using ::oox::core::XmlFilterBase
;
43 const sal_Unicode API_TOKEN_ARRAY_OPEN
= '{';
44 const sal_Unicode API_TOKEN_ARRAY_CLOSE
= '}';
45 const sal_Unicode API_TOKEN_ARRAY_COLSEP
= ';';
47 static OUString
lclGenerateApiArray(const std::vector
<Any
>& rRow
, sal_Int32 nStart
, sal_Int32 nCount
)
49 OSL_ENSURE( !rRow
.empty(), "ChartConverter::lclGenerateApiArray - missing matrix values" );
50 OUStringBuffer
aBuffer(( OUStringChar(API_TOKEN_ARRAY_OPEN
) ));
51 for (auto aBeg
= rRow
.begin() + nStart
, aIt
= aBeg
, aEnd
= aBeg
+ nCount
; aIt
!= aEnd
; ++aIt
)
56 aBuffer
.append( API_TOKEN_ARRAY_COLSEP
);
58 aBuffer
.append( fValue
);
59 else if( *aIt
>>= aString
)
61 // Code similar to sc/source/filter/oox/formulabase.cxx
62 aBuffer
.append( "\"" + aString
.replaceAll(u
"\"", u
"\"\"") + "\"" );
65 aBuffer
.append( "\"\"" );
67 aBuffer
.append( API_TOKEN_ARRAY_CLOSE
);
68 return aBuffer
.makeStringAndClear();
71 ChartConverter::ChartConverter()
75 ChartConverter::~ChartConverter()
79 void ChartConverter::convertFromModel( XmlFilterBase
& rFilter
,
80 ChartSpaceModel
& rChartModel
, const Reference
< XChartDocument
>& rxChartDoc
,
81 const Reference
< XShapes
>& rxExternalPage
, const awt::Point
& rChartPos
, const awt::Size
& rChartSize
)
83 OSL_ENSURE( rxChartDoc
.is(), "ChartConverter::convertFromModel - missing chart document" );
86 Reference
< data::XDataReceiver
> xDataReceiver( rxChartDoc
, uno::UNO_QUERY_THROW
);
87 Reference
< util::XNumberFormatsSupplier
> xNumberFormatsSupplier( rFilter
.getModel(), uno::UNO_QUERY
);
88 if (xNumberFormatsSupplier
.is())
89 xDataReceiver
->attachNumberFormatsSupplier( xNumberFormatsSupplier
);
91 ConverterRoot
aConvBase( rFilter
, *this, rChartModel
, rxChartDoc
, rChartSize
);
92 ChartSpaceConverter
aSpaceConv( aConvBase
, rChartModel
);
93 aSpaceConv
.convertFromModel( rxExternalPage
, rChartPos
);
97 void ChartConverter::createDataProvider( const Reference
< XChartDocument
>& rxChartDoc
)
101 if( !rxChartDoc
->hasInternalDataProvider() )
102 rxChartDoc
->createInternalDataProvider( false );
109 Reference
< XDataSequence
> ChartConverter::createDataSequence(
110 const Reference
< XDataProvider
>& rxDataProvider
, const DataSequenceModel
& rDataSeq
,
111 const OUString
& rRole
, const OUString
& rRoleQualifier
)
113 Reference
< XDataSequence
> xDataSeq
;
114 if( rxDataProvider
.is() )
117 if( !rDataSeq
.maData
.empty() || (rRole
== "values-y" && rDataSeq
.mnPointCount
> 0) ) try
119 // create a single-row array from constant source data
120 // (multiple levels in the case of complex categories)
121 std::vector
<Any
> aRow(rDataSeq
.mnLevelCount
* rDataSeq
.mnPointCount
);
122 for (auto const& elem
: rDataSeq
.maData
)
123 aRow
.at(elem
.first
) = elem
.second
;
125 for (sal_Int32 i
= rDataSeq
.mnLevelCount
-1; i
>= 0; i
--)
127 aRangeRep
= lclGenerateApiArray( aRow
, i
* rDataSeq
.mnPointCount
, rDataSeq
.mnPointCount
);
129 if (!aRangeRep
.isEmpty())
131 // create or add a new level to the data sequence
132 xDataSeq
= rxDataProvider
->createDataSequenceByValueArray(rRole
, aRangeRep
, rRoleQualifier
);
140 OSL_FAIL( "ChartConverter::createDataSequence - cannot create data sequence" );
147 } // namespace oox::drawingml::chart
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */