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 <ObjectIdentifier.hxx>
24 #include <LabelPositionHelper.hxx>
25 #include "BarPositionHelper.hxx"
26 #include <VLegendSymbolFactory.hxx>
27 #include <FormattedStringHelper.hxx>
28 #include <DataSeriesHelper.hxx>
29 #include <DateHelper.hxx>
30 #include <rtl/math.hxx>
31 #include <editeng/unoprnms.hxx>
35 using namespace ::com::sun::star
;
36 using namespace ::rtl::math
;
37 using namespace ::com::sun::star::chart2
;
39 CandleStickChart::CandleStickChart( const uno::Reference
<XChartType
>& xChartTypeModel
40 , sal_Int32 nDimensionCount
)
41 : VSeriesPlotter( xChartTypeModel
, nDimensionCount
)
42 , m_pMainPosHelper( new BarPositionHelper() )
44 PlotterBase::m_pPosHelper
= m_pMainPosHelper
.get();
45 VSeriesPlotter::m_pMainPosHelper
= m_pMainPosHelper
.get();
48 CandleStickChart::~CandleStickChart()
52 // MinimumAndMaximumSupplier
54 bool CandleStickChart::isSeparateStackingForDifferentSigns( sal_Int32
/* nDimensionIndex */ )
59 LegendSymbolStyle
CandleStickChart::getLegendSymbolStyle()
61 return LegendSymbolStyle_LINE
;
64 drawing::Direction3D
CandleStickChart::getPreferredDiagramAspectRatio() const
66 return drawing::Direction3D(-1,-1,-1);
69 void CandleStickChart::addSeries( VDataSeries
* pSeries
, sal_Int32
/* zSlot */, sal_Int32 xSlot
, sal_Int32 ySlot
)
71 //ignore y stacking for candle stick chart
72 VSeriesPlotter::addSeries( pSeries
, 0, xSlot
, ySlot
);
75 void CandleStickChart::createShapes()
77 if( m_aZSlots
.empty() ) //no series
83 OSL_ENSURE(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is(),"CandleStickChart is not proper initialized");
84 if(!(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is()))
87 //the text labels should be always on top of the other series shapes
88 //therefore create an own group for the texts to move them to front
89 //(because the text group is created after the series group the texts are displayed on top)
91 uno::Reference
< drawing::XShapes
> xSeriesTarget(
92 createGroupShape( m_xLogicTarget
));
93 uno::Reference
< drawing::XShapes
> xLossTarget(
94 createGroupShape( m_xLogicTarget
, ObjectIdentifier::createClassifiedIdentifier(
95 OBJECTTYPE_DATA_STOCK_LOSS
, OUString() )));
96 uno::Reference
< drawing::XShapes
> xGainTarget(
97 createGroupShape( m_xLogicTarget
, ObjectIdentifier::createClassifiedIdentifier(
98 OBJECTTYPE_DATA_STOCK_GAIN
, OUString() )));
99 uno::Reference
< drawing::XShapes
> xTextTarget(
100 m_pShapeFactory
->createGroup2D( m_xFinalTarget
));
102 //check necessary here that different Y axis can not be stacked in the same group? ... hm?
104 bool bJapaneseStyle
=true;//@todo is this the correct default?
105 bool bShowFirst
= true;//is only important if bJapaneseStyle == false
106 tNameSequence aWhiteBox_Names
, aBlackBox_Names
;
107 tAnySequence aWhiteBox_Values
, aBlackBox_Values
;
110 if( m_xChartTypeModelProps
.is() )
112 m_xChartTypeModelProps
->getPropertyValue( "ShowFirst" ) >>= bShowFirst
;
114 uno::Reference
< beans::XPropertySet
> xWhiteDayProps(nullptr);
115 uno::Reference
< beans::XPropertySet
> xBlackDayProps(nullptr);
116 m_xChartTypeModelProps
->getPropertyValue( "Japanese" ) >>= bJapaneseStyle
;
117 m_xChartTypeModelProps
->getPropertyValue( "WhiteDay" ) >>= xWhiteDayProps
;
118 m_xChartTypeModelProps
->getPropertyValue( "BlackDay" ) >>= xBlackDayProps
;
120 tPropertyNameValueMap aWhiteBox_Map
;
121 PropertyMapper::getValueMap( aWhiteBox_Map
, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xWhiteDayProps
);
122 PropertyMapper::getMultiPropertyListsFromValueMap( aWhiteBox_Names
, aWhiteBox_Values
, aWhiteBox_Map
);
124 tPropertyNameValueMap aBlackBox_Map
;
125 PropertyMapper::getValueMap( aBlackBox_Map
, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xBlackDayProps
);
126 PropertyMapper::getMultiPropertyListsFromValueMap( aBlackBox_Names
, aBlackBox_Values
, aBlackBox_Map
);
129 catch( const uno::Exception
& e
)
131 SAL_WARN("chart2", "Exception caught. " << e
);
134 //(@todo maybe different iteration for breaks in axis ?)
135 sal_Int32 nEndIndex
= VSeriesPlotter::getPointCount();
136 double fLogicZ
= 1.5;//as defined
137 //iterate through all x values per indices
138 for( sal_Int32 nIndex
= 0; nIndex
< nEndIndex
; nIndex
++ )
140 for( auto const& rZSlot
: m_aZSlots
)
142 sal_Int32 nAttachedAxisIndex
= 0;
143 BarPositionHelper
* pPosHelper
= m_pMainPosHelper
.get();
146 nAttachedAxisIndex
= rZSlot
.front().getAttachedAxisIndexForFirstSeries();
147 //2ND_AXIS_IN_BARS so far one can assume to have the same plotter for each z slot
148 pPosHelper
= dynamic_cast<BarPositionHelper
*>(&( getPlottingPositionHelper( nAttachedAxisIndex
) ) );
150 pPosHelper
= m_pMainPosHelper
.get();
152 PlotterBase::m_pPosHelper
= pPosHelper
;
154 //update/create information for current group
155 pPosHelper
->updateSeriesCount( rZSlot
.size() );
157 //iterate through all x slots in this category
158 for( auto const& rXSlot
: rZSlot
)
160 //iterate through all series in this x slot
161 for( VDataSeries
* const pSeries
: rXSlot
.m_aSeriesVector
)
163 //collect data point information (logic coordinates, style ):
164 double fUnscaledX
= pSeries
->getXValue( nIndex
);
165 if( m_pExplicitCategoriesProvider
&& m_pExplicitCategoriesProvider
->isDateAxis() )
166 fUnscaledX
= DateHelper::RasterizeDateValue( fUnscaledX
, m_aNullDate
, m_nTimeResolution
);
167 if(fUnscaledX
<pPosHelper
->getLogicMinX() || fUnscaledX
>pPosHelper
->getLogicMaxX())
168 continue;//point not visible
169 double fScaledX
= pPosHelper
->getScaledSlotPos( fUnscaledX
, fSlotX
);
171 double fUnscaledY_First
= pSeries
->getY_First( nIndex
);
172 double fUnscaledY_Last
= pSeries
->getY_Last( nIndex
);
173 double fUnscaledY_Min
= pSeries
->getY_Min( nIndex
);
174 double fUnscaledY_Max
= pSeries
->getY_Max( nIndex
);
177 if(fUnscaledY_Last
<=fUnscaledY_First
)
179 std::swap(fUnscaledY_First
,fUnscaledY_Last
);
182 if(fUnscaledY_Max
<fUnscaledY_Min
)
183 std::swap(fUnscaledY_Min
,fUnscaledY_Max
);
184 //transformation 3) -> 4)
185 double fHalfScaledWidth
= pPosHelper
->getScaledSlotWidth()/2.0;
187 double fScaledY_First(fUnscaledY_First
);
188 double fScaledY_Last(fUnscaledY_Last
);
189 double fScaledY_Min(fUnscaledY_Min
);
190 double fScaledY_Max(fUnscaledY_Max
);
191 pPosHelper
->clipLogicValues( nullptr,&fScaledY_First
,nullptr );
192 pPosHelper
->clipLogicValues( nullptr,&fScaledY_Last
,nullptr );
193 pPosHelper
->clipLogicValues( nullptr,&fScaledY_Min
,nullptr );
194 pPosHelper
->clipLogicValues( nullptr,&fScaledY_Max
,nullptr );
195 pPosHelper
->doLogicScaling( nullptr,&fScaledY_First
,nullptr );
196 pPosHelper
->doLogicScaling( nullptr,&fScaledY_Last
,nullptr );
197 pPosHelper
->doLogicScaling( nullptr,&fScaledY_Min
,nullptr );
198 pPosHelper
->doLogicScaling( nullptr,&fScaledY_Max
,nullptr );
200 drawing::Position3D
aPosLeftFirst( pPosHelper
->transformScaledLogicToScene( fScaledX
-fHalfScaledWidth
, fScaledY_First
,0 ,true ) );
201 drawing::Position3D
aPosRightLast( pPosHelper
->transformScaledLogicToScene( fScaledX
+fHalfScaledWidth
, fScaledY_Last
,0 ,true ) );
202 drawing::Position3D
aPosMiddleFirst( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_First
,0 ,true ) );
203 drawing::Position3D
aPosMiddleLast( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_Last
,0 ,true ) );
204 drawing::Position3D
aPosMiddleMinimum( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_Min
,0 ,true ) );
205 drawing::Position3D
aPosMiddleMaximum( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_Max
,0 ,true ) );
207 uno::Reference
< drawing::XShapes
> xLossGainTarget( xGainTarget
);
209 xLossGainTarget
= xLossTarget
;
211 uno::Reference
< beans::XPropertySet
> xPointProp( pSeries
->getPropertiesOfPoint( nIndex
));
212 uno::Reference
< drawing::XShapes
> xPointGroupShape_Shapes(nullptr);
214 OUString aPointCID
= ObjectIdentifier::createPointCID( pSeries
->getPointCID_Stub(), nIndex
);
215 uno::Reference
< drawing::XShapes
> xSeriesGroupShape_Shapes( getSeriesGroupShape(pSeries
, xSeriesTarget
) );
216 xPointGroupShape_Shapes
= createGroupShape(xSeriesGroupShape_Shapes
,aPointCID
);
219 //create min-max line
220 if( isValidPosition(aPosMiddleMinimum
) && isValidPosition(aPosMiddleMaximum
) )
222 drawing::PolyPolygonShape3D aPoly
;
223 sal_Int32 nLineIndex
=0;
224 AddPointToPoly( aPoly
, aPosMiddleMinimum
, nLineIndex
);
225 AddPointToPoly( aPoly
, aPosMiddleMaximum
, nLineIndex
);
227 uno::Reference
< drawing::XShape
> xShape
=
228 m_pShapeFactory
->createLine2D( xPointGroupShape_Shapes
,
229 PolyToPointSequence(aPoly
));
230 setMappedProperties( xShape
, xPointProp
, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
233 //create first-last shape
234 if(bJapaneseStyle
&& isValidPosition(aPosLeftFirst
) && isValidPosition(aPosRightLast
) )
236 drawing::Direction3D aDiff
= aPosRightLast
-aPosLeftFirst
;
237 awt::Size
aAWTSize( Direction3DToAWTSize( aDiff
));
238 // workaround for bug in drawing: if height is 0 the box gets infinitely large
239 if( aAWTSize
.Height
== 0 )
242 tNameSequence aNames
;
243 tAnySequence aValues
;
245 uno::Reference
< drawing::XShape
> xShape
=
246 m_pShapeFactory
->createRectangle( xLossGainTarget
,
247 aAWTSize
, Position3DToAWTPoint( aPosLeftFirst
),
250 uno::Reference
< beans::XPropertySet
> xProp( xShape
, uno::UNO_QUERY
);
254 PropertyMapper::setMultiProperties( aBlackBox_Names
, aBlackBox_Values
, xProp
);
256 PropertyMapper::setMultiProperties( aWhiteBox_Names
, aWhiteBox_Values
, xProp
);
261 drawing::PolyPolygonShape3D aPoly
;
263 sal_Int32 nLineIndex
= 0;
264 if( bShowFirst
&& pPosHelper
->isLogicVisible( fUnscaledX
, fUnscaledY_First
,fLogicZ
)
265 && isValidPosition(aPosLeftFirst
) && isValidPosition(aPosMiddleFirst
) )
267 AddPointToPoly( aPoly
, aPosLeftFirst
, nLineIndex
);
268 AddPointToPoly( aPoly
, aPosMiddleFirst
, nLineIndex
++ );
270 if( pPosHelper
->isLogicVisible( fUnscaledX
, fUnscaledY_Last
,fLogicZ
)
271 && isValidPosition(aPosMiddleLast
) && isValidPosition(aPosRightLast
) )
273 AddPointToPoly( aPoly
, aPosMiddleLast
, nLineIndex
);
274 AddPointToPoly( aPoly
, aPosRightLast
, nLineIndex
);
277 if( aPoly
.SequenceX
.getLength() )
279 uno::Reference
< drawing::XShape
> xShape
=
280 m_pShapeFactory
->createLine2D( xPointGroupShape_Shapes
,
281 PolyToPointSequence(aPoly
) );
282 uno::Reference
< beans::XPropertySet
> xProp( xShape
, uno::UNO_QUERY
);
285 setMappedProperties( xShape
, xPointProp
, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
290 //create data point label
291 if( pSeries
->getDataPointLabelIfLabel(nIndex
) )
293 if(isValidPosition(aPosMiddleFirst
))
294 createDataLabel( xTextTarget
, *pSeries
, nIndex
295 , fUnscaledY_First
, 1.0, Position3DToAWTPoint(aPosMiddleFirst
), LABEL_ALIGN_LEFT_BOTTOM
);
296 if(isValidPosition(aPosMiddleLast
))
297 createDataLabel( xTextTarget
, *pSeries
, nIndex
298 , fUnscaledY_Last
, 1.0, Position3DToAWTPoint(aPosMiddleLast
), LABEL_ALIGN_RIGHT_TOP
);
299 if(isValidPosition(aPosMiddleMinimum
))
300 createDataLabel( xTextTarget
, *pSeries
, nIndex
301 , fUnscaledY_Min
, 1.0, Position3DToAWTPoint(aPosMiddleMinimum
), LABEL_ALIGN_BOTTOM
);
302 if(isValidPosition(aPosMiddleMaximum
))
303 createDataLabel( xTextTarget
, *pSeries
, nIndex
304 , fUnscaledY_Max
, 1.0, Position3DToAWTPoint(aPosMiddleMaximum
), LABEL_ALIGN_TOP
);
306 }//next series in x slot (next y slot)
311 /* @todo remove series shapes if empty
312 //remove and delete point-group-shape if empty
313 if(!xSeriesGroupShape_Shapes->getCount())
315 pSeries->m_xShape.set(NULL);
316 m_xLogicTarget->remove(xSeriesGroupShape_Shape);
320 //remove and delete series-group-shape if empty
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */