merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / view / charttypes / BubbleChart.cxx
blobc5454edcc6fd0787989e52d171f01a432c6b19d8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
31 #include "BubbleChart.hxx"
32 #include "PlottingPositionHelper.hxx"
33 #include "ShapeFactory.hxx"
34 #include "CommonConverters.hxx"
35 #include "macros.hxx"
36 #include "ViewDefines.hxx"
37 #include "ObjectIdentifier.hxx"
38 #include "Splines.hxx"
39 #include "LabelPositionHelper.hxx"
40 #include "Clipping.hxx"
41 #include "Stripe.hxx"
43 #include <com/sun/star/chart2/Symbol.hpp>
44 #include <com/sun/star/chart/DataLabelPlacement.hpp>
45 #include <tools/debug.hxx>
46 #include <editeng/unoprnms.hxx>
47 #include <rtl/math.hxx>
48 #include <com/sun/star/drawing/DoubleSequence.hpp>
49 #include <com/sun/star/drawing/NormalsKind.hpp>
50 #include <com/sun/star/lang/XServiceName.hpp>
52 //.............................................................................
53 namespace chart
55 //.............................................................................
56 using namespace ::com::sun::star;
57 using namespace ::rtl::math;
58 using namespace ::com::sun::star::chart2;
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 BubbleChart::BubbleChart( const uno::Reference<XChartType>& xChartTypeModel
65 , sal_Int32 nDimensionCount )
66 : VSeriesPlotter( xChartTypeModel, nDimensionCount, false )
67 , m_bShowNegativeValues(false)
68 , m_bBubbleSizeAsArea(true)
69 , m_fBubbleSizeScaling(1.0)
70 , m_fMaxLogicBubbleSize( 0.0 )
71 , m_fBubbleSizeFactorToScreen( 1.0 )
73 if( !m_pMainPosHelper )
74 m_pMainPosHelper = new PlottingPositionHelper();
75 PlotterBase::m_pPosHelper = m_pMainPosHelper;
76 VSeriesPlotter::m_pMainPosHelper = m_pMainPosHelper;
79 BubbleChart::~BubbleChart()
81 delete m_pMainPosHelper;
84 void BubbleChart::calculateMaximumLogicBubbleSize()
86 double fMaxSize = 0.0;
88 sal_Int32 nStartIndex = 0;
89 sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
90 for( sal_Int32 nIndex = nStartIndex; nIndex < nEndIndex; nIndex++ )
92 ::std::vector< ::std::vector< VDataSeriesGroup > >::iterator aZSlotIter = m_aZSlots.begin();
93 const ::std::vector< ::std::vector< VDataSeriesGroup > >::const_iterator aZSlotEnd = m_aZSlots.end();
94 for( ; aZSlotIter != aZSlotEnd; aZSlotIter++ )
96 ::std::vector< VDataSeriesGroup >::iterator aXSlotIter = aZSlotIter->begin();
97 const ::std::vector< VDataSeriesGroup >::const_iterator aXSlotEnd = aZSlotIter->end();
98 for( ; aXSlotIter != aXSlotEnd; aXSlotIter++ )
100 ::std::vector< VDataSeries* >* pSeriesList = &(aXSlotIter->m_aSeriesVector);
101 ::std::vector< VDataSeries* >::const_iterator aSeriesIter = pSeriesList->begin();
102 const ::std::vector< VDataSeries* >::const_iterator aSeriesEnd = pSeriesList->end();
103 for( ; aSeriesIter != aSeriesEnd; aSeriesIter++ )
105 VDataSeries* pSeries( *aSeriesIter );
106 if(!pSeries)
107 continue;
109 double fSize = pSeries->getBubble_Size( nIndex );
110 if( m_bShowNegativeValues )
111 fSize = fabs(fSize);
112 if( fSize > fMaxSize )
113 fMaxSize = fSize;
119 m_fMaxLogicBubbleSize = fMaxSize;
122 void BubbleChart::calculateBubbleSizeScalingFactor()
124 double fLogicZ=0.5;
125 drawing::Position3D aSceneMinPos( m_pMainPosHelper->transformLogicToScene( m_pMainPosHelper->getLogicMinX(),m_pMainPosHelper->getLogicMinY(),fLogicZ, false ) );
126 drawing::Position3D aSceneMaxPos( m_pMainPosHelper->transformLogicToScene( m_pMainPosHelper->getLogicMaxX(),m_pMainPosHelper->getLogicMaxY(),fLogicZ, false ) );
128 awt::Point aScreenMinPos( LabelPositionHelper(m_pMainPosHelper,m_nDimension,m_xLogicTarget,m_pShapeFactory).transformSceneToScreenPosition( aSceneMinPos ) );
129 awt::Point aScreenMaxPos( LabelPositionHelper(m_pMainPosHelper,m_nDimension,m_xLogicTarget,m_pShapeFactory).transformSceneToScreenPosition( aSceneMaxPos ) );
131 sal_Int32 nWidth = abs( aScreenMaxPos.X - aScreenMinPos.X );
132 sal_Int32 nHeight = abs( aScreenMaxPos.Y - aScreenMinPos.Y );
134 sal_Int32 nMinExtend = std::min( nWidth, nHeight );
135 m_fBubbleSizeFactorToScreen = nMinExtend * 0.25;//max bubble size is 25 percent of diagram size
138 drawing::Direction3D BubbleChart::transformToScreenBubbleSize( double fLogicSize )
140 drawing::Direction3D aRet(0,0,0);
142 if( ::rtl::math::isNan(fLogicSize) || ::rtl::math::isInf(fLogicSize) )
143 return aRet;
145 if( m_bShowNegativeValues )
146 fLogicSize = fabs(fLogicSize);
148 double fMaxSize = m_fMaxLogicBubbleSize;
150 double fMaxRadius = fMaxSize;
151 double fRaduis = fLogicSize;
152 if( m_bBubbleSizeAsArea )
154 fMaxRadius = sqrt( fMaxSize / F_PI );
155 fRaduis = sqrt( fLogicSize / F_PI );
158 aRet.DirectionX = m_fBubbleSizeScaling * m_fBubbleSizeFactorToScreen * fRaduis / fMaxRadius;
159 aRet.DirectionY = aRet.DirectionX;
161 return aRet;
164 bool BubbleChart::isExpandIfValuesCloseToBorder( sal_Int32 /*nDimensionIndex*/ )
166 return true;
169 bool BubbleChart::isSeperateStackingForDifferentSigns( sal_Int32 /*nDimensionIndex*/ )
171 return false;
174 //-----------------------------------------------------------------
176 LegendSymbolStyle BubbleChart::getLegendSymbolStyle()
178 return chart2::LegendSymbolStyle_CIRCLE;
181 drawing::Direction3D BubbleChart::getPreferredDiagramAspectRatio() const
183 return drawing::Direction3D(-1,-1,-1);
186 void BubbleChart::addSeries( VDataSeries* pSeries, sal_Int32 zSlot, sal_Int32 xSlot, sal_Int32 ySlot )
188 VSeriesPlotter::addSeries( pSeries, zSlot, xSlot, ySlot );
191 //better performance for big data
192 struct FormerPoint
194 FormerPoint( double fX, double fY, double fZ )
195 : m_fX(fX), m_fY(fY), m_fZ(fZ)
197 FormerPoint()
199 ::rtl::math::setNan( &m_fX );
200 ::rtl::math::setNan( &m_fY );
201 ::rtl::math::setNan( &m_fZ );
204 double m_fX;
205 double m_fY;
206 double m_fZ;
209 void BubbleChart::createShapes()
211 if( m_aZSlots.begin() == m_aZSlots.end() ) //no series
212 return;
214 DBG_ASSERT(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is(),"BubbleChart is not proper initialized");
215 if(!(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is()))
216 return;
218 //therefore create an own group for the texts and the error bars to move them to front
219 //(because the text group is created after the series group the texts are displayed on top)
220 uno::Reference< drawing::XShapes > xSeriesTarget(
221 createGroupShape( m_xLogicTarget,rtl::OUString() ));
222 uno::Reference< drawing::XShapes > xTextTarget(
223 m_pShapeFactory->createGroup2D( m_xFinalTarget,rtl::OUString() ));
225 //update/create information for current group
226 double fLogicZ = 0.5;//as defined
228 sal_Int32 nStartIndex = 0; // inclusive ;..todo get somehow from x scale
229 sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
230 if(nEndIndex<=0)
231 nEndIndex=1;
233 //better performance for big data
234 std::map< VDataSeries*, FormerPoint > aSeriesFormerPointMap;
235 m_bPointsWereSkipped = false;
236 sal_Int32 nSkippedPoints = 0;
237 sal_Int32 nCreatedPoints = 0;
240 calculateMaximumLogicBubbleSize();
241 calculateBubbleSizeScalingFactor();
242 if( m_fMaxLogicBubbleSize <= 0 || m_fBubbleSizeFactorToScreen <= 0 )
243 return;
245 //=============================================================================
246 //iterate through all x values per indices
247 for( sal_Int32 nIndex = nStartIndex; nIndex < nEndIndex; nIndex++ )
249 ::std::vector< ::std::vector< VDataSeriesGroup > >::iterator aZSlotIter = m_aZSlots.begin();
250 const ::std::vector< ::std::vector< VDataSeriesGroup > >::const_iterator aZSlotEnd = m_aZSlots.end();
252 aZSlotIter = m_aZSlots.begin();
253 for( sal_Int32 nZ=1; aZSlotIter != aZSlotEnd; aZSlotIter++, nZ++ )
255 ::std::vector< VDataSeriesGroup >::iterator aXSlotIter = aZSlotIter->begin();
256 const ::std::vector< VDataSeriesGroup >::const_iterator aXSlotEnd = aZSlotIter->end();
258 aXSlotIter = aZSlotIter->begin();
259 for( sal_Int32 nX=0; aXSlotIter != aXSlotEnd; aXSlotIter++, nX++ )
261 ::std::vector< VDataSeries* >* pSeriesList = &(aXSlotIter->m_aSeriesVector);
262 ::std::vector< VDataSeries* >::const_iterator aSeriesIter = pSeriesList->begin();
263 const ::std::vector< VDataSeries* >::const_iterator aSeriesEnd = pSeriesList->end();
265 //=============================================================================
266 //iterate through all series
267 for( sal_Int32 nSeriesIndex = 0; aSeriesIter != aSeriesEnd; aSeriesIter++, nSeriesIndex++ )
269 VDataSeries* pSeries( *aSeriesIter );
270 if(!pSeries)
271 continue;
273 uno::Reference< drawing::XShapes > xSeriesGroupShape_Shapes = getSeriesGroupShape(*aSeriesIter, xSeriesTarget);
275 sal_Int32 nAttachedAxisIndex = pSeries->getAttachedAxisIndex();
276 PlottingPositionHelper* pPosHelper = &(this->getPlottingPositionHelper( nAttachedAxisIndex ));
277 if(!pPosHelper)
278 pPosHelper = m_pMainPosHelper;
279 PlotterBase::m_pPosHelper = pPosHelper;
281 if(m_nDimension==3)
282 fLogicZ = nZ+0.5;
284 //collect data point information (logic coordinates, style ):
285 double fLogicX = pSeries->getXValue(nIndex);
286 double fLogicY = pSeries->getYValue(nIndex);
287 double fBubbleSize = pSeries->getBubble_Size( nIndex );
289 if( !m_bShowNegativeValues && fBubbleSize<0.0 )
290 continue;
292 if( ::rtl::math::approxEqual( fBubbleSize, 0.0 ) || ::rtl::math::isNan(fBubbleSize) )
293 continue;
295 if( ::rtl::math::isNan(fLogicX) || ::rtl::math::isInf(fLogicX)
296 || ::rtl::math::isNan(fLogicY) || ::rtl::math::isInf(fLogicY) )
297 continue;
299 bool bIsVisible = pPosHelper->isLogicVisible( fLogicX, fLogicY, fLogicZ );
301 drawing::Position3D aUnscaledLogicPosition( fLogicX, fLogicY, fLogicZ );
302 drawing::Position3D aScaledLogicPosition(aUnscaledLogicPosition);
303 pPosHelper->doLogicScaling( aScaledLogicPosition );
305 //transformation 3) -> 4)
306 drawing::Position3D aScenePosition( pPosHelper->transformLogicToScene( fLogicX,fLogicY,fLogicZ, false ) );
308 //better performance for big data
309 FormerPoint aFormerPoint( aSeriesFormerPointMap[pSeries] );
310 pPosHelper->setCoordinateSystemResolution( m_aCoordinateSystemResolution );
311 if( !pSeries->isAttributedDataPoint(nIndex)
313 pPosHelper->isSameForGivenResolution( aFormerPoint.m_fX, aFormerPoint.m_fY, aFormerPoint.m_fZ
314 , aScaledLogicPosition.PositionX, aScaledLogicPosition.PositionY, aScaledLogicPosition.PositionZ ) )
316 nSkippedPoints++;
317 m_bPointsWereSkipped = true;
318 continue;
320 aSeriesFormerPointMap[pSeries] = FormerPoint(aScaledLogicPosition.PositionX, aScaledLogicPosition.PositionY, aScaledLogicPosition.PositionZ);
322 //create a single datapoint if point is visible
323 if( !bIsVisible )
324 continue;
326 //create a group shape for this point and add to the series shape:
327 rtl::OUString aPointCID = ObjectIdentifier::createPointCID(
328 pSeries->getPointCID_Stub(), nIndex );
329 uno::Reference< drawing::XShapes > xPointGroupShape_Shapes(
330 createGroupShape(xSeriesGroupShape_Shapes,aPointCID) );
331 uno::Reference<drawing::XShape> xPointGroupShape_Shape =
332 uno::Reference<drawing::XShape>( xPointGroupShape_Shapes, uno::UNO_QUERY );
335 nCreatedPoints++;
337 //create data point
338 drawing::Direction3D aSymbolSize = transformToScreenBubbleSize( fBubbleSize );
339 if(m_nDimension!=3)
341 uno::Reference<drawing::XShape> xShape;
342 xShape = m_pShapeFactory->createCircle2D( xPointGroupShape_Shapes
343 , aScenePosition, aSymbolSize );
345 this->setMappedProperties( xShape
346 , pSeries->getPropertiesOfPoint( nIndex )
347 , PropertyMapper::getPropertyNameMapForFilledSeriesProperties() );
349 m_pShapeFactory->setShapeName( xShape, C2U("MarkHandles") );
352 //create data point label
353 if( (**aSeriesIter).getDataPointLabelIfLabel(nIndex) )
355 LabelAlignment eAlignment = LABEL_ALIGN_TOP;
356 drawing::Position3D aScenePosition3D( aScenePosition.PositionX
357 , aScenePosition.PositionY
358 , aScenePosition.PositionZ+this->getTransformedDepth() );
360 sal_Int32 nLabelPlacement = pSeries->getLabelPlacement( nIndex, m_xChartTypeModel, m_nDimension, pPosHelper->isSwapXAndY() );
362 switch(nLabelPlacement)
364 case ::com::sun::star::chart::DataLabelPlacement::TOP:
365 aScenePosition3D.PositionY -= (aSymbolSize.DirectionY/2+1);
366 eAlignment = LABEL_ALIGN_TOP;
367 break;
368 case ::com::sun::star::chart::DataLabelPlacement::BOTTOM:
369 aScenePosition3D.PositionY += (aSymbolSize.DirectionY/2+1);
370 eAlignment = LABEL_ALIGN_BOTTOM;
371 break;
372 case ::com::sun::star::chart::DataLabelPlacement::LEFT:
373 aScenePosition3D.PositionX -= (aSymbolSize.DirectionX/2+1);
374 eAlignment = LABEL_ALIGN_LEFT;
375 break;
376 case ::com::sun::star::chart::DataLabelPlacement::RIGHT:
377 aScenePosition3D.PositionX += (aSymbolSize.DirectionX/2+1);
378 eAlignment = LABEL_ALIGN_RIGHT;
379 break;
380 case ::com::sun::star::chart::DataLabelPlacement::CENTER:
381 eAlignment = LABEL_ALIGN_CENTER;
382 break;
383 default:
384 DBG_ERROR("this label alignment is not implemented yet");
385 aScenePosition3D.PositionY -= (aSymbolSize.DirectionY/2+1);
386 eAlignment = LABEL_ALIGN_TOP;
387 break;
391 awt::Point aScreenPosition2D( LabelPositionHelper(pPosHelper,m_nDimension,m_xLogicTarget,m_pShapeFactory)
392 .transformSceneToScreenPosition( aScenePosition3D ) );
393 sal_Int32 nOffset = 0;
394 if(LABEL_ALIGN_CENTER!=eAlignment)
395 nOffset = 100;//add some spacing //@todo maybe get more intelligent values
396 this->createDataLabel( xTextTarget, **aSeriesIter, nIndex
397 , fBubbleSize, fBubbleSize, aScreenPosition2D, eAlignment, nOffset );
401 //remove PointGroupShape if empty
402 if(!xPointGroupShape_Shapes->getCount())
403 xSeriesGroupShape_Shapes->remove(xPointGroupShape_Shape);
405 }//next series in x slot (next y slot)
406 }//next x slot
407 }//next z slot
408 }//next category
409 //=============================================================================
410 //=============================================================================
411 //=============================================================================
412 OSL_TRACE( "\nPPPPPPPPP<<<<<<<<<<<< area chart :: createShapes():: skipped points: %d created points: %d", nSkippedPoints, nCreatedPoints );
415 //.............................................................................
416 } //namespace chart
417 //.............................................................................