1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
26 #include <com/sun/star/chart/DataLabelPlacement.hpp>
27 #include <rtl/math.hxx>
28 #include <sal/log.hxx>
29 #include <osl/diagnose.h>
30 #include <com/sun/star/drawing/XShapes.hpp>
31 #include <com/sun/star/beans/XPropertySet.hpp>
35 using namespace ::com::sun::star
;
36 using namespace ::rtl::math
;
37 using namespace ::com::sun::star::chart2
;
39 BubbleChart::BubbleChart( const uno::Reference
<XChartType
>& xChartTypeModel
40 , sal_Int32 nDimensionCount
)
41 : VSeriesPlotter( xChartTypeModel
, nDimensionCount
, false )
42 , m_fMaxLogicBubbleSize( 0.0 )
43 , m_fBubbleSizeFactorToScreen( 1.0 )
45 // We only support 2 dimensional bubble charts
46 assert(nDimensionCount
== 2);
48 if( !m_pMainPosHelper
)
49 m_pMainPosHelper
= new PlottingPositionHelper();
50 PlotterBase::m_pPosHelper
= m_pMainPosHelper
;
53 BubbleChart::~BubbleChart()
55 delete m_pMainPosHelper
;
58 void BubbleChart::calculateMaximumLogicBubbleSize()
60 double fMaxSize
= 0.0;
62 sal_Int32 nEndIndex
= VSeriesPlotter::getPointCount();
63 for( sal_Int32 nIndex
= 0; nIndex
< nEndIndex
; nIndex
++ )
65 for( auto const& rZSlot
: m_aZSlots
)
67 for( auto const& rXSlot
: rZSlot
)
69 for( std::unique_ptr
<VDataSeries
> const & pSeries
: rXSlot
.m_aSeriesVector
)
74 double fSize
= pSeries
->getBubble_Size( nIndex
);
75 if( fSize
> fMaxSize
)
82 m_fMaxLogicBubbleSize
= fMaxSize
;
85 void BubbleChart::calculateBubbleSizeScalingFactor()
88 drawing::Position3D
aSceneMinPos( m_pMainPosHelper
->transformLogicToScene( m_pMainPosHelper
->getLogicMinX(),m_pMainPosHelper
->getLogicMinY(),fLogicZ
, false ) );
89 drawing::Position3D
aSceneMaxPos( m_pMainPosHelper
->transformLogicToScene( m_pMainPosHelper
->getLogicMaxX(),m_pMainPosHelper
->getLogicMaxY(),fLogicZ
, false ) );
91 awt::Point
aScreenMinPos( LabelPositionHelper(m_nDimension
,m_xLogicTarget
,m_pShapeFactory
).transformSceneToScreenPosition( aSceneMinPos
) );
92 awt::Point
aScreenMaxPos( LabelPositionHelper(m_nDimension
,m_xLogicTarget
,m_pShapeFactory
).transformSceneToScreenPosition( aSceneMaxPos
) );
94 sal_Int32 nWidth
= abs( aScreenMaxPos
.X
- aScreenMinPos
.X
);
95 sal_Int32 nHeight
= abs( aScreenMaxPos
.Y
- aScreenMinPos
.Y
);
97 sal_Int32 nMinExtend
= std::min( nWidth
, nHeight
);
98 m_fBubbleSizeFactorToScreen
= nMinExtend
* 0.25;//max bubble size is 25 percent of diagram size
101 drawing::Direction3D
BubbleChart::transformToScreenBubbleSize( double fLogicSize
)
103 drawing::Direction3D
aRet(0,0,0);
105 if( std::isnan(fLogicSize
) || std::isinf(fLogicSize
) )
108 double fMaxSize
= m_fMaxLogicBubbleSize
;
110 double fMaxRadius
= sqrt( fMaxSize
/ F_PI
);
111 double fRadius
= sqrt( fLogicSize
/ F_PI
);
113 aRet
.DirectionX
= m_fBubbleSizeFactorToScreen
* fRadius
/ fMaxRadius
;
114 aRet
.DirectionY
= aRet
.DirectionX
;
119 bool BubbleChart::isExpandIfValuesCloseToBorder( sal_Int32
/*nDimensionIndex*/ )
124 bool BubbleChart::isSeparateStackingForDifferentSigns( sal_Int32
/*nDimensionIndex*/ )
129 LegendSymbolStyle
BubbleChart::getLegendSymbolStyle()
131 return LegendSymbolStyle::Circle
;
134 drawing::Direction3D
BubbleChart::getPreferredDiagramAspectRatio() const
136 return drawing::Direction3D(-1,-1,-1);
141 //better performance for big data
144 FormerPoint( double fX
, double fY
, double fZ
)
145 : m_fX(fX
), m_fY(fY
), m_fZ(fZ
)
149 ::rtl::math::setNan( &m_fX
);
150 ::rtl::math::setNan( &m_fY
);
151 ::rtl::math::setNan( &m_fZ
);
161 void BubbleChart::createShapes()
163 if( m_aZSlots
.empty() ) //no series
166 OSL_ENSURE(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is(),"BubbleChart is not proper initialized");
167 if(!(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is()))
170 //therefore create an own group for the texts and the error bars to move them to front
171 //(because the text group is created after the series group the texts are displayed on top)
172 uno::Reference
< drawing::XShapes
> xSeriesTarget(
173 createGroupShape( m_xLogicTarget
));
174 uno::Reference
< drawing::XShapes
> xTextTarget(
175 m_pShapeFactory
->createGroup2D( m_xFinalTarget
));
177 //update/create information for current group
178 double fLogicZ
= 1.0;//as defined
180 sal_Int32
const nStartIndex
= 0; // inclusive ;..todo get somehow from x scale
181 sal_Int32 nEndIndex
= VSeriesPlotter::getPointCount();
185 //better performance for big data
186 std::map
< VDataSeries
*, FormerPoint
> aSeriesFormerPointMap
;
187 m_bPointsWereSkipped
= false;
188 sal_Int32 nSkippedPoints
= 0;
189 sal_Int32 nCreatedPoints
= 0;
191 calculateMaximumLogicBubbleSize();
192 calculateBubbleSizeScalingFactor();
193 if( m_fMaxLogicBubbleSize
<= 0 || m_fBubbleSizeFactorToScreen
<= 0 )
196 //iterate through all x values per indices
197 for( sal_Int32 nIndex
= nStartIndex
; nIndex
< nEndIndex
; nIndex
++ )
199 for( auto const& rZSlot
: m_aZSlots
)
201 for( auto const& rXSlot
: rZSlot
)
203 //iterate through all series
204 for( std::unique_ptr
<VDataSeries
> const & pSeries
: rXSlot
.m_aSeriesVector
)
209 bool bHasFillColorMapping
= pSeries
->hasPropertyMapping("FillColor");
210 bool bHasBorderColorMapping
= pSeries
->hasPropertyMapping("LineColor");
212 uno::Reference
< drawing::XShapes
> xSeriesGroupShape_Shapes
= getSeriesGroupShape(pSeries
.get(), xSeriesTarget
);
214 sal_Int32 nAttachedAxisIndex
= pSeries
->getAttachedAxisIndex();
215 PlottingPositionHelper
& rPosHelper
216 = getPlottingPositionHelper(nAttachedAxisIndex
);
217 m_pPosHelper
= &rPosHelper
;
219 //collect data point information (logic coordinates, style ):
220 double fLogicX
= pSeries
->getXValue(nIndex
);
221 double fLogicY
= pSeries
->getYValue(nIndex
);
222 double fBubbleSize
= pSeries
->getBubble_Size( nIndex
);
224 if( fBubbleSize
<0.0 )
227 if( fBubbleSize
== 0.0 || std::isnan(fBubbleSize
) )
230 if( std::isnan(fLogicX
) || std::isinf(fLogicX
)
231 || std::isnan(fLogicY
) || std::isinf(fLogicY
) )
234 bool bIsVisible
= rPosHelper
.isLogicVisible(fLogicX
, fLogicY
, fLogicZ
);
236 drawing::Position3D
aUnscaledLogicPosition( fLogicX
, fLogicY
, fLogicZ
);
237 drawing::Position3D
aScaledLogicPosition(aUnscaledLogicPosition
);
238 rPosHelper
.doLogicScaling(aScaledLogicPosition
);
240 //transformation 3) -> 4)
241 drawing::Position3D
aScenePosition(
242 rPosHelper
.transformLogicToScene(fLogicX
, fLogicY
, fLogicZ
, false));
244 //better performance for big data
245 FormerPoint
aFormerPoint( aSeriesFormerPointMap
[pSeries
.get()] );
246 rPosHelper
.setCoordinateSystemResolution(m_aCoordinateSystemResolution
);
247 if (!pSeries
->isAttributedDataPoint(nIndex
)
248 && rPosHelper
.isSameForGivenResolution(
249 aFormerPoint
.m_fX
, aFormerPoint
.m_fY
, aFormerPoint
.m_fZ
,
250 aScaledLogicPosition
.PositionX
, aScaledLogicPosition
.PositionY
,
251 aScaledLogicPosition
.PositionZ
))
254 m_bPointsWereSkipped
= true;
257 aSeriesFormerPointMap
[pSeries
.get()] = FormerPoint(aScaledLogicPosition
.PositionX
, aScaledLogicPosition
.PositionY
, aScaledLogicPosition
.PositionZ
);
259 //create a single datapoint if point is visible
263 //create a group shape for this point and add to the series shape:
264 OUString aPointCID
= ObjectIdentifier::createPointCID(
265 pSeries
->getPointCID_Stub(), nIndex
);
266 uno::Reference
< drawing::XShapes
> xPointGroupShape_Shapes(
267 createGroupShape(xSeriesGroupShape_Shapes
,aPointCID
) );
268 uno::Reference
<drawing::XShape
> xPointGroupShape_Shape( xPointGroupShape_Shapes
, uno::UNO_QUERY
);
274 drawing::Direction3D aSymbolSize
= transformToScreenBubbleSize( fBubbleSize
);
275 uno::Reference
<drawing::XShape
> xShape
= m_pShapeFactory
->createCircle2D( xPointGroupShape_Shapes
276 , aScenePosition
, aSymbolSize
);
278 setMappedProperties( xShape
279 , pSeries
->getPropertiesOfPoint( nIndex
)
280 , PropertyMapper::getPropertyNameMapForFilledSeriesProperties() );
282 if(bHasFillColorMapping
)
284 double nPropVal
= pSeries
->getValueByProperty(nIndex
, "FillColor");
285 if(!std::isnan(nPropVal
))
287 uno::Reference
< beans::XPropertySet
> xProps( xShape
, uno::UNO_QUERY_THROW
);
288 xProps
->setPropertyValue("FillColor", uno::Any(static_cast<sal_Int32
>(nPropVal
)));
291 if(bHasBorderColorMapping
)
293 double nPropVal
= pSeries
->getValueByProperty(nIndex
, "LineColor");
294 if(!std::isnan(nPropVal
))
296 uno::Reference
< beans::XPropertySet
> xProps( xShape
, uno::UNO_QUERY_THROW
);
297 xProps
->setPropertyValue("LineColor", uno::Any(static_cast<sal_Int32
>(nPropVal
)));
301 ::chart::ShapeFactory::setShapeName( xShape
, "MarkHandles" );
303 //create data point label
304 if( pSeries
->getDataPointLabelIfLabel(nIndex
) )
306 LabelAlignment eAlignment
= LABEL_ALIGN_TOP
;
307 drawing::Position3D
aScenePosition3D( aScenePosition
.PositionX
308 , aScenePosition
.PositionY
309 , aScenePosition
.PositionZ
+getTransformedDepth() );
311 sal_Int32 nLabelPlacement
= pSeries
->getLabelPlacement(
312 nIndex
, m_xChartTypeModel
, rPosHelper
.isSwapXAndY());
314 switch(nLabelPlacement
)
316 case css::chart::DataLabelPlacement::TOP
:
317 aScenePosition3D
.PositionY
-= (aSymbolSize
.DirectionY
/2+1);
318 eAlignment
= LABEL_ALIGN_TOP
;
320 case css::chart::DataLabelPlacement::BOTTOM
:
321 aScenePosition3D
.PositionY
+= (aSymbolSize
.DirectionY
/2+1);
322 eAlignment
= LABEL_ALIGN_BOTTOM
;
324 case css::chart::DataLabelPlacement::LEFT
:
325 aScenePosition3D
.PositionX
-= (aSymbolSize
.DirectionX
/2+1);
326 eAlignment
= LABEL_ALIGN_LEFT
;
328 case css::chart::DataLabelPlacement::RIGHT
:
329 aScenePosition3D
.PositionX
+= (aSymbolSize
.DirectionX
/2+1);
330 eAlignment
= LABEL_ALIGN_RIGHT
;
332 case css::chart::DataLabelPlacement::CENTER
:
333 eAlignment
= LABEL_ALIGN_CENTER
;
336 OSL_FAIL("this label alignment is not implemented yet");
337 aScenePosition3D
.PositionY
-= (aSymbolSize
.DirectionY
/2+1);
338 eAlignment
= LABEL_ALIGN_TOP
;
342 awt::Point
aScreenPosition2D( LabelPositionHelper(m_nDimension
,m_xLogicTarget
,m_pShapeFactory
)
343 .transformSceneToScreenPosition( aScenePosition3D
) );
344 sal_Int32 nOffset
= 0;
345 if(eAlignment
!=LABEL_ALIGN_CENTER
)
346 nOffset
= 100;//add some spacing //@todo maybe get more intelligent values
347 createDataLabel( xTextTarget
, *pSeries
, nIndex
348 , fBubbleSize
, fBubbleSize
, aScreenPosition2D
, eAlignment
, nOffset
);
352 //remove PointGroupShape if empty
353 if(!xPointGroupShape_Shapes
->getCount())
354 xSeriesGroupShape_Shapes
->remove(xPointGroupShape_Shape
);
356 }//next series in x slot (next y slot)
362 "skipped points: " << nSkippedPoints
<< " created points: "
368 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */