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)
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(NULL
)
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 void TickFactory::updateScreenValues( TickInfoArraysType
& /*rAllTickInfos*/ ) const
139 // ___TickFactory_2D___
140 TickFactory2D::TickFactory2D(
141 const ExplicitScaleData
& rScale
, const ExplicitIncrementData
& rIncrement
142 //, double fStretch_SceneToScreen, double fOffset_SceneToScreen )
143 , const B2DVector
& rStartScreenPos
, const B2DVector
& rEndScreenPos
144 , const B2DVector
& rAxisLineToLabelLineShift
)
145 : TickFactory( rScale
, rIncrement
)
146 , m_aAxisStartScreenPosition2D(rStartScreenPos
)
147 , m_aAxisEndScreenPosition2D(rEndScreenPos
)
148 , m_aAxisLineToLabelLineShift(rAxisLineToLabelLineShift
)
149 , m_fStretch_LogicToScreen(1.0)
150 , m_fOffset_LogicToScreen(0.0)
152 double fWidthY
= m_fScaledVisibleMax
- m_fScaledVisibleMin
;
153 if (chart2::AxisOrientation_MATHEMATICAL
== m_rScale
.Orientation
)
155 m_fStretch_LogicToScreen
= 1.0/fWidthY
;
156 m_fOffset_LogicToScreen
= -m_fScaledVisibleMin
;
160 B2DVector
aSwap(m_aAxisStartScreenPosition2D
);
161 m_aAxisStartScreenPosition2D
= m_aAxisEndScreenPosition2D
;
162 m_aAxisEndScreenPosition2D
= aSwap
;
164 m_fStretch_LogicToScreen
= -1.0/fWidthY
;
165 m_fOffset_LogicToScreen
= -m_fScaledVisibleMax
;
169 TickFactory2D::~TickFactory2D()
173 bool TickFactory2D::isHorizontalAxis() const
175 return ( m_aAxisStartScreenPosition2D
.getY() == m_aAxisEndScreenPosition2D
.getY() );
177 bool TickFactory2D::isVerticalAxis() const
179 return ( m_aAxisStartScreenPosition2D
.getX() == m_aAxisEndScreenPosition2D
.getX() );
183 sal_Int32
TickFactory2D::getTickScreenDistance( TickIter
& rIter
)
185 //return the positive distance between the two first tickmarks in screen values
186 //if there are less than two tickmarks -1 is returned
188 const TickInfo
* pFirstTickInfo
= rIter
.firstInfo();
189 const TickInfo
* pSecondTickInfo
= rIter
.nextInfo();
190 if(!pSecondTickInfo
|| !pFirstTickInfo
)
193 return pFirstTickInfo
->getScreenDistanceBetweenTicks( *pSecondTickInfo
);
196 B2DVector
TickFactory2D::getTickScreenPosition2D( double fScaledLogicTickValue
) const
198 B2DVector
aRet(m_aAxisStartScreenPosition2D
);
199 aRet
+= (m_aAxisEndScreenPosition2D
-m_aAxisStartScreenPosition2D
)
200 *((fScaledLogicTickValue
+m_fOffset_LogicToScreen
)*m_fStretch_LogicToScreen
);
204 void TickFactory2D::addPointSequenceForTickLine( drawing::PointSequenceSequence
& rPoints
205 , sal_Int32 nSequenceIndex
206 , double fScaledLogicTickValue
, double fInnerDirectionSign
207 , const TickmarkProperties
& rTickmarkProperties
208 , bool bPlaceAtLabels
) const
210 if( fInnerDirectionSign
==0.0 )
211 fInnerDirectionSign
= 1.0;
213 B2DVector aTickScreenPosition
= this->getTickScreenPosition2D(fScaledLogicTickValue
);
215 aTickScreenPosition
+= m_aAxisLineToLabelLineShift
;
217 B2DVector aMainDirection
= m_aAxisEndScreenPosition2D
-m_aAxisStartScreenPosition2D
;
218 aMainDirection
.normalize();
219 B2DVector
aOrthoDirection(-aMainDirection
.getY(),aMainDirection
.getX());
220 aOrthoDirection
*= fInnerDirectionSign
;
221 aOrthoDirection
.normalize();
223 B2DVector aStart
= aTickScreenPosition
+ aOrthoDirection
*rTickmarkProperties
.RelativePos
;
224 B2DVector aEnd
= aStart
- aOrthoDirection
*rTickmarkProperties
.Length
;
226 rPoints
[nSequenceIndex
].realloc(2);
227 rPoints
[nSequenceIndex
][0].X
= static_cast<sal_Int32
>(aStart
.getX());
228 rPoints
[nSequenceIndex
][0].Y
= static_cast<sal_Int32
>(aStart
.getY());
229 rPoints
[nSequenceIndex
][1].X
= static_cast<sal_Int32
>(aEnd
.getX());
230 rPoints
[nSequenceIndex
][1].Y
= static_cast<sal_Int32
>(aEnd
.getY());
233 B2DVector
TickFactory2D::getDistanceAxisTickToText( const AxisProperties
& rAxisProperties
, bool bIncludeFarAwayDistanceIfSo
, bool bIncludeSpaceBetweenTickAndText
) const
235 bool bFarAwayLabels
= false;
236 if( ::com::sun::star::chart::ChartAxisLabelPosition_OUTSIDE_START
== rAxisProperties
.m_eLabelPos
237 || ::com::sun::star::chart::ChartAxisLabelPosition_OUTSIDE_END
== rAxisProperties
.m_eLabelPos
)
238 bFarAwayLabels
= true;
240 double fInnerDirectionSign
= rAxisProperties
.maLabelAlignment
.mfInnerTickDirection
;
241 if( fInnerDirectionSign
==0.0 )
242 fInnerDirectionSign
= 1.0;
244 B2DVector aMainDirection
= m_aAxisEndScreenPosition2D
-m_aAxisStartScreenPosition2D
;
245 aMainDirection
.normalize();
246 B2DVector
aOrthoDirection(-aMainDirection
.getY(),aMainDirection
.getX());
247 aOrthoDirection
*= fInnerDirectionSign
;
248 aOrthoDirection
.normalize();
250 B2DVector
aStart(0,0), aEnd(0,0);
253 TickmarkProperties
aProps( AxisProperties::getBiggestTickmarkProperties() );
254 aStart
= aOrthoDirection
*aProps
.RelativePos
;
255 aEnd
= aStart
- aOrthoDirection
*aProps
.Length
;
259 for( sal_Int32 nN
=rAxisProperties
.m_aTickmarkPropertiesList
.size();nN
--;)
261 const TickmarkProperties
& rProps
= rAxisProperties
.m_aTickmarkPropertiesList
[nN
];
262 B2DVector aNewStart
= aOrthoDirection
*rProps
.RelativePos
;
263 B2DVector aNewEnd
= aNewStart
- aOrthoDirection
*rProps
.Length
;
264 if(aNewStart
.getLength()>aStart
.getLength())
266 if(aNewEnd
.getLength()>aEnd
.getLength())
271 B2DVector
aLabelDirection(aStart
);
272 if (rAxisProperties
.maLabelAlignment
.mfInnerTickDirection
!= rAxisProperties
.maLabelAlignment
.mfLabelDirection
)
273 aLabelDirection
= aEnd
;
275 B2DVector
aOrthoLabelDirection(aOrthoDirection
);
276 if (rAxisProperties
.maLabelAlignment
.mfInnerTickDirection
!= rAxisProperties
.maLabelAlignment
.mfLabelDirection
)
277 aOrthoLabelDirection
*=-1.0;
278 aOrthoLabelDirection
.normalize();
279 if( bIncludeSpaceBetweenTickAndText
)
280 aLabelDirection
+= aOrthoLabelDirection
*AXIS2D_TICKLABELSPACING
;
281 if( bFarAwayLabels
&& bIncludeFarAwayDistanceIfSo
)
282 aLabelDirection
+= m_aAxisLineToLabelLineShift
;
283 return aLabelDirection
;
286 void TickFactory2D::createPointSequenceForAxisMainLine( drawing::PointSequenceSequence
& rPoints
) const
288 rPoints
[0].realloc(2);
289 rPoints
[0][0].X
= static_cast<sal_Int32
>(m_aAxisStartScreenPosition2D
.getX());
290 rPoints
[0][0].Y
= static_cast<sal_Int32
>(m_aAxisStartScreenPosition2D
.getY());
291 rPoints
[0][1].X
= static_cast<sal_Int32
>(m_aAxisEndScreenPosition2D
.getX());
292 rPoints
[0][1].Y
= static_cast<sal_Int32
>(m_aAxisEndScreenPosition2D
.getY());
295 void TickFactory2D::updateScreenValues( TickInfoArraysType
& rAllTickInfos
) const
297 //get the transformed screen values for all tickmarks in rAllTickInfos
298 TickInfoArraysType::iterator aDepthIter
= rAllTickInfos
.begin();
299 const TickInfoArraysType::const_iterator aDepthEnd
= rAllTickInfos
.end();
300 for( ; aDepthIter
!= aDepthEnd
; ++aDepthIter
)
302 TickInfoArrayType::iterator aTickIter
= (*aDepthIter
).begin();
303 const TickInfoArrayType::const_iterator aTickEnd
= (*aDepthIter
).end();
304 for( ; aTickIter
!= aTickEnd
; ++aTickIter
)
306 TickInfo
& rTickInfo
= (*aTickIter
);
307 rTickInfo
.aTickScreenPosition
=
308 this->getTickScreenPosition2D( rTickInfo
.fScaledTickValue
);
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */