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 0 : inline void lclSetValueOrClearAny( Any& orAny, const OptValue< double >& rofValue )
56 : {
57 0 : if( rofValue.has() ) orAny <<= rofValue.get(); else orAny.clear();
58 0 : }
59 :
60 0 : bool lclIsLogarithmicScale( const AxisModel& rAxisModel )
61 : {
62 0 : return rAxisModel.mofLogBase.has() && (2.0 <= rAxisModel.mofLogBase.get()) && (rAxisModel.mofLogBase.get() <= 1000.0);
63 : }
64 :
65 0 : sal_Int32 lclGetApiTimeUnit( sal_Int32 nTimeUnit )
66 : {
67 : using namespace ::com::sun::star::chart;
68 0 : switch( nTimeUnit )
69 : {
70 0 : case XML_days: return TimeUnit::DAY;
71 0 : 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 0 : void lclConvertTimeInterval( Any& orInterval, const OptValue< double >& rofUnit, sal_Int32 nTimeUnit )
79 : {
80 0 : 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 0 : orInterval.clear();
84 0 : }
85 :
86 0 : ::com::sun::star::chart::ChartAxisLabelPosition lclGetLabelPosition( sal_Int32 nToken )
87 : {
88 : using namespace ::com::sun::star::chart;
89 0 : switch( nToken )
90 : {
91 0 : case XML_high: return ChartAxisLabelPosition_OUTSIDE_END;
92 0 : case XML_low: return ChartAxisLabelPosition_OUTSIDE_START;
93 0 : case XML_nextTo: return ChartAxisLabelPosition_NEAR_AXIS;
94 : }
95 0 : return ChartAxisLabelPosition_NEAR_AXIS;
96 : }
97 :
98 0 : sal_Int32 lclGetTickMark( sal_Int32 nToken )
99 : {
100 : using namespace ::com::sun::star::chart2::TickmarkStyle;
101 0 : switch( nToken )
102 : {
103 0 : case XML_in: return INNER;
104 0 : case XML_out: return OUTER;
105 0 : case XML_cross: return INNER | OUTER;
106 : }
107 0 : return NONE;
108 : }
109 :
110 : } // namespace
111 :
112 :
113 :
114 0 : AxisConverter::AxisConverter( const ConverterRoot& rParent, AxisModel& rModel ) :
115 0 : ConverterBase< AxisModel >( rParent, rModel )
116 : {
117 0 : }
118 :
119 0 : AxisConverter::~AxisConverter()
120 : {
121 0 : }
122 :
123 0 : void AxisConverter::convertFromModel( const Reference< XCoordinateSystem >& rxCoordSystem,
124 : TypeGroupConverter& rTypeGroup, const AxisModel* pCrossingAxis, sal_Int32 nAxesSetIdx, sal_Int32 nAxisIdx )
125 : {
126 0 : Reference< XAxis > xAxis;
127 : try
128 : {
129 : namespace cssc = ::com::sun::star::chart;
130 : namespace cssc2 = ::com::sun::star::chart2;
131 :
132 0 : const TypeGroupInfo& rTypeInfo = rTypeGroup.getTypeInfo();
133 0 : ObjectFormatter& rFormatter = getFormatter();
134 :
135 : // create the axis object (always)
136 0 : xAxis.set( createInstance( "com.sun.star.chart2.Axis" ), UNO_QUERY_THROW );
137 0 : PropertySet aAxisProp( xAxis );
138 : // #i58688# axis enabled
139 0 : aAxisProp.setProperty( PROP_Show, !mrModel.mbDeleted );
140 :
141 : // axis line, tick, and gridline properties ---------------------------
142 :
143 : // show axis labels
144 0 : aAxisProp.setProperty( PROP_DisplayLabels, mrModel.mnTickLabelPos != XML_none );
145 0 : aAxisProp.setProperty( PROP_LabelPosition, lclGetLabelPosition( mrModel.mnTickLabelPos ) );
146 : // no X axis line in radar charts
147 0 : 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 0 : rFormatter.convertFormatting( aAxisProp, mrModel.mxShapeProp, mrModel.mxTextProp, OBJECTTYPE_AXIS );
151 : // tick label rotation
152 0 : rFormatter.convertTextRotation( aAxisProp, mrModel.mxTextProp, true );
153 :
154 : // tick mark style
155 0 : aAxisProp.setProperty( PROP_MajorTickmarks, lclGetTickMark( mrModel.mnMajorTickMark ) );
156 0 : aAxisProp.setProperty( PROP_MinorTickmarks, lclGetTickMark( mrModel.mnMinorTickMark ) );
157 0 : aAxisProp.setProperty( PROP_MarkPosition, cssc::ChartAxisMarkPosition_AT_AXIS );
158 :
159 : // main grid
160 0 : PropertySet aGridProp( xAxis->getGridProperties() );
161 0 : aGridProp.setProperty( PROP_Show, mrModel.mxMajorGridLines.is() );
162 0 : if( mrModel.mxMajorGridLines.is() )
163 0 : rFormatter.convertFrameFormatting( aGridProp, mrModel.mxMajorGridLines, OBJECTTYPE_MAJORGRIDLINE );
164 :
165 : // sub grid
166 0 : Sequence< Reference< XPropertySet > > aSubGridPropSeq = xAxis->getSubGridProperties();
167 0 : if( aSubGridPropSeq.hasElements() )
168 : {
169 0 : PropertySet aSubGridProp( aSubGridPropSeq[ 0 ] );
170 0 : aSubGridProp.setProperty( PROP_Show, mrModel.mxMinorGridLines.is() );
171 0 : if( mrModel.mxMinorGridLines.is() )
172 0 : rFormatter.convertFrameFormatting( aSubGridProp, mrModel.mxMinorGridLines, OBJECTTYPE_MINORGRIDLINE );
173 : }
174 :
175 : // axis type and X axis categories ------------------------------------
176 :
177 0 : ScaleData aScaleData = xAxis->getScaleData();
178 : // set axis type
179 0 : switch( nAxisIdx )
180 : {
181 : case API_X_AXIS:
182 0 : 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 0 : 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 0 : aScaleData.AxisType = (bDateAxis && !mrModel.mbAuto) ? cssc2::AxisType::DATE : cssc2::AxisType::CATEGORY;
191 0 : aScaleData.AutoDateAxis = mrModel.mbAuto;
192 0 : 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 0 : aScaleData.AxisType = cssc2::AxisType::REALNUMBER;
198 : }
199 0 : break;
200 : case API_Y_AXIS:
201 : OSL_ENSURE( mrModel.mnTypeId == C_TOKEN( valAx ), "AxisConverter::convertFromModel - unexpected axis model type (must: c:valAx)" );
202 0 : aScaleData.AxisType = rTypeGroup.isPercent() ? cssc2::AxisType::PERCENT : cssc2::AxisType::REALNUMBER;
203 0 : 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 0 : 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 0 : if( mrModel.mnTypeId == C_TOKEN( dateAx ) )
223 : {
224 : // scaling algorithm
225 0 : aScaleData.Scaling = LinearScaling::create( comphelper::getProcessComponentContext() );
226 : // min/max
227 0 : lclSetValueOrClearAny( aScaleData.Minimum, mrModel.mofMin );
228 0 : lclSetValueOrClearAny( aScaleData.Maximum, mrModel.mofMax );
229 : // major/minor increment
230 0 : lclConvertTimeInterval( aScaleData.TimeIncrement.MajorTimeInterval, mrModel.mofMajorUnit, mrModel.mnMajorTimeUnit );
231 0 : lclConvertTimeInterval( aScaleData.TimeIncrement.MinorTimeInterval, mrModel.mofMinorUnit, mrModel.mnMinorTimeUnit );
232 : // base time unit
233 0 : if( mrModel.monBaseTimeUnit.has() )
234 0 : aScaleData.TimeIncrement.TimeResolution <<= lclGetApiTimeUnit( mrModel.monBaseTimeUnit.get() );
235 : else
236 0 : aScaleData.TimeIncrement.TimeResolution.clear();
237 : }
238 : else
239 : {
240 : // do not overlap text unless all labels are visible
241 0 : aAxisProp.setProperty( PROP_TextOverlap, mrModel.mnTickLabelSkip == 1 );
242 : // do not break text into several lines
243 0 : aAxisProp.setProperty( PROP_TextBreak, false );
244 : // do not stagger labels in two lines
245 0 : aAxisProp.setProperty( PROP_ArrangeOrder, cssc::ChartAxisArrangeOrderType_SIDE_BY_SIDE );
246 : //! TODO #i58731# show n-th category
247 : }
248 : }
249 0 : break;
250 : case cssc2::AxisType::REALNUMBER:
251 : case cssc2::AxisType::PERCENT:
252 : {
253 : // scaling algorithm
254 0 : bool bLogScale = lclIsLogarithmicScale( mrModel );
255 0 : if( bLogScale )
256 0 : aScaleData.Scaling = LogarithmicScaling::create( comphelper::getProcessComponentContext() );
257 : else
258 0 : aScaleData.Scaling = LinearScaling::create( comphelper::getProcessComponentContext() );
259 : // min/max
260 0 : lclSetValueOrClearAny( aScaleData.Minimum, mrModel.mofMin );
261 0 : lclSetValueOrClearAny( aScaleData.Maximum, mrModel.mofMax );
262 : // major increment
263 0 : IncrementData& rIncrementData = aScaleData.IncrementData;
264 0 : if( mrModel.mofMajorUnit.has() && aScaleData.Scaling.is() )
265 0 : rIncrementData.Distance <<= aScaleData.Scaling->doScaling( mrModel.mofMajorUnit.get() );
266 : else
267 0 : lclSetValueOrClearAny( rIncrementData.Distance, mrModel.mofMajorUnit );
268 : // minor increment
269 0 : Sequence< SubIncrement >& rSubIncrementSeq = rIncrementData.SubIncrements;
270 0 : rSubIncrementSeq.realloc( 1 );
271 0 : Any& rIntervalCount = rSubIncrementSeq[ 0 ].IntervalCount;
272 0 : rIntervalCount.clear();
273 0 : if( bLogScale )
274 : {
275 0 : if( mrModel.mofMinorUnit.has() )
276 0 : rIntervalCount <<= sal_Int32( 9 );
277 : }
278 0 : 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 0 : 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 0 : 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 0 : ((nAxisIdx == API_Y_AXIS) && (rTypeInfo.meTypeCategory == TYPECATEGORY_PIE)) ||
300 0 : ((nAxisIdx == API_X_AXIS) && (rTypeInfo.meTypeCategory == TYPECATEGORY_RADAR));
301 0 : bool bReverse = (mrModel.mnOrientation == XML_maxMin) != bMirrorDirection;
302 0 : aScaleData.Orientation = bReverse ? cssc2::AxisOrientation_REVERSE : cssc2::AxisOrientation_MATHEMATICAL;
303 :
304 : // write back scaling data
305 0 : xAxis->setScaleData( aScaleData );
306 :
307 : // number format ------------------------------------------------------
308 :
309 0 : if( (aScaleData.AxisType == cssc2::AxisType::REALNUMBER) || (aScaleData.AxisType == cssc2::AxisType::PERCENT) )
310 : {
311 0 : bool bPercent = false;
312 0 : if( mrModel.maNumberFormat.maFormatCode.indexOf('%') >= 0)
313 0 : bPercent = true;
314 0 : mrModel.maNumberFormat.mbSourceLinked = false;
315 0 : getFormatter().convertNumberFormat( aAxisProp, mrModel.maNumberFormat, bPercent );
316 : }
317 :
318 : // position of crossing axis ------------------------------------------
319 :
320 0 : bool bManualCrossing = mrModel.mofCrossesAt.has();
321 0 : cssc::ChartAxisPosition eAxisPos = cssc::ChartAxisPosition_VALUE;
322 0 : if( !bManualCrossing ) switch( mrModel.mnCrossMode )
323 : {
324 0 : case XML_min: eAxisPos = cssc::ChartAxisPosition_START; break;
325 0 : case XML_max: eAxisPos = cssc::ChartAxisPosition_END; break;
326 0 : case XML_autoZero: eAxisPos = cssc::ChartAxisPosition_ZERO; break;
327 : }
328 0 : if( !mrModel.mbAuto )
329 0 : aAxisProp.setProperty( PROP_CrossoverPosition, eAxisPos );
330 :
331 : // calculate automatic origin depending on scaling mode of crossing axis
332 0 : bool bCrossingLogScale = pCrossingAxis && lclIsLogarithmicScale( *pCrossingAxis );
333 0 : double fCrossingPos = bManualCrossing ? mrModel.mofCrossesAt.get() : (bCrossingLogScale ? 1.0 : 0.0);
334 0 : aAxisProp.setProperty( PROP_CrossoverValue, fCrossingPos );
335 :
336 : // axis title ---------------------------------------------------------
337 :
338 : // in radar charts, title objects may exist, but are not shown
339 0 : if( mrModel.mxTitle.is() && (rTypeGroup.getTypeInfo().meTypeCategory != TYPECATEGORY_RADAR) )
340 : {
341 0 : Reference< XTitled > xTitled( xAxis, UNO_QUERY_THROW );
342 0 : TitleConverter aTitleConv( *this, *mrModel.mxTitle );
343 0 : aTitleConv.convertFromModel( xTitled, "Axis Title", OBJECTTYPE_AXISTITLE, nAxesSetIdx, nAxisIdx );
344 : }
345 :
346 : // axis data unit label -----------------------------------------------
347 0 : AxisDispUnitsConverter axisDispUnitsConverter (*this, mrModel.mxDispUnits.getOrCreate());
348 0 : axisDispUnitsConverter.convertFromModel(xAxis);
349 : }
350 0 : catch( Exception& )
351 : {
352 : }
353 :
354 0 : if( xAxis.is() && rxCoordSystem.is() ) try
355 : {
356 : // insert axis into coordinate system
357 0 : rxCoordSystem->setAxisByDimension( nAxisIdx, xAxis, nAxesSetIdx );
358 : }
359 0 : catch( Exception& )
360 : {
361 : OSL_FAIL( "AxisConverter::convertFromModel - cannot insert axis into coordinate system" );
362 0 : }
363 0 : }
364 :
365 :
366 :
367 0 : AxisDispUnitsConverter::AxisDispUnitsConverter( const ConverterRoot& rParent, AxisDispUnitsModel& rModel ) :
368 0 : ConverterBase< AxisDispUnitsModel >( rParent, rModel )
369 : {
370 0 : }
371 :
372 0 : AxisDispUnitsConverter::~AxisDispUnitsConverter()
373 : {
374 0 : }
375 :
376 0 : void AxisDispUnitsConverter::convertFromModel( const Reference< XAxis >& rxAxis )
377 : {
378 0 : PropertySet aPropSet( rxAxis );
379 0 : if (!(mrModel.mnBuiltInUnit).isEmpty() )
380 : {
381 0 : aPropSet.setProperty(PROP_DisplayUnits, true);
382 0 : aPropSet.setProperty( PROP_BuiltInUnit, mrModel.mnBuiltInUnit );
383 0 : }
384 0 : }
385 :
386 :
387 : } // namespace chart
388 : } // namespace drawingml
389 0 : } // namespace oox
390 :
391 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|