LibreOffice Module chart2 (master) 1
PolarLabelPositionHelper.cxx
Go to the documentation of this file.
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
24
25#include <com/sun/star/chart/DataLabelPlacement.hpp>
26
27namespace chart
28{
29using namespace ::com::sun::star;
30using namespace ::com::sun::star::chart2;
31
34 , sal_Int32 nDimensionCount
35 , const rtl::Reference<SvxShapeGroupAnyD>& xLogicTarget )
36 : LabelPositionHelper( nDimensionCount, xLogicTarget )
37 , m_pPosHelper(pPosHelper)
38{
39}
40
42{
43}
44
46 LabelAlignment& rAlignment
47 , double fLogicValueOnAngleAxis
48 , double fLogicValueOnRadiusAxis
49 , double fLogicZ
50 , sal_Int32 nScreenValueOffsetInRadiusDirection ) const
51{
52 double fUnitCircleAngleDegree = m_pPosHelper->transformToAngleDegree( fLogicValueOnAngleAxis );
53 double fUnitCircleRadius = m_pPosHelper->transformToRadius( fLogicValueOnRadiusAxis );
54
56 rAlignment, css::chart::DataLabelPlacement::OUTSIDE
57 , fUnitCircleAngleDegree, 0.0
58 , fUnitCircleRadius, fUnitCircleRadius, fLogicZ, nScreenValueOffsetInRadiusDirection );
59}
60
62 LabelAlignment& rAlignment, sal_Int32 nLabelPlacement
63 , double fUnitCircleStartAngleDegree, double fUnitCircleWidthAngleDegree
64 , double fUnitCircleInnerRadius, double fUnitCircleOuterRadius
65 , double fLogicZ
66 , sal_Int32 nScreenValueOffsetInRadiusDirection ) const
67{
68 bool bCenter = (nLabelPlacement != css::chart::DataLabelPlacement::OUTSIDE)
69 && (nLabelPlacement != css::chart::DataLabelPlacement::INSIDE);
70
71 double fAngleDegree = fUnitCircleStartAngleDegree + fUnitCircleWidthAngleDegree/2.0;
72 double fRadius = 0.0;
73 if( !bCenter ) //e.g. for pure pie chart(one ring only) or for angle axis of polar coordinate system
74 fRadius = fUnitCircleOuterRadius;
75 else
76 fRadius = fUnitCircleInnerRadius + (fUnitCircleOuterRadius-fUnitCircleInnerRadius)/2.0 ;
77
78 awt::Point aRet( transformSceneToScreenPosition(
79 m_pPosHelper->transformUnitCircleToScene( fAngleDegree, fRadius, fLogicZ+0.5 ) ) );
80
81 if(m_nDimensionCount==3 && nLabelPlacement == css::chart::DataLabelPlacement::OUTSIDE)
82 {
83 //check whether the upper or the downer edge is more distant from the center
84 //take the farthest point to put the label to
85
86 awt::Point aP0( transformSceneToScreenPosition(
87 m_pPosHelper->transformUnitCircleToScene( 0, 0, fLogicZ ) ) );
88 awt::Point aP1(aRet);
89 awt::Point aP2( transformSceneToScreenPosition(
90 m_pPosHelper->transformUnitCircleToScene( fAngleDegree, fRadius, fLogicZ-0.5 ) ) );
91
92 ::basegfx::B2DVector aV0( aP0.X, aP0.Y );
93 ::basegfx::B2DVector aV1( aP1.X, aP1.Y );
94 ::basegfx::B2DVector aV2( aP2.X, aP2.Y );
95
96 double fL1 = ::basegfx::B2DVector(aV1-aV0).getLength();
97 double fL2 = ::basegfx::B2DVector(aV2-aV0).getLength();
98
99 if(fL2>fL1)
100 aRet = aP2;
101
102 //calculate new angle for alignment
103 double fDX = aRet.X-aP0.X;
104 double fDY = aRet.Y-aP0.Y;
105 fDY*=-1.0;//drawing layer has inverse y values
106
107 fAngleDegree = basegfx::rad2deg(atan2(fDY,fDX));
108 }
109 //set LabelAlignment
110 if( !bCenter )
111 {
112 // tdf#123504: both 0 and 360 are valid and different values here!
113 while (fAngleDegree > 360.0)
114 fAngleDegree -= 360.0;
115 while (fAngleDegree < 0.0)
116 fAngleDegree += 360.0;
117
118 bool bOutside = nLabelPlacement == css::chart::DataLabelPlacement::OUTSIDE;
119
120 if (fAngleDegree <= 5 || fAngleDegree >= 355)
121 rAlignment = bOutside ? LABEL_ALIGN_RIGHT : LABEL_ALIGN_LEFT;
122 else if (fAngleDegree < 85)
123 rAlignment = bOutside ? LABEL_ALIGN_RIGHT_TOP : LABEL_ALIGN_LEFT_BOTTOM;
124 else if (fAngleDegree <= 95)
125 rAlignment = bOutside ? LABEL_ALIGN_TOP : LABEL_ALIGN_BOTTOM;
126 else if (fAngleDegree < 175)
127 rAlignment = bOutside ? LABEL_ALIGN_LEFT_TOP : LABEL_ALIGN_RIGHT_BOTTOM;
128 else if (fAngleDegree <= 185)
129 rAlignment = bOutside ? LABEL_ALIGN_LEFT : LABEL_ALIGN_RIGHT;
130 else if (fAngleDegree < 265)
131 rAlignment = bOutside ? LABEL_ALIGN_LEFT_BOTTOM : LABEL_ALIGN_RIGHT_TOP;
132 else if (fAngleDegree <= 275)
133 rAlignment = bOutside ? LABEL_ALIGN_BOTTOM : LABEL_ALIGN_TOP;
134 else
135 rAlignment = bOutside ? LABEL_ALIGN_RIGHT_BOTTOM : LABEL_ALIGN_LEFT_TOP;
136 }
137 else
138 {
139 rAlignment = LABEL_ALIGN_CENTER;
140 }
141
142 //add a scaling independent Offset if requested
143 if( nScreenValueOffsetInRadiusDirection != 0)
144 {
145 awt::Point aOrigin( transformSceneToScreenPosition(
146 m_pPosHelper->transformUnitCircleToScene( 0.0, 0.0, fLogicZ+0.5 ) ) );
147 basegfx::B2IVector aDirection( aRet.X- aOrigin.X, aRet.Y- aOrigin.Y );
148 aDirection.setLength(nScreenValueOffsetInRadiusDirection);
149 aRet.X += aDirection.getX();
150 aRet.Y += aDirection.getY();
151 }
152
153 return aRet;
154}
155
156} //namespace chart
157
158/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
double getLength() const
css::awt::Point transformSceneToScreenPosition(const css::drawing::Position3D &rScenePosition3D) const
PolarLabelPositionHelper(PolarPlottingPositionHelper *pPosHelper, sal_Int32 nDimensionCount, const rtl::Reference< SvxShapeGroupAnyD > &xLogicTarget)
PolarPlottingPositionHelper * m_pPosHelper
css::awt::Point getLabelScreenPositionAndAlignmentForUnitCircleValues(LabelAlignment &rAlignment, sal_Int32 nLabelPlacement, double fUnitCircleStartAngleDegree, double fUnitCircleWidthAngleDegree, double fUnitCircleInnerRadius, double fUnitCircleOuterRadius, double fLogicZ, sal_Int32 nScreenValueOffsetInRadiusDirection) const
Calculate the anchor point position for a text label.
css::awt::Point getLabelScreenPositionAndAlignmentForLogicValues(LabelAlignment &rAlignment, double fLogicValueOnAngleAxis, double fLogicValueOnRadiusAxis, double fLogicZ, sal_Int32 nScreenValueOffsetInRadiusDirection) const
double transformToAngleDegree(double fLogicValueOnAngleAxis, bool bDoScaling=true) const
Given a value in the angle axis scale range (e.g.
css::drawing::Position3D transformUnitCircleToScene(double fUnitAngleDegree, double fUnitRadius, double fLogicZ) const
It returns the scene coordinates of the passed point: this point is described through a normalized cy...
double transformToRadius(double fLogicValueOnRadiusAxis, bool bDoScaling=true) const
Given a value in the radius axis scale range, it returns the normalized value.
constexpr double rad2deg(double v)
@ LABEL_ALIGN_CENTER
@ LABEL_ALIGN_RIGHT_TOP
@ LABEL_ALIGN_RIGHT_BOTTOM
@ LABEL_ALIGN_TOP
@ LABEL_ALIGN_LEFT_BOTTOM
@ LABEL_ALIGN_LEFT_TOP
@ LABEL_ALIGN_RIGHT
@ LABEL_ALIGN_LEFT
@ LABEL_ALIGN_BOTTOM
css::drawing::Direction3D aDirection