1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_chart2.hxx"
26 
27 #include "BubbleChart.hxx"
28 #include "PlottingPositionHelper.hxx"
29 #include "ShapeFactory.hxx"
30 #include "CommonConverters.hxx"
31 #include "macros.hxx"
32 #include "ViewDefines.hxx"
33 #include "ObjectIdentifier.hxx"
34 #include "Splines.hxx"
35 #include "LabelPositionHelper.hxx"
36 #include "Clipping.hxx"
37 #include "Stripe.hxx"
38 
39 #include <com/sun/star/chart2/Symbol.hpp>
40 #include <com/sun/star/chart/DataLabelPlacement.hpp>
41 #include <tools/debug.hxx>
42 #include <editeng/unoprnms.hxx>
43 #include <rtl/math.hxx>
44 #include <com/sun/star/drawing/DoubleSequence.hpp>
45 #include <com/sun/star/drawing/NormalsKind.hpp>
46 #include <com/sun/star/lang/XServiceName.hpp>
47 
48 //.............................................................................
49 namespace chart
50 {
51 //.............................................................................
52 using namespace ::com::sun::star;
53 using namespace ::rtl::math;
54 using namespace ::com::sun::star::chart2;
55 
56 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 
BubbleChart(const uno::Reference<XChartType> & xChartTypeModel,sal_Int32 nDimensionCount)60 BubbleChart::BubbleChart( const uno::Reference<XChartType>& xChartTypeModel
61                      , sal_Int32 nDimensionCount )
62         : VSeriesPlotter( xChartTypeModel, nDimensionCount, false )
63         , m_bShowNegativeValues(false)
64         , m_bBubbleSizeAsArea(true)
65         , m_fBubbleSizeScaling(1.0)
66         , m_fMaxLogicBubbleSize( 0.0 )
67         , m_fBubbleSizeFactorToScreen( 1.0 )
68 {
69     if( !m_pMainPosHelper )
70         m_pMainPosHelper = new PlottingPositionHelper();
71     PlotterBase::m_pPosHelper = m_pMainPosHelper;
72     VSeriesPlotter::m_pMainPosHelper = m_pMainPosHelper;
73 }
74 
~BubbleChart()75 BubbleChart::~BubbleChart()
76 {
77     delete m_pMainPosHelper;
78 }
79 
calculateMaximumLogicBubbleSize()80 void BubbleChart::calculateMaximumLogicBubbleSize()
81 {
82     double fMaxSize = 0.0;
83 
84     sal_Int32 nStartIndex = 0;
85     sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
86     for( sal_Int32 nIndex = nStartIndex; nIndex < nEndIndex; nIndex++ )
87     {
88         ::std::vector< ::std::vector< VDataSeriesGroup > >::iterator             aZSlotIter = m_aZSlots.begin();
89         const ::std::vector< ::std::vector< VDataSeriesGroup > >::const_iterator  aZSlotEnd = m_aZSlots.end();
90         for( ; aZSlotIter != aZSlotEnd; aZSlotIter++ )
91         {
92             ::std::vector< VDataSeriesGroup >::iterator             aXSlotIter = aZSlotIter->begin();
93             const ::std::vector< VDataSeriesGroup >::const_iterator aXSlotEnd = aZSlotIter->end();
94             for( ; aXSlotIter != aXSlotEnd; aXSlotIter++ )
95             {
96                 ::std::vector< VDataSeries* >* pSeriesList = &(aXSlotIter->m_aSeriesVector);
97                 ::std::vector< VDataSeries* >::const_iterator       aSeriesIter = pSeriesList->begin();
98                 const ::std::vector< VDataSeries* >::const_iterator aSeriesEnd  = pSeriesList->end();
99                 for( ; aSeriesIter != aSeriesEnd; aSeriesIter++ )
100                 {
101                     VDataSeries* pSeries( *aSeriesIter );
102                     if(!pSeries)
103                         continue;
104 
105                     double fSize = pSeries->getBubble_Size( nIndex );
106                     if( m_bShowNegativeValues )
107                         fSize = fabs(fSize);
108                     if( fSize > fMaxSize )
109                         fMaxSize = fSize;
110                 }
111             }
112         }
113     }
114 
115     m_fMaxLogicBubbleSize = fMaxSize;
116 }
117 
calculateBubbleSizeScalingFactor()118 void BubbleChart::calculateBubbleSizeScalingFactor()
119 {
120     double fLogicZ=1.0;
121     drawing::Position3D aSceneMinPos( m_pMainPosHelper->transformLogicToScene( m_pMainPosHelper->getLogicMinX(),m_pMainPosHelper->getLogicMinY(),fLogicZ, false ) );
122     drawing::Position3D aSceneMaxPos( m_pMainPosHelper->transformLogicToScene( m_pMainPosHelper->getLogicMaxX(),m_pMainPosHelper->getLogicMaxY(),fLogicZ, false ) );
123 
124     awt::Point aScreenMinPos( LabelPositionHelper(m_pMainPosHelper,m_nDimension,m_xLogicTarget,m_pShapeFactory).transformSceneToScreenPosition( aSceneMinPos ) );
125     awt::Point aScreenMaxPos( LabelPositionHelper(m_pMainPosHelper,m_nDimension,m_xLogicTarget,m_pShapeFactory).transformSceneToScreenPosition( aSceneMaxPos ) );
126 
127     sal_Int32 nWidth = abs( aScreenMaxPos.X - aScreenMinPos.X );
128     sal_Int32 nHeight = abs( aScreenMaxPos.Y - aScreenMinPos.Y );
129 
130     sal_Int32 nMinExtend = std::min( nWidth, nHeight );
131     m_fBubbleSizeFactorToScreen = nMinExtend * 0.25;//max bubble size is 25 percent of diagram size
132 }
133 
transformToScreenBubbleSize(double fLogicSize)134 drawing::Direction3D BubbleChart::transformToScreenBubbleSize( double fLogicSize )
135 {
136     drawing::Direction3D aRet(0,0,0);
137 
138     if( ::rtl::math::isNan(fLogicSize) || ::rtl::math::isInf(fLogicSize) )
139         return aRet;
140 
141     if( m_bShowNegativeValues )
142         fLogicSize = fabs(fLogicSize);
143 
144     double fMaxSize = m_fMaxLogicBubbleSize;
145 
146     double fMaxRadius = fMaxSize;
147     double fRaduis = fLogicSize;
148     if( m_bBubbleSizeAsArea )
149     {
150         fMaxRadius = sqrt( fMaxSize / F_PI );
151         fRaduis = sqrt( fLogicSize / F_PI );
152     }
153 
154     aRet.DirectionX = m_fBubbleSizeScaling * m_fBubbleSizeFactorToScreen * fRaduis / fMaxRadius;
155     aRet.DirectionY = aRet.DirectionX;
156 
157     return aRet;
158 }
159 
isExpandIfValuesCloseToBorder(sal_Int32)160 bool BubbleChart::isExpandIfValuesCloseToBorder( sal_Int32 /*nDimensionIndex*/ )
161 {
162     return true;
163 }
164 
isSeperateStackingForDifferentSigns(sal_Int32)165 bool BubbleChart::isSeperateStackingForDifferentSigns( sal_Int32 /*nDimensionIndex*/ )
166 {
167     return false;
168 }
169 
170 //-----------------------------------------------------------------
171 
getLegendSymbolStyle()172 LegendSymbolStyle BubbleChart::getLegendSymbolStyle()
173 {
174     return LegendSymbolStyle_CIRCLE;
175 }
176 
getPreferredDiagramAspectRatio() const177 drawing::Direction3D BubbleChart::getPreferredDiagramAspectRatio() const
178 {
179     return drawing::Direction3D(-1,-1,-1);
180 }
181 
addSeries(VDataSeries * pSeries,sal_Int32 zSlot,sal_Int32 xSlot,sal_Int32 ySlot)182 void BubbleChart::addSeries( VDataSeries* pSeries, sal_Int32 zSlot, sal_Int32 xSlot, sal_Int32 ySlot )
183 {
184     VSeriesPlotter::addSeries( pSeries, zSlot, xSlot, ySlot );
185 }
186 
187 //better performance for big data
188 struct FormerPoint
189 {
FormerPointchart::FormerPoint190     FormerPoint( double fX, double fY, double fZ )
191         : m_fX(fX), m_fY(fY), m_fZ(fZ)
192         {}
FormerPointchart::FormerPoint193     FormerPoint()
194     {
195         ::rtl::math::setNan( &m_fX );
196         ::rtl::math::setNan( &m_fY );
197         ::rtl::math::setNan( &m_fZ );
198     }
199 
200     double m_fX;
201     double m_fY;
202     double m_fZ;
203 };
204 
createShapes()205 void BubbleChart::createShapes()
206 {
207     if( m_aZSlots.begin() == m_aZSlots.end() ) //no series
208         return;
209 
210     DBG_ASSERT(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is(),"BubbleChart is not proper initialized");
211     if(!(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is()))
212         return;
213 
214     //therefore create an own group for the texts and the error bars to move them to front
215     //(because the text group is created after the series group the texts are displayed on top)
216     uno::Reference< drawing::XShapes > xSeriesTarget(
217         createGroupShape( m_xLogicTarget,rtl::OUString() ));
218     uno::Reference< drawing::XShapes > xTextTarget(
219         m_pShapeFactory->createGroup2D( m_xFinalTarget,rtl::OUString() ));
220 
221     //update/create information for current group
222     double fLogicZ        = 1.0;//as defined
223 
224     sal_Int32 nStartIndex = 0; // inclusive       ;..todo get somehow from x scale
225     sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
226     if(nEndIndex<=0)
227         nEndIndex=1;
228 
229     //better performance for big data
230     std::map< VDataSeries*, FormerPoint > aSeriesFormerPointMap;
231     m_bPointsWereSkipped = false;
232     sal_Int32 nSkippedPoints = 0;
233     sal_Int32 nCreatedPoints = 0;
234     //
235 
236     calculateMaximumLogicBubbleSize();
237     calculateBubbleSizeScalingFactor();
238     if( m_fMaxLogicBubbleSize <= 0 || m_fBubbleSizeFactorToScreen <= 0 )
239         return;
240 
241 //=============================================================================
242     //iterate through all x values per indices
243     for( sal_Int32 nIndex = nStartIndex; nIndex < nEndIndex; nIndex++ )
244     {
245         ::std::vector< ::std::vector< VDataSeriesGroup > >::iterator             aZSlotIter = m_aZSlots.begin();
246         const ::std::vector< ::std::vector< VDataSeriesGroup > >::const_iterator  aZSlotEnd = m_aZSlots.end();
247 
248         aZSlotIter = m_aZSlots.begin();
249         for( sal_Int32 nZ=1; aZSlotIter != aZSlotEnd; aZSlotIter++, nZ++ )
250         {
251             ::std::vector< VDataSeriesGroup >::iterator             aXSlotIter = aZSlotIter->begin();
252             const ::std::vector< VDataSeriesGroup >::const_iterator aXSlotEnd = aZSlotIter->end();
253 
254             aXSlotIter = aZSlotIter->begin();
255             for( sal_Int32 nX=0; aXSlotIter != aXSlotEnd; aXSlotIter++, nX++ )
256             {
257                 ::std::vector< VDataSeries* >* pSeriesList = &(aXSlotIter->m_aSeriesVector);
258                 ::std::vector< VDataSeries* >::const_iterator       aSeriesIter = pSeriesList->begin();
259                 const ::std::vector< VDataSeries* >::const_iterator aSeriesEnd  = pSeriesList->end();
260 
261     //=============================================================================
262                 //iterate through all series
263                 for( sal_Int32 nSeriesIndex = 0; aSeriesIter != aSeriesEnd; aSeriesIter++, nSeriesIndex++ )
264                 {
265                     VDataSeries* pSeries( *aSeriesIter );
266                     if(!pSeries)
267                         continue;
268 
269                     uno::Reference< drawing::XShapes > xSeriesGroupShape_Shapes = getSeriesGroupShape(*aSeriesIter, xSeriesTarget);
270 
271                     sal_Int32 nAttachedAxisIndex = pSeries->getAttachedAxisIndex();
272                     PlottingPositionHelper* pPosHelper = &(this->getPlottingPositionHelper( nAttachedAxisIndex ));
273                     if(!pPosHelper)
274                         pPosHelper = m_pMainPosHelper;
275                     PlotterBase::m_pPosHelper = pPosHelper;
276 
277                     if(m_nDimension==3)
278                         fLogicZ = nZ+0.5;
279 
280                     //collect data point information (logic coordinates, style ):
281                     double fLogicX = pSeries->getXValue(nIndex);
282                     double fLogicY = pSeries->getYValue(nIndex);
283                     double fBubbleSize = pSeries->getBubble_Size( nIndex );
284 
285                     if( !m_bShowNegativeValues && fBubbleSize<0.0 )
286                         continue;
287 
288                     if( ::rtl::math::approxEqual( fBubbleSize, 0.0 ) || ::rtl::math::isNan(fBubbleSize) )
289                         continue;
290 
291                     if(    ::rtl::math::isNan(fLogicX) || ::rtl::math::isInf(fLogicX)
292                         || ::rtl::math::isNan(fLogicY) || ::rtl::math::isInf(fLogicY) )
293                         continue;
294 
295                     bool bIsVisible = pPosHelper->isLogicVisible( fLogicX, fLogicY, fLogicZ );
296 
297                     drawing::Position3D aUnscaledLogicPosition( fLogicX, fLogicY, fLogicZ );
298                     drawing::Position3D aScaledLogicPosition(aUnscaledLogicPosition);
299                     pPosHelper->doLogicScaling( aScaledLogicPosition );
300 
301                     //transformation 3) -> 4)
302                     drawing::Position3D aScenePosition( pPosHelper->transformLogicToScene( fLogicX,fLogicY,fLogicZ, false ) );
303 
304                     //better performance for big data
305                     FormerPoint aFormerPoint( aSeriesFormerPointMap[pSeries] );
306                     pPosHelper->setCoordinateSystemResolution( m_aCoordinateSystemResolution );
307                     if( !pSeries->isAttributedDataPoint(nIndex)
308                             &&
309                         pPosHelper->isSameForGivenResolution( aFormerPoint.m_fX, aFormerPoint.m_fY, aFormerPoint.m_fZ
310                                                             , aScaledLogicPosition.PositionX, aScaledLogicPosition.PositionY, aScaledLogicPosition.PositionZ ) )
311                     {
312                         nSkippedPoints++;
313                         m_bPointsWereSkipped = true;
314                         continue;
315                     }
316                     aSeriesFormerPointMap[pSeries] = FormerPoint(aScaledLogicPosition.PositionX, aScaledLogicPosition.PositionY, aScaledLogicPosition.PositionZ);
317 
318                     //create a single datapoint if point is visible
319                     if( !bIsVisible )
320                         continue;
321 
322                     //create a group shape for this point and add to the series shape:
323                     rtl::OUString aPointCID = ObjectIdentifier::createPointCID(
324                         pSeries->getPointCID_Stub(), nIndex );
325                     uno::Reference< drawing::XShapes > xPointGroupShape_Shapes(
326                         createGroupShape(xSeriesGroupShape_Shapes,aPointCID) );
327                     uno::Reference<drawing::XShape> xPointGroupShape_Shape =
328                             uno::Reference<drawing::XShape>( xPointGroupShape_Shapes, uno::UNO_QUERY );
329 
330                     {
331                         nCreatedPoints++;
332 
333                         //create data point
334                         drawing::Direction3D aSymbolSize = transformToScreenBubbleSize( fBubbleSize );
335                         if(m_nDimension!=3)
336                         {
337                             uno::Reference<drawing::XShape> xShape;
338                             xShape = m_pShapeFactory->createCircle2D( xPointGroupShape_Shapes
339                                                                       , aScenePosition, aSymbolSize );
340 
341                             this->setMappedProperties( xShape
342                                                        , pSeries->getPropertiesOfPoint( nIndex )
343                                                        , PropertyMapper::getPropertyNameMapForFilledSeriesProperties() );
344 
345                             m_pShapeFactory->setShapeName( xShape, C2U("MarkHandles") );
346                         }
347 
348                         //create data point label
349                         if( (**aSeriesIter).getDataPointLabelIfLabel(nIndex) )
350                         {
351                             LabelAlignment eAlignment = LABEL_ALIGN_TOP;
352                             drawing::Position3D aScenePosition3D( aScenePosition.PositionX
353                                         , aScenePosition.PositionY
354                                         , aScenePosition.PositionZ+this->getTransformedDepth() );
355 
356                             sal_Int32 nLabelPlacement = pSeries->getLabelPlacement( nIndex, m_xChartTypeModel, m_nDimension, pPosHelper->isSwapXAndY() );
357 
358                             switch(nLabelPlacement)
359                             {
360                             case ::com::sun::star::chart::DataLabelPlacement::TOP:
361                                 aScenePosition3D.PositionY -= (aSymbolSize.DirectionY/2+1);
362                                 eAlignment = LABEL_ALIGN_TOP;
363                                 break;
364                             case ::com::sun::star::chart::DataLabelPlacement::BOTTOM:
365                                 aScenePosition3D.PositionY += (aSymbolSize.DirectionY/2+1);
366                                 eAlignment = LABEL_ALIGN_BOTTOM;
367                                 break;
368                             case ::com::sun::star::chart::DataLabelPlacement::LEFT:
369                                 aScenePosition3D.PositionX -= (aSymbolSize.DirectionX/2+1);
370                                 eAlignment = LABEL_ALIGN_LEFT;
371                                 break;
372                             case ::com::sun::star::chart::DataLabelPlacement::RIGHT:
373                                 aScenePosition3D.PositionX += (aSymbolSize.DirectionX/2+1);
374                                 eAlignment = LABEL_ALIGN_RIGHT;
375                                 break;
376                             case ::com::sun::star::chart::DataLabelPlacement::CENTER:
377                                 eAlignment = LABEL_ALIGN_CENTER;
378                                 break;
379                             default:
380                                 DBG_ERROR("this label alignment is not implemented yet");
381                                 aScenePosition3D.PositionY -= (aSymbolSize.DirectionY/2+1);
382                                 eAlignment = LABEL_ALIGN_TOP;
383                                 break;
384                             }
385 
386 
387                             awt::Point aScreenPosition2D( LabelPositionHelper(pPosHelper,m_nDimension,m_xLogicTarget,m_pShapeFactory)
388                                 .transformSceneToScreenPosition( aScenePosition3D ) );
389                             sal_Int32 nOffset = 0;
390                             if(LABEL_ALIGN_CENTER!=eAlignment)
391                                 nOffset = 100;//add some spacing //@todo maybe get more intelligent values
392                             this->createDataLabel( xTextTarget, **aSeriesIter, nIndex
393                                             , fBubbleSize, fBubbleSize, aScreenPosition2D, eAlignment, nOffset );
394                         }
395                     }
396 
397                     //remove PointGroupShape if empty
398                     if(!xPointGroupShape_Shapes->getCount())
399                         xSeriesGroupShape_Shapes->remove(xPointGroupShape_Shape);
400 
401                 }//next series in x slot (next y slot)
402             }//next x slot
403         }//next z slot
404     }//next category
405 //=============================================================================
406 //=============================================================================
407 //=============================================================================
408     OSL_TRACE( "\nPPPPPPPPP<<<<<<<<<<<< area chart :: createShapes():: skipped points: %d created points: %d", nSkippedPoints, nCreatedPoints );
409 }
410 
411 //.............................................................................
412 } //namespace chart
413 //.............................................................................
414