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( ::rtl::math::isNan(fLogicSize
) || ::rtl::math::isInf(fLogicSize
) )
108 double fMaxSize
= m_fMaxLogicBubbleSize
;
110 double fMaxRadius
= sqrt( fMaxSize
/ F_PI
);
111 double fRaduis
= sqrt( fLogicSize
/ F_PI
);
113 aRet
.DirectionX
= m_fBubbleSizeFactorToScreen
* fRaduis
/ 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);
139 //better performance for big data
142 FormerPoint( double fX
, double fY
, double fZ
)
143 : m_fX(fX
), m_fY(fY
), m_fZ(fZ
)
147 ::rtl::math::setNan( &m_fX
);
148 ::rtl::math::setNan( &m_fY
);
149 ::rtl::math::setNan( &m_fZ
);
157 void BubbleChart::createShapes()
159 if( m_aZSlots
.empty() ) //no series
162 OSL_ENSURE(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is(),"BubbleChart is not proper initialized");
163 if(!(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is()))
166 //therefore create an own group for the texts and the error bars to move them to front
167 //(because the text group is created after the series group the texts are displayed on top)
168 uno::Reference
< drawing::XShapes
> xSeriesTarget(
169 createGroupShape( m_xLogicTarget
));
170 uno::Reference
< drawing::XShapes
> xTextTarget(
171 m_pShapeFactory
->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();
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 )
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
)
205 bool bHasFillColorMapping
= pSeries
->hasPropertyMapping("FillColor");
206 bool bHasBorderColorMapping
= pSeries
->hasPropertyMapping("LineColor");
208 uno::Reference
< drawing::XShapes
> 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 if( fBubbleSize
<0.0 )
223 if( fBubbleSize
== 0.0 || ::rtl::math::isNan(fBubbleSize
) )
226 if( ::rtl::math::isNan(fLogicX
) || ::rtl::math::isInf(fLogicX
)
227 || ::rtl::math::isNan(fLogicY
) || ::rtl::math::isInf(fLogicY
) )
230 bool bIsVisible
= rPosHelper
.isLogicVisible(fLogicX
, fLogicY
, fLogicZ
);
232 drawing::Position3D
aUnscaledLogicPosition( fLogicX
, fLogicY
, fLogicZ
);
233 drawing::Position3D
aScaledLogicPosition(aUnscaledLogicPosition
);
234 rPosHelper
.doLogicScaling(aScaledLogicPosition
);
236 //transformation 3) -> 4)
237 drawing::Position3D
aScenePosition(
238 rPosHelper
.transformLogicToScene(fLogicX
, fLogicY
, fLogicZ
, false));
240 //better performance for big data
241 FormerPoint
aFormerPoint( aSeriesFormerPointMap
[pSeries
.get()] );
242 rPosHelper
.setCoordinateSystemResolution(m_aCoordinateSystemResolution
);
243 if (!pSeries
->isAttributedDataPoint(nIndex
)
244 && rPosHelper
.isSameForGivenResolution(
245 aFormerPoint
.m_fX
, aFormerPoint
.m_fY
, aFormerPoint
.m_fZ
,
246 aScaledLogicPosition
.PositionX
, aScaledLogicPosition
.PositionY
,
247 aScaledLogicPosition
.PositionZ
))
250 m_bPointsWereSkipped
= true;
253 aSeriesFormerPointMap
[pSeries
.get()] = FormerPoint(aScaledLogicPosition
.PositionX
, aScaledLogicPosition
.PositionY
, aScaledLogicPosition
.PositionZ
);
255 //create a single datapoint if point is visible
259 //create a group shape for this point and add to the series shape:
260 OUString aPointCID
= ObjectIdentifier::createPointCID(
261 pSeries
->getPointCID_Stub(), nIndex
);
262 uno::Reference
< drawing::XShapes
> xPointGroupShape_Shapes(
263 createGroupShape(xSeriesGroupShape_Shapes
,aPointCID
) );
264 uno::Reference
<drawing::XShape
> xPointGroupShape_Shape( xPointGroupShape_Shapes
, uno::UNO_QUERY
);
270 drawing::Direction3D aSymbolSize
= transformToScreenBubbleSize( fBubbleSize
);
271 uno::Reference
<drawing::XShape
> xShape
= m_pShapeFactory
->createCircle2D( xPointGroupShape_Shapes
272 , aScenePosition
, aSymbolSize
);
274 setMappedProperties( xShape
275 , pSeries
->getPropertiesOfPoint( nIndex
)
276 , PropertyMapper::getPropertyNameMapForFilledSeriesProperties() );
278 if(bHasFillColorMapping
)
280 double nPropVal
= pSeries
->getValueByProperty(nIndex
, "FillColor");
281 if(!rtl::math::isNan(nPropVal
))
283 uno::Reference
< beans::XPropertySet
> xProps( xShape
, uno::UNO_QUERY_THROW
);
284 xProps
->setPropertyValue("FillColor", uno::Any(static_cast<sal_Int32
>(nPropVal
)));
287 if(bHasBorderColorMapping
)
289 double nPropVal
= pSeries
->getValueByProperty(nIndex
, "LineColor");
290 if(!rtl::math::isNan(nPropVal
))
292 uno::Reference
< beans::XPropertySet
> xProps( xShape
, uno::UNO_QUERY_THROW
);
293 xProps
->setPropertyValue("LineColor", uno::Any(static_cast<sal_Int32
>(nPropVal
)));
297 ::chart::ShapeFactory::setShapeName( xShape
, "MarkHandles" );
299 //create data point label
300 if( pSeries
->getDataPointLabelIfLabel(nIndex
) )
302 LabelAlignment eAlignment
= LABEL_ALIGN_TOP
;
303 drawing::Position3D
aScenePosition3D( aScenePosition
.PositionX
304 , aScenePosition
.PositionY
305 , aScenePosition
.PositionZ
+getTransformedDepth() );
307 sal_Int32 nLabelPlacement
= pSeries
->getLabelPlacement(
308 nIndex
, m_xChartTypeModel
, rPosHelper
.isSwapXAndY());
310 switch(nLabelPlacement
)
312 case css::chart::DataLabelPlacement::TOP
:
313 aScenePosition3D
.PositionY
-= (aSymbolSize
.DirectionY
/2+1);
314 eAlignment
= LABEL_ALIGN_TOP
;
316 case css::chart::DataLabelPlacement::BOTTOM
:
317 aScenePosition3D
.PositionY
+= (aSymbolSize
.DirectionY
/2+1);
318 eAlignment
= LABEL_ALIGN_BOTTOM
;
320 case css::chart::DataLabelPlacement::LEFT
:
321 aScenePosition3D
.PositionX
-= (aSymbolSize
.DirectionX
/2+1);
322 eAlignment
= LABEL_ALIGN_LEFT
;
324 case css::chart::DataLabelPlacement::RIGHT
:
325 aScenePosition3D
.PositionX
+= (aSymbolSize
.DirectionX
/2+1);
326 eAlignment
= LABEL_ALIGN_RIGHT
;
328 case css::chart::DataLabelPlacement::CENTER
:
329 eAlignment
= LABEL_ALIGN_CENTER
;
332 OSL_FAIL("this label alignment is not implemented yet");
333 aScenePosition3D
.PositionY
-= (aSymbolSize
.DirectionY
/2+1);
334 eAlignment
= LABEL_ALIGN_TOP
;
338 awt::Point
aScreenPosition2D( LabelPositionHelper(m_nDimension
,m_xLogicTarget
,m_pShapeFactory
)
339 .transformSceneToScreenPosition( aScenePosition3D
) );
340 sal_Int32 nOffset
= 0;
341 if(eAlignment
!=LABEL_ALIGN_CENTER
)
342 nOffset
= 100;//add some spacing //@todo maybe get more intelligent values
343 createDataLabel( xTextTarget
, *pSeries
, nIndex
344 , fBubbleSize
, fBubbleSize
, aScreenPosition2D
, eAlignment
, nOffset
);
348 //remove PointGroupShape if empty
349 if(!xPointGroupShape_Shapes
->getCount())
350 xSeriesGroupShape_Shapes
->remove(xPointGroupShape_Shape
);
352 }//next series in x slot (next y slot)
358 "skipped points: " << nSkippedPoints
<< " created points: "
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */