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 .
21 #include "BubbleChart.hxx"
22 #include "PlottingPositionHelper.hxx"
23 #include "ShapeFactory.hxx"
24 #include "CommonConverters.hxx"
26 #include "ViewDefines.hxx"
27 #include "ObjectIdentifier.hxx"
28 #include "Splines.hxx"
29 #include "LabelPositionHelper.hxx"
30 #include "Clipping.hxx"
33 #include <com/sun/star/chart2/Symbol.hpp>
34 #include <com/sun/star/chart/DataLabelPlacement.hpp>
35 #include <editeng/unoprnms.hxx>
36 #include <rtl/math.hxx>
37 #include <com/sun/star/drawing/DoubleSequence.hpp>
38 #include <com/sun/star/drawing/NormalsKind.hpp>
39 #include <com/sun/star/lang/XServiceName.hpp>
41 //.............................................................................
44 //.............................................................................
45 using namespace ::com::sun::star
;
46 using namespace ::rtl::math
;
47 using namespace ::com::sun::star::chart2
;
49 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 BubbleChart::BubbleChart( const uno::Reference
<XChartType
>& xChartTypeModel
54 , sal_Int32 nDimensionCount
)
55 : VSeriesPlotter( xChartTypeModel
, nDimensionCount
, false )
56 , m_bShowNegativeValues(false)
57 , m_bBubbleSizeAsArea(true)
58 , m_fBubbleSizeScaling(1.0)
59 , m_fMaxLogicBubbleSize( 0.0 )
60 , m_fBubbleSizeFactorToScreen( 1.0 )
62 if( !m_pMainPosHelper
)
63 m_pMainPosHelper
= new PlottingPositionHelper();
64 PlotterBase::m_pPosHelper
= m_pMainPosHelper
;
67 BubbleChart::~BubbleChart()
69 delete m_pMainPosHelper
;
72 void BubbleChart::calculateMaximumLogicBubbleSize()
74 double fMaxSize
= 0.0;
76 sal_Int32 nStartIndex
= 0;
77 sal_Int32 nEndIndex
= VSeriesPlotter::getPointCount();
78 for( sal_Int32 nIndex
= nStartIndex
; nIndex
< nEndIndex
; nIndex
++ )
80 ::std::vector
< ::std::vector
< VDataSeriesGroup
> >::iterator aZSlotIter
= m_aZSlots
.begin();
81 const ::std::vector
< ::std::vector
< VDataSeriesGroup
> >::const_iterator aZSlotEnd
= m_aZSlots
.end();
82 for( ; aZSlotIter
!= aZSlotEnd
; ++aZSlotIter
)
84 ::std::vector
< VDataSeriesGroup
>::iterator aXSlotIter
= aZSlotIter
->begin();
85 const ::std::vector
< VDataSeriesGroup
>::const_iterator aXSlotEnd
= aZSlotIter
->end();
86 for( ; aXSlotIter
!= aXSlotEnd
; ++aXSlotIter
)
88 ::std::vector
< VDataSeries
* >* pSeriesList
= &(aXSlotIter
->m_aSeriesVector
);
89 ::std::vector
< VDataSeries
* >::const_iterator aSeriesIter
= pSeriesList
->begin();
90 const ::std::vector
< VDataSeries
* >::const_iterator aSeriesEnd
= pSeriesList
->end();
91 for( ; aSeriesIter
!= aSeriesEnd
; ++aSeriesIter
)
93 VDataSeries
* pSeries( *aSeriesIter
);
97 double fSize
= pSeries
->getBubble_Size( nIndex
);
98 if( m_bShowNegativeValues
)
100 if( fSize
> fMaxSize
)
107 m_fMaxLogicBubbleSize
= fMaxSize
;
110 void BubbleChart::calculateBubbleSizeScalingFactor()
113 drawing::Position3D
aSceneMinPos( m_pMainPosHelper
->transformLogicToScene( m_pMainPosHelper
->getLogicMinX(),m_pMainPosHelper
->getLogicMinY(),fLogicZ
, false ) );
114 drawing::Position3D
aSceneMaxPos( m_pMainPosHelper
->transformLogicToScene( m_pMainPosHelper
->getLogicMaxX(),m_pMainPosHelper
->getLogicMaxY(),fLogicZ
, false ) );
116 awt::Point
aScreenMinPos( LabelPositionHelper(m_pMainPosHelper
,m_nDimension
,m_xLogicTarget
,m_pShapeFactory
).transformSceneToScreenPosition( aSceneMinPos
) );
117 awt::Point
aScreenMaxPos( LabelPositionHelper(m_pMainPosHelper
,m_nDimension
,m_xLogicTarget
,m_pShapeFactory
).transformSceneToScreenPosition( aSceneMaxPos
) );
119 sal_Int32 nWidth
= abs( aScreenMaxPos
.X
- aScreenMinPos
.X
);
120 sal_Int32 nHeight
= abs( aScreenMaxPos
.Y
- aScreenMinPos
.Y
);
122 sal_Int32 nMinExtend
= std::min( nWidth
, nHeight
);
123 m_fBubbleSizeFactorToScreen
= nMinExtend
* 0.25;//max bubble size is 25 percent of diagram size
126 drawing::Direction3D
BubbleChart::transformToScreenBubbleSize( double fLogicSize
)
128 drawing::Direction3D
aRet(0,0,0);
130 if( ::rtl::math::isNan(fLogicSize
) || ::rtl::math::isInf(fLogicSize
) )
133 if( m_bShowNegativeValues
)
134 fLogicSize
= fabs(fLogicSize
);
136 double fMaxSize
= m_fMaxLogicBubbleSize
;
138 double fMaxRadius
= fMaxSize
;
139 double fRaduis
= fLogicSize
;
140 if( m_bBubbleSizeAsArea
)
142 fMaxRadius
= sqrt( fMaxSize
/ F_PI
);
143 fRaduis
= sqrt( fLogicSize
/ F_PI
);
146 aRet
.DirectionX
= m_fBubbleSizeScaling
* m_fBubbleSizeFactorToScreen
* fRaduis
/ fMaxRadius
;
147 aRet
.DirectionY
= aRet
.DirectionX
;
152 bool BubbleChart::isExpandIfValuesCloseToBorder( sal_Int32
/*nDimensionIndex*/ )
157 bool BubbleChart::isSeparateStackingForDifferentSigns( sal_Int32
/*nDimensionIndex*/ )
162 //-----------------------------------------------------------------
164 LegendSymbolStyle
BubbleChart::getLegendSymbolStyle()
166 return LegendSymbolStyle_CIRCLE
;
169 drawing::Direction3D
BubbleChart::getPreferredDiagramAspectRatio() const
171 return drawing::Direction3D(-1,-1,-1);
174 void BubbleChart::addSeries( VDataSeries
* pSeries
, sal_Int32 zSlot
, sal_Int32 xSlot
, sal_Int32 ySlot
)
176 VSeriesPlotter::addSeries( pSeries
, zSlot
, xSlot
, ySlot
);
179 //better performance for big data
182 FormerPoint( double fX
, double fY
, double fZ
)
183 : m_fX(fX
), m_fY(fY
), m_fZ(fZ
)
187 ::rtl::math::setNan( &m_fX
);
188 ::rtl::math::setNan( &m_fY
);
189 ::rtl::math::setNan( &m_fZ
);
197 void BubbleChart::createShapes()
199 if( m_aZSlots
.begin() == m_aZSlots
.end() ) //no series
202 OSL_ENSURE(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is(),"BubbleChart is not proper initialized");
203 if(!(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is()))
206 //therefore create an own group for the texts and the error bars to move them to front
207 //(because the text group is created after the series group the texts are displayed on top)
208 uno::Reference
< drawing::XShapes
> xSeriesTarget(
209 createGroupShape( m_xLogicTarget
,OUString() ));
210 uno::Reference
< drawing::XShapes
> xTextTarget(
211 m_pShapeFactory
->createGroup2D( m_xFinalTarget
,OUString() ));
213 //update/create information for current group
214 double fLogicZ
= 1.0;//as defined
216 sal_Int32 nStartIndex
= 0; // inclusive ;..todo get somehow from x scale
217 sal_Int32 nEndIndex
= VSeriesPlotter::getPointCount();
221 //better performance for big data
222 std::map
< VDataSeries
*, FormerPoint
> aSeriesFormerPointMap
;
223 m_bPointsWereSkipped
= false;
224 sal_Int32 nSkippedPoints
= 0;
225 sal_Int32 nCreatedPoints
= 0;
228 calculateMaximumLogicBubbleSize();
229 calculateBubbleSizeScalingFactor();
230 if( m_fMaxLogicBubbleSize
<= 0 || m_fBubbleSizeFactorToScreen
<= 0 )
233 //=============================================================================
234 //iterate through all x values per indices
235 for( sal_Int32 nIndex
= nStartIndex
; nIndex
< nEndIndex
; nIndex
++ )
237 ::std::vector
< ::std::vector
< VDataSeriesGroup
> >::iterator aZSlotIter
= m_aZSlots
.begin();
238 const ::std::vector
< ::std::vector
< VDataSeriesGroup
> >::const_iterator aZSlotEnd
= m_aZSlots
.end();
240 for( sal_Int32 nZ
=1; aZSlotIter
!= aZSlotEnd
; ++aZSlotIter
, nZ
++ )
242 ::std::vector
< VDataSeriesGroup
>::iterator aXSlotIter
= aZSlotIter
->begin();
243 const ::std::vector
< VDataSeriesGroup
>::const_iterator aXSlotEnd
= aZSlotIter
->end();
245 for( sal_Int32 nX
=0; aXSlotIter
!= aXSlotEnd
; ++aXSlotIter
, ++nX
)
247 ::std::vector
< VDataSeries
* >* pSeriesList
= &(aXSlotIter
->m_aSeriesVector
);
248 ::std::vector
< VDataSeries
* >::const_iterator aSeriesIter
= pSeriesList
->begin();
249 const ::std::vector
< VDataSeries
* >::const_iterator aSeriesEnd
= pSeriesList
->end();
251 //=============================================================================
252 //iterate through all series
253 for( sal_Int32 nSeriesIndex
= 0; aSeriesIter
!= aSeriesEnd
; ++aSeriesIter
, ++nSeriesIndex
)
255 VDataSeries
* pSeries( *aSeriesIter
);
259 uno::Reference
< drawing::XShapes
> xSeriesGroupShape_Shapes
= getSeriesGroupShape(*aSeriesIter
, xSeriesTarget
);
261 sal_Int32 nAttachedAxisIndex
= pSeries
->getAttachedAxisIndex();
262 PlottingPositionHelper
* pPosHelper
= &(this->getPlottingPositionHelper( nAttachedAxisIndex
));
264 pPosHelper
= m_pMainPosHelper
;
265 PlotterBase::m_pPosHelper
= pPosHelper
;
270 //collect data point information (logic coordinates, style ):
271 double fLogicX
= pSeries
->getXValue(nIndex
);
272 double fLogicY
= pSeries
->getYValue(nIndex
);
273 double fBubbleSize
= pSeries
->getBubble_Size( nIndex
);
275 if( !m_bShowNegativeValues
&& fBubbleSize
<0.0 )
278 if( ::rtl::math::approxEqual( fBubbleSize
, 0.0 ) || ::rtl::math::isNan(fBubbleSize
) )
281 if( ::rtl::math::isNan(fLogicX
) || ::rtl::math::isInf(fLogicX
)
282 || ::rtl::math::isNan(fLogicY
) || ::rtl::math::isInf(fLogicY
) )
285 bool bIsVisible
= pPosHelper
->isLogicVisible( fLogicX
, fLogicY
, fLogicZ
);
287 drawing::Position3D
aUnscaledLogicPosition( fLogicX
, fLogicY
, fLogicZ
);
288 drawing::Position3D
aScaledLogicPosition(aUnscaledLogicPosition
);
289 pPosHelper
->doLogicScaling( aScaledLogicPosition
);
291 //transformation 3) -> 4)
292 drawing::Position3D
aScenePosition( pPosHelper
->transformLogicToScene( fLogicX
,fLogicY
,fLogicZ
, false ) );
294 //better performance for big data
295 FormerPoint
aFormerPoint( aSeriesFormerPointMap
[pSeries
] );
296 pPosHelper
->setCoordinateSystemResolution( m_aCoordinateSystemResolution
);
297 if( !pSeries
->isAttributedDataPoint(nIndex
)
299 pPosHelper
->isSameForGivenResolution( aFormerPoint
.m_fX
, aFormerPoint
.m_fY
, aFormerPoint
.m_fZ
300 , aScaledLogicPosition
.PositionX
, aScaledLogicPosition
.PositionY
, aScaledLogicPosition
.PositionZ
) )
303 m_bPointsWereSkipped
= true;
306 aSeriesFormerPointMap
[pSeries
] = FormerPoint(aScaledLogicPosition
.PositionX
, aScaledLogicPosition
.PositionY
, aScaledLogicPosition
.PositionZ
);
308 //create a single datapoint if point is visible
312 //create a group shape for this point and add to the series shape:
313 OUString aPointCID
= ObjectIdentifier::createPointCID(
314 pSeries
->getPointCID_Stub(), nIndex
);
315 uno::Reference
< drawing::XShapes
> xPointGroupShape_Shapes(
316 createGroupShape(xSeriesGroupShape_Shapes
,aPointCID
) );
317 uno::Reference
<drawing::XShape
> xPointGroupShape_Shape
=
318 uno::Reference
<drawing::XShape
>( xPointGroupShape_Shapes
, uno::UNO_QUERY
);
324 drawing::Direction3D aSymbolSize
= transformToScreenBubbleSize( fBubbleSize
);
327 uno::Reference
<drawing::XShape
> xShape
;
328 xShape
= m_pShapeFactory
->createCircle2D( xPointGroupShape_Shapes
329 , aScenePosition
, aSymbolSize
);
331 this->setMappedProperties( xShape
332 , pSeries
->getPropertiesOfPoint( nIndex
)
333 , PropertyMapper::getPropertyNameMapForFilledSeriesProperties() );
335 m_pShapeFactory
->setShapeName( xShape
, "MarkHandles" );
338 //create data point label
339 if( (**aSeriesIter
).getDataPointLabelIfLabel(nIndex
) )
341 LabelAlignment eAlignment
= LABEL_ALIGN_TOP
;
342 drawing::Position3D
aScenePosition3D( aScenePosition
.PositionX
343 , aScenePosition
.PositionY
344 , aScenePosition
.PositionZ
+this->getTransformedDepth() );
346 sal_Int32 nLabelPlacement
= pSeries
->getLabelPlacement( nIndex
, m_xChartTypeModel
, m_nDimension
, pPosHelper
->isSwapXAndY() );
348 switch(nLabelPlacement
)
350 case ::com::sun::star::chart::DataLabelPlacement::TOP
:
351 aScenePosition3D
.PositionY
-= (aSymbolSize
.DirectionY
/2+1);
352 eAlignment
= LABEL_ALIGN_TOP
;
354 case ::com::sun::star::chart::DataLabelPlacement::BOTTOM
:
355 aScenePosition3D
.PositionY
+= (aSymbolSize
.DirectionY
/2+1);
356 eAlignment
= LABEL_ALIGN_BOTTOM
;
358 case ::com::sun::star::chart::DataLabelPlacement::LEFT
:
359 aScenePosition3D
.PositionX
-= (aSymbolSize
.DirectionX
/2+1);
360 eAlignment
= LABEL_ALIGN_LEFT
;
362 case ::com::sun::star::chart::DataLabelPlacement::RIGHT
:
363 aScenePosition3D
.PositionX
+= (aSymbolSize
.DirectionX
/2+1);
364 eAlignment
= LABEL_ALIGN_RIGHT
;
366 case ::com::sun::star::chart::DataLabelPlacement::CENTER
:
367 eAlignment
= LABEL_ALIGN_CENTER
;
370 OSL_FAIL("this label alignment is not implemented yet");
371 aScenePosition3D
.PositionY
-= (aSymbolSize
.DirectionY
/2+1);
372 eAlignment
= LABEL_ALIGN_TOP
;
377 awt::Point
aScreenPosition2D( LabelPositionHelper(pPosHelper
,m_nDimension
,m_xLogicTarget
,m_pShapeFactory
)
378 .transformSceneToScreenPosition( aScenePosition3D
) );
379 sal_Int32 nOffset
= 0;
380 if(LABEL_ALIGN_CENTER
!=eAlignment
)
381 nOffset
= 100;//add some spacing //@todo maybe get more intelligent values
382 this->createDataLabel( xTextTarget
, **aSeriesIter
, nIndex
383 , fBubbleSize
, fBubbleSize
, aScreenPosition2D
, eAlignment
, nOffset
);
387 //remove PointGroupShape if empty
388 if(!xPointGroupShape_Shapes
->getCount())
389 xSeriesGroupShape_Shapes
->remove(xPointGroupShape_Shape
);
391 }//next series in x slot (next y slot)
395 //=============================================================================
396 //=============================================================================
397 //=============================================================================
398 OSL_TRACE( "\nPPPPPPPPP<<<<<<<<<<<< area chart :: createShapes():: skipped points: %d created points: %d", nSkippedPoints
, nCreatedPoints
);
401 //.............................................................................
403 //.............................................................................
405 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */