LCOV - code coverage report
Current view: top level - chart2/source/view/main - PolarLabelPositionHelper.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 44 72 61.1 %
Date: 2015-06-13 12:38:46 Functions: 4 5 80.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 "PolarLabelPositionHelper.hxx"
      21             : #include "PlottingPositionHelper.hxx"
      22             : #include "CommonConverters.hxx"
      23             : #include <basegfx/vector/b2dvector.hxx>
      24             : #include <basegfx/vector/b2ivector.hxx>
      25             : 
      26             : #include <com/sun/star/chart/DataLabelPlacement.hpp>
      27             : 
      28             : namespace chart
      29             : {
      30             : using namespace ::com::sun::star;
      31             : using namespace ::com::sun::star::chart2;
      32             : 
      33         162 : PolarLabelPositionHelper::PolarLabelPositionHelper(
      34             :                     PolarPlottingPositionHelper* pPosHelper
      35             :                     , sal_Int32 nDimensionCount
      36             :                     , const uno::Reference< drawing::XShapes >& xLogicTarget
      37             :                     , AbstractShapeFactory* pShapeFactory )
      38             :                     : LabelPositionHelper( pPosHelper, nDimensionCount, xLogicTarget, pShapeFactory )
      39         162 :                     , m_pPosHelper(pPosHelper)
      40             : {
      41         162 : }
      42             : 
      43         162 : PolarLabelPositionHelper::~PolarLabelPositionHelper()
      44             : {
      45         162 : }
      46             : 
      47          68 : awt::Point PolarLabelPositionHelper::getLabelScreenPositionAndAlignmentForLogicValues(
      48             :         LabelAlignment& rAlignment
      49             :         , double fLogicValueOnAngleAxis
      50             :         , double fLogicValueOnRadiusAxis
      51             :         , double fLogicZ
      52             :         , sal_Int32 nScreenValueOffsetInRadiusDirection ) const
      53             : {
      54          68 :     double fUnitCircleAngleDegree = m_pPosHelper->transformToAngleDegree( fLogicValueOnAngleAxis );
      55          68 :     double fUnitCircleRadius = m_pPosHelper->transformToRadius( fLogicValueOnRadiusAxis );
      56             : 
      57             :     return getLabelScreenPositionAndAlignmentForUnitCircleValues(
      58             :            rAlignment, ::com::sun::star::chart::DataLabelPlacement::OUTSIDE
      59             :            , fUnitCircleAngleDegree, 0.0
      60          68 :            , fUnitCircleRadius, fUnitCircleRadius, fLogicZ, nScreenValueOffsetInRadiusDirection );
      61             : }
      62             : 
      63         162 : awt::Point PolarLabelPositionHelper::getLabelScreenPositionAndAlignmentForUnitCircleValues(
      64             :         LabelAlignment& rAlignment, sal_Int32 nLabelPlacement
      65             :         , double fUnitCircleStartAngleDegree, double fUnitCircleWidthAngleDegree
      66             :         , double fUnitCircleInnerRadius, double fUnitCircleOuterRadius
      67             :         , double fLogicZ
      68             :         , sal_Int32 nScreenValueOffsetInRadiusDirection ) const
      69             : {
      70             :     bool bCenter = (nLabelPlacement != ::com::sun::star::chart::DataLabelPlacement::OUTSIDE)
      71         162 :                 && (nLabelPlacement != ::com::sun::star::chart::DataLabelPlacement::INSIDE);
      72             : 
      73         162 :     double fAngleDegree = fUnitCircleStartAngleDegree + fUnitCircleWidthAngleDegree/2.0;
      74         162 :     double fRadius = 0.0;
      75         162 :     if( !bCenter ) //e.g. for pure pie chart(one ring only) or for angle axis of polyar coordinate system
      76          68 :         fRadius = fUnitCircleOuterRadius;
      77             :     else
      78          94 :         fRadius = fUnitCircleInnerRadius + (fUnitCircleOuterRadius-fUnitCircleInnerRadius)/2.0 ;
      79             : 
      80             :     awt::Point aRet( this->transformSceneToScreenPosition(
      81         162 :         m_pPosHelper->transformUnitCircleToScene( fAngleDegree, fRadius, fLogicZ+0.5 ) ) );
      82             : 
      83         162 :     if(3==m_nDimensionCount && nLabelPlacement == ::com::sun::star::chart::DataLabelPlacement::OUTSIDE)
      84             :     {
      85             :         //check whether the upper or the downer edge is more distant from the center
      86             :         //take the farest point to put the label to
      87             : 
      88             :         awt::Point aP0( this->transformSceneToScreenPosition(
      89           0 :             m_pPosHelper->transformUnitCircleToScene( 0, 0, fLogicZ ) ) );
      90           0 :         awt::Point aP1(aRet);
      91             :         awt::Point aP2( this->transformSceneToScreenPosition(
      92           0 :             m_pPosHelper->transformUnitCircleToScene( fAngleDegree, fRadius, fLogicZ-0.5 ) ) );
      93             : 
      94           0 :         ::basegfx::B2DVector aV0( aP0.X, aP0.Y );
      95           0 :         ::basegfx::B2DVector aV1( aP1.X, aP1.Y );
      96           0 :         ::basegfx::B2DVector aV2( aP2.X, aP2.Y );
      97             : 
      98           0 :         double fL1 = ::basegfx::B2DVector(aV1-aV0).getLength();
      99           0 :         double fL2 = ::basegfx::B2DVector(aV2-aV0).getLength();
     100             : 
     101           0 :         if(fL2>fL1)
     102           0 :             aRet = aP2;
     103             : 
     104             :         //calculate new angle for alignment
     105           0 :         double fDX = aRet.X-aP0.X;
     106           0 :         double fDY = aRet.Y-aP0.Y;
     107           0 :         fDY*=-1.0;//drawing layer has inverse y values
     108           0 :         if( fDX != 0.0 )
     109             :         {
     110           0 :             fAngleDegree = atan(fDY/fDX)*180.0/F_PI;
     111           0 :             if(fDX<0.0)
     112           0 :                 fAngleDegree+=180.0;
     113             :         }
     114             :         else
     115             :         {
     116           0 :             if(fDY>0.0)
     117           0 :                 fAngleDegree = 90.0;
     118             :             else
     119           0 :                 fAngleDegree = 270.0;
     120           0 :         }
     121             :     }
     122             :     //set LabelAlignment
     123         162 :     if( !bCenter )
     124             :     {
     125         136 :         while(fAngleDegree>360.0)
     126           0 :             fAngleDegree-=360.0;
     127         136 :         while(fAngleDegree<0.0)
     128           0 :             fAngleDegree+=360.0;
     129             : 
     130          68 :         bool bOutside = nLabelPlacement == ::com::sun::star::chart::DataLabelPlacement::OUTSIDE;
     131             : 
     132          68 :         if(fAngleDegree==0.0)
     133           0 :             rAlignment = LABEL_ALIGN_CENTER;
     134          68 :         else if(fAngleDegree<=22.5)
     135          12 :             rAlignment = bOutside ? LABEL_ALIGN_RIGHT : LABEL_ALIGN_LEFT;
     136          56 :         else if(fAngleDegree<67.5)
     137           0 :             rAlignment = bOutside ? LABEL_ALIGN_RIGHT_TOP : LABEL_ALIGN_LEFT_BOTTOM;
     138          56 :         else if(fAngleDegree<112.5)
     139          20 :             rAlignment = bOutside ? LABEL_ALIGN_TOP : LABEL_ALIGN_BOTTOM;
     140          36 :         else if(fAngleDegree<=157.5)
     141           0 :             rAlignment = bOutside ? LABEL_ALIGN_LEFT_TOP : LABEL_ALIGN_RIGHT_BOTTOM;
     142          36 :         else if(fAngleDegree<=202.5)
     143          12 :             rAlignment = bOutside ? LABEL_ALIGN_LEFT : LABEL_ALIGN_RIGHT;
     144          24 :         else if(fAngleDegree<247.5)
     145          12 :             rAlignment = bOutside ? LABEL_ALIGN_LEFT_BOTTOM : LABEL_ALIGN_RIGHT_TOP;
     146          12 :         else if(fAngleDegree<292.5)
     147           0 :             rAlignment = bOutside ? LABEL_ALIGN_BOTTOM : LABEL_ALIGN_TOP;
     148          12 :         else if(fAngleDegree<337.5)
     149          12 :             rAlignment = bOutside ? LABEL_ALIGN_RIGHT_BOTTOM : LABEL_ALIGN_LEFT_TOP;
     150             :         else
     151           0 :             rAlignment = bOutside ? LABEL_ALIGN_RIGHT : LABEL_ALIGN_LEFT;
     152             :     }
     153             :     else
     154             :     {
     155          94 :         rAlignment = LABEL_ALIGN_CENTER;
     156             :     }
     157             : 
     158             :     //add a scaling independent Offset if requested
     159         162 :     if( nScreenValueOffsetInRadiusDirection != 0)
     160             :     {
     161             :         awt::Point aOrigin( this->transformSceneToScreenPosition(
     162          68 :             m_pPosHelper->transformUnitCircleToScene( 0.0, 0.0, fLogicZ+0.5 ) ) );
     163          68 :         basegfx::B2IVector aDirection( aRet.X- aOrigin.X, aRet.Y- aOrigin.Y );
     164          68 :         aDirection.setLength(nScreenValueOffsetInRadiusDirection);
     165          68 :         aRet.X += aDirection.getX();
     166          68 :         aRet.Y += aDirection.getY();
     167             :     }
     168             : 
     169         162 :     return aRet;
     170             : }
     171             : 
     172             : } //namespace chart
     173             : 
     174             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11