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 
31 using ::oox::drawingml::chart::DataSequenceModel;
32 using ::com::sun::star::uno::Any;
33 namespace oox::drawingml::chart {
34 
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;
40 
41 using ::oox::core::XmlFilterBase;
42 
46 
47 // Code similar to oox/source/xls/formulabase.cxx
48 static OUString lclGenerateApiString( const OUString& rString )
49 {
50  OUString aRetString = rString;
51  sal_Int32 nQuotePos = aRetString.getLength();
52  while( (nQuotePos = aRetString.lastIndexOf( '"', nQuotePos )) >= 0 )
53  aRetString = aRetString.replaceAt( nQuotePos, 1, "\"\"" );
54  return "\"" + aRetString + "\"";
55 }
56 
57 static OUString lclGenerateApiArray(const std::vector<Any>& rRow, sal_Int32 nStart, sal_Int32 nCount)
58 {
59  OSL_ENSURE( !rRow.empty(), "ChartConverter::lclGenerateApiArray - missing matrix values" );
60  OUStringBuffer aBuffer;
61  aBuffer.append( API_TOKEN_ARRAY_OPEN );
62  for (auto aBeg = rRow.begin() + nStart, aIt = aBeg, aEnd = aBeg + nCount; aIt != aEnd; ++aIt)
63  {
64  double fValue = 0.0;
65  OUString aString;
66  if( aIt != aBeg )
67  aBuffer.append( API_TOKEN_ARRAY_COLSEP );
68  if( *aIt >>= fValue )
69  aBuffer.append( fValue );
70  else if( *aIt >>= aString )
71  aBuffer.append( lclGenerateApiString( aString ) );
72  else
73  aBuffer.append( "\"\"" );
74  }
75  aBuffer.append( API_TOKEN_ARRAY_CLOSE );
76  return aBuffer.makeStringAndClear();
77 }
78 
80 {
81 }
82 
84 {
85 }
86 
88  ChartSpaceModel& rChartModel, const Reference< XChartDocument >& rxChartDoc,
89  const Reference< XShapes >& rxExternalPage, const awt::Point& rChartPos, const awt::Size& rChartSize )
90 {
91  OSL_ENSURE( rxChartDoc.is(), "ChartConverter::convertFromModel - missing chart document" );
92  if( rxChartDoc.is() )
93  {
94  Reference< data::XDataReceiver > xDataReceiver( rxChartDoc, uno::UNO_QUERY_THROW );
95  Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( rFilter.getModel(), uno::UNO_QUERY );
96  if (xNumberFormatsSupplier.is())
97  xDataReceiver->attachNumberFormatsSupplier( xNumberFormatsSupplier );
98 
99  ConverterRoot aConvBase( rFilter, *this, rChartModel, rxChartDoc, rChartSize );
100  ChartSpaceConverter aSpaceConv( aConvBase, rChartModel );
101  aSpaceConv.convertFromModel( rxExternalPage, rChartPos );
102  }
103 }
104 
106 {
107  try
108  {
109  if( !rxChartDoc->hasInternalDataProvider() )
110  rxChartDoc->createInternalDataProvider( false );
111  }
112  catch( Exception& )
113  {
114  }
115 }
116 
118  const Reference< XDataProvider >& rxDataProvider, const DataSequenceModel& rDataSeq,
119  const OUString& rRole, const OUString& rRoleQualifier )
120 {
122  if( rxDataProvider.is() )
123  {
124  OUString aRangeRep;
125  if( !rDataSeq.maData.empty() || (rRole == "values-y" && rDataSeq.mnPointCount > 0) ) try
126  {
127  // create a single-row array from constant source data
128  // (multiple levels in the case of complex categories)
129  std::vector<Any> aRow(rDataSeq.mnLevelCount * rDataSeq.mnPointCount);
130  for (auto const& elem : rDataSeq.maData)
131  aRow.at(elem.first) = elem.second;
132 
133  for (sal_Int32 i = rDataSeq.mnLevelCount-1; i >= 0; i--)
134  {
135  aRangeRep = lclGenerateApiArray( aRow, i * rDataSeq.mnPointCount, rDataSeq.mnPointCount);
136 
137  if (!aRangeRep.isEmpty())
138  {
139  // create or add a new level to the data sequence
140  xDataSeq = rxDataProvider->createDataSequenceByValueArray(rRole, aRangeRep, rRoleQualifier);
141  if (i == 0)
142  return xDataSeq;
143  }
144  }
145  }
146  catch( Exception& )
147  {
148  OSL_FAIL( "ChartConverter::createDataSequence - cannot create data sequence" );
149  }
150  }
151 
152  return nullptr;
153 }
154 
155 } // namespace oox::drawingml::chart
156 
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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.
const sal_Unicode API_TOKEN_ARRAY_COLSEP
sal_uInt16 sal_Unicode
int nCount
sal_Int32 mnPointCount
Number format for double values.
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(::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.
const css::uno::Reference< css::frame::XModel > & getModel() const
Returns the document model (always existing).
Definition: filterbase.cxx:217
std::unique_ptr< char[]> aBuffer
sal_Int32 mnLevelCount
Number of points in this series source.
static OUString lclGenerateApiString(const OUString &rString)
static OUString lclGenerateApiArray(const std::vector< Any > &rRow, sal_Int32 nStart, sal_Int32 nCount)
virtual void createDataProvider(const css::uno::Reference< css::chart2::XChartDocument > &rxChartDoc)
Creates an internal data provider.
const sal_Unicode API_TOKEN_ARRAY_OPEN
const sal_Unicode API_TOKEN_ARRAY_CLOSE