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