Branch data 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 "oox/drawingml/chart/axisconverter.hxx"
21 : :
22 : : #include <com/sun/star/chart/ChartAxisArrangeOrderType.hpp>
23 : : #include <com/sun/star/chart/ChartAxisLabelPosition.hpp>
24 : : #include <com/sun/star/chart/ChartAxisMarkPosition.hpp>
25 : : #include <com/sun/star/chart/ChartAxisPosition.hpp>
26 : : #include <com/sun/star/chart/TimeInterval.hpp>
27 : : #include <com/sun/star/chart/TimeUnit.hpp>
28 : : #include <com/sun/star/chart2/AxisType.hpp>
29 : : #include <com/sun/star/chart2/TickmarkStyle.hpp>
30 : : #include <com/sun/star/chart2/XAxis.hpp>
31 : : #include <com/sun/star/chart2/XCoordinateSystem.hpp>
32 : : #include <com/sun/star/chart2/XTitled.hpp>
33 : : #include "oox/drawingml/chart/axismodel.hxx"
34 : : #include "oox/drawingml/chart/titleconverter.hxx"
35 : : #include "oox/drawingml/chart/typegroupconverter.hxx"
36 : : #include "oox/drawingml/lineproperties.hxx"
37 : :
38 : : namespace oox {
39 : : namespace drawingml {
40 : : namespace chart {
41 : :
42 : : // ============================================================================
43 : :
44 : : using namespace ::com::sun::star::beans;
45 : : using namespace ::com::sun::star::chart2;
46 : : using namespace ::com::sun::star::uno;
47 : :
48 : : using ::rtl::OUString;
49 : :
50 : : // ============================================================================
51 : :
52 : : namespace {
53 : :
54 : 0 : inline void lclSetValueOrClearAny( Any& orAny, const OptValue< double >& rofValue )
55 : : {
56 [ # # ]: 0 : if( rofValue.has() ) orAny <<= rofValue.get(); else orAny.clear();
57 : 0 : }
58 : :
59 : 0 : bool lclIsLogarithmicScale( const AxisModel& rAxisModel )
60 : : {
61 [ # # ][ # # ]: 0 : return rAxisModel.mofLogBase.has() && (2.0 <= rAxisModel.mofLogBase.get()) && (rAxisModel.mofLogBase.get() <= 1000.0);
[ # # ]
62 : : }
63 : :
64 : 0 : sal_Int32 lclGetApiTimeUnit( sal_Int32 nTimeUnit )
65 : : {
66 : : using namespace ::com::sun::star::chart;
67 [ # # # # ]: 0 : switch( nTimeUnit )
68 : : {
69 : 0 : case XML_days: return TimeUnit::DAY;
70 : 0 : case XML_months: return TimeUnit::MONTH;
71 : 0 : case XML_years: return TimeUnit::YEAR;
72 : : default: OSL_ENSURE( false, "lclGetApiTimeUnit - unexpected time unit" );
73 : : }
74 : 0 : return TimeUnit::DAY;
75 : : }
76 : :
77 : 0 : void lclConvertTimeInterval( Any& orInterval, const OptValue< double >& rofUnit, sal_Int32 nTimeUnit )
78 : : {
79 [ # # ][ # # ]: 0 : if( rofUnit.has() && (1.0 <= rofUnit.get()) && (rofUnit.get() <= SAL_MAX_INT32) )
[ # # ][ # # ]
80 [ # # ]: 0 : orInterval <<= ::com::sun::star::chart::TimeInterval( static_cast< sal_Int32 >( rofUnit.get() ), lclGetApiTimeUnit( nTimeUnit ) );
81 : : else
82 : 0 : orInterval.clear();
83 : 0 : }
84 : :
85 : 0 : ::com::sun::star::chart::ChartAxisLabelPosition lclGetLabelPosition( sal_Int32 nToken )
86 : : {
87 : : using namespace ::com::sun::star::chart;
88 [ # # # # ]: 0 : switch( nToken )
89 : : {
90 : 0 : case XML_high: return ChartAxisLabelPosition_OUTSIDE_END;
91 : 0 : case XML_low: return ChartAxisLabelPosition_OUTSIDE_START;
92 : 0 : case XML_nextTo: return ChartAxisLabelPosition_NEAR_AXIS;
93 : : }
94 : 0 : return ChartAxisLabelPosition_NEAR_AXIS;
95 : : }
96 : :
97 : 0 : sal_Int32 lclGetTickMark( sal_Int32 nToken )
98 : : {
99 : : using namespace ::com::sun::star::chart2::TickmarkStyle;
100 [ # # # # ]: 0 : switch( nToken )
101 : : {
102 : 0 : case XML_in: return INNER;
103 : 0 : case XML_out: return OUTER;
104 : 0 : case XML_cross: return INNER | OUTER;
105 : : }
106 : 0 : return NONE;
107 : : }
108 : :
109 : : } // namespace
110 : :
111 : : // ============================================================================
112 : :
113 : 0 : AxisConverter::AxisConverter( const ConverterRoot& rParent, AxisModel& rModel ) :
114 : 0 : ConverterBase< AxisModel >( rParent, rModel )
115 : : {
116 : 0 : }
117 : :
118 : 0 : AxisConverter::~AxisConverter()
119 : : {
120 [ # # ]: 0 : }
121 : :
122 : 0 : void AxisConverter::convertFromModel( const Reference< XCoordinateSystem >& rxCoordSystem,
123 : : TypeGroupConverter& rTypeGroup, const AxisModel* pCrossingAxis, sal_Int32 nAxesSetIdx, sal_Int32 nAxisIdx )
124 : : {
125 : 0 : Reference< XAxis > xAxis;
126 : : try
127 : : {
128 : : namespace cssc = ::com::sun::star::chart;
129 : : namespace cssc2 = ::com::sun::star::chart2;
130 : :
131 : 0 : const TypeGroupInfo& rTypeInfo = rTypeGroup.getTypeInfo();
132 [ # # ]: 0 : ObjectFormatter& rFormatter = getFormatter();
133 : :
134 : : // create the axis object (always)
135 [ # # ][ # # ]: 0 : xAxis.set( createInstance( CREATE_OUSTRING( "com.sun.star.chart2.Axis" ) ), UNO_QUERY_THROW );
[ # # ]
136 [ # # ]: 0 : PropertySet aAxisProp( xAxis );
137 : : // #i58688# axis enabled
138 [ # # ]: 0 : aAxisProp.setProperty( PROP_Show, !mrModel.mbDeleted );
139 : :
140 : : // axis line, tick, and gridline properties ---------------------------
141 : :
142 : : // show axis labels
143 [ # # ]: 0 : aAxisProp.setProperty( PROP_DisplayLabels, mrModel.mnTickLabelPos != XML_none );
144 [ # # ]: 0 : aAxisProp.setProperty( PROP_LabelPosition, lclGetLabelPosition( mrModel.mnTickLabelPos ) );
145 : : // no X axis line in radar charts
146 [ # # ][ # # ]: 0 : if( (nAxisIdx == API_X_AXIS) && (rTypeInfo.meTypeCategory == TYPECATEGORY_RADAR) )
147 [ # # ][ # # ]: 0 : mrModel.mxShapeProp.getOrCreate().getLineProperties().maLineFill.moFillType = XML_noFill;
[ # # ]
148 : : // axis line and tick label formatting
149 [ # # ]: 0 : rFormatter.convertFormatting( aAxisProp, mrModel.mxShapeProp, mrModel.mxTextProp, OBJECTTYPE_AXIS );
150 : : // tick label rotation
151 [ # # ]: 0 : rFormatter.convertTextRotation( aAxisProp, mrModel.mxTextProp, true );
152 : :
153 : : // tick mark style
154 [ # # ]: 0 : aAxisProp.setProperty( PROP_MajorTickmarks, lclGetTickMark( mrModel.mnMajorTickMark ) );
155 [ # # ]: 0 : aAxisProp.setProperty( PROP_MinorTickmarks, lclGetTickMark( mrModel.mnMinorTickMark ) );
156 [ # # ]: 0 : aAxisProp.setProperty( PROP_MarkPosition, cssc::ChartAxisMarkPosition_AT_AXIS );
157 : :
158 : : // main grid
159 [ # # ][ # # ]: 0 : PropertySet aGridProp( xAxis->getGridProperties() );
[ # # ]
160 [ # # ][ # # ]: 0 : aGridProp.setProperty( PROP_Show, mrModel.mxMajorGridLines.is() );
161 [ # # ][ # # ]: 0 : if( mrModel.mxMajorGridLines.is() )
162 [ # # ]: 0 : rFormatter.convertFrameFormatting( aGridProp, mrModel.mxMajorGridLines, OBJECTTYPE_MAJORGRIDLINE );
163 : :
164 : : // sub grid
165 [ # # ][ # # ]: 0 : Sequence< Reference< XPropertySet > > aSubGridPropSeq = xAxis->getSubGridProperties();
166 [ # # ]: 0 : if( aSubGridPropSeq.hasElements() )
167 : : {
168 [ # # ][ # # ]: 0 : PropertySet aSubGridProp( aSubGridPropSeq[ 0 ] );
169 [ # # ][ # # ]: 0 : aSubGridProp.setProperty( PROP_Show, mrModel.mxMinorGridLines.is() );
170 [ # # ][ # # ]: 0 : if( mrModel.mxMinorGridLines.is() )
171 [ # # ][ # # ]: 0 : rFormatter.convertFrameFormatting( aSubGridProp, mrModel.mxMinorGridLines, OBJECTTYPE_MINORGRIDLINE );
172 : : }
173 : :
174 : : // axis type and X axis categories ------------------------------------
175 : :
176 [ # # ][ # # ]: 0 : ScaleData aScaleData = xAxis->getScaleData();
177 : : // set axis type
178 [ # # # # ]: 0 : switch( nAxisIdx )
179 : : {
180 : : case API_X_AXIS:
181 [ # # ]: 0 : if( rTypeInfo.mbCategoryAxis )
182 : : {
183 : : OSL_ENSURE( (mrModel.mnTypeId == C_TOKEN( catAx )) || (mrModel.mnTypeId == C_TOKEN( dateAx )),
184 : : "AxisConverter::convertFromModel - unexpected axis model type (must: c:catAx or c:dateAx)" );
185 : 0 : bool bDateAxis = mrModel.mnTypeId == C_TOKEN( dateAx );
186 : : /* Chart2 requires axis type CATEGORY for automatic
187 : : category/date axis (even if it is a date axis
188 : : currently). */
189 [ # # ][ # # ]: 0 : aScaleData.AxisType = (bDateAxis && !mrModel.mbAuto) ? cssc2::AxisType::DATE : cssc2::AxisType::CATEGORY;
190 : 0 : aScaleData.AutoDateAxis = mrModel.mbAuto;
191 [ # # ][ # # ]: 0 : aScaleData.Categories = rTypeGroup.createCategorySequence();
192 : : }
193 : : else
194 : : {
195 : : OSL_ENSURE( mrModel.mnTypeId == C_TOKEN( valAx ), "AxisConverter::convertFromModel - unexpected axis model type (must: c:valAx)" );
196 : 0 : aScaleData.AxisType = cssc2::AxisType::REALNUMBER;
197 : : }
198 : 0 : break;
199 : : case API_Y_AXIS:
200 : : OSL_ENSURE( mrModel.mnTypeId == C_TOKEN( valAx ), "AxisConverter::convertFromModel - unexpected axis model type (must: c:valAx)" );
201 [ # # ][ # # ]: 0 : aScaleData.AxisType = rTypeGroup.isPercent() ? cssc2::AxisType::PERCENT : cssc2::AxisType::REALNUMBER;
202 : 0 : break;
203 : : case API_Z_AXIS:
204 : : OSL_ENSURE( mrModel.mnTypeId == C_TOKEN( serAx ), "AxisConverter::convertFromModel - unexpected axis model type (must: c:serAx)" );
205 : : OSL_ENSURE( rTypeGroup.isDeep3dChart(), "AxisConverter::convertFromModel - series axis not supported by this chart type" );
206 : 0 : aScaleData.AxisType = cssc2::AxisType::SERIES;
207 : 0 : break;
208 : : }
209 : :
210 : : // axis scaling and increment -----------------------------------------
211 : :
212 [ # # # ]: 0 : switch( aScaleData.AxisType )
213 : : {
214 : : case cssc2::AxisType::CATEGORY:
215 : : case cssc2::AxisType::SERIES:
216 : : case cssc2::AxisType::DATE:
217 : : {
218 : : /* Determine date axis type from XML type identifier, and not
219 : : via aScaleData.AxisType, as this value sticks to CATEGORY
220 : : for automatic category/date axes). */
221 [ # # ]: 0 : if( mrModel.mnTypeId == C_TOKEN( dateAx ) )
222 : : {
223 : : // scaling algorithm
224 [ # # ][ # # ]: 0 : aScaleData.Scaling.set( createInstance( CREATE_OUSTRING( "com.sun.star.chart2.LinearScaling" ) ), UNO_QUERY );
[ # # ]
225 : : // min/max
226 [ # # ]: 0 : lclSetValueOrClearAny( aScaleData.Minimum, mrModel.mofMin );
227 [ # # ]: 0 : lclSetValueOrClearAny( aScaleData.Maximum, mrModel.mofMax );
228 : : // major/minor increment
229 [ # # ]: 0 : lclConvertTimeInterval( aScaleData.TimeIncrement.MajorTimeInterval, mrModel.mofMajorUnit, mrModel.mnMajorTimeUnit );
230 [ # # ]: 0 : lclConvertTimeInterval( aScaleData.TimeIncrement.MinorTimeInterval, mrModel.mofMinorUnit, mrModel.mnMinorTimeUnit );
231 : : // base time unit
232 [ # # ]: 0 : if( mrModel.monBaseTimeUnit.has() )
233 [ # # ]: 0 : aScaleData.TimeIncrement.TimeResolution <<= lclGetApiTimeUnit( mrModel.monBaseTimeUnit.get() );
234 : : else
235 : 0 : aScaleData.TimeIncrement.TimeResolution.clear();
236 : : }
237 : : else
238 : : {
239 : : // do not overlap text unless all labels are visible
240 [ # # ]: 0 : aAxisProp.setProperty( PROP_TextOverlap, mrModel.mnTickLabelSkip == 1 );
241 : : // do not break text into several lines
242 [ # # ]: 0 : aAxisProp.setProperty( PROP_TextBreak, false );
243 : : // do not stagger labels in two lines
244 [ # # ]: 0 : aAxisProp.setProperty( PROP_ArrangeOrder, cssc::ChartAxisArrangeOrderType_SIDE_BY_SIDE );
245 : : //! TODO #i58731# show n-th category
246 : : }
247 : : }
248 : 0 : break;
249 : : case cssc2::AxisType::REALNUMBER:
250 : : case cssc2::AxisType::PERCENT:
251 : : {
252 : : // scaling algorithm
253 [ # # ]: 0 : bool bLogScale = lclIsLogarithmicScale( mrModel );
254 : : OUString aScalingService = bLogScale ?
255 : : CREATE_OUSTRING( "com.sun.star.chart2.LogarithmicScaling" ) :
256 [ # # ][ # # ]: 0 : CREATE_OUSTRING( "com.sun.star.chart2.LinearScaling" );
[ # # ]
257 [ # # ][ # # ]: 0 : aScaleData.Scaling.set( createInstance( aScalingService ), UNO_QUERY );
258 : : // min/max
259 [ # # ]: 0 : lclSetValueOrClearAny( aScaleData.Minimum, mrModel.mofMin );
260 [ # # ]: 0 : lclSetValueOrClearAny( aScaleData.Maximum, mrModel.mofMax );
261 : : // major increment
262 : 0 : IncrementData& rIncrementData = aScaleData.IncrementData;
263 [ # # ][ # # ]: 0 : if( mrModel.mofMajorUnit.has() && aScaleData.Scaling.is() )
[ # # ]
264 [ # # ][ # # ]: 0 : rIncrementData.Distance <<= aScaleData.Scaling->doScaling( mrModel.mofMajorUnit.get() );
[ # # ]
265 : : else
266 [ # # ]: 0 : lclSetValueOrClearAny( rIncrementData.Distance, mrModel.mofMajorUnit );
267 : : // minor increment
268 : 0 : Sequence< SubIncrement >& rSubIncrementSeq = rIncrementData.SubIncrements;
269 [ # # ]: 0 : rSubIncrementSeq.realloc( 1 );
270 [ # # ]: 0 : Any& rIntervalCount = rSubIncrementSeq[ 0 ].IntervalCount;
271 : 0 : rIntervalCount.clear();
272 [ # # ]: 0 : if( bLogScale )
273 : : {
274 [ # # ]: 0 : if( mrModel.mofMinorUnit.has() )
275 [ # # ]: 0 : rIntervalCount <<= sal_Int32( 9 );
276 : : }
277 [ # # ][ # # ]: 0 : else if( mrModel.mofMajorUnit.has() && mrModel.mofMinorUnit.has() && (0.0 < mrModel.mofMinorUnit.get()) && (mrModel.mofMinorUnit.get() <= mrModel.mofMajorUnit.get()) )
[ # # ][ # # ]
[ # # ]
278 : : {
279 : 0 : double fCount = mrModel.mofMajorUnit.get() / mrModel.mofMinorUnit.get() + 0.5;
280 [ # # ][ # # ]: 0 : if( (1.0 <= fCount) && (fCount < 1001.0) )
281 [ # # ]: 0 : rIntervalCount <<= static_cast< sal_Int32 >( fCount );
282 : 0 : }
283 : : }
284 : 0 : break;
285 : : default:
286 : : OSL_FAIL( "AxisConverter::convertFromModel - unknown axis type" );
287 : : }
288 : :
289 : : /* Do not set a value to the Origin member anymore (already done via
290 : : new axis properties 'CrossoverPosition' and 'CrossoverValue'). */
291 : 0 : aScaleData.Origin.clear();
292 : :
293 : : // axis orientation ---------------------------------------------------
294 : :
295 : : // #i85167# pie/donut charts need opposite direction at Y axis
296 : : // #i87747# radar charts need opposite direction at X axis
297 : : bool bMirrorDirection =
298 : : ((nAxisIdx == API_Y_AXIS) && (rTypeInfo.meTypeCategory == TYPECATEGORY_PIE)) ||
299 [ # # ][ # # ]: 0 : ((nAxisIdx == API_X_AXIS) && (rTypeInfo.meTypeCategory == TYPECATEGORY_RADAR));
[ # # ][ # # ]
300 : 0 : bool bReverse = (mrModel.mnOrientation == XML_maxMin) != bMirrorDirection;
301 [ # # ]: 0 : aScaleData.Orientation = bReverse ? cssc2::AxisOrientation_REVERSE : cssc2::AxisOrientation_MATHEMATICAL;
302 : :
303 : : // write back scaling data
304 [ # # ][ # # ]: 0 : xAxis->setScaleData( aScaleData );
305 : :
306 : : // number format ------------------------------------------------------
307 : :
308 [ # # ][ # # ]: 0 : if( (aScaleData.AxisType == cssc2::AxisType::REALNUMBER) || (aScaleData.AxisType == cssc2::AxisType::PERCENT) )
309 : : {
310 [ # # ]: 0 : if( mrModel.maNumberFormat.maFormatCode.indexOf('%') >= 0)
311 : 0 : mrModel.maNumberFormat.mbSourceLinked = false;
312 [ # # ][ # # ]: 0 : getFormatter().convertNumberFormat( aAxisProp, mrModel.maNumberFormat );
313 : : }
314 : :
315 : : // position of crossing axis ------------------------------------------
316 : :
317 : 0 : bool bManualCrossing = mrModel.mofCrossesAt.has();
318 : 0 : cssc::ChartAxisPosition eAxisPos = cssc::ChartAxisPosition_VALUE;
319 [ # # # # ]: 0 : if( !bManualCrossing ) switch( mrModel.mnCrossMode )
[ # # ]
320 : : {
321 : 0 : case XML_min: eAxisPos = cssc::ChartAxisPosition_START; break;
322 : 0 : case XML_max: eAxisPos = cssc::ChartAxisPosition_END; break;
323 : 0 : case XML_autoZero: eAxisPos = cssc::ChartAxisPosition_VALUE; break;
324 : : }
325 [ # # ]: 0 : aAxisProp.setProperty( PROP_CrossoverPosition, eAxisPos );
326 : :
327 : : // calculate automatic origin depending on scaling mode of crossing axis
328 [ # # ][ # # ]: 0 : bool bCrossingLogScale = pCrossingAxis && lclIsLogarithmicScale( *pCrossingAxis );
[ # # ]
329 [ # # ][ # # ]: 0 : double fCrossingPos = bManualCrossing ? mrModel.mofCrossesAt.get() : (bCrossingLogScale ? 1.0 : 0.0);
330 [ # # ]: 0 : aAxisProp.setProperty( PROP_CrossoverValue, fCrossingPos );
331 : :
332 : : // axis title ---------------------------------------------------------
333 : :
334 : : // in radar charts, title objects may exist, but are not shown
335 [ # # ][ # # ]: 0 : if( mrModel.mxTitle.is() && (rTypeGroup.getTypeInfo().meTypeCategory != TYPECATEGORY_RADAR) )
[ # # ][ # # ]
336 : : {
337 [ # # ]: 0 : Reference< XTitled > xTitled( xAxis, UNO_QUERY_THROW );
338 [ # # ]: 0 : TitleConverter aTitleConv( *this, *mrModel.mxTitle );
339 [ # # ][ # # ]: 0 : aTitleConv.convertFromModel( xTitled, CREATE_OUSTRING( "Axis Title" ), OBJECTTYPE_AXISTITLE, nAxesSetIdx, nAxisIdx );
[ # # ]
340 [ # # ][ # # ]: 0 : }
[ # # ][ # # ]
[ # # ]
341 : : }
342 [ # # # # ]: 0 : catch( Exception& )
343 : : {
344 : : }
345 : :
346 [ # # ][ # # ]: 0 : if( xAxis.is() && rxCoordSystem.is() ) try
[ # # ]
347 : : {
348 : : // insert axis into coordinate system
349 [ # # ][ # # ]: 0 : rxCoordSystem->setAxisByDimension( nAxisIdx, xAxis, nAxesSetIdx );
350 : : }
351 [ # # ]: 0 : catch( Exception& )
352 : : {
353 : : OSL_FAIL( "AxisConverter::convertFromModel - cannot insert axis into coordinate system" );
354 : 0 : }
355 : 0 : }
356 : :
357 : : // ============================================================================
358 : :
359 : : } // namespace chart
360 : : } // namespace drawingml
361 [ + - ][ + - ]: 285 : } // namespace oox
362 : :
363 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|