1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/plotareaconverter.hxx"
22 #include <com/sun/star/chart/XChartDocument.hpp>
23 #include <com/sun/star/chart/XDiagramPositioning.hpp>
24 #include <com/sun/star/chart2/XChartDocument.hpp>
25 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
26 #include <com/sun/star/chart2/XDiagram.hpp>
27 #include <com/sun/star/drawing/Direction3D.hpp>
28 #include <com/sun/star/drawing/ProjectionMode.hpp>
29 #include <com/sun/star/drawing/ShadeMode.hpp>
30 #include "oox/drawingml/chart/axisconverter.hxx"
31 #include "oox/drawingml/chart/plotareamodel.hxx"
32 #include "oox/drawingml/chart/typegroupconverter.hxx"
38 // ============================================================================
40 using namespace ::com::sun::star
;
41 using namespace ::com::sun::star::chart2
;
42 using namespace ::com::sun::star::uno
;
44 // ============================================================================
48 /** Axes set model. This is a helper for the plot area converter collecting all
49 type groups and axes of the primary or secondary axes set. */
52 typedef ModelVector
< TypeGroupModel
> TypeGroupVector
;
53 typedef ModelMap
< sal_Int32
, AxisModel
> AxisMap
;
55 TypeGroupVector maTypeGroups
; /// All type groups containing data series.
56 AxisMap maAxes
; /// All axes mapped by API axis type.
58 inline explicit AxesSetModel() {}
59 inline ~AxesSetModel() {}
62 // ============================================================================
64 /** Axes set converter. This is a helper class for the plot area converter. */
65 class AxesSetConverter
: public ConverterBase
< AxesSetModel
>
68 explicit AxesSetConverter( const ConverterRoot
& rParent
, AxesSetModel
& rModel
);
69 virtual ~AxesSetConverter();
71 /** Converts the axes set model to a chart2 diagram. Returns an automatic
72 chart title from a single series title, if possible. */
73 void convertFromModel(
74 const Reference
< XDiagram
>& rxDiagram
,
75 View3DModel
& rView3DModel
,
76 sal_Int32 nAxesSetIdx
,
77 bool bSupportsVaryColorsByPoint
);
79 /** Returns the automatic chart title if the axes set contains only one series. */
80 inline const OUString
& getAutomaticTitle() const { return maAutoTitle
; }
81 /** Returns true, if the chart is three-dimensional. */
82 inline bool is3dChart() const { return mb3dChart
; }
83 /** Returns true, if chart type supports wall and floor format in 3D mode. */
84 inline bool isWall3dChart() const { return mbWall3dChart
; }
85 /** Returns true, if chart is a pie chart or doughnut chart. */
86 inline bool isPieChart() const { return mbPieChart
; }
95 // ----------------------------------------------------------------------------
97 AxesSetConverter::AxesSetConverter( const ConverterRoot
& rParent
, AxesSetModel
& rModel
) :
98 ConverterBase
< AxesSetModel
>( rParent
, rModel
),
100 mbWall3dChart( false ),
105 AxesSetConverter::~AxesSetConverter()
109 ModelRef
< AxisModel
> lclGetOrCreateAxis( const AxesSetModel::AxisMap
& rFromAxes
, sal_Int32 nAxisIdx
, sal_Int32 nDefTypeId
)
111 ModelRef
< AxisModel
> xAxis
= rFromAxes
.get( nAxisIdx
);
113 xAxis
.create( nDefTypeId
).mbDeleted
= true; // missing axis is invisible
117 void AxesSetConverter::convertFromModel( const Reference
< XDiagram
>& rxDiagram
,
118 View3DModel
& rView3DModel
, sal_Int32 nAxesSetIdx
, bool bSupportsVaryColorsByPoint
)
120 // create type group converter objects for all type groups
121 typedef RefVector
< TypeGroupConverter
> TypeGroupConvVector
;
122 TypeGroupConvVector aTypeGroups
;
123 for( AxesSetModel::TypeGroupVector::iterator aIt
= mrModel
.maTypeGroups
.begin(), aEnd
= mrModel
.maTypeGroups
.end(); aIt
!= aEnd
; ++aIt
)
124 aTypeGroups
.push_back( TypeGroupConvVector::value_type( new TypeGroupConverter( *this, **aIt
) ) );
126 OSL_ENSURE( !aTypeGroups
.empty(), "AxesSetConverter::convertFromModel - no type groups in axes set" );
127 if( !aTypeGroups
.empty() ) try
129 // first type group needed for coordinate system and axis conversion
130 TypeGroupConverter
& rFirstTypeGroup
= *aTypeGroups
.front();
132 // get automatic chart title, if there is only one type group
133 if( aTypeGroups
.size() == 1 )
134 maAutoTitle
= rFirstTypeGroup
.getSingleSeriesTitle();
136 /* Create a coordinate system. For now, all type groups from all axes sets
137 have to be inserted into one coordinate system. Later, chart2 should
138 support using one coordinate system for each axes set. */
139 Reference
< XCoordinateSystem
> xCoordSystem
;
140 Reference
< XCoordinateSystemContainer
> xCoordSystemCont( rxDiagram
, UNO_QUERY_THROW
);
141 Sequence
< Reference
< XCoordinateSystem
> > aCoordSystems
= xCoordSystemCont
->getCoordinateSystems();
142 if( aCoordSystems
.hasElements() )
144 OSL_ENSURE( aCoordSystems
.getLength() == 1, "AxesSetConverter::convertFromModel - too many coordinate systems" );
145 xCoordSystem
= aCoordSystems
[ 0 ];
146 OSL_ENSURE( xCoordSystem
.is(), "AxesSetConverter::convertFromModel - invalid coordinate system" );
150 xCoordSystem
= rFirstTypeGroup
.createCoordinateSystem();
151 if( xCoordSystem
.is() )
152 xCoordSystemCont
->addCoordinateSystem( xCoordSystem
);
156 mb3dChart
= rFirstTypeGroup
.is3dChart();
157 mbWall3dChart
= rFirstTypeGroup
.isWall3dChart();
158 mbPieChart
= rFirstTypeGroup
.getTypeInfo().meTypeCategory
== TYPECATEGORY_PIE
;
161 View3DConverter
aView3DConv( *this, rView3DModel
);
162 aView3DConv
.convertFromModel( rxDiagram
, rFirstTypeGroup
);
165 /* Convert all chart type groups. Each type group will add its series
166 to the data provider attached to the chart document. */
167 if( xCoordSystem
.is() )
169 // convert all axes (create missing axis models)
170 ModelRef
< AxisModel
> xXAxis
= lclGetOrCreateAxis( mrModel
.maAxes
, API_X_AXIS
, rFirstTypeGroup
.getTypeInfo().mbCategoryAxis
? C_TOKEN( catAx
) : C_TOKEN( valAx
) );
171 ModelRef
< AxisModel
> xYAxis
= lclGetOrCreateAxis( mrModel
.maAxes
, API_Y_AXIS
, C_TOKEN( valAx
) );
173 AxisConverter
aXAxisConv( *this, *xXAxis
);
174 aXAxisConv
.convertFromModel( xCoordSystem
, rFirstTypeGroup
, xYAxis
.get(), nAxesSetIdx
, API_X_AXIS
);
175 AxisConverter
aYAxisConv( *this, *xYAxis
);
176 aYAxisConv
.convertFromModel( xCoordSystem
, rFirstTypeGroup
, xXAxis
.get(), nAxesSetIdx
, API_Y_AXIS
);
178 if( rFirstTypeGroup
.isDeep3dChart() )
180 ModelRef
< AxisModel
> xZAxis
= lclGetOrCreateAxis( mrModel
.maAxes
, API_Z_AXIS
, C_TOKEN( serAx
) );
181 AxisConverter
aZAxisConv( *this, *xZAxis
);
182 aZAxisConv
.convertFromModel( xCoordSystem
, rFirstTypeGroup
, 0, nAxesSetIdx
, API_Z_AXIS
);
185 // convert all chart type groups, this converts all series data and formatting
186 for( TypeGroupConvVector::iterator aTIt
= aTypeGroups
.begin(), aTEnd
= aTypeGroups
.end(); aTIt
!= aTEnd
; ++aTIt
)
187 (*aTIt
)->convertFromModel( rxDiagram
, xCoordSystem
, nAxesSetIdx
, bSupportsVaryColorsByPoint
);
197 // ============================================================================
199 View3DConverter::View3DConverter( const ConverterRoot
& rParent
, View3DModel
& rModel
) :
200 ConverterBase
< View3DModel
>( rParent
, rModel
)
204 View3DConverter::~View3DConverter()
208 void View3DConverter::convertFromModel( const Reference
< XDiagram
>& rxDiagram
, TypeGroupConverter
& rTypeGroup
)
210 namespace cssd
= ::com::sun::star::drawing
;
211 PropertySet
aPropSet( rxDiagram
);
213 sal_Int32 nRotationY
= 0;
214 sal_Int32 nRotationX
= 0;
215 bool bRightAngled
= false;
216 sal_Int32 nAmbientColor
= 0;
217 sal_Int32 nLightColor
= 0;
219 if( rTypeGroup
.getTypeInfo().meTypeCategory
== TYPECATEGORY_PIE
)
221 // Y rotation used as 'first pie slice angle' in 3D pie charts
222 rTypeGroup
.convertPieRotation( aPropSet
, mrModel
.monRotationY
.get( 0 ) );
223 // X rotation a.k.a. elevation (map OOXML [0..90] to Chart2 [-90,0])
224 nRotationX
= getLimitedValue
< sal_Int32
, sal_Int32
>( mrModel
.monRotationX
.get( 15 ), 0, 90 ) - 90;
225 // no right-angled axes in pie charts
226 bRightAngled
= false;
227 // ambient color (Gray 30%)
228 nAmbientColor
= 0xB3B3B3;
229 // light color (Gray 70%)
230 nLightColor
= 0x4C4C4C;
232 else // 3D bar/area/line charts
234 // Y rotation (OOXML [0..359], Chart2 [-179,180])
235 nRotationY
= mrModel
.monRotationY
.get( 20 );
236 // X rotation a.k.a. elevation (OOXML [-90..90], Chart2 [-179,180])
237 nRotationX
= getLimitedValue
< sal_Int32
, sal_Int32
>( mrModel
.monRotationX
.get( 15 ), -90, 90 );
239 bRightAngled
= mrModel
.mbRightAngled
;
240 // ambient color (Gray 20%)
241 nAmbientColor
= 0xCCCCCC;
242 // light color (Gray 60%)
243 nLightColor
= 0x666666;
246 // Y rotation (map OOXML [0..359] to Chart2 [-179,180])
248 if( nRotationY
> 180 ) nRotationY
-= 360;
249 /* Perspective (map OOXML [0..200] to Chart2 [0,100]). Seems that MSO 2007 is
250 buggy here, the XML plugin of MSO 2003 writes the correct perspective in
251 the range from 0 to 100. We will emulate the wrong behaviour of MSO 2007. */
252 sal_Int32 nPerspective
= getLimitedValue
< sal_Int32
, sal_Int32
>( mrModel
.mnPerspective
/ 2, 0, 100 );
253 // projection mode (parallel axes, if right-angled, #i90360# or if perspective is at 0%)
254 bool bParallel
= bRightAngled
|| (nPerspective
== 0);
255 cssd::ProjectionMode eProjMode
= bParallel
? cssd::ProjectionMode_PARALLEL
: cssd::ProjectionMode_PERSPECTIVE
;
257 // set rotation properties
258 aPropSet
.setProperty( PROP_RotationVertical
, nRotationY
);
259 aPropSet
.setProperty( PROP_RotationHorizontal
, nRotationX
);
260 aPropSet
.setProperty( PROP_Perspective
, nPerspective
);
261 aPropSet
.setProperty( PROP_RightAngledAxes
, bRightAngled
);
262 aPropSet
.setProperty( PROP_D3DScenePerspective
, eProjMode
);
264 // set light settings
265 aPropSet
.setProperty( PROP_D3DSceneShadeMode
, cssd::ShadeMode_FLAT
);
266 aPropSet
.setProperty( PROP_D3DSceneAmbientColor
, nAmbientColor
);
267 aPropSet
.setProperty( PROP_D3DSceneLightOn1
, false );
268 aPropSet
.setProperty( PROP_D3DSceneLightOn2
, true );
269 aPropSet
.setProperty( PROP_D3DSceneLightColor2
, nLightColor
);
270 aPropSet
.setProperty( PROP_D3DSceneLightDirection2
, cssd::Direction3D( 0.2, 0.4, 1.0 ) );
273 // ============================================================================
275 WallFloorConverter::WallFloorConverter( const ConverterRoot
& rParent
, WallFloorModel
& rModel
) :
276 ConverterBase
< WallFloorModel
>( rParent
, rModel
)
280 WallFloorConverter::~WallFloorConverter()
284 void WallFloorConverter::convertFromModel( const Reference
< XDiagram
>& rxDiagram
, ObjectType eObjType
)
288 PropertySet aPropSet
;
291 case OBJECTTYPE_FLOOR
: aPropSet
.set( rxDiagram
->getFloor() ); break;
292 case OBJECTTYPE_WALL
: aPropSet
.set( rxDiagram
->getWall() ); break;
293 default: OSL_FAIL( "WallFloorConverter::convertFromModel - invalid object type" );
296 getFormatter().convertFrameFormatting( aPropSet
, mrModel
.mxShapeProp
, mrModel
.mxPicOptions
.getOrCreate(), eObjType
);
300 // ============================================================================
302 PlotAreaConverter::PlotAreaConverter( const ConverterRoot
& rParent
, PlotAreaModel
& rModel
) :
303 ConverterBase
< PlotAreaModel
>( rParent
, rModel
),
305 mbWall3dChart( false ),
310 PlotAreaConverter::~PlotAreaConverter()
314 void PlotAreaConverter::convertFromModel( View3DModel
& rView3DModel
)
316 /* Create the diagram object and attach it to the chart document. One
317 diagram is used to carry all coordinate systems and data series. */
318 Reference
< XDiagram
> xDiagram
;
321 xDiagram
.set( createInstance( "com.sun.star.chart2.Diagram" ), UNO_QUERY_THROW
);
322 getChartDocument()->setFirstDiagram( xDiagram
);
328 // store all axis models in a map, keyed by axis identifier
329 typedef ModelMap
< sal_Int32
, AxisModel
> AxisMap
;
331 for( PlotAreaModel::AxisVector::iterator aAIt
= mrModel
.maAxes
.begin(), aAEnd
= mrModel
.maAxes
.end(); aAIt
!= aAEnd
; ++aAIt
)
333 PlotAreaModel::AxisVector::value_type xAxis
= *aAIt
;
334 OSL_ENSURE( xAxis
->mnAxisId
>= 0, "PlotAreaConverter::convertFromModel - invalid axis identifier" );
335 OSL_ENSURE( !aAxisMap
.has( xAxis
->mnAxisId
), "PlotAreaConverter::convertFromModel - axis identifiers not unique" );
336 if( xAxis
->mnAxisId
>= 0 )
337 aAxisMap
[ xAxis
->mnAxisId
] = xAxis
;
340 // group the type group models into different axes sets
341 typedef ModelVector
< AxesSetModel
> AxesSetVector
;
342 AxesSetVector aAxesSets
;
343 sal_Int32 nMaxSeriesIdx
= -1;
344 for( PlotAreaModel::TypeGroupVector::iterator aTIt
= mrModel
.maTypeGroups
.begin(), aTEnd
= mrModel
.maTypeGroups
.end(); aTIt
!= aTEnd
; ++aTIt
)
346 PlotAreaModel::TypeGroupVector::value_type xTypeGroup
= *aTIt
;
347 if( !xTypeGroup
->maSeries
.empty() )
349 // try to find a compatible axes set for the type group
350 AxesSetModel
* pAxesSet
= 0;
351 for( AxesSetVector::iterator aASIt
= aAxesSets
.begin(), aASEnd
= aAxesSets
.end(); !pAxesSet
&& (aASIt
!= aASEnd
); ++aASIt
)
352 if( (*aASIt
)->maTypeGroups
.front()->maAxisIds
== xTypeGroup
->maAxisIds
)
353 pAxesSet
= aASIt
->get();
355 // not possible to insert into an existing axes set -> start a new axes set
358 pAxesSet
= &aAxesSets
.create();
359 // find axis models used by the type group
360 const TypeGroupModel::AxisIdVector
& rAxisIds
= xTypeGroup
->maAxisIds
;
361 if( rAxisIds
.size() >= 1 )
362 pAxesSet
->maAxes
[ API_X_AXIS
] = aAxisMap
.get( rAxisIds
[ 0 ] );
363 if( rAxisIds
.size() >= 2 )
364 pAxesSet
->maAxes
[ API_Y_AXIS
] = aAxisMap
.get( rAxisIds
[ 1 ] );
365 if( rAxisIds
.size() >= 3 )
366 pAxesSet
->maAxes
[ API_Z_AXIS
] = aAxisMap
.get( rAxisIds
[ 2 ] );
369 // insert the type group model
370 pAxesSet
->maTypeGroups
.push_back( xTypeGroup
);
372 // collect the maximum series index for automatic series formatting
373 for( TypeGroupModel::SeriesVector::iterator aSIt
= xTypeGroup
->maSeries
.begin(), aSEnd
= xTypeGroup
->maSeries
.end(); aSIt
!= aSEnd
; ++aSIt
)
374 nMaxSeriesIdx
= ::std::max( nMaxSeriesIdx
, (*aSIt
)->mnIndex
);
377 getFormatter().setMaxSeriesIndex( nMaxSeriesIdx
);
379 // varying point colors only for single series in single chart type
380 bool bSupportsVaryColorsByPoint
= mrModel
.maTypeGroups
.size() == 1;
382 // convert all axes sets
383 for( AxesSetVector::iterator aASBeg
= aAxesSets
.begin(), aASIt
= aASBeg
, aASEnd
= aAxesSets
.end(); aASIt
!= aASEnd
; ++aASIt
)
385 AxesSetConverter
aAxesSetConv( *this, **aASIt
);
386 sal_Int32 nAxesSetIdx
= static_cast< sal_Int32
>( aASIt
- aASBeg
);
387 aAxesSetConv
.convertFromModel( xDiagram
, rView3DModel
, nAxesSetIdx
, bSupportsVaryColorsByPoint
);
388 if( nAxesSetIdx
== 0 )
390 maAutoTitle
= aAxesSetConv
.getAutomaticTitle();
391 mb3dChart
= aAxesSetConv
.is3dChart();
392 mbWall3dChart
= aAxesSetConv
.isWall3dChart();
393 mbPieChart
= aAxesSetConv
.isPieChart();
397 maAutoTitle
= OUString();
401 // plot area formatting
402 if( xDiagram
.is() && !mb3dChart
)
404 PropertySet
aPropSet( xDiagram
->getWall() );
405 getFormatter().convertFrameFormatting( aPropSet
, mrModel
.mxShapeProp
, OBJECTTYPE_PLOTAREA2D
);
409 void PlotAreaConverter::convertPositionFromModel()
411 LayoutModel
& rLayout
= mrModel
.mxLayout
.getOrCreate();
412 LayoutConverter
aLayoutConv( *this, rLayout
);
413 awt::Rectangle aDiagramRect
;
414 if( aLayoutConv
.calcAbsRectangle( aDiagramRect
) ) try
416 namespace cssc
= ::com::sun::star::chart
;
417 Reference
< cssc::XChartDocument
> xChart1Doc( getChartDocument(), UNO_QUERY_THROW
);
418 Reference
< cssc::XDiagramPositioning
> xPositioning( xChart1Doc
->getDiagram(), UNO_QUERY_THROW
);
419 // for pie charts, always set inner plot area size to exclude the data labels as Excel does
420 sal_Int32 nTarget
= (mbPieChart
&& (rLayout
.mnTarget
== XML_outer
)) ? XML_inner
: rLayout
.mnTarget
;
424 xPositioning
->setDiagramPositionExcludingAxes( aDiagramRect
);
427 xPositioning
->setDiagramPositionIncludingAxes( aDiagramRect
);
430 OSL_FAIL( "PlotAreaConverter::convertPositionFromModel - unknown positioning target" );
438 // ============================================================================
441 } // namespace drawingml
444 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */