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 28 : 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 28 : , m_pPosHelper(pPosHelper)
40 : {
41 28 : }
42 :
43 28 : PolarLabelPositionHelper::~PolarLabelPositionHelper()
44 : {
45 28 : }
46 :
47 0 : awt::Point PolarLabelPositionHelper::getLabelScreenPositionAndAlignmentForLogicValues(
48 : LabelAlignment& rAlignment
49 : , double fLogicValueOnAngleAxis
50 : , double fLogicValueOnRadiusAxis
51 : , double fLogicZ
52 : , sal_Int32 nScreenValueOffsetInRadiusDirection ) const
53 : {
54 0 : double fUnitCircleAngleDegree = m_pPosHelper->transformToAngleDegree( fLogicValueOnAngleAxis );
55 0 : double fUnitCircleRadius = m_pPosHelper->transformToRadius( fLogicValueOnRadiusAxis );
56 :
57 : return getLabelScreenPositionAndAlignmentForUnitCircleValues(
58 : rAlignment, ::com::sun::star::chart::DataLabelPlacement::OUTSIDE
59 : , fUnitCircleAngleDegree, 0.0
60 0 : , fUnitCircleRadius, fUnitCircleRadius, fLogicZ, nScreenValueOffsetInRadiusDirection );
61 : }
62 :
63 28 : 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 28 : && (nLabelPlacement != ::com::sun::star::chart::DataLabelPlacement::INSIDE);
72 :
73 28 : double fAngleDegree = fUnitCircleStartAngleDegree + fUnitCircleWidthAngleDegree/2.0;
74 28 : double fRadius = 0.0;
75 28 : if( !bCenter ) //e.g. for pure pie chart(one ring only) or for angle axis of polyar coordinate system
76 0 : fRadius = fUnitCircleOuterRadius;
77 : else
78 28 : fRadius = fUnitCircleInnerRadius + (fUnitCircleOuterRadius-fUnitCircleInnerRadius)/2.0 ;
79 :
80 : awt::Point aRet( this->transformSceneToScreenPosition(
81 28 : m_pPosHelper->transformUnitCircleToScene( fAngleDegree, fRadius, fLogicZ+0.5 ) ) );
82 :
83 28 : 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 28 : if( !bCenter )
124 : {
125 0 : while(fAngleDegree>360.0)
126 0 : fAngleDegree-=360.0;
127 0 : while(fAngleDegree<0.0)
128 0 : fAngleDegree+=360.0;
129 :
130 0 : bool bOutside = nLabelPlacement == ::com::sun::star::chart::DataLabelPlacement::OUTSIDE;
131 :
132 0 : if(fAngleDegree==0.0)
133 0 : rAlignment = LABEL_ALIGN_CENTER;
134 0 : else if(fAngleDegree<=22.5)
135 0 : rAlignment = bOutside ? LABEL_ALIGN_RIGHT : LABEL_ALIGN_LEFT;
136 0 : else if(fAngleDegree<67.5)
137 0 : rAlignment = bOutside ? LABEL_ALIGN_RIGHT_TOP : LABEL_ALIGN_LEFT_BOTTOM;
138 0 : else if(fAngleDegree<112.5)
139 0 : rAlignment = bOutside ? LABEL_ALIGN_TOP : LABEL_ALIGN_BOTTOM;
140 0 : else if(fAngleDegree<=157.5)
141 0 : rAlignment = bOutside ? LABEL_ALIGN_LEFT_TOP : LABEL_ALIGN_RIGHT_BOTTOM;
142 0 : else if(fAngleDegree<=202.5)
143 0 : rAlignment = bOutside ? LABEL_ALIGN_LEFT : LABEL_ALIGN_RIGHT;
144 0 : else if(fAngleDegree<247.5)
145 0 : rAlignment = bOutside ? LABEL_ALIGN_LEFT_BOTTOM : LABEL_ALIGN_RIGHT_TOP;
146 0 : else if(fAngleDegree<292.5)
147 0 : rAlignment = bOutside ? LABEL_ALIGN_BOTTOM : LABEL_ALIGN_TOP;
148 0 : else if(fAngleDegree<337.5)
149 0 : 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 28 : rAlignment = LABEL_ALIGN_CENTER;
156 : }
157 :
158 : //add a scaling independent Offset if requested
159 28 : if( nScreenValueOffsetInRadiusDirection != 0)
160 : {
161 : awt::Point aOrigin( this->transformSceneToScreenPosition(
162 0 : m_pPosHelper->transformUnitCircleToScene( 0.0, 0.0, fLogicZ+0.5 ) ) );
163 0 : basegfx::B2IVector aDirection( aRet.X- aOrigin.X, aRet.Y- aOrigin.Y );
164 0 : aDirection.setLength(nScreenValueOffsetInRadiusDirection);
165 0 : aRet.X += aDirection.getX();
166 0 : aRet.Y += aDirection.getY();
167 : }
168 :
169 28 : return aRet;
170 : }
171 :
172 : } //namespace chart
173 :
174 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|