update dev300-m58
[ooovba.git] / chart2 / source / view / charttypes / BubbleChart.cxx
blob0189689b78bef4f3379d123f385dde3da7f01b00
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ,v $
10 * $Revision: $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
34 #include "BubbleChart.hxx"
35 #include "PlottingPositionHelper.hxx"
36 #include "ShapeFactory.hxx"
37 #include "CommonConverters.hxx"
38 #include "macros.hxx"
39 #include "ViewDefines.hxx"
40 #include "ObjectIdentifier.hxx"
41 #include "Splines.hxx"
42 #include "LabelPositionHelper.hxx"
43 #include "Clipping.hxx"
44 #include "Stripe.hxx"
46 #include <com/sun/star/chart2/Symbol.hpp>
47 #include <com/sun/star/chart/DataLabelPlacement.hpp>
48 #include <tools/debug.hxx>
49 #include <svx/unoprnms.hxx>
50 #include <rtl/math.hxx>
51 #include <com/sun/star/drawing/DoubleSequence.hpp>
52 #include <com/sun/star/drawing/NormalsKind.hpp>
53 #include <com/sun/star/lang/XServiceName.hpp>
55 //.............................................................................
56 namespace chart
58 //.............................................................................
59 using namespace ::com::sun::star;
60 using namespace ::rtl::math;
61 using namespace ::com::sun::star::chart2;
63 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 BubbleChart::BubbleChart( const uno::Reference<XChartType>& xChartTypeModel
68 , sal_Int32 nDimensionCount )
69 : VSeriesPlotter( xChartTypeModel, nDimensionCount, false )
70 , m_bShowNegativeValues(false)
71 , m_bBubbleSizeAsArea(true)
72 , m_fBubbleSizeScaling(1.0)
73 , m_fMaxLogicBubbleSize( 0.0 )
74 , m_fBubbleSizeFactorToScreen( 1.0 )
76 if( !m_pMainPosHelper )
77 m_pMainPosHelper = new PlottingPositionHelper();
78 PlotterBase::m_pPosHelper = m_pMainPosHelper;
79 VSeriesPlotter::m_pMainPosHelper = m_pMainPosHelper;
82 BubbleChart::~BubbleChart()
84 delete m_pMainPosHelper;
87 void BubbleChart::calculateMaximumLogicBubbleSize()
89 double fMaxSize = 0.0;
91 sal_Int32 nStartIndex = 0;
92 sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
93 for( sal_Int32 nIndex = nStartIndex; nIndex < nEndIndex; nIndex++ )
95 ::std::vector< ::std::vector< VDataSeriesGroup > >::iterator aZSlotIter = m_aZSlots.begin();
96 const ::std::vector< ::std::vector< VDataSeriesGroup > >::const_iterator aZSlotEnd = m_aZSlots.end();
97 for( ; aZSlotIter != aZSlotEnd; aZSlotIter++ )
99 ::std::vector< VDataSeriesGroup >::iterator aXSlotIter = aZSlotIter->begin();
100 const ::std::vector< VDataSeriesGroup >::const_iterator aXSlotEnd = aZSlotIter->end();
101 for( ; aXSlotIter != aXSlotEnd; aXSlotIter++ )
103 ::std::vector< VDataSeries* >* pSeriesList = &(aXSlotIter->m_aSeriesVector);
104 ::std::vector< VDataSeries* >::const_iterator aSeriesIter = pSeriesList->begin();
105 const ::std::vector< VDataSeries* >::const_iterator aSeriesEnd = pSeriesList->end();
106 for( ; aSeriesIter != aSeriesEnd; aSeriesIter++ )
108 VDataSeries* pSeries( *aSeriesIter );
109 if(!pSeries)
110 continue;
112 double fSize = pSeries->getBubble_Size( nIndex );
113 if( m_bShowNegativeValues )
114 fSize = fabs(fSize);
115 if( fSize > fMaxSize )
116 fMaxSize = fSize;
122 m_fMaxLogicBubbleSize = fMaxSize;
125 void BubbleChart::calculateBubbleSizeScalingFactor()
127 double fLogicZ=0.5;
128 drawing::Position3D aSceneMinPos( m_pMainPosHelper->transformLogicToScene( m_pMainPosHelper->getLogicMinX(),m_pMainPosHelper->getLogicMinY(),fLogicZ, false ) );
129 drawing::Position3D aSceneMaxPos( m_pMainPosHelper->transformLogicToScene( m_pMainPosHelper->getLogicMaxX(),m_pMainPosHelper->getLogicMaxY(),fLogicZ, false ) );
131 awt::Point aScreenMinPos( LabelPositionHelper(m_pMainPosHelper,m_nDimension,m_xLogicTarget,m_pShapeFactory).transformSceneToScreenPosition( aSceneMinPos ) );
132 awt::Point aScreenMaxPos( LabelPositionHelper(m_pMainPosHelper,m_nDimension,m_xLogicTarget,m_pShapeFactory).transformSceneToScreenPosition( aSceneMaxPos ) );
134 sal_Int32 nWidth = abs( aScreenMaxPos.X - aScreenMinPos.X );
135 sal_Int32 nHeight = abs( aScreenMaxPos.Y - aScreenMinPos.Y );
137 sal_Int32 nMinExtend = std::min( nWidth, nHeight );
138 m_fBubbleSizeFactorToScreen = nMinExtend * 0.25;//max bubble size is 25 percent of diagram size
141 drawing::Direction3D BubbleChart::transformToScreenBubbleSize( double fLogicSize )
143 drawing::Direction3D aRet(0,0,0);
145 if( ::rtl::math::isNan(fLogicSize) || ::rtl::math::isInf(fLogicSize) )
146 return aRet;
148 if( m_bShowNegativeValues )
149 fLogicSize = fabs(fLogicSize);
151 double fMaxSize = m_fMaxLogicBubbleSize;
153 double fMaxRadius = fMaxSize;
154 double fRaduis = fLogicSize;
155 if( m_bBubbleSizeAsArea )
157 fMaxRadius = sqrt( fMaxSize / F_PI );
158 fRaduis = sqrt( fLogicSize / F_PI );
161 aRet.DirectionX = m_fBubbleSizeScaling * m_fBubbleSizeFactorToScreen * fRaduis / fMaxRadius;
162 aRet.DirectionY = aRet.DirectionX;
164 return aRet;
167 bool BubbleChart::isExpandIfValuesCloseToBorder( sal_Int32 /*nDimensionIndex*/ )
169 return true;
172 bool BubbleChart::isSeperateStackingForDifferentSigns( sal_Int32 /*nDimensionIndex*/ )
174 return false;
177 //-----------------------------------------------------------------
179 LegendSymbolStyle BubbleChart::getLegendSymbolStyle()
181 return chart2::LegendSymbolStyle_CIRCLE;
184 drawing::Direction3D BubbleChart::getPreferredDiagramAspectRatio() const
186 return drawing::Direction3D(-1,-1,-1);
189 void BubbleChart::addSeries( VDataSeries* pSeries, sal_Int32 zSlot, sal_Int32 xSlot, sal_Int32 ySlot )
191 VSeriesPlotter::addSeries( pSeries, zSlot, xSlot, ySlot );
194 //better performance for big data
195 struct FormerPoint
197 FormerPoint( double fX, double fY, double fZ )
198 : m_fX(fX), m_fY(fY), m_fZ(fZ)
200 FormerPoint()
202 ::rtl::math::setNan( &m_fX );
203 ::rtl::math::setNan( &m_fY );
204 ::rtl::math::setNan( &m_fZ );
207 double m_fX;
208 double m_fY;
209 double m_fZ;
212 void BubbleChart::createShapes()
214 if( m_aZSlots.begin() == m_aZSlots.end() ) //no series
215 return;
217 DBG_ASSERT(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is(),"BubbleChart is not proper initialized");
218 if(!(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is()))
219 return;
221 //therefore create an own group for the texts and the error bars to move them to front
222 //(because the text group is created after the series group the texts are displayed on top)
223 uno::Reference< drawing::XShapes > xSeriesTarget(
224 createGroupShape( m_xLogicTarget,rtl::OUString() ));
225 uno::Reference< drawing::XShapes > xTextTarget(
226 m_pShapeFactory->createGroup2D( m_xFinalTarget,rtl::OUString() ));
228 //update/create information for current group
229 double fLogicZ = 0.5;//as defined
231 sal_Int32 nStartIndex = 0; // inclusive ;..todo get somehow from x scale
232 sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
233 if(nEndIndex<=0)
234 nEndIndex=1;
236 //better performance for big data
237 std::map< VDataSeries*, FormerPoint > aSeriesFormerPointMap;
238 m_bPointsWereSkipped = false;
239 sal_Int32 nSkippedPoints = 0;
240 sal_Int32 nCreatedPoints = 0;
243 calculateMaximumLogicBubbleSize();
244 calculateBubbleSizeScalingFactor();
245 if( m_fMaxLogicBubbleSize <= 0 || m_fBubbleSizeFactorToScreen <= 0 )
246 return;
248 //=============================================================================
249 //iterate through all x values per indices
250 for( sal_Int32 nIndex = nStartIndex; nIndex < nEndIndex; nIndex++ )
252 ::std::vector< ::std::vector< VDataSeriesGroup > >::iterator aZSlotIter = m_aZSlots.begin();
253 const ::std::vector< ::std::vector< VDataSeriesGroup > >::const_iterator aZSlotEnd = m_aZSlots.end();
255 aZSlotIter = m_aZSlots.begin();
256 for( sal_Int32 nZ=1; aZSlotIter != aZSlotEnd; aZSlotIter++, nZ++ )
258 ::std::vector< VDataSeriesGroup >::iterator aXSlotIter = aZSlotIter->begin();
259 const ::std::vector< VDataSeriesGroup >::const_iterator aXSlotEnd = aZSlotIter->end();
261 aXSlotIter = aZSlotIter->begin();
262 for( sal_Int32 nX=0; aXSlotIter != aXSlotEnd; aXSlotIter++, nX++ )
264 ::std::vector< VDataSeries* >* pSeriesList = &(aXSlotIter->m_aSeriesVector);
265 ::std::vector< VDataSeries* >::const_iterator aSeriesIter = pSeriesList->begin();
266 const ::std::vector< VDataSeries* >::const_iterator aSeriesEnd = pSeriesList->end();
268 //=============================================================================
269 //iterate through all series
270 for( sal_Int32 nSeriesIndex = 0; aSeriesIter != aSeriesEnd; aSeriesIter++, nSeriesIndex++ )
272 VDataSeries* pSeries( *aSeriesIter );
273 if(!pSeries)
274 continue;
276 uno::Reference< drawing::XShapes > xSeriesGroupShape_Shapes = getSeriesGroupShape(*aSeriesIter, xSeriesTarget);
278 sal_Int32 nAttachedAxisIndex = pSeries->getAttachedAxisIndex();
279 PlottingPositionHelper* pPosHelper = &(this->getPlottingPositionHelper( nAttachedAxisIndex ));
280 if(!pPosHelper)
281 pPosHelper = m_pMainPosHelper;
282 PlotterBase::m_pPosHelper = pPosHelper;
284 if(m_nDimension==3)
285 fLogicZ = nZ+0.5;
287 //collect data point information (logic coordinates, style ):
288 double fLogicX = pSeries->getXValue(nIndex);
289 double fLogicY = pSeries->getYValue(nIndex);
290 double fBubbleSize = pSeries->getBubble_Size( nIndex );
292 if( !m_bShowNegativeValues && fBubbleSize<0.0 )
293 continue;
295 if( ::rtl::math::approxEqual( fBubbleSize, 0.0 ) || ::rtl::math::isNan(fBubbleSize) )
296 continue;
298 if( ::rtl::math::isNan(fLogicX) || ::rtl::math::isInf(fLogicX)
299 || ::rtl::math::isNan(fLogicY) || ::rtl::math::isInf(fLogicY) )
300 continue;
302 bool bIsVisible = pPosHelper->isLogicVisible( fLogicX, fLogicY, fLogicZ );
304 drawing::Position3D aUnscaledLogicPosition( fLogicX, fLogicY, fLogicZ );
305 drawing::Position3D aScaledLogicPosition(aUnscaledLogicPosition);
306 pPosHelper->doLogicScaling( aScaledLogicPosition );
308 //transformation 3) -> 4)
309 drawing::Position3D aScenePosition( pPosHelper->transformLogicToScene( fLogicX,fLogicY,fLogicZ, false ) );
311 //better performance for big data
312 FormerPoint aFormerPoint( aSeriesFormerPointMap[pSeries] );
313 pPosHelper->setCoordinateSystemResolution( m_aCoordinateSystemResolution );
314 if( !pSeries->isAttributedDataPoint(nIndex)
316 pPosHelper->isSameForGivenResolution( aFormerPoint.m_fX, aFormerPoint.m_fY, aFormerPoint.m_fZ
317 , aScaledLogicPosition.PositionX, aScaledLogicPosition.PositionY, aScaledLogicPosition.PositionZ ) )
319 nSkippedPoints++;
320 m_bPointsWereSkipped = true;
321 continue;
323 aSeriesFormerPointMap[pSeries] = FormerPoint(aScaledLogicPosition.PositionX, aScaledLogicPosition.PositionY, aScaledLogicPosition.PositionZ);
325 //create a single datapoint if point is visible
326 if( !bIsVisible )
327 continue;
329 //create a group shape for this point and add to the series shape:
330 rtl::OUString aPointCID = ObjectIdentifier::createPointCID(
331 pSeries->getPointCID_Stub(), nIndex );
332 uno::Reference< drawing::XShapes > xPointGroupShape_Shapes(
333 createGroupShape(xSeriesGroupShape_Shapes,aPointCID) );
334 uno::Reference<drawing::XShape> xPointGroupShape_Shape =
335 uno::Reference<drawing::XShape>( xPointGroupShape_Shapes, uno::UNO_QUERY );
338 nCreatedPoints++;
340 //create data point
341 drawing::Direction3D aSymbolSize = transformToScreenBubbleSize( fBubbleSize );
342 if(m_nDimension!=3)
344 uno::Reference<drawing::XShape> xShape;
345 xShape = m_pShapeFactory->createCircle2D( xPointGroupShape_Shapes
346 , aScenePosition, aSymbolSize );
348 this->setMappedProperties( xShape
349 , pSeries->getPropertiesOfPoint( nIndex )
350 , PropertyMapper::getPropertyNameMapForFilledSeriesProperties() );
352 m_pShapeFactory->setShapeName( xShape, C2U("MarkHandles") );
355 //create data point label
356 if( (**aSeriesIter).getDataPointLabelIfLabel(nIndex) )
358 LabelAlignment eAlignment = LABEL_ALIGN_TOP;
359 drawing::Position3D aScenePosition3D( aScenePosition.PositionX
360 , aScenePosition.PositionY
361 , aScenePosition.PositionZ+this->getTransformedDepth() );
363 sal_Int32 nLabelPlacement = pSeries->getLabelPlacement( nIndex, m_xChartTypeModel, m_nDimension, pPosHelper->isSwapXAndY() );
365 switch(nLabelPlacement)
367 case ::com::sun::star::chart::DataLabelPlacement::TOP:
368 aScenePosition3D.PositionY -= (aSymbolSize.DirectionY/2+1);
369 eAlignment = LABEL_ALIGN_TOP;
370 break;
371 case ::com::sun::star::chart::DataLabelPlacement::BOTTOM:
372 aScenePosition3D.PositionY += (aSymbolSize.DirectionY/2+1);
373 eAlignment = LABEL_ALIGN_BOTTOM;
374 break;
375 case ::com::sun::star::chart::DataLabelPlacement::LEFT:
376 aScenePosition3D.PositionX -= (aSymbolSize.DirectionX/2+1);
377 eAlignment = LABEL_ALIGN_LEFT;
378 break;
379 case ::com::sun::star::chart::DataLabelPlacement::RIGHT:
380 aScenePosition3D.PositionX += (aSymbolSize.DirectionX/2+1);
381 eAlignment = LABEL_ALIGN_RIGHT;
382 break;
383 case ::com::sun::star::chart::DataLabelPlacement::CENTER:
384 eAlignment = LABEL_ALIGN_CENTER;
385 break;
386 default:
387 DBG_ERROR("this label alignment is not implemented yet");
388 aScenePosition3D.PositionY -= (aSymbolSize.DirectionY/2+1);
389 eAlignment = LABEL_ALIGN_TOP;
390 break;
394 awt::Point aScreenPosition2D( LabelPositionHelper(pPosHelper,m_nDimension,m_xLogicTarget,m_pShapeFactory)
395 .transformSceneToScreenPosition( aScenePosition3D ) );
396 sal_Int32 nOffset = 0;
397 if(LABEL_ALIGN_CENTER!=eAlignment)
398 nOffset = 100;//add some spacing //@todo maybe get more intelligent values
399 this->createDataLabel( xTextTarget, **aSeriesIter, nIndex
400 , fBubbleSize, fBubbleSize, aScreenPosition2D, eAlignment, nOffset );
404 //remove PointGroupShape if empty
405 if(!xPointGroupShape_Shapes->getCount())
406 xSeriesGroupShape_Shapes->remove(xPointGroupShape_Shape);
408 }//next series in x slot (next y slot)
409 }//next x slot
410 }//next z slot
411 }//next category
412 //=============================================================================
413 //=============================================================================
414 //=============================================================================
415 OSL_TRACE( "\nPPPPPPPPP<<<<<<<<<<<< area chart :: createShapes():: skipped points: %d created points: %d", nSkippedPoints, nCreatedPoints );
418 //.............................................................................
419 } //namespace chart
420 //.............................................................................