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