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 "VAxisProperties.hxx"
21 : #include "macros.hxx"
22 : #include "ViewDefines.hxx"
23 : #include "CommonConverters.hxx"
24 : #include "AxisHelper.hxx"
25 : #include "DiagramHelper.hxx"
26 : #include "ChartModelHelper.hxx"
27 :
28 : #include <com/sun/star/beans/XPropertySet.hpp>
29 : #include <com/sun/star/chart/ChartAxisArrangeOrderType.hpp>
30 : #include <com/sun/star/drawing/LineStyle.hpp>
31 : #include <com/sun/star/text/WritingMode2.hpp>
32 :
33 : //.............................................................................
34 : namespace chart
35 : {
36 : //.............................................................................
37 : using namespace ::com::sun::star;
38 : using namespace ::com::sun::star::chart2;
39 :
40 82 : sal_Int32 lcl_calcTickLengthForDepth(sal_Int32 nDepth,sal_Int32 nTickmarkStyle)
41 : {
42 82 : sal_Int32 nWidth = AXIS2D_TICKLENGTH; //@maybefuturetodo this length could be offered by the model
43 82 : double fPercent = 1.0;
44 82 : switch(nDepth)
45 : {
46 : case 0:
47 82 : fPercent = 1.0;
48 82 : break;
49 : case 1:
50 0 : fPercent = 0.75;//percentage like in the old chart
51 0 : break;
52 : case 2:
53 0 : fPercent = 0.5;
54 0 : break;
55 : default:
56 0 : fPercent = 0.3;
57 0 : break;
58 : }
59 82 : if(nTickmarkStyle==3)//inner and outer tickmarks
60 0 : fPercent*=2.0;
61 82 : return static_cast<sal_Int32>(nWidth*fPercent);
62 : }
63 :
64 82 : double lcl_getTickOffset(sal_Int32 nLength,sal_Int32 nTickmarkStyle)
65 : {
66 82 : double fPercent = 0.0; //0<=fPercent<=1
67 : //0.0: completly inner
68 : //1.0: completly outer
69 : //0.5: half and half
70 :
71 : /*
72 : nTickmarkStyle:
73 : 1: inner tickmarks
74 : 2: outer tickmarks
75 : 3: inner and outer tickmarks
76 : */
77 82 : switch(nTickmarkStyle)
78 : {
79 : case 1:
80 0 : fPercent = 0.0;
81 0 : break;
82 : case 2:
83 82 : fPercent = 1.0;
84 82 : break;
85 : default:
86 0 : fPercent = 0.5;
87 0 : break;
88 : }
89 82 : return fPercent*nLength;
90 : }
91 :
92 82 : VLineProperties AxisProperties::makeLinePropertiesForDepth( sal_Int32 /* nDepth */ ) const
93 : {
94 : //@todo get this from somewhere; maybe for each subincrement
95 : //so far the model does not offer different settings for each tick depth
96 82 : return m_aLineProperties;
97 : }
98 :
99 82 : TickmarkProperties AxisProperties::makeTickmarkProperties(
100 : sal_Int32 nDepth ) const
101 : {
102 : /*
103 : nTickmarkStyle:
104 : 1: inner tickmarks
105 : 2: outer tickmarks
106 : 3: inner and outer tickmarks
107 : */
108 82 : sal_Int32 nTickmarkStyle = 1;
109 82 : if(nDepth==0)
110 : {
111 82 : nTickmarkStyle = m_nMajorTickmarks;
112 82 : if(!nTickmarkStyle)
113 : {
114 : //create major tickmarks as if they were minor tickmarks
115 0 : nDepth = 1;
116 0 : nTickmarkStyle = m_nMinorTickmarks;
117 : }
118 : }
119 0 : else if( nDepth==1)
120 : {
121 0 : nTickmarkStyle = m_nMinorTickmarks;
122 : }
123 :
124 82 : if( m_fInnerDirectionSign == 0.0 )
125 : {
126 0 : if( nTickmarkStyle != 0 )
127 0 : nTickmarkStyle = 3; //inner and outer tickmarks
128 : }
129 :
130 82 : TickmarkProperties aTickmarkProperties;
131 82 : aTickmarkProperties.Length = lcl_calcTickLengthForDepth(nDepth,nTickmarkStyle);
132 82 : aTickmarkProperties.RelativePos = static_cast<sal_Int32>(lcl_getTickOffset(aTickmarkProperties.Length,nTickmarkStyle));
133 82 : aTickmarkProperties.aLineProperties = this->makeLinePropertiesForDepth( nDepth );
134 82 : return aTickmarkProperties;
135 : }
136 :
137 0 : TickmarkProperties AxisProperties::makeTickmarkPropertiesForComplexCategories(
138 : sal_Int32 nTickLength, sal_Int32 nTickStartDistanceToAxis, sal_Int32 /*nTextLevel*/ ) const
139 : {
140 0 : sal_Int32 nTickmarkStyle = (m_fLabelDirectionSign==m_fInnerDirectionSign) ? 2/*outside*/ : 1/*inside*/;
141 :
142 0 : TickmarkProperties aTickmarkProperties;
143 0 : aTickmarkProperties.Length = nTickLength;// + nTextLevel*( lcl_calcTickLengthForDepth(0,nTickmarkStyle) );
144 0 : aTickmarkProperties.RelativePos = static_cast<sal_Int32>(lcl_getTickOffset(aTickmarkProperties.Length+nTickStartDistanceToAxis,nTickmarkStyle));
145 0 : aTickmarkProperties.aLineProperties = this->makeLinePropertiesForDepth( 0 );
146 0 : return aTickmarkProperties;
147 : }
148 :
149 0 : TickmarkProperties AxisProperties::getBiggestTickmarkProperties()
150 : {
151 0 : TickmarkProperties aTickmarkProperties;
152 0 : sal_Int32 nDepth = 0;
153 0 : sal_Int32 nTickmarkStyle = 3;//inner and outer tickmarks
154 0 : aTickmarkProperties.Length = lcl_calcTickLengthForDepth( nDepth,nTickmarkStyle );
155 0 : aTickmarkProperties.RelativePos = static_cast<sal_Int32>( lcl_getTickOffset( aTickmarkProperties.Length, nTickmarkStyle ) );
156 0 : return aTickmarkProperties;
157 : }
158 :
159 : //--------------------------------------------------------------------------
160 :
161 82 : AxisProperties::AxisProperties( const uno::Reference< XAxis >& xAxisModel
162 : , ExplicitCategoriesProvider* pExplicitCategoriesProvider )
163 : : m_xAxisModel(xAxisModel)
164 : , m_nDimensionIndex(0)
165 : , m_bIsMainAxis(true)
166 : , m_bSwapXAndY(false)
167 : , m_eCrossoverType( ::com::sun::star::chart::ChartAxisPosition_ZERO )
168 : , m_eLabelPos( ::com::sun::star::chart::ChartAxisLabelPosition_NEAR_AXIS )
169 : , m_eTickmarkPos( ::com::sun::star::chart::ChartAxisMarkPosition_AT_LABELS_AND_AXIS )
170 : , m_pfMainLinePositionAtOtherAxis(NULL)
171 : , m_pfExrtaLinePositionAtOtherAxis(NULL)
172 : , m_bCrossingAxisHasReverseDirection(false)
173 : , m_bCrossingAxisIsCategoryAxes(false)
174 : , m_fLabelDirectionSign(1.0)
175 : , m_fInnerDirectionSign(1.0)
176 : , m_aLabelAlignment(LABEL_ALIGN_RIGHT_TOP)
177 : , m_bDisplayLabels( true )
178 : , m_nNumberFormatKey(0)
179 : , m_nMajorTickmarks(1)
180 : , m_nMinorTickmarks(1)
181 : , m_aTickmarkPropertiesList()
182 : , m_aLineProperties()
183 : //for category axes
184 : , m_nAxisType(AxisType::REALNUMBER)
185 : , m_bComplexCategories(false)
186 : , m_pExplicitCategoriesProvider(pExplicitCategoriesProvider)
187 82 : , m_xAxisTextProvider(0)
188 : {
189 82 : }
190 :
191 82 : AxisProperties::AxisProperties( const AxisProperties& rAxisProperties )
192 : : m_xAxisModel( rAxisProperties.m_xAxisModel )
193 : , m_nDimensionIndex( rAxisProperties.m_nDimensionIndex )
194 : , m_bIsMainAxis( rAxisProperties.m_bIsMainAxis )
195 : , m_bSwapXAndY( rAxisProperties.m_bSwapXAndY )
196 : , m_eCrossoverType( rAxisProperties.m_eCrossoverType )
197 : , m_eLabelPos( rAxisProperties.m_eLabelPos )
198 : , m_eTickmarkPos( rAxisProperties.m_eTickmarkPos )
199 : , m_pfMainLinePositionAtOtherAxis( NULL )
200 : , m_pfExrtaLinePositionAtOtherAxis( NULL )
201 : , m_bCrossingAxisHasReverseDirection( rAxisProperties.m_bCrossingAxisHasReverseDirection )
202 : , m_bCrossingAxisIsCategoryAxes( rAxisProperties.m_bCrossingAxisIsCategoryAxes )
203 : , m_fLabelDirectionSign( rAxisProperties.m_fLabelDirectionSign )
204 : , m_fInnerDirectionSign( rAxisProperties.m_fInnerDirectionSign )
205 : , m_aLabelAlignment( rAxisProperties.m_aLabelAlignment )
206 : , m_bDisplayLabels( rAxisProperties.m_bDisplayLabels )
207 : , m_nNumberFormatKey( rAxisProperties.m_nNumberFormatKey )
208 : , m_nMajorTickmarks( rAxisProperties.m_nMajorTickmarks )
209 : , m_nMinorTickmarks( rAxisProperties.m_nMinorTickmarks )
210 : , m_aTickmarkPropertiesList( rAxisProperties.m_aTickmarkPropertiesList )
211 : , m_aLineProperties( rAxisProperties.m_aLineProperties )
212 : //for category axes
213 : , m_nAxisType( rAxisProperties.m_nAxisType )
214 : , m_bComplexCategories( rAxisProperties.m_bComplexCategories )
215 : , m_pExplicitCategoriesProvider( rAxisProperties.m_pExplicitCategoriesProvider )
216 82 : , m_xAxisTextProvider( rAxisProperties.m_xAxisTextProvider )
217 : {
218 82 : if( rAxisProperties.m_pfMainLinePositionAtOtherAxis )
219 82 : m_pfMainLinePositionAtOtherAxis = new double(*rAxisProperties.m_pfMainLinePositionAtOtherAxis);
220 82 : if( rAxisProperties.m_pfExrtaLinePositionAtOtherAxis )
221 0 : m_pfExrtaLinePositionAtOtherAxis = new double (*rAxisProperties.m_pfExrtaLinePositionAtOtherAxis);
222 82 : }
223 :
224 328 : AxisProperties::~AxisProperties()
225 : {
226 164 : delete m_pfMainLinePositionAtOtherAxis;
227 164 : delete m_pfExrtaLinePositionAtOtherAxis;
228 164 : }
229 :
230 0 : LabelAlignment lcl_getLabelAlignmentForZAxis( const AxisProperties& rAxisProperties )
231 : {
232 0 : LabelAlignment aRet( LABEL_ALIGN_RIGHT );
233 0 : if( rAxisProperties.m_fLabelDirectionSign<0 )
234 0 : aRet = LABEL_ALIGN_LEFT;
235 0 : return aRet;
236 : }
237 :
238 41 : LabelAlignment lcl_getLabelAlignmentForYAxis( const AxisProperties& rAxisProperties )
239 : {
240 41 : LabelAlignment aRet( LABEL_ALIGN_RIGHT );
241 41 : if( rAxisProperties.m_fLabelDirectionSign<0 )
242 41 : aRet = LABEL_ALIGN_LEFT;
243 41 : return aRet;
244 : }
245 :
246 41 : LabelAlignment lcl_getLabelAlignmentForXAxis( const AxisProperties& rAxisProperties )
247 : {
248 41 : LabelAlignment aRet( LABEL_ALIGN_BOTTOM );
249 41 : if( rAxisProperties.m_fLabelDirectionSign<0 )
250 0 : aRet = LABEL_ALIGN_TOP;
251 41 : return aRet;
252 : }
253 :
254 82 : void AxisProperties::initAxisPositioning( const uno::Reference< beans::XPropertySet >& xAxisProp )
255 : {
256 82 : if( !xAxisProp.is() )
257 82 : return;
258 : try
259 : {
260 82 : if( AxisHelper::isAxisPositioningEnabled() )
261 : {
262 82 : xAxisProp->getPropertyValue(C2U( "CrossoverPosition" )) >>= m_eCrossoverType;
263 82 : if( ::com::sun::star::chart::ChartAxisPosition_VALUE == m_eCrossoverType )
264 : {
265 0 : double fValue = 0.0;
266 0 : xAxisProp->getPropertyValue(C2U( "CrossoverValue" )) >>= fValue;
267 :
268 0 : if( m_bCrossingAxisIsCategoryAxes )
269 0 : fValue = ::rtl::math::round(fValue);
270 0 : m_pfMainLinePositionAtOtherAxis = new double(fValue);
271 : }
272 82 : else if( ::com::sun::star::chart::ChartAxisPosition_ZERO == m_eCrossoverType )
273 82 : m_pfMainLinePositionAtOtherAxis = new double(0.0);
274 :
275 82 : xAxisProp->getPropertyValue(C2U( "LabelPosition" )) >>= m_eLabelPos;
276 82 : xAxisProp->getPropertyValue(C2U( "MarkPosition" )) >>= m_eTickmarkPos;
277 : }
278 : else
279 : {
280 0 : m_eCrossoverType = ::com::sun::star::chart::ChartAxisPosition_START;
281 0 : if( m_bIsMainAxis == m_bCrossingAxisHasReverseDirection )
282 0 : m_eCrossoverType = ::com::sun::star::chart::ChartAxisPosition_END;
283 0 : m_eLabelPos = ::com::sun::star::chart::ChartAxisLabelPosition_NEAR_AXIS;
284 0 : m_eTickmarkPos = ::com::sun::star::chart::ChartAxisMarkPosition_AT_LABELS;
285 : }
286 : }
287 0 : catch( const uno::Exception& e )
288 : {
289 : ASSERT_EXCEPTION( e );
290 : }
291 : }
292 :
293 82 : void AxisProperties::init( bool bCartesian )
294 : {
295 : uno::Reference< beans::XPropertySet > xProp =
296 82 : uno::Reference<beans::XPropertySet>::query( this->m_xAxisModel );
297 82 : if( !xProp.is() )
298 82 : return;
299 :
300 82 : if( m_nDimensionIndex<2 )
301 82 : initAxisPositioning( xProp );
302 :
303 82 : ScaleData aScaleData = m_xAxisModel->getScaleData();
304 82 : if( m_nDimensionIndex==0 )
305 41 : AxisHelper::checkDateAxis( aScaleData, m_pExplicitCategoriesProvider, bCartesian );
306 82 : m_nAxisType = aScaleData.AxisType;
307 :
308 82 : if( bCartesian )
309 : {
310 123 : if( m_nDimensionIndex == 0 && m_nAxisType == AxisType::CATEGORY
311 41 : && m_pExplicitCategoriesProvider && m_pExplicitCategoriesProvider->hasComplexCategories() )
312 0 : m_bComplexCategories = true;
313 :
314 82 : if( ::com::sun::star::chart::ChartAxisPosition_END == m_eCrossoverType )
315 0 : m_fInnerDirectionSign = m_bCrossingAxisHasReverseDirection ? 1 : -1;
316 : else
317 82 : m_fInnerDirectionSign = m_bCrossingAxisHasReverseDirection ? -1 : 1;
318 :
319 82 : if( ::com::sun::star::chart::ChartAxisLabelPosition_NEAR_AXIS == m_eLabelPos )
320 82 : m_fLabelDirectionSign = m_fInnerDirectionSign;
321 0 : else if( ::com::sun::star::chart::ChartAxisLabelPosition_NEAR_AXIS_OTHER_SIDE == m_eLabelPos )
322 0 : m_fLabelDirectionSign = -m_fInnerDirectionSign;
323 0 : else if( ::com::sun::star::chart::ChartAxisLabelPosition_OUTSIDE_START == m_eLabelPos )
324 0 : m_fLabelDirectionSign = m_bCrossingAxisHasReverseDirection ? -1 : 1;
325 0 : else if( ::com::sun::star::chart::ChartAxisLabelPosition_OUTSIDE_END == m_eLabelPos )
326 0 : m_fLabelDirectionSign = m_bCrossingAxisHasReverseDirection ? 1 : -1;
327 :
328 82 : if( m_nDimensionIndex==2 )
329 0 : m_aLabelAlignment = lcl_getLabelAlignmentForZAxis(*this);
330 : else
331 : {
332 41 : bool bIsYAxisPosition = (m_nDimensionIndex==1 && !m_bSwapXAndY)
333 123 : || (m_nDimensionIndex==0 && m_bSwapXAndY);
334 82 : if( bIsYAxisPosition )
335 : {
336 41 : m_fLabelDirectionSign*=-1;
337 41 : m_fInnerDirectionSign*=-1;
338 : }
339 :
340 82 : if( bIsYAxisPosition )
341 41 : m_aLabelAlignment = lcl_getLabelAlignmentForYAxis(*this);
342 : else
343 41 : m_aLabelAlignment = lcl_getLabelAlignmentForXAxis(*this);
344 : }
345 : }
346 :
347 : try
348 : {
349 : //init LineProperties
350 82 : m_aLineProperties.initFromPropertySet( xProp );
351 :
352 : //init display labels
353 82 : xProp->getPropertyValue( C2U( "DisplayLabels" ) ) >>= m_bDisplayLabels;
354 :
355 : //init TickmarkProperties
356 82 : xProp->getPropertyValue( C2U( "MajorTickmarks" ) ) >>= m_nMajorTickmarks;
357 82 : xProp->getPropertyValue( C2U( "MinorTickmarks" ) ) >>= m_nMinorTickmarks;
358 :
359 82 : sal_Int32 nMaxDepth = 0;
360 82 : if(m_nMinorTickmarks!=0)
361 0 : nMaxDepth=2;
362 82 : else if(m_nMajorTickmarks!=0)
363 82 : nMaxDepth=1;
364 :
365 82 : this->m_aTickmarkPropertiesList.clear();
366 164 : for( sal_Int32 nDepth=0; nDepth<nMaxDepth; nDepth++ )
367 : {
368 82 : TickmarkProperties aTickmarkProperties = this->makeTickmarkProperties( nDepth );
369 82 : this->m_aTickmarkPropertiesList.push_back( aTickmarkProperties );
370 82 : }
371 : }
372 0 : catch( const uno::Exception& e )
373 : {
374 : ASSERT_EXCEPTION( e );
375 82 : }
376 : }
377 :
378 : //-----------------------------------------------------------------------------
379 :
380 82 : AxisLabelProperties::AxisLabelProperties()
381 : : m_aFontReferenceSize( ChartModelHelper::getDefaultPageSize() )
382 : , m_aMaximumSpaceForLabels( 0 , 0, m_aFontReferenceSize.Width, m_aFontReferenceSize.Height )
383 : , nNumberFormatKey(0)
384 : , eStaggering( SIDE_BY_SIDE )
385 : , bLineBreakAllowed( false )
386 : , bOverlapAllowed( false )
387 : , bStackCharacters( false )
388 : , fRotationAngleDegree( 0.0 )
389 : , nRhythm( 1 )
390 82 : , bRhythmIsFix(false)
391 : {
392 :
393 82 : }
394 :
395 82 : void AxisLabelProperties::init( const uno::Reference< XAxis >& xAxisModel )
396 : {
397 : uno::Reference< beans::XPropertySet > xProp =
398 82 : uno::Reference<beans::XPropertySet>::query( xAxisModel );
399 82 : if(xProp.is())
400 : {
401 : try
402 : {
403 82 : xProp->getPropertyValue( C2U( "TextBreak" ) ) >>= this->bLineBreakAllowed;
404 82 : xProp->getPropertyValue( C2U( "TextOverlap" ) ) >>= this->bOverlapAllowed;
405 82 : xProp->getPropertyValue( C2U( "StackCharacters" ) ) >>= this->bStackCharacters;
406 82 : xProp->getPropertyValue( C2U( "TextRotation" ) ) >>= this->fRotationAngleDegree;
407 :
408 : ::com::sun::star::chart::ChartAxisArrangeOrderType eArrangeOrder;
409 82 : xProp->getPropertyValue( C2U( "ArrangeOrder" ) ) >>= eArrangeOrder;
410 82 : switch(eArrangeOrder)
411 : {
412 : case ::com::sun::star::chart::ChartAxisArrangeOrderType_SIDE_BY_SIDE:
413 0 : this->eStaggering = SIDE_BY_SIDE;
414 0 : break;
415 : case ::com::sun::star::chart::ChartAxisArrangeOrderType_STAGGER_EVEN:
416 0 : this->eStaggering = STAGGER_EVEN;
417 0 : break;
418 : case ::com::sun::star::chart::ChartAxisArrangeOrderType_STAGGER_ODD:
419 0 : this->eStaggering = STAGGER_ODD;
420 0 : break;
421 : default:
422 82 : this->eStaggering = STAGGER_AUTO;
423 82 : break;
424 : }
425 : }
426 0 : catch( const uno::Exception& e )
427 : {
428 : ASSERT_EXCEPTION( e );
429 : }
430 82 : }
431 82 : }
432 :
433 410 : bool AxisLabelProperties::getIsStaggered() const
434 : {
435 410 : return ( STAGGER_ODD == eStaggering || STAGGER_EVEN == eStaggering );
436 : }
437 :
438 : //.............................................................................
439 : } //namespace chart
440 : //.............................................................................
441 :
442 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|