1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "Tickmarks.hxx"
21 #include "Tickmarks_Equidistant.hxx"
22 #include "Tickmarks_Dates.hxx"
23 #include <ViewDefines.hxx>
24 #include <rtl/math.hxx>
26 using namespace ::com::sun::star
;
27 using namespace ::rtl::math
;
28 using ::basegfx::B2DVector
;
32 TickInfo::TickInfo( const uno::Reference
<chart2::XScaling
>& xInverse
)
33 : fScaledTickValue( 0.0 )
34 , xInverseScaling( xInverse
)
35 , aTickScreenPosition(0.0,0.0)
37 , xTextShape( nullptr )
38 , nFactorForLimitedTextWidth(1)
42 double TickInfo::getUnscaledTickValue() const
44 if( xInverseScaling
.is() )
45 return xInverseScaling
->doScaling( fScaledTickValue
);
47 return fScaledTickValue
;
50 sal_Int32
TickInfo::getScreenDistanceBetweenTicks( const TickInfo
& rOherTickInfo
) const
52 //return the positive distance between the two first tickmarks in screen values
54 B2DVector aDistance
= rOherTickInfo
.aTickScreenPosition
- aTickScreenPosition
;
55 sal_Int32 nRet
= static_cast<sal_Int32
>(aDistance
.getLength());
61 PureTickIter::PureTickIter( TickInfoArrayType
& rTickInfoVector
)
62 : m_rTickVector(rTickInfoVector
)
63 , m_aTickIter(m_rTickVector
.begin())
66 PureTickIter::~PureTickIter()
69 TickInfo
* PureTickIter::firstInfo()
71 m_aTickIter
= m_rTickVector
.begin();
72 if(m_aTickIter
!=m_rTickVector
.end())
76 TickInfo
* PureTickIter::nextInfo()
78 if(m_aTickIter
!=m_rTickVector
.end())
81 if(m_aTickIter
!=m_rTickVector
.end())
87 TickFactory::TickFactory(
88 const ExplicitScaleData
& rScale
, const ExplicitIncrementData
& rIncrement
)
90 , m_rIncrement( rIncrement
)
91 , m_xInverseScaling(nullptr)
93 //@todo: make sure that the scale is valid for the scaling
95 if( m_rScale
.Scaling
.is() )
97 m_xInverseScaling
= m_rScale
.Scaling
->getInverseScaling();
98 OSL_ENSURE( m_xInverseScaling
.is(), "each Scaling needs to return a inverse Scaling" );
101 m_fScaledVisibleMin
= m_rScale
.Minimum
;
102 if( m_xInverseScaling
.is() )
103 m_fScaledVisibleMin
= m_rScale
.Scaling
->doScaling(m_fScaledVisibleMin
);
105 m_fScaledVisibleMax
= m_rScale
.Maximum
;
106 if( m_xInverseScaling
.is() )
107 m_fScaledVisibleMax
= m_rScale
.Scaling
->doScaling(m_fScaledVisibleMax
);
110 TickFactory::~TickFactory()
114 bool TickFactory::isDateAxis() const
116 return m_rScale
.AxisType
== chart2::AxisType::DATE
;
119 void TickFactory::getAllTicks( TickInfoArraysType
& rAllTickInfos
) const
122 DateTickFactory( m_rScale
, m_rIncrement
).getAllTicks( rAllTickInfos
);
124 EquidistantTickFactory( m_rScale
, m_rIncrement
).getAllTicks( rAllTickInfos
);
127 void TickFactory::getAllTicksShifted( TickInfoArraysType
& rAllTickInfos
) const
130 DateTickFactory( m_rScale
, m_rIncrement
).getAllTicksShifted( rAllTickInfos
);
132 EquidistantTickFactory( m_rScale
, m_rIncrement
).getAllTicksShifted( rAllTickInfos
);
135 // ___TickFactory_2D___
136 TickFactory2D::TickFactory2D(
137 const ExplicitScaleData
& rScale
, const ExplicitIncrementData
& rIncrement
138 //, double fStretch_SceneToScreen, double fOffset_SceneToScreen )
139 , const B2DVector
& rStartScreenPos
, const B2DVector
& rEndScreenPos
140 , const B2DVector
& rAxisLineToLabelLineShift
)
141 : TickFactory( rScale
, rIncrement
)
142 , m_aAxisStartScreenPosition2D(rStartScreenPos
)
143 , m_aAxisEndScreenPosition2D(rEndScreenPos
)
144 , m_aAxisLineToLabelLineShift(rAxisLineToLabelLineShift
)
145 , m_fStretch_LogicToScreen(1.0)
146 , m_fOffset_LogicToScreen(0.0)
148 double fWidthY
= m_fScaledVisibleMax
- m_fScaledVisibleMin
;
149 if (m_rScale
.Orientation
== chart2::AxisOrientation_MATHEMATICAL
)
151 m_fStretch_LogicToScreen
= 1.0/fWidthY
;
152 m_fOffset_LogicToScreen
= -m_fScaledVisibleMin
;
156 B2DVector
aSwap(m_aAxisStartScreenPosition2D
);
157 m_aAxisStartScreenPosition2D
= m_aAxisEndScreenPosition2D
;
158 m_aAxisEndScreenPosition2D
= aSwap
;
160 m_fStretch_LogicToScreen
= -1.0/fWidthY
;
161 m_fOffset_LogicToScreen
= -m_fScaledVisibleMax
;
165 TickFactory2D::~TickFactory2D()
169 bool TickFactory2D::isHorizontalAxis() const
171 // check trivial cases:
172 if ( m_aAxisStartScreenPosition2D
.getY() == m_aAxisEndScreenPosition2D
.getY() )
174 if ( m_aAxisStartScreenPosition2D
.getX() == m_aAxisEndScreenPosition2D
.getX() )
177 // for skew axes compare angle with horizontal vector
178 double fInclination
= std::abs(B2DVector(m_aAxisEndScreenPosition2D
-m_aAxisStartScreenPosition2D
).angle(B2DVector(1.0, 0.0)));
179 return fInclination
< F_PI4
|| fInclination
> (F_PI
-F_PI4
);
181 bool TickFactory2D::isVerticalAxis() const
183 // check trivial cases:
184 if ( m_aAxisStartScreenPosition2D
.getX() == m_aAxisEndScreenPosition2D
.getX() )
186 if ( m_aAxisStartScreenPosition2D
.getY() == m_aAxisEndScreenPosition2D
.getY() )
189 // for skew axes compare angle with vertical vector
190 double fInclination
= std::abs(B2DVector(m_aAxisEndScreenPosition2D
-m_aAxisStartScreenPosition2D
).angle(B2DVector(0.0, -1.0)));
191 return fInclination
< F_PI4
|| fInclination
> (F_PI
-F_PI4
);
194 sal_Int32
TickFactory2D::getTickScreenDistance( TickIter
& rIter
)
196 //return the positive distance between the two first tickmarks in screen values
197 //if there are less than two tickmarks -1 is returned
199 const TickInfo
* pFirstTickInfo
= rIter
.firstInfo();
200 const TickInfo
* pSecondTickInfo
= rIter
.nextInfo();
201 if(!pSecondTickInfo
|| !pFirstTickInfo
)
204 return pFirstTickInfo
->getScreenDistanceBetweenTicks( *pSecondTickInfo
);
207 B2DVector
TickFactory2D::getTickScreenPosition2D( double fScaledLogicTickValue
) const
209 B2DVector
aRet(m_aAxisStartScreenPosition2D
);
210 aRet
+= (m_aAxisEndScreenPosition2D
-m_aAxisStartScreenPosition2D
)
211 *((fScaledLogicTickValue
+m_fOffset_LogicToScreen
)*m_fStretch_LogicToScreen
);
215 void TickFactory2D::addPointSequenceForTickLine( drawing::PointSequenceSequence
& rPoints
216 , sal_Int32 nSequenceIndex
217 , double fScaledLogicTickValue
, double fInnerDirectionSign
218 , const TickmarkProperties
& rTickmarkProperties
219 , bool bPlaceAtLabels
) const
221 if( fInnerDirectionSign
==0.0 )
222 fInnerDirectionSign
= 1.0;
224 B2DVector aTickScreenPosition
= getTickScreenPosition2D(fScaledLogicTickValue
);
226 aTickScreenPosition
+= m_aAxisLineToLabelLineShift
;
228 B2DVector aMainDirection
= m_aAxisEndScreenPosition2D
-m_aAxisStartScreenPosition2D
;
229 aMainDirection
.normalize();
230 B2DVector
aOrthoDirection(-aMainDirection
.getY(),aMainDirection
.getX());
231 aOrthoDirection
*= fInnerDirectionSign
;
232 aOrthoDirection
.normalize();
234 B2DVector aStart
= aTickScreenPosition
+ aOrthoDirection
*rTickmarkProperties
.RelativePos
;
235 B2DVector aEnd
= aStart
- aOrthoDirection
*rTickmarkProperties
.Length
;
237 rPoints
[nSequenceIndex
].realloc(2);
238 rPoints
[nSequenceIndex
][0].X
= static_cast<sal_Int32
>(aStart
.getX());
239 rPoints
[nSequenceIndex
][0].Y
= static_cast<sal_Int32
>(aStart
.getY());
240 rPoints
[nSequenceIndex
][1].X
= static_cast<sal_Int32
>(aEnd
.getX());
241 rPoints
[nSequenceIndex
][1].Y
= static_cast<sal_Int32
>(aEnd
.getY());
244 B2DVector
TickFactory2D::getDistanceAxisTickToText( const AxisProperties
& rAxisProperties
, bool bIncludeFarAwayDistanceIfSo
, bool bIncludeSpaceBetweenTickAndText
) const
246 bool bFarAwayLabels
= false;
247 if( rAxisProperties
.m_eLabelPos
== css::chart::ChartAxisLabelPosition_OUTSIDE_START
248 || rAxisProperties
.m_eLabelPos
== css::chart::ChartAxisLabelPosition_OUTSIDE_END
)
249 bFarAwayLabels
= true;
251 double fInnerDirectionSign
= rAxisProperties
.maLabelAlignment
.mfInnerTickDirection
;
252 if( fInnerDirectionSign
==0.0 )
253 fInnerDirectionSign
= 1.0;
255 B2DVector aMainDirection
= m_aAxisEndScreenPosition2D
-m_aAxisStartScreenPosition2D
;
256 aMainDirection
.normalize();
257 B2DVector
aOrthoDirection(-aMainDirection
.getY(),aMainDirection
.getX());
258 aOrthoDirection
*= fInnerDirectionSign
;
259 aOrthoDirection
.normalize();
261 B2DVector
aStart(0,0), aEnd(0,0);
264 TickmarkProperties
aProps( AxisProperties::getBiggestTickmarkProperties() );
265 aStart
= aOrthoDirection
*aProps
.RelativePos
;
266 aEnd
= aStart
- aOrthoDirection
*aProps
.Length
;
270 for( sal_Int32 nN
=rAxisProperties
.m_aTickmarkPropertiesList
.size();nN
--;)
272 const TickmarkProperties
& rProps
= rAxisProperties
.m_aTickmarkPropertiesList
[nN
];
273 B2DVector aNewStart
= aOrthoDirection
*rProps
.RelativePos
;
274 B2DVector aNewEnd
= aNewStart
- aOrthoDirection
*rProps
.Length
;
275 if(aNewStart
.getLength()>aStart
.getLength())
277 if(aNewEnd
.getLength()>aEnd
.getLength())
282 B2DVector
aLabelDirection(aStart
);
283 if (rAxisProperties
.maLabelAlignment
.mfInnerTickDirection
!= rAxisProperties
.maLabelAlignment
.mfLabelDirection
)
284 aLabelDirection
= aEnd
;
286 B2DVector
aOrthoLabelDirection(aOrthoDirection
);
287 if (rAxisProperties
.maLabelAlignment
.mfInnerTickDirection
!= rAxisProperties
.maLabelAlignment
.mfLabelDirection
)
288 aOrthoLabelDirection
*=-1.0;
289 aOrthoLabelDirection
.normalize();
290 if( bIncludeSpaceBetweenTickAndText
)
291 aLabelDirection
+= aOrthoLabelDirection
*AXIS2D_TICKLABELSPACING
;
292 if( bFarAwayLabels
&& bIncludeFarAwayDistanceIfSo
)
293 aLabelDirection
+= m_aAxisLineToLabelLineShift
;
294 return aLabelDirection
;
297 void TickFactory2D::createPointSequenceForAxisMainLine( drawing::PointSequenceSequence
& rPoints
) const
299 rPoints
[0].realloc(2);
300 rPoints
[0][0].X
= static_cast<sal_Int32
>(m_aAxisStartScreenPosition2D
.getX());
301 rPoints
[0][0].Y
= static_cast<sal_Int32
>(m_aAxisStartScreenPosition2D
.getY());
302 rPoints
[0][1].X
= static_cast<sal_Int32
>(m_aAxisEndScreenPosition2D
.getX());
303 rPoints
[0][1].Y
= static_cast<sal_Int32
>(m_aAxisEndScreenPosition2D
.getY());
306 void TickFactory2D::updateScreenValues( TickInfoArraysType
& rAllTickInfos
) const
308 //get the transformed screen values for all tickmarks in rAllTickInfos
309 for (auto & tickInfos
: rAllTickInfos
)
311 for (auto & tickInfo
: tickInfos
)
313 tickInfo
.aTickScreenPosition
=
314 getTickScreenPosition2D(tickInfo
.fScaledTickValue
);
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */