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

Generated by: LCOV version 1.10