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

Generated by: LCOV version 1.10