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