From 75156c6fd73dc202df541306e1636727d51d6fc3 Mon Sep 17 00:00:00 2001 From: Balazs Varga Date: Thu, 16 Apr 2020 22:08:15 +0200 Subject: tdf#132076 Chart OOXML: fix lost date format of X axis MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit labels. Set the number format of category or date axes too and use date axis type for date axes at import. Export the real axis type of the X axis. Change-Id: I45f5c69f0aadc4ec1db0b8873b5e374e2728d39c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/92396 Tested-by: László Németh Reviewed-by: László Németh --- oox/source/drawingml/chart/axisconverter.cxx | 10 ++--- oox/source/export/chartexport.cxx | 62 +++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 10 deletions(-) (limited to 'oox') diff --git a/oox/source/drawingml/chart/axisconverter.cxx b/oox/source/drawingml/chart/axisconverter.cxx index 9e62b61d73bc..d73632476550 100644 --- a/oox/source/drawingml/chart/axisconverter.cxx +++ b/oox/source/drawingml/chart/axisconverter.cxx @@ -205,10 +205,8 @@ void AxisConverter::convertFromModel( OSL_ENSURE( (mrModel.mnTypeId == C_TOKEN( catAx )) || (mrModel.mnTypeId == C_TOKEN( dateAx )), "AxisConverter::convertFromModel - unexpected axis model type (must: c:catAx or c:dateAx)" ); bool bDateAxis = mrModel.mnTypeId == C_TOKEN( dateAx ); - /* Chart2 requires axis type CATEGORY for automatic - category/date axis (even if it is a date axis - currently). */ - aScaleData.AxisType = (bDateAxis && !mrModel.mbAuto) ? cssc2::AxisType::DATE : cssc2::AxisType::CATEGORY; + // tdf#132076: set axis type to date, if it is a date axis! + aScaleData.AxisType = bDateAxis ? cssc2::AxisType::DATE : cssc2::AxisType::CATEGORY; aScaleData.AutoDateAxis = mrModel.mbAuto; aScaleData.Categories = rTypeGroups.front()->createCategorySequence(); /* set default ShiftedCategoryPosition values for some charttype, @@ -341,8 +339,10 @@ void AxisConverter::convertFromModel( xAxis->setScaleData( aScaleData ); // number format ------------------------------------------------------ - if( !mrModel.mbDeleted && ((aScaleData.AxisType == cssc2::AxisType::REALNUMBER) || (aScaleData.AxisType == cssc2::AxisType::PERCENT)) ) + if( !mrModel.mbDeleted && aScaleData.AxisType != cssc2::AxisType::SERIES ) + { getFormatter().convertNumberFormat(aAxisProp, mrModel.maNumberFormat, true); + } // position of crossing axis ------------------------------------------ diff --git a/oox/source/export/chartexport.cxx b/oox/source/export/chartexport.cxx index 16c4788b32b7..556e8a8a5958 100644 --- a/oox/source/export/chartexport.cxx +++ b/oox/source/export/chartexport.cxx @@ -67,6 +67,7 @@ #include #include #include +#include #include #include @@ -260,6 +261,39 @@ static bool lcl_isCategoryAxisShifted(const Reference< chart2::XChartDocument >& return isCategoryPositionShifted; } +static sal_Int32 lcl_getCategoryAxisType( const Reference< chart2::XDiagram >& xDiagram, sal_Int32 nDimensionIndex, sal_Int32 nAxisIndex ) +{ + sal_Int32 nAxisType = -1; + try + { + Reference< chart2::XCoordinateSystemContainer > xCooSysCnt( + xDiagram, uno::UNO_QUERY_THROW); + const Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq( + xCooSysCnt->getCoordinateSystems()); + for( const auto& xCooSys : aCooSysSeq ) + { + OSL_ASSERT(xCooSys.is()); + if( nDimensionIndex < xCooSys->getDimension() && nAxisIndex <= xCooSys->getMaximumAxisIndexByDimension(nDimensionIndex) ) + { + Reference< chart2::XAxis > xAxis = xCooSys->getAxisByDimension(nDimensionIndex, nAxisIndex); + OSL_ASSERT(xAxis.is()); + if( xAxis.is() ) + { + chart2::ScaleData aScaleData = xAxis->getScaleData(); + nAxisType = aScaleData.AxisType; + break; + } + } + } + } + catch (const uno::Exception&) + { + DBG_UNHANDLED_EXCEPTION("oox"); + } + + return nAxisType; +} + static bool lcl_isSeriesAttachedToFirstAxis( const Reference< chart2::XDataSeries > & xDataSeries ) { @@ -2644,7 +2678,7 @@ void ChartExport::exportAxes( ) namespace { -sal_Int32 getXAxisType(sal_Int32 eChartType) +sal_Int32 getXAxisTypeByChartType(sal_Int32 eChartType) { if( (eChartType == chart::TYPEID_SCATTER) || (eChartType == chart::TYPEID_BUBBLE) ) @@ -2655,6 +2689,18 @@ sal_Int32 getXAxisType(sal_Int32 eChartType) return XML_catAx; } +sal_Int32 getRealXAxisType(sal_Int32 nAxisType) +{ + if( nAxisType == chart2::AxisType::CATEGORY ) + return XML_catAx; + else if( nAxisType == chart2::AxisType::DATE ) + return XML_dateAx; + else if( nAxisType == chart2::AxisType::SERIES ) + return XML_serAx; + + return XML_valAx; +} + } void ChartExport::exportAxis(const AxisIdPair& rAxisIdPair) @@ -2709,8 +2755,11 @@ void ChartExport::exportAxis(const AxisIdPair& rAxisIdPair) if( bHasXAxisMinorGrid ) xMinorGrid = xAxisXSupp->getXHelpGrid(); - sal_Int32 eChartType = getChartType(); - nAxisType = getXAxisType(eChartType); + nAxisType = lcl_getCategoryAxisType(mxNewDiagram, 0, 0); + if( nAxisType != -1 ) + nAxisType = getRealXAxisType(nAxisType); + else + nAxisType = getXAxisTypeByChartType( getChartType() ); // FIXME: axPos, need to check axis direction sAxPos = "b"; break; @@ -2767,8 +2816,11 @@ void ChartExport::exportAxis(const AxisIdPair& rAxisIdPair) xAxisTitle = xAxisSupp->getSecondXAxisTitle(); } - sal_Int32 eChartType = getChartType(); - nAxisType = getXAxisType(eChartType); + nAxisType = lcl_getCategoryAxisType(mxNewDiagram, 0, 1); + if( nAxisType != -1 ) + nAxisType = getRealXAxisType(nAxisType); + else + nAxisType = getXAxisTypeByChartType( getChartType() ); // FIXME: axPos, need to check axis direction sAxPos = "t"; break; -- cgit