LibreOffice Module oox (master) 1
chartconverter.cxx
Go to the documentation of this file.
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
21
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>
29#include <osl/diagnose.h>
30
31using ::oox::drawingml::chart::DataSequenceModel;
32using ::com::sun::star::uno::Any;
33namespace oox::drawingml::chart {
34
35using namespace ::com::sun::star;
36using namespace ::com::sun::star::chart2;
37using namespace ::com::sun::star::chart2::data;
38using namespace ::com::sun::star::drawing;
39using namespace ::com::sun::star::uno;
40
41using ::oox::core::XmlFilterBase;
42
46
47static OUString lclGenerateApiArray(const std::vector<Any>& rRow, sal_Int32 nStart, sal_Int32 nCount)
48{
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)
52 {
53 double fValue = 0.0;
54 OUString aString;
55 if( aIt != aBeg )
57 if( *aIt >>= fValue )
58 aBuffer.append( fValue );
59 else if( *aIt >>= aString )
60 {
61 // Code similar to sc/source/filter/oox/formulabase.cxx
62 aBuffer.append( "\"" + aString.replaceAll(u"\"", u"\"\"") + "\"" );
63 }
64 else
65 aBuffer.append( "\"\"" );
66 }
68 return aBuffer.makeStringAndClear();
69}
70
72{
73}
74
76{
77}
78
80 ChartSpaceModel& rChartModel, const Reference< XChartDocument >& rxChartDoc,
81 const Reference< XShapes >& rxExternalPage, const awt::Point& rChartPos, const awt::Size& rChartSize )
82{
83 OSL_ENSURE( rxChartDoc.is(), "ChartConverter::convertFromModel - missing chart document" );
84 if( rxChartDoc.is() )
85 {
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 );
90
91 ConverterRoot aConvBase( rFilter, *this, rChartModel, rxChartDoc, rChartSize );
92 ChartSpaceConverter aSpaceConv( aConvBase, rChartModel );
93 aSpaceConv.convertFromModel( rxExternalPage, rChartPos );
94 }
95}
96
98{
99 try
100 {
101 if( !rxChartDoc->hasInternalDataProvider() )
102 rxChartDoc->createInternalDataProvider( false );
103 }
104 catch( Exception& )
105 {
106 }
107}
108
110 const Reference< XDataProvider >& rxDataProvider, const DataSequenceModel& rDataSeq,
111 const OUString& rRole, const OUString& rRoleQualifier )
112{
114 if( rxDataProvider.is() )
115 {
116 OUString aRangeRep;
117 if( !rDataSeq.maData.empty() || (rRole == "values-y" && rDataSeq.mnPointCount > 0) ) try
118 {
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;
124
125 for (sal_Int32 i = rDataSeq.mnLevelCount-1; i >= 0; i--)
126 {
127 aRangeRep = lclGenerateApiArray( aRow, i * rDataSeq.mnPointCount, rDataSeq.mnPointCount);
128
129 if (!aRangeRep.isEmpty())
130 {
131 // create or add a new level to the data sequence
132 xDataSeq = rxDataProvider->createDataSequenceByValueArray(rRole, aRangeRep, rRoleQualifier);
133 if (i == 0)
134 return xDataSeq;
135 }
136 }
137 }
138 catch( Exception& )
139 {
140 OSL_FAIL( "ChartConverter::createDataSequence - cannot create data sequence" );
141 }
142 }
143
144 return nullptr;
145}
146
147} // namespace oox::drawingml::chart
148
149/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const css::uno::Reference< css::frame::XModel > & getModel() const
Returns the document model (always existing).
Definition: filterbase.cxx:220
void convertFromModel(::oox::core::XmlFilterBase &rFilter, ChartSpaceModel &rChartModel, const css::uno::Reference< css::chart2::XChartDocument > &rxChartDoc, const css::uno::Reference< css::drawing::XShapes > &rxExternalPage, const css::awt::Point &rChartPos, const css::awt::Size &rChartSize)
Converts the passed OOXML chart model to the passed chart2 document.
virtual void createDataProvider(const css::uno::Reference< css::chart2::XChartDocument > &rxChartDoc)
Creates an internal data provider.
virtual css::uno::Reference< css::chart2::data::XDataSequence > createDataSequence(const css::uno::Reference< css::chart2::data::XDataProvider > &rxDataProvider, const DataSequenceModel &rDataSeq, const OUString &rRole, const OUString &aRoleQualifier)
Creates a data sequence from a formula.
void convertFromModel(const css::uno::Reference< css::drawing::XShapes > &rxExternalPage, const css::awt::Point &rChartPos)
Converts the contained OOXML chart model to a chart2 document.
int nCount
float u
@ Exception
const sal_Unicode API_TOKEN_ARRAY_OPEN
const sal_Unicode API_TOKEN_ARRAY_CLOSE
const sal_Unicode API_TOKEN_ARRAY_COLSEP
static OUString lclGenerateApiArray(const std::vector< Any > &rRow, sal_Int32 nStart, sal_Int32 nCount)
sal_Int32 mnPointCount
Number format for double values.
sal_Int32 mnLevelCount
Number of points in this series source.
sal_uInt16 sal_Unicode
std::unique_ptr< char[]> aBuffer