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 "Tickmarks.hxx"
21 : #include "Tickmarks_Equidistant.hxx"
22 : #include "Tickmarks_Dates.hxx"
23 : #include "ViewDefines.hxx"
24 : #include <rtl/math.hxx>
25 :
26 : namespace chart
27 : {
28 : using namespace ::com::sun::star;
29 : using namespace ::com::sun::star::chart2;
30 : using namespace ::rtl::math;
31 : using ::basegfx::B2DVector;
32 :
33 45063 : TickInfo::TickInfo( const ::com::sun::star::uno::Reference<
34 : ::com::sun::star::chart2::XScaling >& xInverse )
35 : : fScaledTickValue( 0.0 )
36 : , xInverseScaling( xInverse )
37 : , aTickScreenPosition(0.0,0.0)
38 : , bPaintIt( true )
39 : , xTextShape( NULL )
40 45063 : , nFactorForLimitedTextWidth(1)
41 : {
42 45063 : }
43 :
44 10349 : double TickInfo::getUnscaledTickValue() const
45 : {
46 10349 : if( xInverseScaling.is() )
47 5469 : return xInverseScaling->doScaling( fScaledTickValue );
48 : else
49 4880 : return fScaledTickValue;
50 : }
51 :
52 1081 : sal_Int32 TickInfo::getScreenDistanceBetweenTicks( const TickInfo& rOherTickInfo ) const
53 : {
54 : //return the positive distance between the two first tickmarks in screen values
55 :
56 1081 : B2DVector aDistance = rOherTickInfo.aTickScreenPosition - aTickScreenPosition;
57 1081 : sal_Int32 nRet = static_cast<sal_Int32>(aDistance.getLength());
58 1081 : if(nRet<0)
59 0 : nRet *= -1;
60 1081 : return nRet;
61 : }
62 :
63 2216 : PureTickIter::PureTickIter( ::std::vector< TickInfo >& rTickInfoVector )
64 : : m_rTickVector(rTickInfoVector)
65 2216 : , m_aTickIter(m_rTickVector.begin())
66 : {
67 2216 : }
68 3298 : PureTickIter::~PureTickIter()
69 : {
70 3298 : }
71 3379 : TickInfo* PureTickIter::firstInfo()
72 : {
73 3379 : m_aTickIter = m_rTickVector.begin();
74 3379 : if(m_aTickIter!=m_rTickVector.end())
75 3379 : return &*m_aTickIter;
76 0 : return 0;
77 : }
78 12947 : TickInfo* PureTickIter::nextInfo()
79 : {
80 12947 : if(m_aTickIter!=m_rTickVector.end())
81 : {
82 12381 : ++m_aTickIter;
83 12381 : if(m_aTickIter!=m_rTickVector.end())
84 10152 : return &*m_aTickIter;
85 : }
86 2795 : return 0;
87 : }
88 :
89 7973 : TickFactory::TickFactory(
90 : const ExplicitScaleData& rScale, const ExplicitIncrementData& rIncrement )
91 : : m_rScale( rScale )
92 : , m_rIncrement( rIncrement )
93 7973 : , m_xInverseScaling(NULL)
94 : {
95 : //@todo: make sure that the scale is valid for the scaling
96 :
97 7973 : if( m_rScale.Scaling.is() )
98 : {
99 3759 : m_xInverseScaling = m_rScale.Scaling->getInverseScaling();
100 : OSL_ENSURE( m_xInverseScaling.is(), "each Scaling needs to return a inverse Scaling" );
101 : }
102 :
103 7973 : m_fScaledVisibleMin = m_rScale.Minimum;
104 7973 : if( m_xInverseScaling.is() )
105 3759 : m_fScaledVisibleMin = m_rScale.Scaling->doScaling(m_fScaledVisibleMin);
106 :
107 7973 : m_fScaledVisibleMax = m_rScale.Maximum;
108 7973 : if( m_xInverseScaling.is() )
109 3759 : m_fScaledVisibleMax = m_rScale.Scaling->doScaling(m_fScaledVisibleMax);
110 7973 : }
111 :
112 9188 : TickFactory::~TickFactory()
113 : {
114 9188 : }
115 :
116 4072 : bool TickFactory::isDateAxis() const
117 : {
118 4072 : return m_rScale.AxisType == AxisType::DATE;
119 : }
120 :
121 2948 : void TickFactory::getAllTicks( ::std::vector< ::std::vector< TickInfo > >& rAllTickInfos ) const
122 : {
123 2948 : if( isDateAxis() )
124 0 : DateTickFactory( m_rScale, m_rIncrement ).getAllTicks( rAllTickInfos );
125 : else
126 2948 : EquidistantTickFactory( m_rScale, m_rIncrement ).getAllTicks( rAllTickInfos );
127 2948 : }
128 :
129 1124 : void TickFactory::getAllTicksShifted( ::std::vector< ::std::vector< TickInfo > >& rAllTickInfos ) const
130 : {
131 1124 : if( isDateAxis() )
132 0 : DateTickFactory( m_rScale, m_rIncrement ).getAllTicksShifted( rAllTickInfos );
133 : else
134 1124 : EquidistantTickFactory( m_rScale, m_rIncrement ).getAllTicksShifted( rAllTickInfos );
135 1124 : }
136 :
137 : // ___TickFactory_2D___
138 6758 : TickFactory_2D::TickFactory_2D(
139 : const ExplicitScaleData& rScale, const ExplicitIncrementData& rIncrement
140 : //, double fStrech_SceneToScreen, double fOffset_SceneToScreen )
141 : , const B2DVector& rStartScreenPos, const B2DVector& rEndScreenPos
142 : , const B2DVector& rAxisLineToLabelLineShift )
143 : : TickFactory( rScale, rIncrement )
144 : , m_aAxisStartScreenPosition2D(rStartScreenPos)
145 : , m_aAxisEndScreenPosition2D(rEndScreenPos)
146 : , m_aAxisLineToLabelLineShift(rAxisLineToLabelLineShift)
147 : , m_fStrech_LogicToScreen(1.0)
148 6758 : , m_fOffset_LogicToScreen(0.0)
149 : {
150 6758 : double fWidthY = m_fScaledVisibleMax - m_fScaledVisibleMin;
151 6758 : if( AxisOrientation_MATHEMATICAL==m_rScale.Orientation )
152 : {
153 6733 : m_fStrech_LogicToScreen = 1.0/fWidthY;
154 6733 : m_fOffset_LogicToScreen = -m_fScaledVisibleMin;
155 : }
156 : else
157 : {
158 25 : B2DVector aSwap(m_aAxisStartScreenPosition2D);
159 25 : m_aAxisStartScreenPosition2D = m_aAxisEndScreenPosition2D;
160 25 : m_aAxisEndScreenPosition2D = aSwap;
161 :
162 25 : m_fStrech_LogicToScreen = -1.0/fWidthY;
163 25 : m_fOffset_LogicToScreen = -m_fScaledVisibleMax;
164 : }
165 6758 : }
166 :
167 13516 : TickFactory_2D::~TickFactory_2D()
168 : {
169 13516 : }
170 :
171 3292 : bool TickFactory_2D::isHorizontalAxis() const
172 : {
173 3292 : return ( m_aAxisStartScreenPosition2D.getY() == m_aAxisEndScreenPosition2D.getY() );
174 : }
175 3292 : bool TickFactory_2D::isVerticalAxis() const
176 : {
177 3292 : return ( m_aAxisStartScreenPosition2D.getX() == m_aAxisEndScreenPosition2D.getX() );
178 : }
179 :
180 : //static
181 1082 : sal_Int32 TickFactory_2D::getTickScreenDistance( TickIter& rIter )
182 : {
183 : //return the positive distance between the two first tickmarks in screen values
184 : //if there are less than two tickmarks -1 is returned
185 :
186 1082 : const TickInfo* pFirstTickInfo = rIter.firstInfo();
187 1082 : const TickInfo* pSecondTickInfo = rIter.nextInfo();
188 1082 : if(!pSecondTickInfo || !pFirstTickInfo)
189 1 : return -1;
190 :
191 1081 : return pFirstTickInfo->getScreenDistanceBetweenTicks( *pSecondTickInfo );
192 : }
193 :
194 55673 : B2DVector TickFactory_2D::getTickScreenPosition2D( double fScaledLogicTickValue ) const
195 : {
196 55673 : B2DVector aRet(m_aAxisStartScreenPosition2D);
197 167019 : aRet += (m_aAxisEndScreenPosition2D-m_aAxisStartScreenPosition2D)
198 167019 : *((fScaledLogicTickValue+m_fOffset_LogicToScreen)*m_fStrech_LogicToScreen);
199 55673 : return aRet;
200 : }
201 :
202 13581 : void TickFactory_2D::addPointSequenceForTickLine( drawing::PointSequenceSequence& rPoints
203 : , sal_Int32 nSequenceIndex
204 : , double fScaledLogicTickValue, double fInnerDirectionSign
205 : , const TickmarkProperties& rTickmarkProperties
206 : , bool bPlaceAtLabels ) const
207 : {
208 13581 : if( fInnerDirectionSign==0.0 )
209 0 : fInnerDirectionSign = 1.0;
210 :
211 13581 : B2DVector aTickScreenPosition = this->getTickScreenPosition2D(fScaledLogicTickValue);
212 13581 : if( bPlaceAtLabels )
213 6861 : aTickScreenPosition += m_aAxisLineToLabelLineShift;
214 :
215 27162 : B2DVector aMainDirection = m_aAxisEndScreenPosition2D-m_aAxisStartScreenPosition2D;
216 13581 : aMainDirection.normalize();
217 27162 : B2DVector aOrthoDirection(-aMainDirection.getY(),aMainDirection.getX());
218 13581 : aOrthoDirection *= fInnerDirectionSign;
219 13581 : aOrthoDirection.normalize();
220 :
221 27162 : B2DVector aStart = aTickScreenPosition + aOrthoDirection*rTickmarkProperties.RelativePos;
222 27162 : B2DVector aEnd = aStart - aOrthoDirection*rTickmarkProperties.Length;
223 :
224 13581 : rPoints[nSequenceIndex].realloc(2);
225 13581 : rPoints[nSequenceIndex][0].X = static_cast<sal_Int32>(aStart.getX());
226 13581 : rPoints[nSequenceIndex][0].Y = static_cast<sal_Int32>(aStart.getY());
227 13581 : rPoints[nSequenceIndex][1].X = static_cast<sal_Int32>(aEnd.getX());
228 27162 : rPoints[nSequenceIndex][1].Y = static_cast<sal_Int32>(aEnd.getY());
229 13581 : }
230 :
231 8778 : B2DVector TickFactory_2D::getDistanceAxisTickToText( const AxisProperties& rAxisProperties, bool bIncludeFarAwayDistanceIfSo, bool bIncludeSpaceBetweenTickAndText ) const
232 : {
233 8778 : bool bFarAwayLabels = false;
234 8778 : if( ::com::sun::star::chart::ChartAxisLabelPosition_OUTSIDE_START == rAxisProperties.m_eLabelPos
235 8472 : || ::com::sun::star::chart::ChartAxisLabelPosition_OUTSIDE_END == rAxisProperties.m_eLabelPos )
236 306 : bFarAwayLabels = true;
237 :
238 8778 : double fInnerDirectionSign = rAxisProperties.m_fInnerDirectionSign;
239 8778 : if( fInnerDirectionSign==0.0 )
240 0 : fInnerDirectionSign = 1.0;
241 :
242 8778 : B2DVector aMainDirection = m_aAxisEndScreenPosition2D-m_aAxisStartScreenPosition2D;
243 8778 : aMainDirection.normalize();
244 17556 : B2DVector aOrthoDirection(-aMainDirection.getY(),aMainDirection.getX());
245 8778 : aOrthoDirection *= fInnerDirectionSign;
246 8778 : aOrthoDirection.normalize();
247 :
248 17556 : B2DVector aStart(0,0), aEnd(0,0);
249 8778 : if( bFarAwayLabels )
250 : {
251 306 : TickmarkProperties aProps( AxisProperties::getBiggestTickmarkProperties() );
252 306 : aStart = aOrthoDirection*aProps.RelativePos;
253 306 : aEnd = aStart - aOrthoDirection*aProps.Length;
254 : }
255 : else
256 : {
257 25348 : for( sal_Int32 nN=rAxisProperties.m_aTickmarkPropertiesList.size();nN--;)
258 : {
259 8404 : const TickmarkProperties& rProps = rAxisProperties.m_aTickmarkPropertiesList[nN];
260 8404 : B2DVector aNewStart = aOrthoDirection*rProps.RelativePos;
261 16808 : B2DVector aNewEnd = aNewStart - aOrthoDirection*rProps.Length;
262 8404 : if(aNewStart.getLength()>aStart.getLength())
263 8404 : aStart=aNewStart;
264 8404 : if(aNewEnd.getLength()>aEnd.getLength())
265 156 : aEnd=aNewEnd;
266 8404 : }
267 : }
268 :
269 8778 : B2DVector aLabelDirection(aStart);
270 8778 : if( rAxisProperties.m_fInnerDirectionSign != rAxisProperties.m_fLabelDirectionSign )
271 52 : aLabelDirection = aEnd;
272 :
273 17556 : B2DVector aOrthoLabelDirection(aOrthoDirection);
274 8778 : if( rAxisProperties.m_fInnerDirectionSign != rAxisProperties.m_fLabelDirectionSign )
275 52 : aOrthoLabelDirection*=-1.0;
276 8778 : aOrthoLabelDirection.normalize();
277 8778 : if( bIncludeSpaceBetweenTickAndText )
278 8778 : aLabelDirection += aOrthoLabelDirection*AXIS2D_TICKLABELSPACING;
279 8778 : if( bFarAwayLabels && bIncludeFarAwayDistanceIfSo )
280 306 : aLabelDirection += m_aAxisLineToLabelLineShift;
281 17556 : return aLabelDirection;
282 : }
283 :
284 1124 : void TickFactory_2D::createPointSequenceForAxisMainLine( drawing::PointSequenceSequence& rPoints ) const
285 : {
286 1124 : rPoints[0].realloc(2);
287 1124 : rPoints[0][0].X = static_cast<sal_Int32>(m_aAxisStartScreenPosition2D.getX());
288 1124 : rPoints[0][0].Y = static_cast<sal_Int32>(m_aAxisStartScreenPosition2D.getY());
289 1124 : rPoints[0][1].X = static_cast<sal_Int32>(m_aAxisEndScreenPosition2D.getX());
290 1124 : rPoints[0][1].Y = static_cast<sal_Int32>(m_aAxisEndScreenPosition2D.getY());
291 1124 : }
292 :
293 3830 : void TickFactory_2D::updateScreenValues( ::std::vector< ::std::vector< TickInfo > >& rAllTickInfos ) const
294 : {
295 : //get the transformed screen values for all tickmarks in rAllTickInfos
296 3830 : ::std::vector< ::std::vector< TickInfo > >::iterator aDepthIter = rAllTickInfos.begin();
297 3830 : const ::std::vector< ::std::vector< TickInfo > >::const_iterator aDepthEnd = rAllTickInfos.end();
298 11408 : for( ; aDepthIter != aDepthEnd; ++aDepthIter )
299 : {
300 7578 : ::std::vector< TickInfo >::iterator aTickIter = (*aDepthIter).begin();
301 7578 : const ::std::vector< TickInfo >::const_iterator aTickEnd = (*aDepthIter).end();
302 49670 : for( ; aTickIter != aTickEnd; ++aTickIter )
303 : {
304 42092 : TickInfo& rTickInfo = (*aTickIter);
305 84184 : rTickInfo.aTickScreenPosition =
306 42092 : this->getTickScreenPosition2D( rTickInfo.fScaledTickValue );
307 : }
308 : }
309 3830 : }
310 :
311 : } //namespace chart
312 :
313 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|