fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / oox / source / drawingml / chart / axisconverter.cxx
blob886b25298c35f2e1f9548b2ded6364fe1b2efd54
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "oox/drawingml/chart/axisconverter.hxx"
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"
41 namespace oox {
42 namespace drawingml {
43 namespace chart {
45 // ============================================================================
47 using namespace ::com::sun::star::beans;
48 using namespace ::com::sun::star::chart2;
49 using namespace ::com::sun::star::uno;
51 // ============================================================================
53 namespace {
55 inline void lclSetValueOrClearAny( Any& orAny, const OptValue< double >& rofValue )
57 if( rofValue.has() ) orAny <<= rofValue.get(); else orAny.clear();
60 bool lclIsLogarithmicScale( const AxisModel& rAxisModel )
62 return rAxisModel.mofLogBase.has() && (2.0 <= rAxisModel.mofLogBase.get()) && (rAxisModel.mofLogBase.get() <= 1000.0);
65 sal_Int32 lclGetApiTimeUnit( sal_Int32 nTimeUnit )
67 using namespace ::com::sun::star::chart;
68 switch( nTimeUnit )
70 case XML_days: return TimeUnit::DAY;
71 case XML_months: return TimeUnit::MONTH;
72 case XML_years: return TimeUnit::YEAR;
73 default: OSL_ENSURE( false, "lclGetApiTimeUnit - unexpected time unit" );
75 return TimeUnit::DAY;
78 void lclConvertTimeInterval( Any& orInterval, const OptValue< double >& rofUnit, sal_Int32 nTimeUnit )
80 if( rofUnit.has() && (1.0 <= rofUnit.get()) && (rofUnit.get() <= SAL_MAX_INT32) )
81 orInterval <<= ::com::sun::star::chart::TimeInterval( static_cast< sal_Int32 >( rofUnit.get() ), lclGetApiTimeUnit( nTimeUnit ) );
82 else
83 orInterval.clear();
86 ::com::sun::star::chart::ChartAxisLabelPosition lclGetLabelPosition( sal_Int32 nToken )
88 using namespace ::com::sun::star::chart;
89 switch( nToken )
91 case XML_high: return ChartAxisLabelPosition_OUTSIDE_END;
92 case XML_low: return ChartAxisLabelPosition_OUTSIDE_START;
93 case XML_nextTo: return ChartAxisLabelPosition_NEAR_AXIS;
95 return ChartAxisLabelPosition_NEAR_AXIS;
98 sal_Int32 lclGetTickMark( sal_Int32 nToken )
100 using namespace ::com::sun::star::chart2::TickmarkStyle;
101 switch( nToken )
103 case XML_in: return INNER;
104 case XML_out: return OUTER;
105 case XML_cross: return INNER | OUTER;
107 return NONE;
110 } // namespace
112 // ============================================================================
114 AxisConverter::AxisConverter( const ConverterRoot& rParent, AxisModel& rModel ) :
115 ConverterBase< AxisModel >( rParent, rModel )
119 AxisConverter::~AxisConverter()
123 void AxisConverter::convertFromModel( const Reference< XCoordinateSystem >& rxCoordSystem,
124 TypeGroupConverter& rTypeGroup, const AxisModel* pCrossingAxis, sal_Int32 nAxesSetIdx, sal_Int32 nAxisIdx )
126 Reference< XAxis > xAxis;
129 namespace cssc = ::com::sun::star::chart;
130 namespace cssc2 = ::com::sun::star::chart2;
132 const TypeGroupInfo& rTypeInfo = rTypeGroup.getTypeInfo();
133 ObjectFormatter& rFormatter = getFormatter();
135 // create the axis object (always)
136 xAxis.set( createInstance( "com.sun.star.chart2.Axis" ), UNO_QUERY_THROW );
137 PropertySet aAxisProp( xAxis );
138 // #i58688# axis enabled
139 aAxisProp.setProperty( PROP_Show, !mrModel.mbDeleted );
141 // axis line, tick, and gridline properties ---------------------------
143 // show axis labels
144 aAxisProp.setProperty( PROP_DisplayLabels, mrModel.mnTickLabelPos != XML_none );
145 aAxisProp.setProperty( PROP_LabelPosition, lclGetLabelPosition( mrModel.mnTickLabelPos ) );
146 // no X axis line in radar charts
147 if( (nAxisIdx == API_X_AXIS) && (rTypeInfo.meTypeCategory == TYPECATEGORY_RADAR) )
148 mrModel.mxShapeProp.getOrCreate().getLineProperties().maLineFill.moFillType = XML_noFill;
149 // axis line and tick label formatting
150 rFormatter.convertFormatting( aAxisProp, mrModel.mxShapeProp, mrModel.mxTextProp, OBJECTTYPE_AXIS );
151 // tick label rotation
152 rFormatter.convertTextRotation( aAxisProp, mrModel.mxTextProp, true );
154 // tick mark style
155 aAxisProp.setProperty( PROP_MajorTickmarks, lclGetTickMark( mrModel.mnMajorTickMark ) );
156 aAxisProp.setProperty( PROP_MinorTickmarks, lclGetTickMark( mrModel.mnMinorTickMark ) );
157 aAxisProp.setProperty( PROP_MarkPosition, cssc::ChartAxisMarkPosition_AT_AXIS );
159 // main grid
160 PropertySet aGridProp( xAxis->getGridProperties() );
161 aGridProp.setProperty( PROP_Show, mrModel.mxMajorGridLines.is() );
162 if( mrModel.mxMajorGridLines.is() )
163 rFormatter.convertFrameFormatting( aGridProp, mrModel.mxMajorGridLines, OBJECTTYPE_MAJORGRIDLINE );
165 // sub grid
166 Sequence< Reference< XPropertySet > > aSubGridPropSeq = xAxis->getSubGridProperties();
167 if( aSubGridPropSeq.hasElements() )
169 PropertySet aSubGridProp( aSubGridPropSeq[ 0 ] );
170 aSubGridProp.setProperty( PROP_Show, mrModel.mxMinorGridLines.is() );
171 if( mrModel.mxMinorGridLines.is() )
172 rFormatter.convertFrameFormatting( aSubGridProp, mrModel.mxMinorGridLines, OBJECTTYPE_MINORGRIDLINE );
175 // axis type and X axis categories ------------------------------------
177 ScaleData aScaleData = xAxis->getScaleData();
178 // set axis type
179 switch( nAxisIdx )
181 case API_X_AXIS:
182 if( rTypeInfo.mbCategoryAxis )
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 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 aScaleData.AxisType = (bDateAxis && !mrModel.mbAuto) ? cssc2::AxisType::DATE : cssc2::AxisType::CATEGORY;
191 aScaleData.AutoDateAxis = mrModel.mbAuto;
192 aScaleData.Categories = rTypeGroup.createCategorySequence();
194 else
196 OSL_ENSURE( mrModel.mnTypeId == C_TOKEN( valAx ), "AxisConverter::convertFromModel - unexpected axis model type (must: c:valAx)" );
197 aScaleData.AxisType = cssc2::AxisType::REALNUMBER;
199 break;
200 case API_Y_AXIS:
201 OSL_ENSURE( mrModel.mnTypeId == C_TOKEN( valAx ), "AxisConverter::convertFromModel - unexpected axis model type (must: c:valAx)" );
202 aScaleData.AxisType = rTypeGroup.isPercent() ? cssc2::AxisType::PERCENT : cssc2::AxisType::REALNUMBER;
203 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 aScaleData.AxisType = cssc2::AxisType::SERIES;
208 break;
211 // axis scaling and increment -----------------------------------------
213 switch( aScaleData.AxisType )
215 case cssc2::AxisType::CATEGORY:
216 case cssc2::AxisType::SERIES:
217 case cssc2::AxisType::DATE:
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 if( mrModel.mnTypeId == C_TOKEN( dateAx ) )
224 // scaling algorithm
225 aScaleData.Scaling = LinearScaling::create( comphelper::getProcessComponentContext() );
226 // min/max
227 lclSetValueOrClearAny( aScaleData.Minimum, mrModel.mofMin );
228 lclSetValueOrClearAny( aScaleData.Maximum, mrModel.mofMax );
229 // major/minor increment
230 lclConvertTimeInterval( aScaleData.TimeIncrement.MajorTimeInterval, mrModel.mofMajorUnit, mrModel.mnMajorTimeUnit );
231 lclConvertTimeInterval( aScaleData.TimeIncrement.MinorTimeInterval, mrModel.mofMinorUnit, mrModel.mnMinorTimeUnit );
232 // base time unit
233 if( mrModel.monBaseTimeUnit.has() )
234 aScaleData.TimeIncrement.TimeResolution <<= lclGetApiTimeUnit( mrModel.monBaseTimeUnit.get() );
235 else
236 aScaleData.TimeIncrement.TimeResolution.clear();
238 else
240 // do not overlap text unless all labels are visible
241 aAxisProp.setProperty( PROP_TextOverlap, mrModel.mnTickLabelSkip == 1 );
242 // do not break text into several lines
243 aAxisProp.setProperty( PROP_TextBreak, false );
244 // do not stagger labels in two lines
245 aAxisProp.setProperty( PROP_ArrangeOrder, cssc::ChartAxisArrangeOrderType_SIDE_BY_SIDE );
246 //! TODO #i58731# show n-th category
249 break;
250 case cssc2::AxisType::REALNUMBER:
251 case cssc2::AxisType::PERCENT:
253 // scaling algorithm
254 bool bLogScale = lclIsLogarithmicScale( mrModel );
255 if( bLogScale )
256 aScaleData.Scaling = LogarithmicScaling::create( comphelper::getProcessComponentContext() );
257 else
258 aScaleData.Scaling = LinearScaling::create( comphelper::getProcessComponentContext() );
259 // min/max
260 lclSetValueOrClearAny( aScaleData.Minimum, mrModel.mofMin );
261 lclSetValueOrClearAny( aScaleData.Maximum, mrModel.mofMax );
262 // major increment
263 IncrementData& rIncrementData = aScaleData.IncrementData;
264 if( mrModel.mofMajorUnit.has() && aScaleData.Scaling.is() )
265 rIncrementData.Distance <<= aScaleData.Scaling->doScaling( mrModel.mofMajorUnit.get() );
266 else
267 lclSetValueOrClearAny( rIncrementData.Distance, mrModel.mofMajorUnit );
268 // minor increment
269 Sequence< SubIncrement >& rSubIncrementSeq = rIncrementData.SubIncrements;
270 rSubIncrementSeq.realloc( 1 );
271 Any& rIntervalCount = rSubIncrementSeq[ 0 ].IntervalCount;
272 rIntervalCount.clear();
273 if( bLogScale )
275 if( mrModel.mofMinorUnit.has() )
276 rIntervalCount <<= sal_Int32( 9 );
278 else if( mrModel.mofMajorUnit.has() && mrModel.mofMinorUnit.has() && (0.0 < mrModel.mofMinorUnit.get()) && (mrModel.mofMinorUnit.get() <= mrModel.mofMajorUnit.get()) )
280 double fCount = mrModel.mofMajorUnit.get() / mrModel.mofMinorUnit.get() + 0.5;
281 if( (1.0 <= fCount) && (fCount < 1001.0) )
282 rIntervalCount <<= static_cast< sal_Int32 >( fCount );
285 break;
286 default:
287 OSL_FAIL( "AxisConverter::convertFromModel - unknown axis type" );
290 /* Do not set a value to the Origin member anymore (already done via
291 new axis properties 'CrossoverPosition' and 'CrossoverValue'). */
292 aScaleData.Origin.clear();
294 // axis orientation ---------------------------------------------------
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 ((nAxisIdx == API_Y_AXIS) && (rTypeInfo.meTypeCategory == TYPECATEGORY_PIE)) ||
300 ((nAxisIdx == API_X_AXIS) && (rTypeInfo.meTypeCategory == TYPECATEGORY_RADAR));
301 bool bReverse = (mrModel.mnOrientation == XML_maxMin) != bMirrorDirection;
302 aScaleData.Orientation = bReverse ? cssc2::AxisOrientation_REVERSE : cssc2::AxisOrientation_MATHEMATICAL;
304 // write back scaling data
305 xAxis->setScaleData( aScaleData );
307 // number format ------------------------------------------------------
309 if( (aScaleData.AxisType == cssc2::AxisType::REALNUMBER) || (aScaleData.AxisType == cssc2::AxisType::PERCENT) )
311 bool bPercent = false;
312 if( mrModel.maNumberFormat.maFormatCode.indexOf('%') >= 0)
313 bPercent = true;
314 mrModel.maNumberFormat.mbSourceLinked = false;
315 getFormatter().convertNumberFormat( aAxisProp, mrModel.maNumberFormat, bPercent );
318 // position of crossing axis ------------------------------------------
320 bool bManualCrossing = mrModel.mofCrossesAt.has();
321 cssc::ChartAxisPosition eAxisPos = cssc::ChartAxisPosition_VALUE;
322 if( !bManualCrossing ) switch( mrModel.mnCrossMode )
324 case XML_min: eAxisPos = cssc::ChartAxisPosition_START; break;
325 case XML_max: eAxisPos = cssc::ChartAxisPosition_END; break;
326 case XML_autoZero: eAxisPos = cssc::ChartAxisPosition_VALUE; break;
328 aAxisProp.setProperty( PROP_CrossoverPosition, eAxisPos );
330 // calculate automatic origin depending on scaling mode of crossing axis
331 bool bCrossingLogScale = pCrossingAxis && lclIsLogarithmicScale( *pCrossingAxis );
332 double fCrossingPos = bManualCrossing ? mrModel.mofCrossesAt.get() : (bCrossingLogScale ? 1.0 : 0.0);
333 aAxisProp.setProperty( PROP_CrossoverValue, fCrossingPos );
335 // axis title ---------------------------------------------------------
337 // in radar charts, title objects may exist, but are not shown
338 if( mrModel.mxTitle.is() && (rTypeGroup.getTypeInfo().meTypeCategory != TYPECATEGORY_RADAR) )
340 Reference< XTitled > xTitled( xAxis, UNO_QUERY_THROW );
341 TitleConverter aTitleConv( *this, *mrModel.mxTitle );
342 aTitleConv.convertFromModel( xTitled, "Axis Title", OBJECTTYPE_AXISTITLE, nAxesSetIdx, nAxisIdx );
345 catch( Exception& )
349 if( xAxis.is() && rxCoordSystem.is() ) try
351 // insert axis into coordinate system
352 rxCoordSystem->setAxisByDimension( nAxisIdx, xAxis, nAxesSetIdx );
354 catch( Exception& )
356 OSL_FAIL( "AxisConverter::convertFromModel - cannot insert axis into coordinate system" );
360 // ============================================================================
362 } // namespace chart
363 } // namespace drawingml
364 } // namespace oox
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */