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 "BubbleChart.hxx"
21 #include "PlottingPositionHelper.hxx"
22 #include "AbstractShapeFactory.hxx"
23 #include "CommonConverters.hxx"
25 #include "ViewDefines.hxx"
26 #include "ObjectIdentifier.hxx"
27 #include "Splines.hxx"
28 #include "LabelPositionHelper.hxx"
29 #include "Clipping.hxx"
32 #include <com/sun/star/chart2/Symbol.hpp>
33 #include <com/sun/star/chart/DataLabelPlacement.hpp>
34 #include <editeng/unoprnms.hxx>
35 #include <rtl/math.hxx>
36 #include <com/sun/star/drawing/DoubleSequence.hpp>
37 #include <com/sun/star/drawing/NormalsKind.hpp>
38 #include <com/sun/star/lang/XServiceName.hpp>
42 using namespace ::com::sun::star
;
43 using namespace ::rtl::math
;
44 using namespace ::com::sun::star::chart2
;
46 BubbleChart::BubbleChart( const uno::Reference
<XChartType
>& xChartTypeModel
47 , sal_Int32 nDimensionCount
)
48 : VSeriesPlotter( xChartTypeModel
, nDimensionCount
, false )
49 , m_bShowNegativeValues(false)
50 , m_bBubbleSizeAsArea(true)
51 , m_fBubbleSizeScaling(1.0)
52 , m_fMaxLogicBubbleSize( 0.0 )
53 , m_fBubbleSizeFactorToScreen( 1.0 )
55 // We only support 2 dimensional bubble charts
56 assert(nDimensionCount
== 2);
58 if( !m_pMainPosHelper
)
59 m_pMainPosHelper
= new PlottingPositionHelper();
60 PlotterBase::m_pPosHelper
= m_pMainPosHelper
;
63 BubbleChart::~BubbleChart()
65 delete m_pMainPosHelper
;
68 void BubbleChart::calculateMaximumLogicBubbleSize()
70 double fMaxSize
= 0.0;
72 sal_Int32 nStartIndex
= 0;
73 sal_Int32 nEndIndex
= VSeriesPlotter::getPointCount();
74 for( sal_Int32 nIndex
= nStartIndex
; nIndex
< nEndIndex
; nIndex
++ )
76 ::std::vector
< ::std::vector
< VDataSeriesGroup
> >::iterator aZSlotIter
= m_aZSlots
.begin();
77 const ::std::vector
< ::std::vector
< VDataSeriesGroup
> >::const_iterator aZSlotEnd
= m_aZSlots
.end();
78 for( ; aZSlotIter
!= aZSlotEnd
; ++aZSlotIter
)
80 ::std::vector
< VDataSeriesGroup
>::iterator aXSlotIter
= aZSlotIter
->begin();
81 const ::std::vector
< VDataSeriesGroup
>::const_iterator aXSlotEnd
= aZSlotIter
->end();
82 for( ; aXSlotIter
!= aXSlotEnd
; ++aXSlotIter
)
84 ::std::vector
< VDataSeries
* >* pSeriesList
= &(aXSlotIter
->m_aSeriesVector
);
85 ::std::vector
< VDataSeries
* >::const_iterator aSeriesIter
= pSeriesList
->begin();
86 const ::std::vector
< VDataSeries
* >::const_iterator aSeriesEnd
= pSeriesList
->end();
87 for( ; aSeriesIter
!= aSeriesEnd
; ++aSeriesIter
)
89 VDataSeries
* pSeries( *aSeriesIter
);
93 double fSize
= pSeries
->getBubble_Size( nIndex
);
94 if( m_bShowNegativeValues
)
96 if( fSize
> fMaxSize
)
103 m_fMaxLogicBubbleSize
= fMaxSize
;
106 void BubbleChart::calculateBubbleSizeScalingFactor()
109 drawing::Position3D
aSceneMinPos( m_pMainPosHelper
->transformLogicToScene( m_pMainPosHelper
->getLogicMinX(),m_pMainPosHelper
->getLogicMinY(),fLogicZ
, false ) );
110 drawing::Position3D
aSceneMaxPos( m_pMainPosHelper
->transformLogicToScene( m_pMainPosHelper
->getLogicMaxX(),m_pMainPosHelper
->getLogicMaxY(),fLogicZ
, false ) );
112 awt::Point
aScreenMinPos( LabelPositionHelper(m_pMainPosHelper
,m_nDimension
,m_xLogicTarget
,m_pShapeFactory
).transformSceneToScreenPosition( aSceneMinPos
) );
113 awt::Point
aScreenMaxPos( LabelPositionHelper(m_pMainPosHelper
,m_nDimension
,m_xLogicTarget
,m_pShapeFactory
).transformSceneToScreenPosition( aSceneMaxPos
) );
115 sal_Int32 nWidth
= abs( aScreenMaxPos
.X
- aScreenMinPos
.X
);
116 sal_Int32 nHeight
= abs( aScreenMaxPos
.Y
- aScreenMinPos
.Y
);
118 sal_Int32 nMinExtend
= std::min( nWidth
, nHeight
);
119 m_fBubbleSizeFactorToScreen
= nMinExtend
* 0.25;//max bubble size is 25 percent of diagram size
122 drawing::Direction3D
BubbleChart::transformToScreenBubbleSize( double fLogicSize
)
124 drawing::Direction3D
aRet(0,0,0);
126 if( ::rtl::math::isNan(fLogicSize
) || ::rtl::math::isInf(fLogicSize
) )
129 if( m_bShowNegativeValues
)
130 fLogicSize
= fabs(fLogicSize
);
132 double fMaxSize
= m_fMaxLogicBubbleSize
;
134 double fMaxRadius
= fMaxSize
;
135 double fRaduis
= fLogicSize
;
136 if( m_bBubbleSizeAsArea
)
138 fMaxRadius
= sqrt( fMaxSize
/ F_PI
);
139 fRaduis
= sqrt( fLogicSize
/ F_PI
);
142 aRet
.DirectionX
= m_fBubbleSizeScaling
* m_fBubbleSizeFactorToScreen
* fRaduis
/ fMaxRadius
;
143 aRet
.DirectionY
= aRet
.DirectionX
;
148 bool BubbleChart::isExpandIfValuesCloseToBorder( sal_Int32
/*nDimensionIndex*/ )
153 bool BubbleChart::isSeparateStackingForDifferentSigns( sal_Int32
/*nDimensionIndex*/ )
158 LegendSymbolStyle
BubbleChart::getLegendSymbolStyle()
160 return LegendSymbolStyle_CIRCLE
;
163 drawing::Direction3D
BubbleChart::getPreferredDiagramAspectRatio() const
165 return drawing::Direction3D(-1,-1,-1);
168 void BubbleChart::addSeries( VDataSeries
* pSeries
, sal_Int32 zSlot
, sal_Int32 xSlot
, sal_Int32 ySlot
)
170 VSeriesPlotter::addSeries( pSeries
, zSlot
, xSlot
, ySlot
);
173 //better performance for big data
176 FormerPoint( double fX
, double fY
, double fZ
)
177 : m_fX(fX
), m_fY(fY
), m_fZ(fZ
)
181 ::rtl::math::setNan( &m_fX
);
182 ::rtl::math::setNan( &m_fY
);
183 ::rtl::math::setNan( &m_fZ
);
191 void BubbleChart::createShapes()
193 if( m_aZSlots
.empty() ) //no series
196 OSL_ENSURE(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is(),"BubbleChart is not proper initialized");
197 if(!(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is()))
200 //therefore create an own group for the texts and the error bars to move them to front
201 //(because the text group is created after the series group the texts are displayed on top)
202 uno::Reference
< drawing::XShapes
> xSeriesTarget(
203 createGroupShape( m_xLogicTarget
,OUString() ));
204 uno::Reference
< drawing::XShapes
> xTextTarget(
205 m_pShapeFactory
->createGroup2D( m_xFinalTarget
,OUString() ));
207 //update/create information for current group
208 double fLogicZ
= 1.0;//as defined
210 sal_Int32 nStartIndex
= 0; // inclusive ;..todo get somehow from x scale
211 sal_Int32 nEndIndex
= VSeriesPlotter::getPointCount();
215 //better performance for big data
216 std::map
< VDataSeries
*, FormerPoint
> aSeriesFormerPointMap
;
217 m_bPointsWereSkipped
= false;
218 sal_Int32 nSkippedPoints
= 0;
219 sal_Int32 nCreatedPoints
= 0;
221 calculateMaximumLogicBubbleSize();
222 calculateBubbleSizeScalingFactor();
223 if( m_fMaxLogicBubbleSize
<= 0 || m_fBubbleSizeFactorToScreen
<= 0 )
226 //iterate through all x values per indices
227 for( sal_Int32 nIndex
= nStartIndex
; nIndex
< nEndIndex
; nIndex
++ )
229 ::std::vector
< ::std::vector
< VDataSeriesGroup
> >::iterator aZSlotIter
= m_aZSlots
.begin();
230 const ::std::vector
< ::std::vector
< VDataSeriesGroup
> >::const_iterator aZSlotEnd
= m_aZSlots
.end();
232 for( sal_Int32 nZ
=1; aZSlotIter
!= aZSlotEnd
; ++aZSlotIter
, nZ
++ )
234 ::std::vector
< VDataSeriesGroup
>::iterator aXSlotIter
= aZSlotIter
->begin();
235 const ::std::vector
< VDataSeriesGroup
>::const_iterator aXSlotEnd
= aZSlotIter
->end();
237 for( sal_Int32 nX
=0; aXSlotIter
!= aXSlotEnd
; ++aXSlotIter
, ++nX
)
239 ::std::vector
< VDataSeries
* >* pSeriesList
= &(aXSlotIter
->m_aSeriesVector
);
240 ::std::vector
< VDataSeries
* >::const_iterator aSeriesIter
= pSeriesList
->begin();
241 const ::std::vector
< VDataSeries
* >::const_iterator aSeriesEnd
= pSeriesList
->end();
243 //iterate through all series
244 for( sal_Int32 nSeriesIndex
= 0; aSeriesIter
!= aSeriesEnd
; ++aSeriesIter
, ++nSeriesIndex
)
246 VDataSeries
* pSeries( *aSeriesIter
);
250 bool bHasFillColorMapping
= pSeries
->hasPropertyMapping("FillColor");
251 bool bHasBorderColorMapping
= pSeries
->hasPropertyMapping("LineColor");
253 uno::Reference
< drawing::XShapes
> xSeriesGroupShape_Shapes
= getSeriesGroupShape(*aSeriesIter
, xSeriesTarget
);
255 sal_Int32 nAttachedAxisIndex
= pSeries
->getAttachedAxisIndex();
256 PlottingPositionHelper
* pPosHelper
= &(this->getPlottingPositionHelper( nAttachedAxisIndex
));
258 pPosHelper
= m_pMainPosHelper
;
259 PlotterBase::m_pPosHelper
= pPosHelper
;
261 //collect data point information (logic coordinates, style ):
262 double fLogicX
= pSeries
->getXValue(nIndex
);
263 double fLogicY
= pSeries
->getYValue(nIndex
);
264 double fBubbleSize
= pSeries
->getBubble_Size( nIndex
);
266 if( !m_bShowNegativeValues
&& fBubbleSize
<0.0 )
269 if( ::rtl::math::approxEqual( fBubbleSize
, 0.0 ) || ::rtl::math::isNan(fBubbleSize
) )
272 if( ::rtl::math::isNan(fLogicX
) || ::rtl::math::isInf(fLogicX
)
273 || ::rtl::math::isNan(fLogicY
) || ::rtl::math::isInf(fLogicY
) )
276 bool bIsVisible
= pPosHelper
->isLogicVisible( fLogicX
, fLogicY
, fLogicZ
);
278 drawing::Position3D
aUnscaledLogicPosition( fLogicX
, fLogicY
, fLogicZ
);
279 drawing::Position3D
aScaledLogicPosition(aUnscaledLogicPosition
);
280 pPosHelper
->doLogicScaling( aScaledLogicPosition
);
282 //transformation 3) -> 4)
283 drawing::Position3D
aScenePosition( pPosHelper
->transformLogicToScene( fLogicX
,fLogicY
,fLogicZ
, false ) );
285 //better performance for big data
286 FormerPoint
aFormerPoint( aSeriesFormerPointMap
[pSeries
] );
287 pPosHelper
->setCoordinateSystemResolution( m_aCoordinateSystemResolution
);
288 if( !pSeries
->isAttributedDataPoint(nIndex
)
290 pPosHelper
->isSameForGivenResolution( aFormerPoint
.m_fX
, aFormerPoint
.m_fY
, aFormerPoint
.m_fZ
291 , aScaledLogicPosition
.PositionX
, aScaledLogicPosition
.PositionY
, aScaledLogicPosition
.PositionZ
) )
294 m_bPointsWereSkipped
= true;
297 aSeriesFormerPointMap
[pSeries
] = FormerPoint(aScaledLogicPosition
.PositionX
, aScaledLogicPosition
.PositionY
, aScaledLogicPosition
.PositionZ
);
299 //create a single datapoint if point is visible
303 //create a group shape for this point and add to the series shape:
304 OUString aPointCID
= ObjectIdentifier::createPointCID(
305 pSeries
->getPointCID_Stub(), nIndex
);
306 uno::Reference
< drawing::XShapes
> xPointGroupShape_Shapes(
307 createGroupShape(xSeriesGroupShape_Shapes
,aPointCID
) );
308 uno::Reference
<drawing::XShape
> xPointGroupShape_Shape
=
309 uno::Reference
<drawing::XShape
>( xPointGroupShape_Shapes
, uno::UNO_QUERY
);
315 drawing::Direction3D aSymbolSize
= transformToScreenBubbleSize( fBubbleSize
);
316 uno::Reference
<drawing::XShape
> xShape
;
317 xShape
= m_pShapeFactory
->createCircle2D( xPointGroupShape_Shapes
318 , aScenePosition
, aSymbolSize
);
320 setMappedProperties( xShape
321 , pSeries
->getPropertiesOfPoint( nIndex
)
322 , PropertyMapper::getPropertyNameMapForFilledSeriesProperties() );
324 if(bHasFillColorMapping
)
326 double nPropVal
= pSeries
->getValueByProperty(nIndex
, "FillColor");
327 if(!rtl::math::isNan(nPropVal
))
329 uno::Reference
< beans::XPropertySet
> xProps( xShape
, uno::UNO_QUERY_THROW
);
330 xProps
->setPropertyValue("FillColor", uno::makeAny(static_cast<sal_Int32
>(nPropVal
)));
333 if(bHasBorderColorMapping
)
335 double nPropVal
= pSeries
->getValueByProperty(nIndex
, "LineColor");
336 if(!rtl::math::isNan(nPropVal
))
338 uno::Reference
< beans::XPropertySet
> xProps( xShape
, uno::UNO_QUERY_THROW
);
339 xProps
->setPropertyValue("LineColor", uno::makeAny(static_cast<sal_Int32
>(nPropVal
)));
343 ::chart::AbstractShapeFactory::setShapeName( xShape
, "MarkHandles" );
345 //create data point label
346 if( (**aSeriesIter
).getDataPointLabelIfLabel(nIndex
) )
348 LabelAlignment eAlignment
= LABEL_ALIGN_TOP
;
349 drawing::Position3D
aScenePosition3D( aScenePosition
.PositionX
350 , aScenePosition
.PositionY
351 , aScenePosition
.PositionZ
+this->getTransformedDepth() );
353 sal_Int32 nLabelPlacement
= pSeries
->getLabelPlacement( nIndex
, m_xChartTypeModel
, m_nDimension
, pPosHelper
->isSwapXAndY() );
355 switch(nLabelPlacement
)
357 case ::com::sun::star::chart::DataLabelPlacement::TOP
:
358 aScenePosition3D
.PositionY
-= (aSymbolSize
.DirectionY
/2+1);
359 eAlignment
= LABEL_ALIGN_TOP
;
361 case ::com::sun::star::chart::DataLabelPlacement::BOTTOM
:
362 aScenePosition3D
.PositionY
+= (aSymbolSize
.DirectionY
/2+1);
363 eAlignment
= LABEL_ALIGN_BOTTOM
;
365 case ::com::sun::star::chart::DataLabelPlacement::LEFT
:
366 aScenePosition3D
.PositionX
-= (aSymbolSize
.DirectionX
/2+1);
367 eAlignment
= LABEL_ALIGN_LEFT
;
369 case ::com::sun::star::chart::DataLabelPlacement::RIGHT
:
370 aScenePosition3D
.PositionX
+= (aSymbolSize
.DirectionX
/2+1);
371 eAlignment
= LABEL_ALIGN_RIGHT
;
373 case ::com::sun::star::chart::DataLabelPlacement::CENTER
:
374 eAlignment
= LABEL_ALIGN_CENTER
;
377 OSL_FAIL("this label alignment is not implemented yet");
378 aScenePosition3D
.PositionY
-= (aSymbolSize
.DirectionY
/2+1);
379 eAlignment
= LABEL_ALIGN_TOP
;
383 awt::Point
aScreenPosition2D( LabelPositionHelper(pPosHelper
,m_nDimension
,m_xLogicTarget
,m_pShapeFactory
)
384 .transformSceneToScreenPosition( aScenePosition3D
) );
385 sal_Int32 nOffset
= 0;
386 if(LABEL_ALIGN_CENTER
!=eAlignment
)
387 nOffset
= 100;//add some spacing //@todo maybe get more intelligent values
388 createDataLabel( xTextTarget
, **aSeriesIter
, nIndex
389 , fBubbleSize
, fBubbleSize
, aScreenPosition2D
, eAlignment
, nOffset
);
393 //remove PointGroupShape if empty
394 if(!xPointGroupShape_Shapes
->getCount())
395 xSeriesGroupShape_Shapes
->remove(xPointGroupShape_Shape
);
397 }//next series in x slot (next y slot)
403 "skipped points: " << nSkippedPoints
<< " created points: "
409 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */