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