1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PolarLabelPositionHelper.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
34 #include "PolarLabelPositionHelper.hxx"
35 #include "PlottingPositionHelper.hxx"
36 #include "CommonConverters.hxx"
37 #include <basegfx/vector/b2dvector.hxx>
38 #include <basegfx/vector/b2ivector.hxx>
40 #include <com/sun/star/chart/DataLabelPlacement.hpp>
42 //.............................................................................
45 //.............................................................................
46 using namespace ::com::sun::star
;
47 using namespace ::com::sun::star::chart2
;
49 PolarLabelPositionHelper::PolarLabelPositionHelper(
50 PolarPlottingPositionHelper
* pPosHelper
51 , sal_Int32 nDimensionCount
52 , const uno::Reference
< drawing::XShapes
>& xLogicTarget
53 , ShapeFactory
* pShapeFactory
)
54 : LabelPositionHelper( pPosHelper
, nDimensionCount
, xLogicTarget
, pShapeFactory
)
55 , m_pPosHelper(pPosHelper
)
59 PolarLabelPositionHelper::~PolarLabelPositionHelper()
63 awt::Point
PolarLabelPositionHelper::getLabelScreenPositionAndAlignmentForLogicValues(
64 LabelAlignment
& rAlignment
65 , double fLogicValueOnAngleAxis
66 , double fLogicValueOnRadiusAxis
68 , sal_Int32 nScreenValueOffsetInRadiusDirection
) const
70 double fUnitCircleAngleDegree
= m_pPosHelper
->transformToAngleDegree( fLogicValueOnAngleAxis
);
71 double fUnitCircleRadius
= m_pPosHelper
->transformToRadius( fLogicValueOnRadiusAxis
);
73 return getLabelScreenPositionAndAlignmentForUnitCircleValues(
74 rAlignment
, ::com::sun::star::chart::DataLabelPlacement::OUTSIDE
75 , fUnitCircleAngleDegree
, 0.0
76 , fUnitCircleRadius
, fUnitCircleRadius
, fLogicZ
, nScreenValueOffsetInRadiusDirection
);
79 awt::Point
PolarLabelPositionHelper::getLabelScreenPositionAndAlignmentForUnitCircleValues(
80 LabelAlignment
& rAlignment
, sal_Int32 nLabelPlacement
81 , double fUnitCircleStartAngleDegree
, double fUnitCircleWidthAngleDegree
82 , double fUnitCircleInnerRadius
, double fUnitCircleOuterRadius
84 , sal_Int32 nScreenValueOffsetInRadiusDirection
) const
86 bool bCenter
= (nLabelPlacement
!= ::com::sun::star::chart::DataLabelPlacement::OUTSIDE
)
87 && (nLabelPlacement
!= ::com::sun::star::chart::DataLabelPlacement::INSIDE
);
89 double fAngleDegree
= fUnitCircleStartAngleDegree
+ fUnitCircleWidthAngleDegree
/2.0;
91 if( !bCenter
) //e.g. for pure pie chart(one ring only) or for angle axis of polyar coordinate system
92 fRadius
= fUnitCircleOuterRadius
;
94 fRadius
= fUnitCircleInnerRadius
+ (fUnitCircleOuterRadius
-fUnitCircleInnerRadius
)/2.0 ;
96 awt::Point
aRet( this->transformSceneToScreenPosition(
97 m_pPosHelper
->transformUnitCircleToScene( fAngleDegree
, fRadius
, fLogicZ
+0.5 ) ) );
99 if(3==m_nDimensionCount
&& nLabelPlacement
== ::com::sun::star::chart::DataLabelPlacement::OUTSIDE
)
101 //check wether the upper or the downer edge is more distant from the center
102 //take the farest point to put the label to
104 awt::Point
aP0( this->transformSceneToScreenPosition(
105 m_pPosHelper
->transformUnitCircleToScene( 0, 0, fLogicZ
) ) );
106 awt::Point
aP1(aRet
);
107 awt::Point
aP2( this->transformSceneToScreenPosition(
108 m_pPosHelper
->transformUnitCircleToScene( fAngleDegree
, fRadius
, fLogicZ
-0.5 ) ) );
110 ::basegfx::B2DVector
aV0( aP0
.X
, aP0
.Y
);
111 ::basegfx::B2DVector
aV1( aP1
.X
, aP1
.Y
);
112 ::basegfx::B2DVector
aV2( aP2
.X
, aP2
.Y
);
114 double fL1
= ::basegfx::B2DVector(aV1
-aV0
).getLength();
115 double fL2
= ::basegfx::B2DVector(aV2
-aV0
).getLength();
120 //calculate new angle for alignment
121 double fDX
= aRet
.X
-aP0
.X
;
122 double fDY
= aRet
.Y
-aP0
.Y
;
123 fDY
*=-1.0;//drawing layer has inverse y values
126 fAngleDegree
= atan(fDY
/fDX
)*180.0/F_PI
;
135 fAngleDegree
= 270.0;
138 //------------------------------
142 while(fAngleDegree
>360.0)
144 while(fAngleDegree
<0.0)
147 bool bOutside
= nLabelPlacement
== ::com::sun::star::chart::DataLabelPlacement::OUTSIDE
;
149 if(fAngleDegree
==0.0)
150 rAlignment
= LABEL_ALIGN_CENTER
;
151 else if(fAngleDegree
<=22.5)
152 rAlignment
= bOutside
? LABEL_ALIGN_RIGHT
: LABEL_ALIGN_LEFT
;
153 else if(fAngleDegree
<67.5)
154 rAlignment
= bOutside
? LABEL_ALIGN_RIGHT_TOP
: LABEL_ALIGN_LEFT_BOTTOM
;
155 else if(fAngleDegree
<112.5)
156 rAlignment
= bOutside
? LABEL_ALIGN_TOP
: LABEL_ALIGN_BOTTOM
;
157 else if(fAngleDegree
<=157.5)
158 rAlignment
= bOutside
? LABEL_ALIGN_LEFT_TOP
: LABEL_ALIGN_RIGHT_BOTTOM
;
159 else if(fAngleDegree
<=202.5)
160 rAlignment
= bOutside
? LABEL_ALIGN_LEFT
: LABEL_ALIGN_RIGHT
;
161 else if(fAngleDegree
<247.5)
162 rAlignment
= bOutside
? LABEL_ALIGN_LEFT_BOTTOM
: LABEL_ALIGN_RIGHT_TOP
;
163 else if(fAngleDegree
<292.5)
164 rAlignment
= bOutside
? LABEL_ALIGN_BOTTOM
: LABEL_ALIGN_TOP
;
165 else if(fAngleDegree
<337.5)
166 rAlignment
= bOutside
? LABEL_ALIGN_RIGHT_BOTTOM
: LABEL_ALIGN_LEFT_TOP
;
168 rAlignment
= bOutside
? LABEL_ALIGN_RIGHT
: LABEL_ALIGN_LEFT
;
172 rAlignment
= LABEL_ALIGN_CENTER
;
175 //add a scaling independent Offset if requested
176 if( nScreenValueOffsetInRadiusDirection
!= 0)
178 awt::Point
aOrigin( this->transformSceneToScreenPosition(
179 m_pPosHelper
->transformUnitCircleToScene( 0.0, 0.0, fLogicZ
+0.5 ) ) );
180 basegfx::B2IVector
aDirection( aRet
.X
- aOrigin
.X
, aRet
.Y
- aOrigin
.Y
);
181 aDirection
.setLength(nScreenValueOffsetInRadiusDirection
);
182 aRet
.X
+= aDirection
.getX();
183 aRet
.Y
+= aDirection
.getY();
189 //.............................................................................
191 //.............................................................................