Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / chart2 / source / view / charttypes / BubbleChart.cxx
blobced8ddb2f6578688a12ed6d86d9d6b19bf5b513e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "BubbleChart.hxx"
21 #include <PlottingPositionHelper.hxx>
22 #include <AbstractShapeFactory.hxx>
23 #include <CommonConverters.hxx>
24 #include <ViewDefines.hxx>
25 #include <ObjectIdentifier.hxx>
26 #include "Splines.hxx"
27 #include <LabelPositionHelper.hxx>
28 #include <Clipping.hxx>
29 #include <Stripe.hxx>
31 #include <com/sun/star/chart2/Symbol.hpp>
32 #include <com/sun/star/chart/DataLabelPlacement.hpp>
33 #include <editeng/unoprnms.hxx>
34 #include <rtl/math.hxx>
35 #include <com/sun/star/drawing/DoubleSequence.hpp>
36 #include <com/sun/star/drawing/NormalsKind.hpp>
37 #include <com/sun/star/lang/XServiceName.hpp>
39 namespace chart
41 using namespace ::com::sun::star;
42 using namespace ::rtl::math;
43 using namespace ::com::sun::star::chart2;
45 BubbleChart::BubbleChart( const uno::Reference<XChartType>& xChartTypeModel
46 , sal_Int32 nDimensionCount )
47 : VSeriesPlotter( xChartTypeModel, nDimensionCount, false )
48 , m_fBubbleSizeScaling(1.0)
49 , m_fMaxLogicBubbleSize( 0.0 )
50 , m_fBubbleSizeFactorToScreen( 1.0 )
52 // We only support 2 dimensional bubble charts
53 assert(nDimensionCount == 2);
55 if( !m_pMainPosHelper )
56 m_pMainPosHelper = new PlottingPositionHelper();
57 PlotterBase::m_pPosHelper = m_pMainPosHelper;
60 BubbleChart::~BubbleChart()
62 delete m_pMainPosHelper;
65 void BubbleChart::calculateMaximumLogicBubbleSize()
67 double fMaxSize = 0.0;
69 sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
70 for( sal_Int32 nIndex = 0; nIndex < nEndIndex; nIndex++ )
72 for( auto const& rZSlot : m_aZSlots )
74 for( auto const& rXSlot : rZSlot )
76 for( VDataSeries* pSeries : rXSlot.m_aSeriesVector )
78 if(!pSeries)
79 continue;
81 double fSize = pSeries->getBubble_Size( nIndex );
82 if( fSize > fMaxSize )
83 fMaxSize = fSize;
89 m_fMaxLogicBubbleSize = fMaxSize;
92 void BubbleChart::calculateBubbleSizeScalingFactor()
94 double fLogicZ=1.0;
95 drawing::Position3D aSceneMinPos( m_pMainPosHelper->transformLogicToScene( m_pMainPosHelper->getLogicMinX(),m_pMainPosHelper->getLogicMinY(),fLogicZ, false ) );
96 drawing::Position3D aSceneMaxPos( m_pMainPosHelper->transformLogicToScene( m_pMainPosHelper->getLogicMaxX(),m_pMainPosHelper->getLogicMaxY(),fLogicZ, false ) );
98 awt::Point aScreenMinPos( LabelPositionHelper(m_nDimension,m_xLogicTarget,m_pShapeFactory).transformSceneToScreenPosition( aSceneMinPos ) );
99 awt::Point aScreenMaxPos( LabelPositionHelper(m_nDimension,m_xLogicTarget,m_pShapeFactory).transformSceneToScreenPosition( aSceneMaxPos ) );
101 sal_Int32 nWidth = abs( aScreenMaxPos.X - aScreenMinPos.X );
102 sal_Int32 nHeight = abs( aScreenMaxPos.Y - aScreenMinPos.Y );
104 sal_Int32 nMinExtend = std::min( nWidth, nHeight );
105 m_fBubbleSizeFactorToScreen = nMinExtend * 0.25;//max bubble size is 25 percent of diagram size
108 drawing::Direction3D BubbleChart::transformToScreenBubbleSize( double fLogicSize )
110 drawing::Direction3D aRet(0,0,0);
112 if( ::rtl::math::isNan(fLogicSize) || ::rtl::math::isInf(fLogicSize) )
113 return aRet;
115 double fMaxSize = m_fMaxLogicBubbleSize;
117 double fMaxRadius = sqrt( fMaxSize / F_PI );
118 double fRaduis = sqrt( fLogicSize / F_PI );
120 aRet.DirectionX = m_fBubbleSizeScaling * m_fBubbleSizeFactorToScreen * fRaduis / fMaxRadius;
121 aRet.DirectionY = aRet.DirectionX;
123 return aRet;
126 bool BubbleChart::isExpandIfValuesCloseToBorder( sal_Int32 /*nDimensionIndex*/ )
128 return true;
131 bool BubbleChart::isSeparateStackingForDifferentSigns( sal_Int32 /*nDimensionIndex*/ )
133 return false;
136 LegendSymbolStyle BubbleChart::getLegendSymbolStyle()
138 return LegendSymbolStyle_CIRCLE;
141 drawing::Direction3D BubbleChart::getPreferredDiagramAspectRatio() const
143 return drawing::Direction3D(-1,-1,-1);
146 //better performance for big data
147 struct FormerPoint
149 FormerPoint( double fX, double fY, double fZ )
150 : m_fX(fX), m_fY(fY), m_fZ(fZ)
152 FormerPoint()
154 ::rtl::math::setNan( &m_fX );
155 ::rtl::math::setNan( &m_fY );
156 ::rtl::math::setNan( &m_fZ );
159 double m_fX;
160 double m_fY;
161 double m_fZ;
164 void BubbleChart::createShapes()
166 if( m_aZSlots.empty() ) //no series
167 return;
169 OSL_ENSURE(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is(),"BubbleChart is not proper initialized");
170 if(!(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is()))
171 return;
173 //therefore create an own group for the texts and the error bars to move them to front
174 //(because the text group is created after the series group the texts are displayed on top)
175 uno::Reference< drawing::XShapes > xSeriesTarget(
176 createGroupShape( m_xLogicTarget ));
177 uno::Reference< drawing::XShapes > xTextTarget(
178 m_pShapeFactory->createGroup2D( m_xFinalTarget ));
180 //update/create information for current group
181 double fLogicZ = 1.0;//as defined
183 sal_Int32 const nStartIndex = 0; // inclusive ;..todo get somehow from x scale
184 sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
185 if(nEndIndex<=0)
186 nEndIndex=1;
188 //better performance for big data
189 std::map< VDataSeries*, FormerPoint > aSeriesFormerPointMap;
190 m_bPointsWereSkipped = false;
191 sal_Int32 nSkippedPoints = 0;
192 sal_Int32 nCreatedPoints = 0;
194 calculateMaximumLogicBubbleSize();
195 calculateBubbleSizeScalingFactor();
196 if( m_fMaxLogicBubbleSize <= 0 || m_fBubbleSizeFactorToScreen <= 0 )
197 return;
199 //iterate through all x values per indices
200 for( sal_Int32 nIndex = nStartIndex; nIndex < nEndIndex; nIndex++ )
202 for( auto const& rZSlot : m_aZSlots )
204 for( auto const& rXSlot : rZSlot )
206 //iterate through all series
207 for( VDataSeries* pSeries : rXSlot.m_aSeriesVector )
209 if(!pSeries)
210 continue;
212 bool bHasFillColorMapping = pSeries->hasPropertyMapping("FillColor");
213 bool bHasBorderColorMapping = pSeries->hasPropertyMapping("LineColor");
215 uno::Reference< drawing::XShapes > xSeriesGroupShape_Shapes = getSeriesGroupShape(pSeries, xSeriesTarget);
217 sal_Int32 nAttachedAxisIndex = pSeries->getAttachedAxisIndex();
218 PlottingPositionHelper* pPosHelper = &(getPlottingPositionHelper( nAttachedAxisIndex ));
219 if(!pPosHelper)
220 pPosHelper = m_pMainPosHelper;
221 PlotterBase::m_pPosHelper = pPosHelper;
223 //collect data point information (logic coordinates, style ):
224 double fLogicX = pSeries->getXValue(nIndex);
225 double fLogicY = pSeries->getYValue(nIndex);
226 double fBubbleSize = pSeries->getBubble_Size( nIndex );
228 if( fBubbleSize<0.0 )
229 continue;
231 if( fBubbleSize == 0.0 || ::rtl::math::isNan(fBubbleSize) )
232 continue;
234 if( ::rtl::math::isNan(fLogicX) || ::rtl::math::isInf(fLogicX)
235 || ::rtl::math::isNan(fLogicY) || ::rtl::math::isInf(fLogicY) )
236 continue;
238 bool bIsVisible = pPosHelper->isLogicVisible( fLogicX, fLogicY, fLogicZ );
240 drawing::Position3D aUnscaledLogicPosition( fLogicX, fLogicY, fLogicZ );
241 drawing::Position3D aScaledLogicPosition(aUnscaledLogicPosition);
242 pPosHelper->doLogicScaling( aScaledLogicPosition );
244 //transformation 3) -> 4)
245 drawing::Position3D aScenePosition( pPosHelper->transformLogicToScene( fLogicX,fLogicY,fLogicZ, false ) );
247 //better performance for big data
248 FormerPoint aFormerPoint( aSeriesFormerPointMap[pSeries] );
249 pPosHelper->setCoordinateSystemResolution( m_aCoordinateSystemResolution );
250 if( !pSeries->isAttributedDataPoint(nIndex)
252 pPosHelper->isSameForGivenResolution( aFormerPoint.m_fX, aFormerPoint.m_fY, aFormerPoint.m_fZ
253 , aScaledLogicPosition.PositionX, aScaledLogicPosition.PositionY, aScaledLogicPosition.PositionZ ) )
255 nSkippedPoints++;
256 m_bPointsWereSkipped = true;
257 continue;
259 aSeriesFormerPointMap[pSeries] = FormerPoint(aScaledLogicPosition.PositionX, aScaledLogicPosition.PositionY, aScaledLogicPosition.PositionZ);
261 //create a single datapoint if point is visible
262 if( !bIsVisible )
263 continue;
265 //create a group shape for this point and add to the series shape:
266 OUString aPointCID = ObjectIdentifier::createPointCID(
267 pSeries->getPointCID_Stub(), nIndex );
268 uno::Reference< drawing::XShapes > xPointGroupShape_Shapes(
269 createGroupShape(xSeriesGroupShape_Shapes,aPointCID) );
270 uno::Reference<drawing::XShape> xPointGroupShape_Shape =
271 uno::Reference<drawing::XShape>( xPointGroupShape_Shapes, uno::UNO_QUERY );
274 nCreatedPoints++;
276 //create data point
277 drawing::Direction3D aSymbolSize = transformToScreenBubbleSize( fBubbleSize );
278 uno::Reference<drawing::XShape> xShape;
279 xShape = m_pShapeFactory->createCircle2D( xPointGroupShape_Shapes
280 , aScenePosition, aSymbolSize );
282 setMappedProperties( xShape
283 , pSeries->getPropertiesOfPoint( nIndex )
284 , PropertyMapper::getPropertyNameMapForFilledSeriesProperties() );
286 if(bHasFillColorMapping)
288 double nPropVal = pSeries->getValueByProperty(nIndex, "FillColor");
289 if(!rtl::math::isNan(nPropVal))
291 uno::Reference< beans::XPropertySet > xProps( xShape, uno::UNO_QUERY_THROW );
292 xProps->setPropertyValue("FillColor", uno::Any(static_cast<sal_Int32>(nPropVal)));
295 if(bHasBorderColorMapping)
297 double nPropVal = pSeries->getValueByProperty(nIndex, "LineColor");
298 if(!rtl::math::isNan(nPropVal))
300 uno::Reference< beans::XPropertySet > xProps( xShape, uno::UNO_QUERY_THROW );
301 xProps->setPropertyValue("LineColor", uno::Any(static_cast<sal_Int32>(nPropVal)));
305 ::chart::AbstractShapeFactory::setShapeName( xShape, "MarkHandles" );
307 //create data point label
308 if( pSeries->getDataPointLabelIfLabel(nIndex) )
310 LabelAlignment eAlignment = LABEL_ALIGN_TOP;
311 drawing::Position3D aScenePosition3D( aScenePosition.PositionX
312 , aScenePosition.PositionY
313 , aScenePosition.PositionZ+getTransformedDepth() );
315 sal_Int32 nLabelPlacement = pSeries->getLabelPlacement( nIndex, m_xChartTypeModel, pPosHelper->isSwapXAndY() );
317 switch(nLabelPlacement)
319 case css::chart::DataLabelPlacement::TOP:
320 aScenePosition3D.PositionY -= (aSymbolSize.DirectionY/2+1);
321 eAlignment = LABEL_ALIGN_TOP;
322 break;
323 case css::chart::DataLabelPlacement::BOTTOM:
324 aScenePosition3D.PositionY += (aSymbolSize.DirectionY/2+1);
325 eAlignment = LABEL_ALIGN_BOTTOM;
326 break;
327 case css::chart::DataLabelPlacement::LEFT:
328 aScenePosition3D.PositionX -= (aSymbolSize.DirectionX/2+1);
329 eAlignment = LABEL_ALIGN_LEFT;
330 break;
331 case css::chart::DataLabelPlacement::RIGHT:
332 aScenePosition3D.PositionX += (aSymbolSize.DirectionX/2+1);
333 eAlignment = LABEL_ALIGN_RIGHT;
334 break;
335 case css::chart::DataLabelPlacement::CENTER:
336 eAlignment = LABEL_ALIGN_CENTER;
337 break;
338 default:
339 OSL_FAIL("this label alignment is not implemented yet");
340 aScenePosition3D.PositionY -= (aSymbolSize.DirectionY/2+1);
341 eAlignment = LABEL_ALIGN_TOP;
342 break;
345 awt::Point aScreenPosition2D( LabelPositionHelper(m_nDimension,m_xLogicTarget,m_pShapeFactory)
346 .transformSceneToScreenPosition( aScenePosition3D ) );
347 sal_Int32 nOffset = 0;
348 if(eAlignment!=LABEL_ALIGN_CENTER)
349 nOffset = 100;//add some spacing //@todo maybe get more intelligent values
350 createDataLabel( xTextTarget, *pSeries, nIndex
351 , fBubbleSize, fBubbleSize, aScreenPosition2D, eAlignment, nOffset );
355 //remove PointGroupShape if empty
356 if(!xPointGroupShape_Shapes->getCount())
357 xSeriesGroupShape_Shapes->remove(xPointGroupShape_Shape);
359 }//next series in x slot (next y slot)
360 }//next x slot
361 }//next z slot
362 }//next category
363 SAL_INFO(
364 "chart2",
365 "skipped points: " << nSkippedPoints << " created points: "
366 << nCreatedPoints);
369 } //namespace chart
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */