tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / chart2 / source / view / charttypes / BubbleChart.cxx
blob32593df5d0367067a6083959969888c17dd4728c
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 <ShapeFactory.hxx>
23 #include <ObjectIdentifier.hxx>
24 #include <LabelPositionHelper.hxx>
25 #include <ChartType.hxx>
27 #include <com/sun/star/chart/DataLabelPlacement.hpp>
28 #include <sal/log.hxx>
29 #include <osl/diagnose.h>
31 #include <limits>
33 namespace chart
35 using namespace ::com::sun::star;
37 BubbleChart::BubbleChart( const rtl::Reference<ChartType>& xChartTypeModel
38 , sal_Int32 nDimensionCount )
39 : VSeriesPlotter( xChartTypeModel, nDimensionCount, false )
40 , m_fMaxLogicBubbleSize( 0.0 )
41 , m_fBubbleSizeFactorToScreen( 1.0 )
43 // We only support 2 dimensional bubble charts
44 assert(nDimensionCount == 2);
46 if( !m_pMainPosHelper )
47 m_pMainPosHelper = new PlottingPositionHelper();
48 PlotterBase::m_pPosHelper = m_pMainPosHelper;
51 BubbleChart::~BubbleChart()
53 delete m_pMainPosHelper;
56 void BubbleChart::calculateMaximumLogicBubbleSize()
58 double fMaxSize = 0.0;
60 sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
61 for( sal_Int32 nIndex = 0; nIndex < nEndIndex; nIndex++ )
63 for( auto const& rZSlot : m_aZSlots )
65 for( auto const& rXSlot : rZSlot )
67 for( std::unique_ptr<VDataSeries> const & pSeries : rXSlot.m_aSeriesVector )
69 if(!pSeries)
70 continue;
72 double fSize = pSeries->getBubble_Size( nIndex );
73 if( fSize > fMaxSize )
74 fMaxSize = fSize;
80 m_fMaxLogicBubbleSize = fMaxSize;
83 void BubbleChart::calculateBubbleSizeScalingFactor()
85 double fLogicZ=1.0;
86 drawing::Position3D aSceneMinPos( m_pMainPosHelper->transformLogicToScene( m_pMainPosHelper->getLogicMinX(),m_pMainPosHelper->getLogicMinY(),fLogicZ, false ) );
87 drawing::Position3D aSceneMaxPos( m_pMainPosHelper->transformLogicToScene( m_pMainPosHelper->getLogicMaxX(),m_pMainPosHelper->getLogicMaxY(),fLogicZ, false ) );
89 awt::Point aScreenMinPos( LabelPositionHelper(m_nDimension,m_xLogicTarget).transformSceneToScreenPosition( aSceneMinPos ) );
90 awt::Point aScreenMaxPos( LabelPositionHelper(m_nDimension,m_xLogicTarget).transformSceneToScreenPosition( aSceneMaxPos ) );
92 sal_Int32 nWidth = abs( aScreenMaxPos.X - aScreenMinPos.X );
93 sal_Int32 nHeight = abs( aScreenMaxPos.Y - aScreenMinPos.Y );
95 sal_Int32 nMinExtend = std::min( nWidth, nHeight );
96 m_fBubbleSizeFactorToScreen = nMinExtend * 0.25;//max bubble size is 25 percent of diagram size
99 drawing::Direction3D BubbleChart::transformToScreenBubbleSize( double fLogicSize )
101 drawing::Direction3D aRet(0,0,0);
103 if( std::isnan(fLogicSize) || std::isinf(fLogicSize) )
104 return aRet;
106 double fMaxSize = m_fMaxLogicBubbleSize;
108 double fMaxRadius = sqrt( fMaxSize / M_PI );
109 double fRadius = sqrt( fLogicSize / M_PI );
111 aRet.DirectionX = m_fBubbleSizeFactorToScreen * fRadius / fMaxRadius;
112 aRet.DirectionY = aRet.DirectionX;
114 return aRet;
117 bool BubbleChart::isExpandIfValuesCloseToBorder( sal_Int32 /*nDimensionIndex*/ )
119 return true;
122 bool BubbleChart::isSeparateStackingForDifferentSigns( sal_Int32 /*nDimensionIndex*/ )
124 return false;
127 LegendSymbolStyle BubbleChart::getLegendSymbolStyle()
129 return LegendSymbolStyle::Circle;
132 drawing::Direction3D BubbleChart::getPreferredDiagramAspectRatio() const
134 return drawing::Direction3D(-1,-1,-1);
137 namespace {
139 //better performance for big data
140 struct FormerPoint
142 FormerPoint( double fX, double fY, double fZ )
143 : m_fX(fX), m_fY(fY), m_fZ(fZ)
145 FormerPoint()
146 : m_fX(std::numeric_limits<double>::quiet_NaN())
147 , m_fY(std::numeric_limits<double>::quiet_NaN())
148 , m_fZ(std::numeric_limits<double>::quiet_NaN())
152 double m_fX;
153 double m_fY;
154 double m_fZ;
159 void BubbleChart::createShapes()
161 if( m_aZSlots.empty() ) //no series
162 return;
164 OSL_ENSURE(m_xLogicTarget.is()&&m_xFinalTarget.is(),"BubbleChart is not proper initialized");
165 if(!(m_xLogicTarget.is()&&m_xFinalTarget.is()))
166 return;
168 //therefore create an own group for the texts and the error bars to move them to front
169 //(because the text group is created after the series group the texts are displayed on top)
170 rtl::Reference<SvxShapeGroupAnyD> xSeriesTarget = createGroupShape( m_xLogicTarget );
171 rtl::Reference< SvxShapeGroup > xTextTarget = ShapeFactory::createGroup2D( m_xFinalTarget );
173 //update/create information for current group
174 double fLogicZ = 1.0;//as defined
176 sal_Int32 const nStartIndex = 0; // inclusive ;..todo get somehow from x scale
177 sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
178 if(nEndIndex<=0)
179 nEndIndex=1;
181 //better performance for big data
182 std::map< VDataSeries*, FormerPoint > aSeriesFormerPointMap;
183 m_bPointsWereSkipped = false;
184 sal_Int32 nSkippedPoints = 0;
185 sal_Int32 nCreatedPoints = 0;
187 calculateMaximumLogicBubbleSize();
188 calculateBubbleSizeScalingFactor();
189 if( m_fMaxLogicBubbleSize <= 0 || m_fBubbleSizeFactorToScreen <= 0 )
190 return;
192 //iterate through all x values per indices
193 for( sal_Int32 nIndex = nStartIndex; nIndex < nEndIndex; nIndex++ )
195 for( auto const& rZSlot : m_aZSlots )
197 for( auto const& rXSlot : rZSlot )
199 //iterate through all series
200 for( std::unique_ptr<VDataSeries> const & pSeries : rXSlot.m_aSeriesVector )
202 if(!pSeries)
203 continue;
205 bool bHasFillColorMapping = pSeries->hasPropertyMapping(u"FillColor"_ustr);
206 bool bHasBorderColorMapping = pSeries->hasPropertyMapping(u"LineColor"_ustr);
208 rtl::Reference<SvxShapeGroupAnyD> xSeriesGroupShape_Shapes = getSeriesGroupShape(pSeries.get(), xSeriesTarget);
210 sal_Int32 nAttachedAxisIndex = pSeries->getAttachedAxisIndex();
211 PlottingPositionHelper& rPosHelper
212 = getPlottingPositionHelper(nAttachedAxisIndex);
213 m_pPosHelper = &rPosHelper;
215 //collect data point information (logic coordinates, style ):
216 double fLogicX = pSeries->getXValue(nIndex);
217 double fLogicY = pSeries->getYValue(nIndex);
218 double fBubbleSize = pSeries->getBubble_Size( nIndex );
220 bool bInvertNeg(false);
221 uno::Reference< beans::XPropertySet > xPointProperties =
222 pSeries->getPropertiesOfPoint(nIndex);
224 // check point properties, and if none then series
225 // properties
226 try {
227 xPointProperties->getPropertyValue(u"InvertNegative"_ustr) >>= bInvertNeg;
228 } catch (const uno::Exception&)
230 uno::Reference< beans::XPropertySet > xSeriesProperties =
231 pSeries->getPropertiesOfSeries();
232 try {
233 xSeriesProperties->getPropertyValue(u"InvertNegative"_ustr) >>= bInvertNeg;
234 } catch (const uno::Exception&)
238 if( fBubbleSize<0.0 ) {
239 if (bInvertNeg) {
240 fBubbleSize = -fBubbleSize;
241 } else {
242 continue;
246 if( fBubbleSize == 0.0 || std::isnan(fBubbleSize) )
247 continue;
249 if( std::isnan(fLogicX) || std::isinf(fLogicX)
250 || std::isnan(fLogicY) || std::isinf(fLogicY) )
251 continue;
253 bool bIsVisible = rPosHelper.isLogicVisible(fLogicX, fLogicY, fLogicZ);
255 drawing::Position3D aUnscaledLogicPosition( fLogicX, fLogicY, fLogicZ );
256 drawing::Position3D aScaledLogicPosition(aUnscaledLogicPosition);
257 rPosHelper.doLogicScaling(aScaledLogicPosition);
259 //transformation 3) -> 4)
260 drawing::Position3D aScenePosition(
261 rPosHelper.transformLogicToScene(fLogicX, fLogicY, fLogicZ, false));
263 //better performance for big data
264 uno::Reference< beans::XPropertySet > xProps(pSeries->getPropertiesOfPoint( nIndex ));
265 sal_Int16 nFillTransparency(0);
266 xProps->getPropertyValue(u"FillTransparence"_ustr) >>= nFillTransparency;
267 const bool bIsTransparent(nFillTransparency != 0);
269 FormerPoint aFormerPoint( aSeriesFormerPointMap[pSeries.get()] );
270 rPosHelper.setCoordinateSystemResolution(m_aCoordinateSystemResolution);
271 if (!pSeries->isAttributedDataPoint(nIndex)
272 && !bIsTransparent // don't short-cut if there's transparency
273 && rPosHelper.isSameForGivenResolution(
274 aFormerPoint.m_fX, aFormerPoint.m_fY, aFormerPoint.m_fZ,
275 aScaledLogicPosition.PositionX, aScaledLogicPosition.PositionY,
276 aScaledLogicPosition.PositionZ))
278 nSkippedPoints++;
279 m_bPointsWereSkipped = true;
280 continue;
282 aSeriesFormerPointMap[pSeries.get()] = FormerPoint(aScaledLogicPosition.PositionX, aScaledLogicPosition.PositionY, aScaledLogicPosition.PositionZ);
284 //create a single datapoint if point is visible
285 if( !bIsVisible )
286 continue;
288 //create a group shape for this point and add to the series shape:
289 OUString aPointCID = ObjectIdentifier::createPointCID(
290 pSeries->getPointCID_Stub(), nIndex );
291 rtl::Reference<SvxShapeGroupAnyD> xPointGroupShape_Shapes(
292 createGroupShape(xSeriesGroupShape_Shapes,aPointCID) );
295 nCreatedPoints++;
297 //create data point
298 drawing::Direction3D aSymbolSize = transformToScreenBubbleSize( fBubbleSize );
299 rtl::Reference<SvxShapeCircle> xShape = ShapeFactory::createCircle2D( xPointGroupShape_Shapes
300 , aScenePosition, aSymbolSize );
302 PropertyMapper::setMappedProperties( *xShape
303 , pSeries->getPropertiesOfPoint( nIndex )
304 , PropertyMapper::getPropertyNameMapForFilledSeriesProperties() );
306 if(bHasFillColorMapping)
308 double nPropVal = pSeries->getValueByProperty(nIndex, u"FillColor"_ustr);
309 if(!std::isnan(nPropVal))
311 xShape->SvxShape::setPropertyValue(u"FillColor"_ustr, uno::Any(static_cast<sal_Int32>(nPropVal)));
314 if(bHasBorderColorMapping)
316 double nPropVal = pSeries->getValueByProperty(nIndex, u"LineColor"_ustr);
317 if(!std::isnan(nPropVal))
319 xShape->SvxShape::setPropertyValue(u"LineColor"_ustr, uno::Any(static_cast<sal_Int32>(nPropVal)));
323 ::chart::ShapeFactory::setShapeName( xShape, u"MarkHandles"_ustr );
325 //create data point label
326 if( pSeries->getDataPointLabelIfLabel(nIndex) )
328 LabelAlignment eAlignment = LABEL_ALIGN_TOP;
329 drawing::Position3D aScenePosition3D( aScenePosition.PositionX
330 , aScenePosition.PositionY
331 , aScenePosition.PositionZ+getTransformedDepth() );
333 sal_Int32 nLabelPlacement = pSeries->getLabelPlacement(
334 nIndex, m_xChartTypeModel, rPosHelper.isSwapXAndY());
336 switch(nLabelPlacement)
338 case css::chart::DataLabelPlacement::TOP:
339 aScenePosition3D.PositionY -= (aSymbolSize.DirectionY/2+1);
340 eAlignment = LABEL_ALIGN_TOP;
341 break;
342 case css::chart::DataLabelPlacement::BOTTOM:
343 aScenePosition3D.PositionY += (aSymbolSize.DirectionY/2+1);
344 eAlignment = LABEL_ALIGN_BOTTOM;
345 break;
346 case css::chart::DataLabelPlacement::LEFT:
347 aScenePosition3D.PositionX -= (aSymbolSize.DirectionX/2+1);
348 eAlignment = LABEL_ALIGN_LEFT;
349 break;
350 case css::chart::DataLabelPlacement::RIGHT:
351 aScenePosition3D.PositionX += (aSymbolSize.DirectionX/2+1);
352 eAlignment = LABEL_ALIGN_RIGHT;
353 break;
354 case css::chart::DataLabelPlacement::CENTER:
355 eAlignment = LABEL_ALIGN_CENTER;
356 break;
357 default:
358 OSL_FAIL("this label alignment is not implemented yet");
359 aScenePosition3D.PositionY -= (aSymbolSize.DirectionY/2+1);
360 eAlignment = LABEL_ALIGN_TOP;
361 break;
364 awt::Point aScreenPosition2D( LabelPositionHelper(m_nDimension,m_xLogicTarget)
365 .transformSceneToScreenPosition( aScenePosition3D ) );
366 sal_Int32 nOffset = 0;
367 if(eAlignment!=LABEL_ALIGN_CENTER)
368 nOffset = 100;//add some spacing //@todo maybe get more intelligent values
369 createDataLabel( xTextTarget, *pSeries, nIndex
370 , fBubbleSize, fBubbleSize, aScreenPosition2D, eAlignment, nOffset );
374 //remove PointGroupShape if empty
375 if(!xPointGroupShape_Shapes->getCount())
376 xSeriesGroupShape_Shapes->remove(xPointGroupShape_Shapes);
378 }//next series in x slot (next y slot)
379 }//next x slot
380 }//next z slot
381 }//next category
382 SAL_INFO(
383 "chart2",
384 "skipped points: " << nSkippedPoints << " created points: "
385 << nCreatedPoints);
388 } //namespace chart
390 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */