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