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