/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <memory>
#include <sal/config.h>
#include <sal/log.hxx>
#include <sax/tools/converter.hxx>
#include <utility>
#include <xmloff/xmlprmap.hxx>
#include <SchXMLExport.hxx>
#include <XMLChartPropertySetMapper.hxx>
#include "ColorPropertySet.hxx"
#include "SchXMLTools.hxx"
#include "SchXMLEnumConverter.hxx"
#include <comphelper/processfactory.hxx>
#include <tools/globname.hxx>
#include <comphelper/classids.hxx>
#include <comphelper/sequence.hxx>
#include <xmloff/namespacemap.hxx>
#include <xmloff/xmlnamespace.hxx>
#include <xmloff/xmltoken.hxx>
#include <xmloff/families.hxx>
#include <xmloff/xmlaustp.hxx>
#include <xmloff/xmluconv.hxx>
#include <xmloff/SchXMLSeriesHelper.hxx>
#include <rtl/math.hxx>
#include <o3tl/sorted_vector.hxx>
#include <o3tl/string_view.hxx>
#include <limits>
#include <vector>
#include <algorithm>
#include <queue>
#include <iterator>
#include <numeric>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/lang/XServiceName.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/util/XRefreshable.hpp>
#include <com/sun/star/chart/XAxis.hpp>
#include <com/sun/star/chart/XAxisSupplier.hpp>
#include <com/sun/star/chart/XChartDocument.hpp>
#include <com/sun/star/chart/ChartLegendExpansion.hpp>
#include <com/sun/star/chart/ChartDataRowSource.hpp>
#include <com/sun/star/chart/ChartAxisAssign.hpp>
#include <com/sun/star/chart/ErrorBarStyle.hpp>
#include <com/sun/star/chart/DataLabelPlacement.hpp>
#include <com/sun/star/chart/TimeIncrement.hpp>
#include <com/sun/star/chart/TimeInterval.hpp>
#include <com/sun/star/chart/TimeUnit.hpp>
#include <com/sun/star/chart/X3DDisplay.hpp>
#include <com/sun/star/chart/XStatisticDisplay.hpp>
#include <com/sun/star/chart/XDiagramPositioning.hpp>
#include <com/sun/star/chart2/XAnyDescriptionAccess.hpp>
#include <com/sun/star/chart2/AxisType.hpp>
#include <com/sun/star/chart2/XChartDocument.hpp>
#include <com/sun/star/chart2/XDiagram.hpp>
#include <com/sun/star/chart2/RelativePosition.hpp>
#include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
#include <com/sun/star/chart2/XRegressionCurveContainer.hpp>
#include <com/sun/star/chart2/XChartTypeContainer.hpp>
#include <com/sun/star/chart2/XDataSeriesContainer.hpp>
#include <com/sun/star/chart2/XDataPointCustomLabelField.hpp>
#include <com/sun/star/chart2/data/XDataSource.hpp>
#include <com/sun/star/chart2/data/XDataSink.hpp>
#include <com/sun/star/chart2/data/XDataProvider.hpp>
#include <com/sun/star/chart2/data/XDatabaseDataProvider.hpp>
#include <com/sun/star/chart2/data/XPivotTableDataProvider.hpp>
#include <com/sun/star/chart2/data/XRangeXMLConversion.hpp>
#include <com/sun/star/chart2/data/XTextualDataSequence.hpp>
#include <com/sun/star/chart2/data/XNumericalDataSequence.hpp>
#include <com/sun/star/util/MeasureUnit.hpp>
#include <com/sun/star/drawing/XDrawPageSupplier.hpp>
#include <com/sun/star/drawing/XShapes.hpp>
#include <com/sun/star/embed/Aspects.hpp>
#include <com/sun/star/embed/XVisualObject.hpp>
#include <com/sun/star/container/XChild.hpp>
#include <comphelper/diagnose_ex.hxx>
#include "MultiPropertySetHandler.hxx"
#include "PropertyMap.hxx"
using namespace com::sun::star;
using namespace ::xmloff::token;
using ::com::sun::star::uno::Sequence;
using ::com::sun::star::uno::Reference;
using ::com::sun::star::uno::Any;
using ::std::vector;
namespace
{
/**
* Used to store a data point custom-label's fields and also the helper members that
* indicates whether this label's contents are sourced from a cell[range] and
* the address of the cell[range] with guid of the CELLRANGE field.
*/
struct CustomLabelData
{
CustomLabelData():
mbDataLabelsRange(
false )
{
}
/// Label fields.
Sequence<Reference<chart2::XDataPointCustomLabelField>> maFields;
/// Are label's contents sourced from a cell[range] ?
bool mbDataLabelsRange;
/// cell[range] from which label's contents are sourced.
OUString maRange;
/// GUID of the CELLRANGE field.
OUString maGuid;
};
struct SchXMLDataPointStruct
{
OUString maStyleName;
sal_Int32 mnRepeat;
chart2::RelativePosition mCustomLabelPos;
// loext:custom-label-pos-x and -y
// There is no internal equivalent for <chart:data-label>. It will be generated on the fly
// on export. All about data label is hold in the data point.
CustomLabelData mCustomLabel;
// <text:p> child element in <chart:data-label>
OUString msDataLabelStyleName;
// chart:style-name attribute in <chart:data-label>
SchXMLDataPointStruct() : mnRepeat( 1 ) {}
};
}
class SchXMLExportHelper_Impl
{
public :
// first: data sequence for label, second: data sequence for values.
typedef ::std::pair< css::uno::Reference< css::chart2::data::XDataSequence >,
css::uno::Reference< css::chart2::data::XDataSequence > > tLabelValuesDataPair;
typedef ::std::vector< tLabelValuesDataPair > tDataSequenceCont;
public :
SchXMLExportHelper_Impl( SvXMLExport& rExport,
SvXMLAutoStylePoolP& rASPool );
SchXMLExportHelper_Impl(
const SchXMLExportHelper_Impl&) =
delete ;
SchXMLExportHelper_Impl&
operator =(
const SchXMLExportHelper_Impl&) =
delete ;
// auto-styles
/// parse chart and collect all auto-styles used in current pool
void collectAutoStyles( css::uno::Reference< css::chart::XChartDocument >
const & rCh
artDoc );
/// write the styles collected into the current pool as <style:style> elements
void exportAutoStyles();
/** export the <chart:chart> element corresponding to rChartDoc
if bIncludeTable is true, the chart data is exported as <table:table>
element (inside the chart element).
Otherwise the external references stored in the chart document are used
for writing the corresponding attributes at series
All attributes contained in xAttrList are written at the chart element,
which is the outer element of a chart. So these attributes can easily
be parsed again by the container
*/
void exportChart( css::uno::Reference< css::chart::XChartDocument > const & rChartDoc,
bool bIncludeTable );
const rtl::Reference<XMLPropertySetMapper>& GetPropertySetMapper() const ;
void SetChartRangeAddress( const OUString& rAddress )
{ msChartAddress = rAddress; }
void InitRangeSegmentationProperties(
const css::uno::Reference< css::chart2::XChartDocument > & xChartDoc );
static css::awt::Size getPageSize(
const css::uno::Reference< css::chart2::XChartDocument > & xChartDoc );
/** first parseDocument: collect autostyles and store names in this queue
second parseDocument: export content and use names from this queue
*/
::std::queue< OUString > maAutoStyleNameQueue;
void CollectAutoStyle(
std::vector< XMLPropertyState >&& aStates );
void CollectAutoTextStyle(
const css::uno::Reference< css::beans::XPropertySet >& xTitlePropSet );
void AddAutoStyleAttribute(
const std::vector< XMLPropertyState >& aStates );
/// if bExportContent is false the auto-styles are collected
void parseDocument( css::uno::Reference< css::chart::XChartDocument > const & rChartDoc,
bool bExportContent,
bool bIncludeTable = false );
void exportTable();
void exportPlotArea(
const css::uno::Reference< css::chart::XDiagram >& xDiagram,
const css::uno::Reference< css::chart2::XDiagram >& xNewDiagram,
const css::awt::Size & rPageSize,
bool bExportContent,
bool bIncludeTable );
void exportCoordinateRegion( const css::uno::Reference< css::chart::XDiagram >& xDiagram );
void exportAxes( const css::uno::Reference< css::chart::XDiagram > & xDiagram,
const css::uno::Reference< css::chart2::XDiagram > & xNewDiagram,
bool bExportContent );
void exportAxis( enum XMLTokenEnum eDimension, enum XMLTokenEnum eAxisName,
const Reference< beans::XPropertySet >& rAxisProps, const Reference< chart2::XAxis >& rChart2Axis,
const OUString& rCategoriesRanges,
bool bHasTitle, bool bHasMajorGrid, bool bHasMinorGrid, bool bExportContent, std::u16string_view sChartType );
void exportGrid( const Reference< beans::XPropertySet >& rGridProperties, bool bMajor, bool bExportContent );
void exportDateScale( const Reference< beans::XPropertySet >& rAxisProps );
void exportAxisTitle( const Reference< beans::XPropertySet >& rTitleProps, bool bExportContent );
void exportSeries(
const css::uno::Reference< css::chart2::XDiagram > & xNewDiagram,
const css::awt::Size & rPageSize,
bool bExportContent,
bool bHasTwoYAxes );
void exportPropertyMapping(
const css::uno::Reference< css::chart2::data::XDataSource > & xSource,
const Sequence< OUString >& rSupportedMappings );
void exportCandleStickSeries(
const css::uno::Sequence<
css::uno::Reference< css::chart2::XDataSeries > > & aSeriesSeq,
const css::uno::Reference< css::chart2::XDiagram > & xDiagram,
bool bJapaneseCandleSticks,
bool bExportContent );
void exportDataPoints(
const css::uno::Reference< css::beans::XPropertySet > & xSeriesProperties,
sal_Int32 nSeriesLength,
const css::uno::Reference< css::chart2::XDiagram > & xDiagram,
bool bExportContent );
void exportCustomLabel(const SchXMLDataPointStruct& rPoint);
void exportCustomLabelPosition(const chart2::RelativePosition& xCustomLabelPosition);
void exportRegressionCurve(
const css::uno::Reference<css::chart2::XDataSeries>& xSeries,
const css::awt::Size& rPageSize,
bool bExportContent );
void exportErrorBar (
const css::uno::Reference<beans::XPropertySet> &xSeriesProp, bool bYError,
bool bExportContent );
/// add svg position as attribute for current element
void addPosition( const css::awt::Point & rPosition );
void addPosition( const css::uno::Reference< css::drawing::XShape >& xShape );
/// add svg size as attribute for current element
void addSize( const css::awt::Size & rSize, bool bIsOOoNamespace = false );
void addSize( const css::uno::Reference< css::drawing::XShape >& xShape );
/// exports a string as a paragraph element
void exportText( const OUString& rText );
void exportFormattedText( const css::uno::Reference< beans::XPropertySet >& xTitleProps );
public :
SvXMLExport& mrExport;
SvXMLAutoStylePoolP& mrAutoStylePool;
rtl::Reference< XMLPropertySetMapper > mxPropertySetMapper;
rtl::Reference< XMLChartExportPropertyMapper > mxExpPropMapper;
static constexpr OUString gsTableName = u"local-table" _ustr;
OUStringBuffer msStringBuffer;
OUString msString;
// members filled by InitRangeSegmentationProperties (retrieved from DataProvider)
bool mbHasCategoryLabels; //if the categories are only automatically generated this will be false
bool mbRowSourceColumns;
OUString msChartAddress;
css::uno::Sequence< sal_Int32 > maSequenceMapping;
OUString msCLSID;
OUString maSrcShellID;
OUString maDestShellID;
css::uno::Reference< css::drawing::XShapes > mxAdditionalShapes;
tDataSequenceCont m_aDataSequencesToExport;
OUString maCategoriesRange;
};
namespace
{
CustomLabelData lcl_getCustomLabelField(SvXMLExport const & rExport,
sal_Int32 nDataPointIndex,
const uno::Reference< chart2::XDataSeries >& rSeries)
{
if (!rSeries.is())
return CustomLabelData();
// Custom data label text will be written to the <text:p> child element of a
// <chart:data-label> element. That exists only since ODF 1.2.
const SvtSaveOptions::ODFSaneDefaultVersion nCurrentODFVersion(
rExport.getSaneDefaultVersion());
if (nCurrentODFVersion < SvtSaveOptions::ODFSVER_012)
return CustomLabelData();
if (Reference<beans::XPropertySet> xLabels = rSeries->getDataPointByIndex(nDataPointIndex); xLabels.is())
{
if (Any aAny = xLabels->getPropertyValue(u"CustomLabelFields" _ustr); aAny.hasValue())
{
CustomLabelData aData;
Sequence<uno::Reference<chart2::XDataPointCustomLabelField>> aCustomLabels;
aAny >>= aCustomLabels;
for (const auto & rField : aCustomLabels)
{
if (rField->getFieldType() == chart2::DataPointCustomLabelFieldType_CELLRANGE)
{
if (rField->getDataLabelsRange())
aData.mbDataLabelsRange = true ;
aData.maRange = rField->getCellRange();
aData.maGuid = rField->getGuid();
}
}
aData.maFields = std::move(aCustomLabels);
return aData;
}
}
return CustomLabelData();
}
css::chart2::RelativePosition lcl_getCustomLabelPosition(
SvXMLExport const & rExport,
sal_Int32 const nDataPointIndex,
const uno::Reference< chart2::XDataSeries >& rSeries)
{
if (!rSeries.is())
return chart2::RelativePosition();
const SvtSaveOptions::ODFSaneDefaultVersion nCurrentODFVersion(
rExport.getSaneDefaultVersion());
if ((nCurrentODFVersion & SvtSaveOptions::ODFSVER_EXTENDED) == 0) // do not export to ODF 1.3 or older
return chart2::RelativePosition();
if (Reference<beans::XPropertySet> xLabels = rSeries->getDataPointByIndex(nDataPointIndex); xLabels.is())
{
if (Any aAny = xLabels->getPropertyValue(u"CustomLabelPosition" _ustr); aAny.hasValue())
{
chart2::RelativePosition aCustomLabelPos;
aAny >>= aCustomLabelPos;
return aCustomLabelPos;
}
}
return chart2::RelativePosition();
}
class lcl_MatchesRole
{
public :
explicit lcl_MatchesRole( OUString aRole ) :
m_aRole(std::move( aRole ))
{}
bool operator () ( const Reference< chart2::data::XLabeledDataSequence > & xSeq ) const
{
if ( !xSeq.is() )
return false ;
Reference< beans::XPropertySet > xProp( xSeq->getValues(), uno::UNO_QUERY );
OUString aRole;
return ( xProp.is() &&
(xProp->getPropertyValue( u"Role" _ustr ) >>= aRole ) &&
m_aRole == aRole );
}
private :
OUString m_aRole;
};
Reference< chart2::data::XLabeledDataSequence > lcl_getCategories( const Reference< chart2::XDiagram > & xDiagram )
{
Reference< chart2::data::XLabeledDataSequence > xResult;
try
{
Reference< chart2::XCoordinateSystemContainer > xCooSysCnt(
xDiagram, uno::UNO_QUERY_THROW );
const Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq(
xCooSysCnt->getCoordinateSystems());
for ( const auto & rCooSys : aCooSysSeq )
{
Reference< chart2::XCoordinateSystem > xCooSys( rCooSys );
SAL_WARN_IF( !xCooSys.is(), "xmloff.chart" , "xCooSys is NULL" );
for ( sal_Int32 nN = xCooSys->getDimension(); nN--; )
{
const sal_Int32 nMaxAxisIndex = xCooSys->getMaximumAxisIndexByDimension(nN);
for (sal_Int32 nI=0; nI<=nMaxAxisIndex; ++nI)
{
Reference< chart2::XAxis > xAxis = xCooSys->getAxisByDimension( nN, nI );
SAL_WARN_IF( !xAxis.is(), "xmloff.chart" , "xAxis is NULL" );
if ( xAxis.is())
{
chart2::ScaleData aScaleData = xAxis->getScaleData();
if ( aScaleData.Categories.is())
{
xResult.set( aScaleData.Categories );
break ;
}
}
}
}
}
}
catch ( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("xmloff.chart" );
}
return xResult;
}
Reference< chart2::data::XDataSource > lcl_createDataSource(
const Sequence< Reference< chart2::data::XLabeledDataSequence > > & aData )
{
const Reference< uno::XComponentContext >& xContext(
comphelper::getProcessComponentContext() );
Reference< chart2::data::XDataSink > xSink(
xContext->getServiceManager()->createInstanceWithContext(
u"com.sun.star.chart2.data.DataSource" _ustr, xContext ),
uno::UNO_QUERY_THROW );
xSink->setData( aData );
return Reference< chart2::data::XDataSource >( xSink, uno::UNO_QUERY );
}
Sequence< Reference< chart2::data::XLabeledDataSequence > > lcl_getAllSeriesSequences( const Reference< chart2::XChartDocument >& xChartDoc )
{
::std::vector< Reference< chart2::data::XLabeledDataSequence > > aContainer;
if ( xChartDoc.is() )
{
Reference< chart2::XDiagram > xDiagram( xChartDoc->getFirstDiagram());
::std::vector< Reference< chart2::XDataSeries > > aSeriesVector( SchXMLSeriesHelper::getDataSeriesFromDiagram( xDiagram ));
for ( const auto & rSeries : aSeriesVector )
{
Reference< chart2::data::XDataSource > xDataSource( rSeries, uno::UNO_QUERY );
if ( !xDataSource.is() )
continue ;
const uno::Sequence< Reference< chart2::data::XLabeledDataSequence > > aDataSequences( xDataSource->getDataSequences() );
aContainer.insert( aContainer.end(), aDataSequences.begin(), aDataSequences.end() );
}
}
return comphelper::containerToSequence( aContainer );
}
Reference< chart2::data::XLabeledDataSequence >
lcl_getDataSequenceByRole(
const Sequence< Reference< chart2::data::XLabeledDataSequence > > & aLabeledSeq,
const OUString & rRole )
{
Reference< chart2::data::XLabeledDataSequence > aNoResult;
const Reference< chart2::data::XLabeledDataSequence > * pBegin = aLabeledSeq.getConstArray();
const Reference< chart2::data::XLabeledDataSequence > * pEnd = pBegin + aLabeledSeq.getLength();
const Reference< chart2::data::XLabeledDataSequence > * pMatch =
::std::find_if( pBegin, pEnd, lcl_MatchesRole( rRole ));
if ( pMatch != pEnd )
return *pMatch;
return aNoResult;
}
Reference< chart2::data::XDataSource > lcl_pressUsedDataIntoRectangularFormat( const Reference< chart2::XChartDocument >& xChartDoc, bool & rOutSourceHasCategoryLabels )
{
::std::vector< Reference< chart2::data::XLabeledDataSequence > > aLabeledSeqVector;
//categories are always the first sequence
Reference< chart2::XDiagram > xDiagram( xChartDoc->getFirstDiagram());
Reference< chart2::data::XLabeledDataSequence > xCategories( lcl_getCategories( xDiagram ) );
if ( xCategories.is() )
aLabeledSeqVector.push_back( xCategories );
rOutSourceHasCategoryLabels = xCategories.is();
const Sequence< Reference< chart2::data::XLabeledDataSequence > > aSeriesSeqVector(
lcl_getAllSeriesSequences( xChartDoc ) );
//the first x-values is always the next sequence //todo ... other x-values get lost for old format
Reference< chart2::data::XLabeledDataSequence > xXValues(
lcl_getDataSequenceByRole( aSeriesSeqVector, u"values-x" _ustr ) );
if ( xXValues.is() )
aLabeledSeqVector.push_back( xXValues );
//add all other sequences now without x-values
lcl_MatchesRole aHasXValues( u"values-x" _ustr );
std::copy_if(aSeriesSeqVector.begin(), aSeriesSeqVector.end(), std::back_inserter(aLabeledSeqVector),
[&aHasXValues](const auto & rSeriesSeq) { return !aHasXValues( rSeriesSeq ); });
Sequence< Reference< chart2::data::XLabeledDataSequence > > aSeq( comphelper::containerToSequence(aLabeledSeqVector) );
return lcl_createDataSource( aSeq );
}
bool lcl_isSeriesAttachedToFirstAxis(
const Reference< chart2::XDataSeries > & xDataSeries )
{
bool bResult=true ;
try
{
sal_Int32 nAxisIndex = 0;
Reference< beans::XPropertySet > xProp( xDataSeries, uno::UNO_QUERY_THROW );
xProp->getPropertyValue(u"AttachedAxisIndex" _ustr) >>= nAxisIndex;
bResult = (0==nAxisIndex);
}
catch ( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("xmloff.chart" );
}
return bResult;
}
OUString lcl_ConvertRange( const OUString & rRange, const Reference< chart2::XChartDocument > & xDoc )
{
OUString aResult = rRange;
if ( !xDoc.is() )
return aResult;
Reference< chart2::data::XRangeXMLConversion > xConversion(
xDoc->getDataProvider(), uno::UNO_QUERY );
if ( xConversion.is())
aResult = xConversion->convertRangeToXML( rRange );
return aResult;
}
typedef ::std::pair< OUString, OUString > tLabelAndValueRange;
tLabelAndValueRange lcl_getLabelAndValueRangeByRole(
const Sequence< Reference< chart2::data::XLabeledDataSequence > > & aSeqCnt,
const OUString & rRole,
const Reference< chart2::XChartDocument > & xDoc,
SchXMLExportHelper_Impl::tDataSequenceCont & rOutSequencesToExport )
{
tLabelAndValueRange aResult;
Reference< chart2::data::XLabeledDataSequence > xLabeledSeq(
lcl_getDataSequenceByRole( aSeqCnt, rRole ));
if ( xLabeledSeq.is())
{
Reference< chart2::data::XDataSequence > xLabelSeq( xLabeledSeq->getLabel());
if ( xLabelSeq.is())
aResult.first = lcl_ConvertRange( xLabelSeq->getSourceRangeRepresentation(), xDoc );
Reference< chart2::data::XDataSequence > xValueSeq( xLabeledSeq->getValues());
if ( xValueSeq.is())
aResult.second = lcl_ConvertRange( xValueSeq->getSourceRangeRepresentation(), xDoc );
if ( xLabelSeq.is() || xValueSeq.is())
rOutSequencesToExport.emplace_back( xLabelSeq, xValueSeq );
}
return aResult;
}
sal_Int32 lcl_getSequenceLengthByRole(
const Sequence< Reference< chart2::data::XLabeledDataSequence > > & aSeqCnt,
const OUString & rRole )
{
Reference< chart2::data::XLabeledDataSequence > xLabeledSeq(
lcl_getDataSequenceByRole( aSeqCnt, rRole ));
if ( xLabeledSeq.is())
{
Reference< chart2::data::XDataSequence > xSeq( xLabeledSeq->getValues());
return xSeq->getData().getLength();
}
return 0;
}
OUString lcl_flattenStringSequence( const Sequence< OUString > & rSequence )
{
OUStringBuffer aResult;
bool bPrecedeWithSpace = false ;
for ( const auto & rString : rSequence )
{
if ( !rString.isEmpty())
{
if ( bPrecedeWithSpace )
aResult.append( ' ' );
aResult.append( rString );
bPrecedeWithSpace = true ;
}
}
return aResult.makeStringAndClear();
}
void lcl_getLabelStringSequence( Sequence< OUString >& rOutLabels, const Reference< chart2::data::XDataSequence > & xLabelSeq )
{
uno::Reference< chart2::data::XTextualDataSequence > xTextualDataSequence( xLabelSeq, uno::UNO_QUERY );
if ( xTextualDataSequence.is())
{
rOutLabels = xTextualDataSequence->getTextualData();
}
else if ( xLabelSeq.is())
{
Sequence< uno::Any > aAnies( xLabelSeq->getData());
rOutLabels.realloc( aAnies.getLength());
auto pOutLabels = rOutLabels.getArray();
for ( sal_Int32 i=0; i<aAnies.getLength(); ++i )
aAnies[i] >>= pOutLabels[i];
}
}
sal_Int32 lcl_getMaxSequenceLength(
const SchXMLExportHelper_Impl::tDataSequenceCont & rContainer )
{
sal_Int32 nResult = 0;
for ( const auto & rDataSequence : rContainer )
{
if ( rDataSequence.second.is())
{
sal_Int32 nSeqLength = rDataSequence.second->getData().getLength();
if ( nSeqLength > nResult )
nResult = nSeqLength;
}
}
return nResult;
}
uno::Sequence< OUString > lcl_DataSequenceToStringSequence(
const uno::Reference< chart2::data::XDataSequence >& xDataSequence )
{
uno::Sequence< OUString > aResult;
if (!xDataSequence.is())
return aResult;
uno::Reference< chart2::data::XTextualDataSequence > xTextualDataSequence( xDataSequence, uno::UNO_QUERY );
if ( xTextualDataSequence.is() )
{
aResult = xTextualDataSequence->getTextualData();
}
else
{
uno::Sequence< uno::Any > aValues = xDataSequence->getData();
aResult.realloc(aValues.getLength());
auto pResult = aResult.getArray();
for (sal_Int32 nN=aValues.getLength();nN--;)
aValues[nN] >>= pResult[nN];
}
return aResult;
}
::std::vector< double > lcl_getAllValuesFromSequence( const Reference< chart2::data::XDataSequence > & xSeq )
{
::std::vector< double > aResult;
if (!xSeq.is())
return aResult;
uno::Sequence< double > aValuesSequence;
Reference< chart2::data::XNumericalDataSequence > xNumSeq( xSeq, uno::UNO_QUERY );
if ( xNumSeq.is() )
{
aValuesSequence = xNumSeq->getNumericalData();
}
else
{
Sequence< uno::Any > aAnies( xSeq->getData() );
aValuesSequence.realloc( aAnies.getLength() );
auto pValuesSequence = aValuesSequence.getArray();
for ( sal_Int32 i=0; i<aAnies.getLength(); ++i )
aAnies[i] >>= pValuesSequence[i];
}
//special handling for x-values (if x-values do point to categories, indices are used instead )
Reference< beans::XPropertySet > xProp( xSeq, uno::UNO_QUERY );
if ( xProp.is() )
{
OUString aRole;
xProp->getPropertyValue(u"Role" _ustr) >>= aRole;
if ( aRole.match("values-x" ) )
{
//lcl_clearIfNoValuesButTextIsContained - replace by indices if the values are not appropriate
bool bHasValue = std::any_of(std::cbegin(aValuesSequence), std::cend(aValuesSequence),
[](double fValue) { return !std::isnan( fValue ); });
if (!bHasValue)
{
//no double value is contained
//is there any text?
const uno::Sequence< OUString > aStrings( lcl_DataSequenceToStringSequence( xSeq ) );
bool bHasText = std::any_of(aStrings.begin(), aStrings.end(),
[](const OUString& rString) { return !rString.isEmpty(); });
if ( bHasText )
{
auto [begin, end] = asNonConstRange(aValuesSequence);
std::iota(begin, end, 1);
}
}
}
}
aResult.insert( aResult.end(), std::cbegin(aValuesSequence), std::cend(aValuesSequence) );
return aResult;
}
bool lcl_SequenceHasUnhiddenData( const uno::Reference< chart2::data::XDataSequence >& xDataSequence )
{
if ( !xDataSequence.is() )
return false ;
uno::Reference< beans::XPropertySet > xProp( xDataSequence, uno::UNO_QUERY );
if ( xProp.is() )
{
uno::Sequence< sal_Int32 > aHiddenValues;
try
{
xProp->getPropertyValue(u"HiddenValues" _ustr) >>= aHiddenValues;
if ( !aHiddenValues.hasElements() )
return true ;
}
catch ( const uno::Exception& )
{
return true ;
}
}
return xDataSequence->getData().hasElements();
}
typedef vector< OUString > tStringVector;
typedef vector< vector< double > > t2DNumberContainer;
struct lcl_TableData
{
t2DNumberContainer aDataInRows;
tStringVector aDataRangeRepresentations;
tStringVector aColumnDescriptions;
tStringVector aColumnDescriptions_Ranges;
tStringVector aRowDescriptions;
tStringVector aRowDescriptions_Ranges;
Sequence< Sequence< uno::Any > > aComplexColumnDescriptions;//outer index is columns - inner index is level
Sequence< Sequence< uno::Any > > aComplexRowDescriptions;//outer index is rows - inner index is level
::std::vector< sal_Int32 > aHiddenColumns;
};
typedef ::std::map< sal_Int32, SchXMLExportHelper_Impl::tLabelValuesDataPair >
lcl_DataSequenceMap;
struct lcl_SequenceToMapElement
{
std::pair<const sal_Int32, SchXMLExportHelper_Impl::tLabelValuesDataPair>
operator () (const SchXMLExportHelper_Impl::tLabelValuesDataPair& rContent)
{
sal_Int32 nIndex = -1;
if ( rContent.second.is()) //has values
{
OUString aRangeRep( rContent.second->getSourceRangeRepresentation());
nIndex = aRangeRep.toInt32();
}
else if ( rContent.first.is()) //has labels
nIndex = o3tl::toInt32(rContent.first->getSourceRangeRepresentation().subView( sizeof ("label " )));
return std::make_pair(nIndex, rContent);
}
};
void lcl_ReorderInternalSequencesAccordingToTheirRangeName(
SchXMLExportHelper_Impl::tDataSequenceCont & rInOutSequences )
{
lcl_DataSequenceMap aIndexSequenceMap;
::std::transform( rInOutSequences.begin(), rInOutSequences.end(),
::std::inserter( aIndexSequenceMap, aIndexSequenceMap.begin()),
lcl_SequenceToMapElement());
rInOutSequences.clear();
sal_Int32 nIndex = 0;
for ( const auto & rEntry : aIndexSequenceMap )
{
if ( rEntry.first >= 0 )
{
// fill empty columns
rInOutSequences.insert(
rInOutSequences.end(),
rEntry.first - nIndex,
SchXMLExportHelper_Impl::tDataSequenceCont::value_type(
uno::Reference< chart2::data::XDataSequence >(),
uno::Reference< chart2::data::XDataSequence >() ));
nIndex = rEntry.first;
rInOutSequences.push_back( rEntry.second );
}
++nIndex;
}
}
lcl_TableData lcl_getDataForLocalTable(
const SchXMLExportHelper_Impl::tDataSequenceCont & aSequencesToExport,
const Reference< chart2::XAnyDescriptionAccess >& xAnyDescriptionAccess,
const OUString& rCategoriesRange,
bool bSeriesFromColumns,
const Reference< chart2::data::XRangeXMLConversion > & xRangeConversion )
{
lcl_TableData aResult;
try
{
Sequence< OUString > aSimpleCategories;
if ( xAnyDescriptionAccess.is() )
{
//categories
if ( bSeriesFromColumns )
{
aSimpleCategories = xAnyDescriptionAccess->getRowDescriptions();
aResult.aComplexRowDescriptions = xAnyDescriptionAccess->getAnyRowDescriptions();
}
else
{
aSimpleCategories = xAnyDescriptionAccess->getColumnDescriptions();
aResult.aComplexColumnDescriptions = xAnyDescriptionAccess->getAnyColumnDescriptions();
}
}
//series values and series labels
SchXMLExportHelper_Impl::tDataSequenceCont::size_type nNumSequences = aSequencesToExport.size();
auto nMaxSequenceLength( lcl_getMaxSequenceLength( aSequencesToExport ));
if ( aSimpleCategories.getLength() > nMaxSequenceLength )
{
aSimpleCategories.realloc(nMaxSequenceLength);//#i110617#
}
size_t nNumColumns( bSeriesFromColumns ? nNumSequences : nMaxSequenceLength );
size_t nNumRows( bSeriesFromColumns ? nMaxSequenceLength : nNumSequences );
// resize data
aResult.aDataInRows.resize( nNumRows );
for (auto & aData: aResult.aDataInRows)
aData.resize(nNumColumns, std::numeric_limits<double >::quiet_NaN());
aResult.aColumnDescriptions.resize( nNumColumns );
aResult.aComplexColumnDescriptions.realloc( nNumColumns );
aResult.aRowDescriptions.resize( nNumRows );
aResult.aComplexRowDescriptions.realloc( nNumRows );
tStringVector& rCategories = bSeriesFromColumns ? aResult.aRowDescriptions : aResult.aColumnDescriptions;
tStringVector& rLabels = bSeriesFromColumns ? aResult.aColumnDescriptions : aResult.aRowDescriptions;
//categories
rCategories.clear();
rCategories.insert( rCategories.begin(), std::cbegin(aSimpleCategories), std::cend(aSimpleCategories) );
if ( !rCategoriesRange.isEmpty() )
{
OUString aRange(rCategoriesRange);
if ( xRangeConversion.is())
aRange = xRangeConversion->convertRangeToXML( aRange );
if ( bSeriesFromColumns )
aResult.aRowDescriptions_Ranges.push_back( aRange );
else
aResult.aColumnDescriptions_Ranges.push_back( aRange );
}
// iterate over all sequences
size_t nSeqIdx = 0;
Sequence< Sequence< OUString > > aComplexLabels(nNumSequences);
auto aComplexLabelsRange = asNonConstRange(aComplexLabels);
for ( const auto & rDataSequence : aSequencesToExport )
{
OUString aRange;
Sequence< OUString >& rCurrentComplexLabel = aComplexLabelsRange[nSeqIdx];
if ( rDataSequence.first.is())
{
lcl_getLabelStringSequence( rCurrentComplexLabel, rDataSequence.first );
rLabels[nSeqIdx] = lcl_flattenStringSequence( rCurrentComplexLabel );
aRange = rDataSequence.first->getSourceRangeRepresentation();
if ( xRangeConversion.is())
aRange = xRangeConversion->convertRangeToXML( aRange );
}
else if ( rDataSequence.second.is())
{
rCurrentComplexLabel.realloc(1);
rLabels[nSeqIdx] = rCurrentComplexLabel.getArray()[0] = lcl_flattenStringSequence(
rDataSequence.second->generateLabel( chart2::data::LabelOrigin_SHORT_SIDE ));
}
if ( bSeriesFromColumns )
aResult.aColumnDescriptions_Ranges.push_back( aRange );
else
aResult.aRowDescriptions_Ranges.push_back( aRange );
::std::vector< double > aNumbers( lcl_getAllValuesFromSequence( rDataSequence.second ));
if ( bSeriesFromColumns )
{
const sal_Int32 nSize( static_cast < sal_Int32 >( aNumbers.size()));
for ( sal_Int32 nIdx=0; nIdx<nSize; ++nIdx )
aResult.aDataInRows[nIdx][nSeqIdx] = aNumbers[nIdx];
}
else
aResult.aDataInRows[nSeqIdx] = std::move(aNumbers);
if ( rDataSequence.second.is())
{
aRange = rDataSequence.second->getSourceRangeRepresentation();
if ( xRangeConversion.is())
aRange = xRangeConversion->convertRangeToXML( aRange );
}
aResult.aDataRangeRepresentations.push_back( aRange );
//is column hidden?
if ( !lcl_SequenceHasUnhiddenData(rDataSequence.first) && !lcl_SequenceHasUnhiddenData(rDataSequence.second) )
aResult.aHiddenColumns.push_back(nSeqIdx);
++nSeqIdx;
}
Sequence< Sequence< Any > >& rComplexAnyLabels = bSeriesFromColumns ? aResult.aComplexColumnDescriptions : aResult.aComplexRowDescriptions;//#i116544#
rComplexAnyLabels.realloc(aComplexLabels.getLength());
auto pComplexAnyLabels = rComplexAnyLabels.getArray();
for ( sal_Int32 nN=0; nN<aComplexLabels.getLength();nN++ )
{
Sequence< OUString >& rSource = aComplexLabelsRange[nN];
Sequence< Any >& rTarget = pComplexAnyLabels[nN];
rTarget.realloc( rSource.getLength() );
auto pTarget = rTarget.getArray();
for ( sal_Int32 i=0; i<rSource.getLength(); i++ )
pTarget[i] <<= rSource[i];
}
}
catch ( const uno::Exception & )
{
TOOLS_INFO_EXCEPTION("xmloff.chart" , "something went wrong during table data collection" );
}
return aResult;
}
void lcl_exportNumberFormat( const OUString& rPropertyName, const Reference< beans::XPropertySet >& xPropSet,
SvXMLExport& rExport )
{
if ( xPropSet.is())
{
sal_Int32 nNumberFormat = 0;
Any aNumAny = xPropSet->getPropertyValue( rPropertyName );
if ( (aNumAny >>= nNumberFormat) && (nNumberFormat != -1) )
rExport.addDataStyle( nNumberFormat );
}
}
::std::vector< Reference< chart2::data::XDataSequence > >
lcl_getErrorBarSequences( const Reference< beans::XPropertySet > & xErrorBarProp )
{
::std::vector< Reference< chart2::data::XDataSequence > > aResult;
Reference< chart2::data::XDataSource > xErrorBarDataSource( xErrorBarProp, uno::UNO_QUERY );
if ( !xErrorBarDataSource.is())
return aResult;
const Sequence< Reference< chart2::data::XLabeledDataSequence > > aSequences(
xErrorBarDataSource->getDataSequences());
for ( const auto & rSequence : aSequences )
{
try
{
if ( rSequence.is())
{
Reference< chart2::data::XDataSequence > xSequence( rSequence->getValues());
Reference< beans::XPropertySet > xSeqProp( xSequence, uno::UNO_QUERY_THROW );
OUString aRole;
if ( ( xSeqProp->getPropertyValue( u"Role" _ustr ) >>= aRole ) &&
aRole.match( "error-bars-" ))
{
aResult.push_back( xSequence );
}
}
}
catch ( const uno::Exception & )
{
TOOLS_INFO_EXCEPTION("xmloff.chart" , "chart:exporting error bar ranges" );
}
}
return aResult;
}
bool lcl_exportDomainForThisSequence( const Reference< chart2::data::XDataSequence >& rValues, OUString& rFirstRangeForThisDomainIndex, SvXMLExport& rExport )
{
bool bDomainExported = false ;
if ( rValues.is())
{
Reference< chart2::XChartDocument > xNewDoc( rExport.GetModel(), uno::UNO_QUERY );
OUString aRange( lcl_ConvertRange( rValues->getSourceRangeRepresentation(), xNewDoc ) );
//work around error in OOo 2.0 (problems with multiple series having a domain element)
if ( rFirstRangeForThisDomainIndex.isEmpty() || aRange != rFirstRangeForThisDomainIndex )
{
rExport.AddAttribute( XML_NAMESPACE_TABLE, XML_CELL_RANGE_ADDRESS, aRange);
SvXMLElementExport aDomain( rExport, XML_NAMESPACE_CHART, XML_DOMAIN, true , true );
bDomainExported = true ;
}
if ( rFirstRangeForThisDomainIndex.isEmpty() )
rFirstRangeForThisDomainIndex = aRange;
}
return bDomainExported;
}
} // anonymous namespace
SchXMLExportHelper::SchXMLExportHelper( SvXMLExport& rExport, SvXMLAutoStylePoolP& rASPool )
: m_pImpl( new SchXMLExportHelper_Impl( rExport, rASPool ) )
{
}
SchXMLExportHelper::~SchXMLExportHelper()
{
}
const OUString& SchXMLExportHelper::getChartCLSID() const
{
return m_pImpl->msCLSID;
}
void SchXMLExportHelper::SetSourceShellID( const OUString& rShellID )
{
m_pImpl->maSrcShellID = rShellID;
}
void SchXMLExportHelper::SetDestinationShellID( const OUString& rShellID )
{
m_pImpl->maDestShellID = rShellID;
}
const rtl::Reference< XMLPropertySetMapper >& SchXMLExportHelper_Impl::GetPropertySetMapper() const
{
return mxPropertySetMapper;
}
void SchXMLExportHelper_Impl::exportAutoStyles()
{
if ( !mxExpPropMapper.is())
return ;
//ToDo: when embedded in calc/writer this is not necessary because the
// numberformatter is shared between both documents
mrExport.exportAutoDataStyles();
// export chart auto styles
mrAutoStylePool.exportXML( XmlStyleFamily::SCH_CHART_ID );
// export auto styles for additional shapes
mrExport.GetShapeExport()->exportAutoStyles();
// and for text in additional shapes
mrExport.GetTextParagraphExport()->exportTextAutoStyles();
}
// private methods
SchXMLExportHelper_Impl::SchXMLExportHelper_Impl(
SvXMLExport& rExport,
SvXMLAutoStylePoolP& rASPool ) :
mrExport( rExport ),
mrAutoStylePool( rASPool ),
mxPropertySetMapper( new XMLChartPropertySetMapper(&rExport) ),
mxExpPropMapper( new XMLChartExportPropertyMapper( mxPropertySetMapper, rExport ) ),
mbHasCategoryLabels( false ),
mbRowSourceColumns( true ),
msCLSID( SvGlobalName( SO3_SCH_CLASSID ).GetHexName() )
{
// register chart auto-style family
mrAutoStylePool.AddFamily(
XmlStyleFamily::SCH_CHART_ID,
XML_STYLE_FAMILY_SCH_CHART_NAME,
mxExpPropMapper.get(),
XML_STYLE_FAMILY_SCH_CHART_PREFIX);
// register shape family
mrAutoStylePool.AddFamily(
XmlStyleFamily::SD_GRAPHICS_ID,
XML_STYLE_FAMILY_SD_GRAPHICS_NAME,
mxExpPropMapper.get(),
XML_STYLE_FAMILY_SD_GRAPHICS_PREFIX);
// register paragraph family also for shapes
mrAutoStylePool.AddFamily(
XmlStyleFamily::TEXT_PARAGRAPH,
GetXMLToken( XML_PARAGRAPH ),
mxExpPropMapper.get(),
OUString( 'P' ));
// register text family also for shapes
mrAutoStylePool.AddFamily(
XmlStyleFamily::TEXT_TEXT,
GetXMLToken( XML_TEXT ),
mxExpPropMapper.get(),
OUString( 'T' ));
}
void SchXMLExportHelper_Impl::collectAutoStyles( Reference< chart::XChartDocument > const & rChartDoc )
{
parseDocument( rChartDoc, false );
}
void SchXMLExportHelper_Impl::exportChart( Reference< chart::XChartDocument > const & rChartDoc,
bool bIncludeTable )
{
parseDocument( rChartDoc, true , bIncludeTable );
SAL_WARN_IF( !maAutoStyleNameQueue.empty(), "xmloff.chart" , "There are still remaining autostyle names in the queue" );
}
static OUString lcl_GetStringFromNumberSequence( const css::uno::Sequence< sal_Int32 >& rSequenceMapping, bool bRemoveOneFromEachIndex /*should be true if having categories*/ )
{
OUStringBuffer aBuf;
bool bHasPredecessor = false ;
for ( sal_Int32 nIndex : rSequenceMapping )
{
if ( bRemoveOneFromEachIndex )
--nIndex;
if (nIndex>=0)
{
if (bHasPredecessor)
aBuf.append( ' ' );
aBuf.append( nIndex );
bHasPredecessor = true ;
}
}
return aBuf.makeStringAndClear();
}
/// if bExportContent is false the auto-styles are collected
void SchXMLExportHelper_Impl::parseDocument( Reference< chart::XChartDocument > const & rChartDoc,
bool bExportContent,
bool bIncludeTable )
{
Reference< chart2::XChartDocument > xNewDoc( rChartDoc, uno::UNO_QUERY );
if ( !rChartDoc.is() || !xNewDoc.is() )
{
SAL_WARN("xmloff.chart" , "No XChartDocument was given for export." );
return ;
}
const SvtSaveOptions::ODFSaneDefaultVersion nCurrentODFVersion(mrExport.getSaneDefaultVersion());
mxExpPropMapper->setChartDoc(xNewDoc);
awt::Size aPageSize( getPageSize( xNewDoc ));
if ( bExportContent )
addSize( aPageSize );
Reference< chart::XDiagram > xDiagram = rChartDoc->getDiagram();
Reference< chart2::XDiagram > xNewDiagram;
if ( xNewDoc.is())
xNewDiagram.set( xNewDoc->getFirstDiagram());
//todo remove if model changes are notified and view is updated automatically
if ( bExportContent )
{
Reference< util::XRefreshable > xRefreshable( xNewDoc, uno::UNO_QUERY );
if ( xRefreshable.is() )
xRefreshable->refresh();
}
// get Properties of ChartDocument
bool bHasMainTitle = false ;
bool bHasSubTitle = false ;
bool bHasLegend = false ;
util::DateTime aNullDate(0,0,0,0,30,12,1899, false );
std::vector< XMLPropertyState > aPropertyStates;
Reference< beans::XPropertySet > xDocPropSet( rChartDoc, uno::UNO_QUERY );
if ( xDocPropSet.is())
{
try
{
Any aAny = xDocPropSet->getPropertyValue(u"HasMainTitle" _ustr);
aAny >>= bHasMainTitle;
aAny = xDocPropSet->getPropertyValue(u"HasSubTitle" _ustr);
aAny >>= bHasSubTitle;
aAny = xDocPropSet->getPropertyValue(u"HasLegend" _ustr);
aAny >>= bHasLegend;
if ( bIncludeTable )
{
aAny = xDocPropSet->getPropertyValue(u"NullDate" _ustr);
if ( !aAny.hasValue() )
{
Reference<container::XChild> xChild(rChartDoc, uno::UNO_QUERY );
if ( xChild.is() )
{
Reference< beans::XPropertySet > xParentDoc( xChild->getParent(),uno::UNO_QUERY);
if ( xParentDoc.is() && xParentDoc->getPropertySetInfo()->hasPropertyByName(u"NullDate" _ustr) )
aAny = xParentDoc->getPropertyValue(u"NullDate" _ustr);
}
}
aAny >>= aNullDate;
}
}
catch ( const beans::UnknownPropertyException & )
{
SAL_WARN("xmloff.chart" , "Required property not found in ChartDocument" );
}
}
if ( bIncludeTable && (aNullDate.Day != 30 || aNullDate.Month != 12 || aNullDate.Year != 1899 ) )
{
SvXMLElementExport aSet( mrExport, XML_NAMESPACE_TABLE, XML_CALCULATION_SETTINGS, true , true );
{
OUStringBuffer sBuffer;
::sax::Converter::convertDateTime(sBuffer, aNullDate, nullptr);
mrExport.AddAttribute( XML_NAMESPACE_TABLE,XML_DATE_VALUE,sBuffer.makeStringAndClear());
SvXMLElementExport aNull( mrExport, XML_NAMESPACE_TABLE, XML_NULL_DATE, true , true );
}
}
// chart element
std::unique_ptr<SvXMLElementExport> xElChart;
// get property states for autostyles
if ( mxExpPropMapper.is())
{
Reference< beans::XPropertySet > xPropSet = rChartDoc->getArea();
if ( xPropSet.is())
aPropertyStates = mxExpPropMapper->Filter(mrExport, xPropSet);
}
if ( bExportContent )
{
//export data provider in xlink:href attribute
if (nCurrentODFVersion >= SvtSaveOptions::ODFSVER_012)
{
OUString aDataProviderURL( u".." _ustr );
if ( xNewDoc->hasInternalDataProvider() )
aDataProviderURL = "." ;
else //special handling for data base data provider necessary
{
Reference< chart2::data::XDatabaseDataProvider > xDBDataProvider( xNewDoc->getDataProvider(), uno::UNO_QUERY );
if ( xDBDataProvider.is() )
aDataProviderURL = "." ;
}
mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_HREF, aDataProviderURL );
mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE, XML_SIMPLE );
}
Reference<chart2::data::XPivotTableDataProvider> xPivotTableDataProvider(xNewDoc->getDataProvider(), uno::UNO_QUERY);
if (xPivotTableDataProvider.is() && nCurrentODFVersion & SvtSaveOptions::ODFSVER_EXTENDED)
{
OUString sPivotTableName = xPivotTableDataProvider->getPivotTableName();
mrExport.AddAttribute(XML_NAMESPACE_LO_EXT, XML_DATA_PILOT_SOURCE, sPivotTableName);
}
OUString sChartType( xDiagram->getDiagramType() );
// attributes
// determine class
if ( !sChartType.isEmpty())
{
enum XMLTokenEnum eXMLChartType = SchXMLTools::getTokenByChartType( sChartType, true /* bUseOldNames */ );
SAL_WARN_IF( eXMLChartType == XML_TOKEN_INVALID, "xmloff.chart" , "invalid chart class" );
if ( eXMLChartType == XML_TOKEN_INVALID )
eXMLChartType = XML_BAR;
if ( eXMLChartType == XML_ADD_IN )
{
// sChartType is the service-name of the add-in
mrExport.AddAttribute( XML_NAMESPACE_CHART, XML_CLASS,
mrExport.GetNamespaceMap().GetQNameByKey(
XML_NAMESPACE_OOO, sChartType) );
}
else if ( eXMLChartType != XML_TOKEN_INVALID )
{
mrExport.AddAttribute( XML_NAMESPACE_CHART, XML_CLASS,
mrExport.GetNamespaceMap().GetQNameByKey(
XML_NAMESPACE_CHART, GetXMLToken(eXMLChartType )) );
}
bool bIsOfPie = false ;
// Handle subtype for of-pie charts
if (sChartType == u"com.sun.star.chart.BarOfPieDiagram" ) {
mrExport.AddAttribute(XML_NAMESPACE_LO_EXT, XML_SUB_BAR, OUString::boolean(true ));
bIsOfPie = true ;
} else if (sChartType == u"com.sun.star.chart.PieOfPieDiagram" ) {
mrExport.AddAttribute(XML_NAMESPACE_LO_EXT, XML_SUB_PIE, OUString::boolean(true ));
bIsOfPie = true ;
}
if (bIsOfPie) {
// Find the split position. We have to dig deep into the
// structure tree to get it, which is awkward. Part of the
// problem is that the split position is sort of a series-level
// parameter, but is generally handled at the chart level since
// of-pie charts have only a single series.
double fSplitPos = 2.0;
Reference< chart2::XCoordinateSystemContainer > xBCooSysCnt( xNewDiagram, uno::UNO_QUERY );
if (xBCooSysCnt.is()) {
const Sequence< Reference< chart2::XCoordinateSystem > >
aCooSysSeq( xBCooSysCnt->getCoordinateSystems());
for (const auto & rCooSys : aCooSysSeq ) {
Reference< chart2::XChartTypeContainer > xCTCnt( rCooSys, uno::UNO_QUERY );
if ( ! xCTCnt.is())
continue ;
const Sequence< Reference< chart2::XChartType > > aCTSeq( xCTCnt->getChartTypes());
for (const auto & rChartType : aCTSeq ) {
Reference< beans::XPropertySet > xCTProp( rChartType, uno::UNO_QUERY );
if (xCTProp.is()) {
xCTProp->getPropertyValue(u"SplitPos" _ustr) >>= fSplitPos;
}
}
}
}
// Insert split position for of-pie chart
mrExport.AddAttribute(XML_NAMESPACE_LO_EXT, XML_SPLIT_POSITION,
OUString::number(fSplitPos));
}
// The attribute table:cell-range-address was removed from the standard in ODF 1.4.
// The associated attributes chart:column-mapping and chart:row-mapping are deprecated.
//column-mapping or row-mapping
if ( maSequenceMapping.hasElements() && nCurrentODFVersion < SvtSaveOptions::ODFSVER_014)
{
enum XMLTokenEnum eTransToken = ::xmloff::token::XML_ROW_MAPPING;
if ( mbRowSourceColumns )
eTransToken = ::xmloff::token::XML_COLUMN_MAPPING;
OUString aSequenceMappingStr( lcl_GetStringFromNumberSequence(
maSequenceMapping, mbHasCategoryLabels && !xNewDoc->hasInternalDataProvider() ) );
mrExport.AddAttribute( XML_NAMESPACE_CHART,
::xmloff::token::GetXMLToken( eTransToken ),
aSequenceMappingStr );
}
}
// write style name
AddAutoStyleAttribute( aPropertyStates );
//element
xElChart.reset(new SvXMLElementExport( mrExport, XML_NAMESPACE_CHART, XML_CHART, true , true ));
}
else // autostyles
{
CollectAutoStyle( std::move(aPropertyStates) );
}
// remove property states for autostyles
aPropertyStates.clear();
// title element
if ( bHasMainTitle )
{
// get property states for autostyles
Reference< drawing::XShape > xShape = rChartDoc->getTitle();
Reference< beans::XPropertySet > xPropSet(xShape, uno::UNO_QUERY);
if ( mxExpPropMapper.is() && xPropSet.is())
{
aPropertyStates = mxExpPropMapper->Filter(mrExport, xPropSet);
}
if ( bExportContent )
{
if ( xShape.is()) // && "hasTitleBeenMoved"
addPosition( xShape );
// write style name
AddAutoStyleAttribute( aPropertyStates );
// element
SvXMLElementExport aElTitle( mrExport, XML_NAMESPACE_CHART, XML_TITLE, true , true );
// content (text:p)
exportFormattedText(xPropSet);
}
else // autostyles
{
CollectAutoStyle( std::move(aPropertyStates) );
CollectAutoTextStyle( xPropSet );
}
// remove property states for autostyles
aPropertyStates.clear();
}
// subtitle element
if ( bHasSubTitle )
{
// get property states for autostyles
Reference< drawing::XShape > xShape = rChartDoc->getSubTitle();
Reference< beans::XPropertySet > xPropSet(xShape, uno::UNO_QUERY);
if ( mxExpPropMapper.is() && xPropSet.is())
{
aPropertyStates = mxExpPropMapper->Filter(mrExport, xPropSet);
}
if ( bExportContent )
{
if ( xShape.is())
addPosition( xShape );
// write style name
AddAutoStyleAttribute( aPropertyStates );
// element (has no subelements)
SvXMLElementExport aElSubTitle( mrExport, XML_NAMESPACE_CHART, XML_SUBTITLE, true , true );
// content (text:p)
exportFormattedText(xPropSet);
}
else // autostyles
{
CollectAutoStyle( std::move(aPropertyStates) );
CollectAutoTextStyle(xPropSet);
}
// remove property states for autostyles
aPropertyStates.clear();
}
// legend element
if ( bHasLegend )
{
// get property states for autostyles
if ( mxExpPropMapper.is())
{
Reference< beans::XPropertySet > xPropSet( rChartDoc->getLegend(), uno::UNO_QUERY );
if ( xPropSet.is())
aPropertyStates = mxExpPropMapper->Filter(mrExport, xPropSet);
}
if ( bExportContent )
{
Reference< beans::XPropertySet > xProp( rChartDoc->getLegend(), uno::UNO_QUERY );
if ( xProp.is())
{
// export legend anchor position
try
{
Any aAny( xProp->getPropertyValue(u"Alignment" _ustr));
if ( SchXMLEnumConverter::getLegendPositionConverter().exportXML( msString, aAny, mrExport.GetMM100UnitConverter() ) )
mrExport.AddAttribute( XML_NAMESPACE_CHART, XML_LEGEND_POSITION, msString );
}
catch ( const beans::UnknownPropertyException & )
{
SAL_WARN("xmloff.chart" , "Property Align not found in ChartLegend" );
}
// export legend overlay
try
{
if (nCurrentODFVersion & SvtSaveOptions::ODFSVER_EXTENDED)
{
Any aAny( xProp->getPropertyValue(u"Overlay" _ustr));
if (aAny.get<bool >())
mrExport.AddAttribute(XML_NAMESPACE_LO_EXT, XML_OVERLAY, OUString::boolean(true ));
}
}
catch ( const beans::UnknownPropertyException & )
{
SAL_WARN("xmloff.chart" , "Property Overlay not found in ChartLegend" );
}
// export absolute legend position
Reference< drawing::XShape > xLegendShape( xProp, uno::UNO_QUERY );
addPosition( xLegendShape );
// export legend size
if (xLegendShape.is() && nCurrentODFVersion >= SvtSaveOptions::ODFSVER_012)
{
try
{
chart::ChartLegendExpansion nLegendExpansion = chart::ChartLegendExpansion_HIGH;
OUString aExpansionString;
Any aAny( xProp->getPropertyValue(u"Expansion" _ustr));
bool bHasExpansion = (aAny >>= nLegendExpansion);
if ( bHasExpansion && SchXMLEnumConverter::getLegendExpansionConverter().exportXML( aExpansionString, aAny, mrExport.GetMM100UnitConverter() ) )
{
mrExport.AddAttribute( XML_NAMESPACE_STYLE, XML_LEGEND_EXPANSION, aExpansionString );
if ( nLegendExpansion == chart::ChartLegendExpansion_CUSTOM)
{
awt::Size aSize( xLegendShape->getSize() );
// tdf#131966: chart legend attributes width and height shouldn't be exported to ODF 1.2 (strict)
if (nCurrentODFVersion >= SvtSaveOptions::ODFSVER_013)
{ // ODF 1.3 OFFICE-3883
addSize( aSize, false );
}
else if (nCurrentODFVersion & SvtSaveOptions::ODFSVER_EXTENDED)
{
addSize( aSize, true );
}
OUStringBuffer aAspectRatioString;
::sax::Converter::convertDouble(
aAspectRatioString,
(aSize.Height == 0
? 1.0
: double (aSize.Width)/double (aSize.Height)));
mrExport.AddAttribute( XML_NAMESPACE_STYLE, XML_LEGEND_EXPANSION_ASPECT_RATIO, aAspectRatioString.makeStringAndClear() );
}
}
}
catch ( const beans::UnknownPropertyException & )
{
SAL_WARN("xmloff.chart" , "Property Expansion not found in ChartLegend" );
}
}
}
// write style name
AddAutoStyleAttribute( aPropertyStates );
// element
SvXMLElementExport aLegend( mrExport, XML_NAMESPACE_CHART, XML_LEGEND, true , true );
}
else // autostyles
{
CollectAutoStyle( std::move(aPropertyStates) );
}
// remove property states for autostyles
aPropertyStates.clear();
}
// Export data table element and properties
if (xNewDiagram.is() && nCurrentODFVersion & SvtSaveOptions::ODFSVER_EXTENDED)
{
auto xDataTable = xNewDiagram->getDataTable();
if (xDataTable.is())
{
// get property states for autostyles
if (mxExpPropMapper.is())
{
uno::Reference<beans::XPropertySet> xPropSet(xDataTable, uno::UNO_QUERY);
if (xPropSet.is())
aPropertyStates = mxExpPropMapper->Filter(mrExport, xPropSet);
}
if (bExportContent)
{
// add style name attribute
AddAutoStyleAttribute(aPropertyStates);
SvXMLElementExport aDataTableElement(mrExport, XML_NAMESPACE_LO_EXT, XML_DATA_TABLE, true , true );
}
else
{
CollectAutoStyle(std::move(aPropertyStates));
}
}
// remove property states for autostyles
aPropertyStates.clear();
}
// plot-area element
if ( xDiagram.is())
exportPlotArea( xDiagram, xNewDiagram, aPageSize, bExportContent, bIncludeTable );
// export additional shapes
if ( xDocPropSet.is() )
{
if ( bExportContent )
{
if ( mxAdditionalShapes.is())
{
// can't call exportShapes with all shapes because the
// initialisation happened with the complete draw page and not
// the XShapes object used here. Thus the shapes have to be
// exported one by one
rtl::Reference< XMLShapeExport > rShapeExport = mrExport.GetShapeExport();
Reference< drawing::XShape > xShape;
const sal_Int32 nShapeCount( mxAdditionalShapes->getCount());
for ( sal_Int32 nShapeId = 0; nShapeId < nShapeCount; nShapeId++ )
{
mxAdditionalShapes->getByIndex( nShapeId ) >>= xShape;
SAL_WARN_IF( !xShape.is(), "xmloff.chart" , "Shape without an XShape?" );
if ( ! xShape.is())
continue ;
rShapeExport->exportShape( xShape );
}
// this would be the easier way if it worked:
//mrExport.GetShapeExport()->exportShapes( mxAdditionalShapes );
}
}
else
{
// get a sequence of non-chart shapes (inserted via clipboard)
try
{
Any aShapesAny = xDocPropSet->getPropertyValue(u"AdditionalShapes" _ustr);
aShapesAny >>= mxAdditionalShapes;
}
catch ( const uno::Exception & )
{
TOOLS_INFO_EXCEPTION("xmloff.chart" , "AdditionalShapes not found" );
}
if ( mxAdditionalShapes.is())
{
// seek shapes has to be called for the whole page because in
// the shape export the vector of shapes is accessed via the
// ZOrder which might be (actually is) larger than the number of
// shapes in mxAdditionalShapes
Reference< drawing::XDrawPageSupplier > xSupplier( rChartDoc, uno::UNO_QUERY );
SAL_WARN_IF( !xSupplier.is(), "xmloff.chart" , "Cannot retrieve draw page to initialize shape export" );
if ( xSupplier.is() )
{
Reference< drawing::XShapes > xDrawPage = xSupplier->getDrawPage();
SAL_WARN_IF( !xDrawPage.is(), "xmloff.chart" , "Invalid draw page for initializing shape export" );
if ( xDrawPage.is())
mrExport.GetShapeExport()->seekShapes( xDrawPage );
}
// can't call collectShapesAutoStyles with all shapes because
// the initialisation happened with the complete draw page and
// not the XShapes object used here. Thus the shapes have to be
// exported one by one
rtl::Reference< XMLShapeExport > rShapeExport = mrExport.GetShapeExport();
Reference< drawing::XShape > xShape;
const sal_Int32 nShapeCount( mxAdditionalShapes->getCount());
for ( sal_Int32 nShapeId = 0; nShapeId < nShapeCount; nShapeId++ )
{
mxAdditionalShapes->getByIndex( nShapeId ) >>= xShape;
SAL_WARN_IF( !xShape.is(), "xmloff.chart" , "Shape without an XShape?" );
if ( ! xShape.is())
continue ;
rShapeExport->collectShapeAutoStyles( xShape );
}
}
}
}
// table element
// (is included as subelement of chart)
if ( bExportContent )
{
// #85929# always export table, otherwise clipboard may lose data
exportTable();
}
}
static void lcl_exportComplexLabel( const Sequence< uno::Any >& rComplexLabel, SvXMLExport& rExport )
{
sal_Int32 nLength = rComplexLabel.getLength();
if ( nLength<=1 )
return ;
SvXMLElementExport aTextList( rExport, XML_NAMESPACE_TEXT, XML_LIST, true , true );
for (const auto & rElem : rComplexLabel)
{
SvXMLElementExport aListItem( rExport, XML_NAMESPACE_TEXT, XML_LIST_ITEM, true , true );
OUString aString;
if ( !(rElem >>= aString) )
{
double aNum;
if (rElem >>= aNum)
--> --------------------
--> maximum size reached
--> --------------------
Messung V0.5 C=90 H=96 G=93
¤ Dauer der Verarbeitung: 0.9 Sekunden
(vorverarbeitet)
¤
*© Formatika GbR, Deutschland