LCOV - code coverage report
Current view: top level - oox/source/drawingml/chart - axisconverter.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 147 162 90.7 %
Date: 2015-06-13 12:38:46 Functions: 15 17 88.2 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      20             : #include "drawingml/chart/axisconverter.hxx"
      21             : 
      22             : #include <com/sun/star/chart/ChartAxisArrangeOrderType.hpp>
      23             : #include <com/sun/star/chart/ChartAxisLabelPosition.hpp>
      24             : #include <com/sun/star/chart/ChartAxisMarkPosition.hpp>
      25             : #include <com/sun/star/chart/ChartAxisPosition.hpp>
      26             : #include <com/sun/star/chart/TimeInterval.hpp>
      27             : #include <com/sun/star/chart/TimeUnit.hpp>
      28             : #include <com/sun/star/chart2/AxisType.hpp>
      29             : #include <com/sun/star/chart2/TickmarkStyle.hpp>
      30             : #include <com/sun/star/chart2/LinearScaling.hpp>
      31             : #include <com/sun/star/chart2/LogarithmicScaling.hpp>
      32             : #include <com/sun/star/chart2/XAxis.hpp>
      33             : #include <com/sun/star/chart2/XCoordinateSystem.hpp>
      34             : #include <com/sun/star/chart2/XTitled.hpp>
      35             : #include "drawingml/chart/axismodel.hxx"
      36             : #include "drawingml/chart/titleconverter.hxx"
      37             : #include "drawingml/chart/typegroupconverter.hxx"
      38             : #include "oox/drawingml/lineproperties.hxx"
      39             : #include "comphelper/processfactory.hxx"
      40             : #include <osl/diagnose.h>
      41             : 
      42             : namespace oox {
      43             : namespace drawingml {
      44             : namespace chart {
      45             : 
      46             : using namespace ::com::sun::star::beans;
      47             : using namespace ::com::sun::star::chart2;
      48             : using namespace ::com::sun::star::uno;
      49             : 
      50             : namespace {
      51             : 
      52         616 : inline void lclSetValueOrClearAny( Any& orAny, const OptValue< double >& rofValue )
      53             : {
      54         616 :     if( rofValue.has() ) orAny <<= rofValue.get(); else orAny.clear();
      55         616 : }
      56             : 
      57         554 : bool lclIsLogarithmicScale( const AxisModel& rAxisModel )
      58             : {
      59         554 :     return rAxisModel.mofLogBase.has() && (2.0 <= rAxisModel.mofLogBase.get()) && (rAxisModel.mofLogBase.get() <= 1000.0);
      60             : }
      61             : 
      62           2 : sal_Int32 lclGetApiTimeUnit( sal_Int32 nTimeUnit )
      63             : {
      64             :     using namespace ::com::sun::star::chart;
      65           2 :     switch( nTimeUnit )
      66             :     {
      67           1 :         case XML_days:      return TimeUnit::DAY;
      68           1 :         case XML_months:    return TimeUnit::MONTH;
      69           0 :         case XML_years:     return TimeUnit::YEAR;
      70             :         default:            OSL_ENSURE( false, "lclGetApiTimeUnit - unexpected time unit" );
      71             :     }
      72           0 :     return TimeUnit::DAY;
      73             : }
      74             : 
      75          12 : void lclConvertTimeInterval( Any& orInterval, const OptValue< double >& rofUnit, sal_Int32 nTimeUnit )
      76             : {
      77          12 :     if( rofUnit.has() && (1.0 <= rofUnit.get()) && (rofUnit.get() <= SAL_MAX_INT32) )
      78           0 :         orInterval <<= ::com::sun::star::chart::TimeInterval( static_cast< sal_Int32 >( rofUnit.get() ), lclGetApiTimeUnit( nTimeUnit ) );
      79             :     else
      80          12 :         orInterval.clear();
      81          12 : }
      82             : 
      83         352 : ::com::sun::star::chart::ChartAxisLabelPosition lclGetLabelPosition( sal_Int32 nToken )
      84             : {
      85             :     using namespace ::com::sun::star::chart;
      86         352 :     switch( nToken )
      87             :     {
      88           0 :         case XML_high:      return ChartAxisLabelPosition_OUTSIDE_END;
      89           0 :         case XML_low:       return ChartAxisLabelPosition_OUTSIDE_START;
      90         349 :         case XML_nextTo:    return ChartAxisLabelPosition_NEAR_AXIS;
      91             :     }
      92           3 :     return ChartAxisLabelPosition_NEAR_AXIS;
      93             : }
      94             : 
      95         704 : sal_Int32 lclGetTickMark( sal_Int32 nToken )
      96             : {
      97             :     using namespace ::com::sun::star::chart2::TickmarkStyle;
      98         704 :     switch( nToken )
      99             :     {
     100           1 :         case XML_in:    return INNER;
     101         198 :         case XML_out:   return OUTER;
     102          88 :         case XML_cross: return INNER | OUTER;
     103             :     }
     104         417 :     return css::chart2::TickmarkStyle::NONE;
     105             : }
     106             : 
     107             : /**
     108             :  * The groups is of percent type only when all of its members are of percent
     109             :  * type.
     110             :  */
     111         176 : bool isPercent( const RefVector<TypeGroupConverter>& rTypeGroups )
     112             : {
     113         176 :     if (rTypeGroups.empty())
     114           0 :         return false;
     115             : 
     116         176 :     RefVector<TypeGroupConverter>::const_iterator it = rTypeGroups.begin(), itEnd = rTypeGroups.end();
     117         186 :     for (; it != itEnd; ++it)
     118             :     {
     119         177 :         TypeGroupConverter& rConv = **it;
     120         177 :         if (!rConv.isPercent())
     121         167 :             return false;
     122             :     }
     123             : 
     124           9 :     return true;
     125             : }
     126             : 
     127             : } // namespace
     128             : 
     129         352 : AxisConverter::AxisConverter( const ConverterRoot& rParent, AxisModel& rModel ) :
     130         352 :     ConverterBase< AxisModel >( rParent, rModel )
     131             : {
     132         352 : }
     133             : 
     134         352 : AxisConverter::~AxisConverter()
     135             : {
     136         352 : }
     137             : 
     138         352 : void AxisConverter::convertFromModel(
     139             :     const Reference< XCoordinateSystem >& rxCoordSystem,
     140             :     RefVector<TypeGroupConverter>& rTypeGroups, const AxisModel* pCrossingAxis, sal_Int32 nAxesSetIdx, sal_Int32 nAxisIdx )
     141             : {
     142         352 :     if (rTypeGroups.empty())
     143         352 :         return;
     144             : 
     145         352 :     Reference< XAxis > xAxis;
     146             :     try
     147             :     {
     148             :         namespace cssc = ::com::sun::star::chart;
     149             :         namespace cssc2 = ::com::sun::star::chart2;
     150             : 
     151         352 :         const TypeGroupInfo& rTypeInfo = rTypeGroups.front()->getTypeInfo();
     152         352 :         ObjectFormatter& rFormatter = getFormatter();
     153             : 
     154             :         // create the axis object (always)
     155         352 :         xAxis.set( createInstance( "com.sun.star.chart2.Axis" ), UNO_QUERY_THROW );
     156         352 :         PropertySet aAxisProp( xAxis );
     157             :         // #i58688# axis enabled
     158         352 :         aAxisProp.setProperty( PROP_Show, !mrModel.mbDeleted );
     159             : 
     160             :         // axis line, tick, and gridline properties ---------------------------
     161             : 
     162             :         // show axis labels
     163         352 :         aAxisProp.setProperty( PROP_DisplayLabels, mrModel.mnTickLabelPos != XML_none );
     164         352 :         aAxisProp.setProperty( PROP_LabelPosition, lclGetLabelPosition( mrModel.mnTickLabelPos ) );
     165             :         // no X axis line in radar charts
     166         352 :         if( (nAxisIdx == API_X_AXIS) && (rTypeInfo.meTypeCategory == TYPECATEGORY_RADAR) )
     167           2 :             mrModel.mxShapeProp.getOrCreate().getLineProperties().maLineFill.moFillType = XML_noFill;
     168             :         // axis line and tick label formatting
     169         352 :         rFormatter.convertFormatting( aAxisProp, mrModel.mxShapeProp, mrModel.mxTextProp, OBJECTTYPE_AXIS );
     170             :         // tick label rotation
     171         352 :         ObjectFormatter::convertTextRotation( aAxisProp, mrModel.mxTextProp, true );
     172             : 
     173             :         // tick mark style
     174         352 :         aAxisProp.setProperty( PROP_MajorTickmarks, lclGetTickMark( mrModel.mnMajorTickMark ) );
     175         352 :         aAxisProp.setProperty( PROP_MinorTickmarks, lclGetTickMark( mrModel.mnMinorTickMark ) );
     176         352 :         aAxisProp.setProperty( PROP_MarkPosition, cssc::ChartAxisMarkPosition_AT_AXIS );
     177             : 
     178             :         // main grid
     179         704 :         PropertySet aGridProp( xAxis->getGridProperties() );
     180         352 :         aGridProp.setProperty( PROP_Show, mrModel.mxMajorGridLines.is() );
     181         352 :         if( mrModel.mxMajorGridLines.is() )
     182         159 :             rFormatter.convertFrameFormatting( aGridProp, mrModel.mxMajorGridLines, OBJECTTYPE_MAJORGRIDLINE );
     183             : 
     184             :         // sub grid
     185         704 :         Sequence< Reference< XPropertySet > > aSubGridPropSeq = xAxis->getSubGridProperties();
     186         352 :         if( aSubGridPropSeq.hasElements() )
     187             :         {
     188         352 :             PropertySet aSubGridProp( aSubGridPropSeq[ 0 ] );
     189         352 :             aSubGridProp.setProperty( PROP_Show, mrModel.mxMinorGridLines.is() );
     190         352 :             if( mrModel.mxMinorGridLines.is() )
     191          12 :                 rFormatter.convertFrameFormatting( aSubGridProp, mrModel.mxMinorGridLines, OBJECTTYPE_MINORGRIDLINE );
     192             :         }
     193             : 
     194             :         // axis type and X axis categories ------------------------------------
     195             : 
     196         704 :         ScaleData aScaleData = xAxis->getScaleData();
     197             :         // set axis type
     198         352 :         switch( nAxisIdx )
     199             :         {
     200             :             case API_X_AXIS:
     201         176 :                 if( rTypeInfo.mbCategoryAxis )
     202             :                 {
     203             :                     OSL_ENSURE( (mrModel.mnTypeId == C_TOKEN( catAx )) || (mrModel.mnTypeId == C_TOKEN( dateAx )),
     204             :                         "AxisConverter::convertFromModel - unexpected axis model type (must: c:catAx or c:dateAx)" );
     205         150 :                     bool bDateAxis = mrModel.mnTypeId == C_TOKEN( dateAx );
     206             :                     /*  Chart2 requires axis type CATEGORY for automatic
     207             :                         category/date axis (even if it is a date axis
     208             :                         currently). */
     209         150 :                     aScaleData.AxisType = (bDateAxis && !mrModel.mbAuto) ? cssc2::AxisType::DATE : cssc2::AxisType::CATEGORY;
     210         150 :                     aScaleData.AutoDateAxis = mrModel.mbAuto;
     211         150 :                     aScaleData.Categories = rTypeGroups.front()->createCategorySequence();
     212             :                 }
     213             :                 else
     214             :                 {
     215             :                     OSL_ENSURE( mrModel.mnTypeId == C_TOKEN( valAx ), "AxisConverter::convertFromModel - unexpected axis model type (must: c:valAx)" );
     216          26 :                     aScaleData.AxisType = cssc2::AxisType::REALNUMBER;
     217             :                 }
     218         176 :             break;
     219             :             case API_Y_AXIS:
     220             :                 OSL_ENSURE( mrModel.mnTypeId == C_TOKEN( valAx ), "AxisConverter::convertFromModel - unexpected axis model type (must: c:valAx)" );
     221         176 :                 aScaleData.AxisType = isPercent(rTypeGroups) ? cssc2::AxisType::PERCENT : cssc2::AxisType::REALNUMBER;
     222         176 :             break;
     223             :             case API_Z_AXIS:
     224             :                 OSL_ENSURE( mrModel.mnTypeId == C_TOKEN( serAx ), "AxisConverter::convertFromModel - unexpected axis model type (must: c:serAx)" );
     225             :                 OSL_ENSURE( rTypeGroups.front()->isDeep3dChart(), "AxisConverter::convertFromModel - series axis not supported by this chart type" );
     226           0 :                 aScaleData.AxisType = cssc2::AxisType::SERIES;
     227           0 :             break;
     228             :         }
     229             : 
     230             :         // axis scaling and increment -----------------------------------------
     231             : 
     232         352 :         switch( aScaleData.AxisType )
     233             :         {
     234             :             case cssc2::AxisType::CATEGORY:
     235             :             case cssc2::AxisType::SERIES:
     236             :             case cssc2::AxisType::DATE:
     237             :             {
     238             :                 /*  Determine date axis type from XML type identifier, and not
     239             :                     via aScaleData.AxisType, as this value sticks to CATEGORY
     240             :                     for automatic category/date axes). */
     241         150 :                 if( mrModel.mnTypeId == C_TOKEN( dateAx ) )
     242             :                 {
     243             :                     // scaling algorithm
     244           6 :                     aScaleData.Scaling = LinearScaling::create( comphelper::getProcessComponentContext() );
     245             :                     // min/max
     246           6 :                     lclSetValueOrClearAny( aScaleData.Minimum, mrModel.mofMin );
     247           6 :                     lclSetValueOrClearAny( aScaleData.Maximum, mrModel.mofMax );
     248             :                     // major/minor increment
     249           6 :                     lclConvertTimeInterval( aScaleData.TimeIncrement.MajorTimeInterval, mrModel.mofMajorUnit, mrModel.mnMajorTimeUnit );
     250           6 :                     lclConvertTimeInterval( aScaleData.TimeIncrement.MinorTimeInterval, mrModel.mofMinorUnit, mrModel.mnMinorTimeUnit );
     251             :                     // base time unit
     252           6 :                     if( mrModel.monBaseTimeUnit.has() )
     253           2 :                         aScaleData.TimeIncrement.TimeResolution <<= lclGetApiTimeUnit( mrModel.monBaseTimeUnit.get() );
     254             :                     else
     255           4 :                         aScaleData.TimeIncrement.TimeResolution.clear();
     256             :                 }
     257             :                 else
     258             :                 {
     259             :                     // do not overlap text unless all labels are visible
     260         144 :                     aAxisProp.setProperty( PROP_TextOverlap, mrModel.mnTickLabelSkip == 1 );
     261             :                     // do not break text into several lines
     262         144 :                     aAxisProp.setProperty( PROP_TextBreak, false );
     263             :                     // do not stagger labels in two lines
     264         144 :                     aAxisProp.setProperty( PROP_ArrangeOrder, cssc::ChartAxisArrangeOrderType_SIDE_BY_SIDE );
     265             :                     //! TODO #i58731# show n-th category
     266             :                 }
     267             :             }
     268         150 :             break;
     269             :             case cssc2::AxisType::REALNUMBER:
     270             :             case cssc2::AxisType::PERCENT:
     271             :             {
     272             :                 // scaling algorithm
     273         202 :                 bool bLogScale = lclIsLogarithmicScale( mrModel );
     274         202 :                 if( bLogScale )
     275           2 :                     aScaleData.Scaling = LogarithmicScaling::create( comphelper::getProcessComponentContext() );
     276             :                 else
     277         200 :                     aScaleData.Scaling = LinearScaling::create( comphelper::getProcessComponentContext() );
     278             :                 // min/max
     279         202 :                 lclSetValueOrClearAny( aScaleData.Minimum, mrModel.mofMin );
     280         202 :                 lclSetValueOrClearAny( aScaleData.Maximum, mrModel.mofMax );
     281             :                 // major increment
     282         202 :                 IncrementData& rIncrementData = aScaleData.IncrementData;
     283         202 :                 if( mrModel.mofMajorUnit.has() && aScaleData.Scaling.is() )
     284           2 :                     rIncrementData.Distance <<= aScaleData.Scaling->doScaling( mrModel.mofMajorUnit.get() );
     285             :                 else
     286         200 :                     lclSetValueOrClearAny( rIncrementData.Distance, mrModel.mofMajorUnit );
     287             :                 // minor increment
     288         202 :                 Sequence< SubIncrement >& rSubIncrementSeq = rIncrementData.SubIncrements;
     289         202 :                 rSubIncrementSeq.realloc( 1 );
     290         202 :                 Any& rIntervalCount = rSubIncrementSeq[ 0 ].IntervalCount;
     291         202 :                 rIntervalCount.clear();
     292         202 :                 if( bLogScale )
     293             :                 {
     294           2 :                     if( mrModel.mofMinorUnit.has() )
     295           0 :                         rIntervalCount <<= sal_Int32( 9 );
     296             :                 }
     297         200 :                 else if( mrModel.mofMajorUnit.has() && mrModel.mofMinorUnit.has() && (0.0 < mrModel.mofMinorUnit.get()) && (mrModel.mofMinorUnit.get() <= mrModel.mofMajorUnit.get()) )
     298             :                 {
     299           0 :                     double fCount = mrModel.mofMajorUnit.get() / mrModel.mofMinorUnit.get() + 0.5;
     300           0 :                     if( (1.0 <= fCount) && (fCount < 1001.0) )
     301           0 :                         rIntervalCount <<= static_cast< sal_Int32 >( fCount );
     302             :                 }
     303             :             }
     304         202 :             break;
     305             :             default:
     306             :                 OSL_FAIL( "AxisConverter::convertFromModel - unknown axis type" );
     307             :         }
     308             : 
     309             :         /*  Do not set a value to the Origin member anymore (already done via
     310             :             new axis properties 'CrossoverPosition' and 'CrossoverValue'). */
     311         352 :         aScaleData.Origin.clear();
     312             : 
     313             :         // axis orientation ---------------------------------------------------
     314             : 
     315             :         // #i85167# pie/donut charts need opposite direction at Y axis
     316             :         // #i87747# radar charts need opposite direction at X axis
     317             :         bool bMirrorDirection =
     318         377 :             ((nAxisIdx == API_Y_AXIS) && (rTypeInfo.meTypeCategory == TYPECATEGORY_PIE)) ||
     319         528 :             ((nAxisIdx == API_X_AXIS) && (rTypeInfo.meTypeCategory == TYPECATEGORY_RADAR));
     320         352 :         bool bReverse = (mrModel.mnOrientation == XML_maxMin) != bMirrorDirection;
     321         352 :         aScaleData.Orientation = bReverse ? cssc2::AxisOrientation_REVERSE : cssc2::AxisOrientation_MATHEMATICAL;
     322             : 
     323             :         // write back scaling data
     324         352 :         xAxis->setScaleData( aScaleData );
     325             : 
     326             :         // number format ------------------------------------------------------
     327             : 
     328         352 :         if( (aScaleData.AxisType == cssc2::AxisType::REALNUMBER) || (aScaleData.AxisType == cssc2::AxisType::PERCENT) )
     329         202 :             getFormatter().convertNumberFormat(aAxisProp, mrModel.maNumberFormat, true);
     330             : 
     331             :         // position of crossing axis ------------------------------------------
     332             : 
     333         352 :         bool bManualCrossing = mrModel.mofCrossesAt.has();
     334         352 :         cssc::ChartAxisPosition eAxisPos = cssc::ChartAxisPosition_VALUE;
     335         352 :         if( !bManualCrossing ) switch( mrModel.mnCrossMode )
     336             :         {
     337           0 :             case XML_min:       eAxisPos = cssc::ChartAxisPosition_START;   break;
     338          10 :             case XML_max:       eAxisPos = cssc::ChartAxisPosition_END;     break;
     339         340 :             case XML_autoZero:  eAxisPos = cssc::ChartAxisPosition_ZERO;   break;
     340             :         }
     341         352 :         if( !mrModel.mbAuto )
     342         225 :             aAxisProp.setProperty( PROP_CrossoverPosition, eAxisPos );
     343             : 
     344             :         // calculate automatic origin depending on scaling mode of crossing axis
     345         352 :         bool bCrossingLogScale = pCrossingAxis && lclIsLogarithmicScale( *pCrossingAxis );
     346         352 :         double fCrossingPos = bManualCrossing ? mrModel.mofCrossesAt.get() : (bCrossingLogScale ? 1.0 : 0.0);
     347         352 :         aAxisProp.setProperty( PROP_CrossoverValue, fCrossingPos );
     348             : 
     349             :         // axis title ---------------------------------------------------------
     350             : 
     351             :         // in radar charts, title objects may exist, but are not shown
     352         352 :         if( mrModel.mxTitle.is() && (rTypeGroups.front()->getTypeInfo().meTypeCategory != TYPECATEGORY_RADAR) )
     353             :         {
     354          31 :             Reference< XTitled > xTitled( xAxis, UNO_QUERY_THROW );
     355          62 :             TitleConverter aTitleConv( *this, *mrModel.mxTitle );
     356          62 :             aTitleConv.convertFromModel( xTitled, "Axis Title", OBJECTTYPE_AXISTITLE, nAxesSetIdx, nAxisIdx );
     357             :         }
     358             : 
     359             :         // axis data unit label -----------------------------------------------
     360         352 :         AxisDispUnitsConverter axisDispUnitsConverter (*this, mrModel.mxDispUnits.getOrCreate());
     361         704 :         axisDispUnitsConverter.convertFromModel(xAxis);
     362             :     }
     363           0 :     catch( Exception& )
     364             :     {
     365             :     }
     366             : 
     367         352 :     if( xAxis.is() && rxCoordSystem.is() ) try
     368             :     {
     369             :         // insert axis into coordinate system
     370         352 :         rxCoordSystem->setAxisByDimension( nAxisIdx, xAxis, nAxesSetIdx );
     371             :     }
     372           0 :     catch( Exception& )
     373             :     {
     374             :         OSL_FAIL( "AxisConverter::convertFromModel - cannot insert axis into coordinate system" );
     375         352 :     }
     376             : }
     377             : 
     378         352 : AxisDispUnitsConverter::AxisDispUnitsConverter( const ConverterRoot& rParent, AxisDispUnitsModel& rModel ) :
     379         352 :     ConverterBase< AxisDispUnitsModel >( rParent, rModel )
     380             : {
     381         352 : }
     382             : 
     383         352 : AxisDispUnitsConverter::~AxisDispUnitsConverter()
     384             : {
     385         352 : }
     386             : 
     387         352 : void AxisDispUnitsConverter::convertFromModel( const Reference< XAxis >& rxAxis )
     388             : {
     389         352 :     PropertySet aPropSet( rxAxis );
     390         352 :     if (!(mrModel.mnBuiltInUnit).isEmpty() )
     391             :     {
     392           2 :         aPropSet.setProperty(PROP_DisplayUnits, true);
     393           2 :         aPropSet.setProperty( PROP_BuiltInUnit, mrModel.mnBuiltInUnit );
     394         352 :     }
     395         352 : }
     396             : 
     397             : } // namespace chart
     398             : } // namespace drawingml
     399         246 : } // namespace oox
     400             : 
     401             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11