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