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 "CandleStickChart.hxx"
21 #include <ShapeFactory.hxx>
22 #include <CommonConverters.hxx>
23 #include <ExplicitCategoriesProvider.hxx>
24 #include <ObjectIdentifier.hxx>
25 #include "BarPositionHelper.hxx"
26 #include <DateHelper.hxx>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <com/sun/star/drawing/XShape.hpp>
29 #include <tools/diagnose_ex.h>
30 #include <osl/diagnose.h>
34 using namespace ::com::sun::star
;
35 using namespace ::com::sun::star::chart2
;
37 CandleStickChart::CandleStickChart( const uno::Reference
<XChartType
>& xChartTypeModel
38 , sal_Int32 nDimensionCount
)
39 : VSeriesPlotter( xChartTypeModel
, nDimensionCount
)
40 , m_pMainPosHelper( new BarPositionHelper() )
42 PlotterBase::m_pPosHelper
= m_pMainPosHelper
.get();
43 VSeriesPlotter::m_pMainPosHelper
= m_pMainPosHelper
.get();
46 CandleStickChart::~CandleStickChart()
50 // MinimumAndMaximumSupplier
52 bool CandleStickChart::isSeparateStackingForDifferentSigns( sal_Int32
/* nDimensionIndex */ )
57 LegendSymbolStyle
CandleStickChart::getLegendSymbolStyle()
59 return LegendSymbolStyle::Line
;
62 drawing::Direction3D
CandleStickChart::getPreferredDiagramAspectRatio() const
64 return drawing::Direction3D(-1,-1,-1);
67 void CandleStickChart::addSeries( std::unique_ptr
<VDataSeries
> pSeries
, sal_Int32
/* zSlot */, sal_Int32 xSlot
, sal_Int32 ySlot
)
69 //ignore y stacking for candle stick chart
70 VSeriesPlotter::addSeries( std::move(pSeries
), 0, xSlot
, ySlot
);
73 void CandleStickChart::createShapes()
75 if( m_aZSlots
.empty() ) //no series
81 OSL_ENSURE(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is(),"CandleStickChart is not proper initialized");
82 if(!(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is()))
85 //the text labels should be always on top of the other series shapes
86 //therefore create an own group for the texts to move them to front
87 //(because the text group is created after the series group the texts are displayed on top)
89 uno::Reference
< drawing::XShapes
> xSeriesTarget(
90 createGroupShape( m_xLogicTarget
));
91 uno::Reference
< drawing::XShapes
> xLossTarget(
92 createGroupShape( m_xLogicTarget
, ObjectIdentifier::createClassifiedIdentifier(
93 OBJECTTYPE_DATA_STOCK_LOSS
, OUString() )));
94 uno::Reference
< drawing::XShapes
> xGainTarget(
95 createGroupShape( m_xLogicTarget
, ObjectIdentifier::createClassifiedIdentifier(
96 OBJECTTYPE_DATA_STOCK_GAIN
, OUString() )));
97 uno::Reference
< drawing::XShapes
> xTextTarget(
98 m_pShapeFactory
->createGroup2D( m_xFinalTarget
));
100 //check necessary here that different Y axis can not be stacked in the same group? ... hm?
102 bool bJapaneseStyle
=true;//@todo is this the correct default?
103 bool bShowFirst
= true;//is only important if bJapaneseStyle == false
104 tNameSequence aWhiteBox_Names
, aBlackBox_Names
;
105 tAnySequence aWhiteBox_Values
, aBlackBox_Values
;
108 if( m_xChartTypeModelProps
.is() )
110 m_xChartTypeModelProps
->getPropertyValue( "ShowFirst" ) >>= bShowFirst
;
112 uno::Reference
< beans::XPropertySet
> xWhiteDayProps
;
113 uno::Reference
< beans::XPropertySet
> xBlackDayProps
;
114 m_xChartTypeModelProps
->getPropertyValue( "Japanese" ) >>= bJapaneseStyle
;
115 m_xChartTypeModelProps
->getPropertyValue( "WhiteDay" ) >>= xWhiteDayProps
;
116 m_xChartTypeModelProps
->getPropertyValue( "BlackDay" ) >>= xBlackDayProps
;
118 tPropertyNameValueMap aWhiteBox_Map
;
119 PropertyMapper::getValueMap( aWhiteBox_Map
, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xWhiteDayProps
);
120 PropertyMapper::getMultiPropertyListsFromValueMap( aWhiteBox_Names
, aWhiteBox_Values
, aWhiteBox_Map
);
122 tPropertyNameValueMap aBlackBox_Map
;
123 PropertyMapper::getValueMap( aBlackBox_Map
, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xBlackDayProps
);
124 PropertyMapper::getMultiPropertyListsFromValueMap( aBlackBox_Names
, aBlackBox_Values
, aBlackBox_Map
);
127 catch( const uno::Exception
& )
129 TOOLS_WARN_EXCEPTION("chart2", "" );
132 //(@todo maybe different iteration for breaks in axis ?)
133 sal_Int32 nEndIndex
= VSeriesPlotter::getPointCount();
134 double fLogicZ
= 1.5;//as defined
135 //iterate through all x values per indices
136 for( sal_Int32 nIndex
= 0; nIndex
< nEndIndex
; nIndex
++ )
138 for( auto const& rZSlot
: m_aZSlots
)
140 BarPositionHelper
* pPosHelper
= m_pMainPosHelper
.get();
141 if( !rZSlot
.empty() )
143 sal_Int32 nAttachedAxisIndex
= rZSlot
.front().getAttachedAxisIndexForFirstSeries();
144 //2ND_AXIS_IN_BARS so far one can assume to have the same plotter for each z slot
145 pPosHelper
= dynamic_cast<BarPositionHelper
*>(&( getPlottingPositionHelper( nAttachedAxisIndex
) ) );
147 pPosHelper
= m_pMainPosHelper
.get();
149 PlotterBase::m_pPosHelper
= pPosHelper
;
151 //update/create information for current group
152 pPosHelper
->updateSeriesCount( rZSlot
.size() );
154 //iterate through all x slots in this category
155 for( auto const& rXSlot
: rZSlot
)
157 //iterate through all series in this x slot
158 for( std::unique_ptr
<VDataSeries
> const & pSeries
: rXSlot
.m_aSeriesVector
)
160 //collect data point information (logic coordinates, style ):
161 double fUnscaledX
= pSeries
->getXValue( nIndex
);
162 if( m_pExplicitCategoriesProvider
&& m_pExplicitCategoriesProvider
->isDateAxis() )
163 fUnscaledX
= DateHelper::RasterizeDateValue( fUnscaledX
, m_aNullDate
, m_nTimeResolution
);
164 if(fUnscaledX
<pPosHelper
->getLogicMinX() || fUnscaledX
>pPosHelper
->getLogicMaxX())
165 continue;//point not visible
166 double fScaledX
= pPosHelper
->getScaledSlotPos( fUnscaledX
, fSlotX
);
168 double fUnscaledY_First
= pSeries
->getY_First( nIndex
);
169 double fUnscaledY_Last
= pSeries
->getY_Last( nIndex
);
170 double fUnscaledY_Min
= pSeries
->getY_Min( nIndex
);
171 double fUnscaledY_Max
= pSeries
->getY_Max( nIndex
);
174 if(fUnscaledY_Last
<=fUnscaledY_First
)
176 std::swap(fUnscaledY_First
,fUnscaledY_Last
);
179 if(fUnscaledY_Max
<fUnscaledY_Min
)
180 std::swap(fUnscaledY_Min
,fUnscaledY_Max
);
181 //transformation 3) -> 4)
182 double fHalfScaledWidth
= pPosHelper
->getScaledSlotWidth()/2.0;
184 double fScaledY_First(fUnscaledY_First
);
185 double fScaledY_Last(fUnscaledY_Last
);
186 double fScaledY_Min(fUnscaledY_Min
);
187 double fScaledY_Max(fUnscaledY_Max
);
188 pPosHelper
->clipLogicValues( nullptr,&fScaledY_First
,nullptr );
189 pPosHelper
->clipLogicValues( nullptr,&fScaledY_Last
,nullptr );
190 pPosHelper
->clipLogicValues( nullptr,&fScaledY_Min
,nullptr );
191 pPosHelper
->clipLogicValues( nullptr,&fScaledY_Max
,nullptr );
192 pPosHelper
->doLogicScaling( nullptr,&fScaledY_First
,nullptr );
193 pPosHelper
->doLogicScaling( nullptr,&fScaledY_Last
,nullptr );
194 pPosHelper
->doLogicScaling( nullptr,&fScaledY_Min
,nullptr );
195 pPosHelper
->doLogicScaling( nullptr,&fScaledY_Max
,nullptr );
197 drawing::Position3D
aPosLeftFirst( pPosHelper
->transformScaledLogicToScene( fScaledX
-fHalfScaledWidth
, fScaledY_First
,0 ,true ) );
198 drawing::Position3D
aPosRightLast( pPosHelper
->transformScaledLogicToScene( fScaledX
+fHalfScaledWidth
, fScaledY_Last
,0 ,true ) );
199 drawing::Position3D
aPosMiddleFirst( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_First
,0 ,true ) );
200 drawing::Position3D
aPosMiddleLast( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_Last
,0 ,true ) );
201 drawing::Position3D
aPosMiddleMinimum( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_Min
,0 ,true ) );
202 drawing::Position3D
aPosMiddleMaximum( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_Max
,0 ,true ) );
204 uno::Reference
< drawing::XShapes
> xLossGainTarget( xGainTarget
);
206 xLossGainTarget
= xLossTarget
;
208 uno::Reference
< beans::XPropertySet
> xPointProp( pSeries
->getPropertiesOfPoint( nIndex
));
209 uno::Reference
< drawing::XShapes
> xPointGroupShape_Shapes
;
211 OUString aPointCID
= ObjectIdentifier::createPointCID( pSeries
->getPointCID_Stub(), nIndex
);
212 uno::Reference
< drawing::XShapes
> xSeriesGroupShape_Shapes( getSeriesGroupShape(pSeries
.get(), xSeriesTarget
) );
213 xPointGroupShape_Shapes
= createGroupShape(xSeriesGroupShape_Shapes
,aPointCID
);
216 //create min-max line
217 if( isValidPosition(aPosMiddleMinimum
) && isValidPosition(aPosMiddleMaximum
) )
219 drawing::PolyPolygonShape3D aPoly
;
220 sal_Int32 nLineIndex
=0;
221 AddPointToPoly( aPoly
, aPosMiddleMinimum
, nLineIndex
);
222 AddPointToPoly( aPoly
, aPosMiddleMaximum
, nLineIndex
);
224 uno::Reference
< drawing::XShape
> xShape
=
225 m_pShapeFactory
->createLine2D( xPointGroupShape_Shapes
,
226 PolyToPointSequence(aPoly
));
227 setMappedProperties( xShape
, xPointProp
, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
230 //create first-last shape
231 if(bJapaneseStyle
&& isValidPosition(aPosLeftFirst
) && isValidPosition(aPosRightLast
) )
233 drawing::Direction3D aDiff
= aPosRightLast
-aPosLeftFirst
;
234 awt::Size
aAWTSize( Direction3DToAWTSize( aDiff
));
235 // workaround for bug in drawing: if height is 0 the box gets infinitely large
236 if( aAWTSize
.Height
== 0 )
239 tNameSequence aNames
;
240 tAnySequence aValues
;
242 uno::Reference
< drawing::XShape
> xShape
=
243 m_pShapeFactory
->createRectangle( xLossGainTarget
,
244 aAWTSize
, Position3DToAWTPoint( aPosLeftFirst
),
247 uno::Reference
< beans::XPropertySet
> xProp( xShape
, uno::UNO_QUERY
);
251 PropertyMapper::setMultiProperties( aBlackBox_Names
, aBlackBox_Values
, xProp
);
253 PropertyMapper::setMultiProperties( aWhiteBox_Names
, aWhiteBox_Values
, xProp
);
258 drawing::PolyPolygonShape3D aPoly
;
260 sal_Int32 nLineIndex
= 0;
261 if( bShowFirst
&& pPosHelper
->isLogicVisible( fUnscaledX
, fUnscaledY_First
,fLogicZ
)
262 && isValidPosition(aPosLeftFirst
) && isValidPosition(aPosMiddleFirst
) )
264 AddPointToPoly( aPoly
, aPosLeftFirst
, nLineIndex
);
265 AddPointToPoly( aPoly
, aPosMiddleFirst
, nLineIndex
++ );
267 if( pPosHelper
->isLogicVisible( fUnscaledX
, fUnscaledY_Last
,fLogicZ
)
268 && isValidPosition(aPosMiddleLast
) && isValidPosition(aPosRightLast
) )
270 AddPointToPoly( aPoly
, aPosMiddleLast
, nLineIndex
);
271 AddPointToPoly( aPoly
, aPosRightLast
, nLineIndex
);
274 if( aPoly
.SequenceX
.hasElements() )
276 uno::Reference
< drawing::XShape
> xShape
=
277 m_pShapeFactory
->createLine2D( xPointGroupShape_Shapes
,
278 PolyToPointSequence(aPoly
) );
279 uno::Reference
< beans::XPropertySet
> xProp( xShape
, uno::UNO_QUERY
);
282 setMappedProperties( xShape
, xPointProp
, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
287 //create data point label
288 if( pSeries
->getDataPointLabelIfLabel(nIndex
) )
290 if(isValidPosition(aPosMiddleFirst
))
291 createDataLabel( xTextTarget
, *pSeries
, nIndex
292 , fUnscaledY_First
, 1.0, Position3DToAWTPoint(aPosMiddleFirst
), LABEL_ALIGN_LEFT_BOTTOM
);
293 if(isValidPosition(aPosMiddleLast
))
294 createDataLabel( xTextTarget
, *pSeries
, nIndex
295 , fUnscaledY_Last
, 1.0, Position3DToAWTPoint(aPosMiddleLast
), LABEL_ALIGN_RIGHT_TOP
);
296 if(isValidPosition(aPosMiddleMinimum
))
297 createDataLabel( xTextTarget
, *pSeries
, nIndex
298 , fUnscaledY_Min
, 1.0, Position3DToAWTPoint(aPosMiddleMinimum
), LABEL_ALIGN_BOTTOM
);
299 if(isValidPosition(aPosMiddleMaximum
))
300 createDataLabel( xTextTarget
, *pSeries
, nIndex
301 , fUnscaledY_Max
, 1.0, Position3DToAWTPoint(aPosMiddleMaximum
), LABEL_ALIGN_TOP
);
303 }//next series in x slot (next y slot)
308 /* @todo remove series shapes if empty
309 //remove and delete point-group-shape if empty
310 if(!xSeriesGroupShape_Shapes->getCount())
312 pSeries->m_xShape.set(NULL);
313 m_xLogicTarget->remove(xSeriesGroupShape_Shape);
317 //remove and delete series-group-shape if empty
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */