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