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 .
21 #include "CandleStickChart.hxx"
22 #include "ShapeFactory.hxx"
23 #include "CommonConverters.hxx"
24 #include "ObjectIdentifier.hxx"
25 #include "LabelPositionHelper.hxx"
26 #include "BarPositionHelper.hxx"
28 #include "VLegendSymbolFactory.hxx"
29 #include "FormattedStringHelper.hxx"
30 #include "DataSeriesHelper.hxx"
31 #include "DateHelper.hxx"
32 #include <rtl/math.hxx>
33 #include <editeng/unoprnms.hxx>
35 //.............................................................................
38 //.............................................................................
39 using namespace ::com::sun::star
;
40 using namespace ::rtl::math
;
41 using namespace ::com::sun::star::chart2
;
42 using ::com::sun::star::uno::Reference
;
44 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 CandleStickChart::CandleStickChart( const uno::Reference
<XChartType
>& xChartTypeModel
49 , sal_Int32 nDimensionCount
)
50 : VSeriesPlotter( xChartTypeModel
, nDimensionCount
)
51 , m_pMainPosHelper( new BarPositionHelper() )
53 PlotterBase::m_pPosHelper
= m_pMainPosHelper
;
54 VSeriesPlotter::m_pMainPosHelper
= m_pMainPosHelper
;
57 CandleStickChart::~CandleStickChart()
59 delete m_pMainPosHelper
;
62 //-------------------------------------------------------------------------
63 // MinimumAndMaximumSupplier
64 //-------------------------------------------------------------------------
66 bool CandleStickChart::isSeparateStackingForDifferentSigns( sal_Int32
/* nDimensionIndex */ )
71 //-----------------------------------------------------------------
72 //-----------------------------------------------------------------
73 //-----------------------------------------------------------------
75 LegendSymbolStyle
CandleStickChart::getLegendSymbolStyle()
77 return LegendSymbolStyle_LINE
;
80 drawing::Direction3D
CandleStickChart::getPreferredDiagramAspectRatio() const
82 return drawing::Direction3D(-1,-1,-1);
85 void CandleStickChart::addSeries( VDataSeries
* pSeries
, sal_Int32
/* zSlot */, sal_Int32 xSlot
, sal_Int32 ySlot
)
87 //ignore y stacking for candle stick chart
88 VSeriesPlotter::addSeries( pSeries
, 0, xSlot
, ySlot
);
91 void CandleStickChart::createShapes()
93 if( m_aZSlots
.begin() == m_aZSlots
.end() ) //no series
99 OSL_ENSURE(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is(),"CandleStickChart is not proper initialized");
100 if(!(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is()))
103 //the text labels should be always on top of the other series shapes
104 //therefore create an own group for the texts to move them to front
105 //(because the text group is created after the series group the texts are displayed on top)
107 uno::Reference
< drawing::XShapes
> xSeriesTarget(
108 createGroupShape( m_xLogicTarget
,OUString() ));
109 uno::Reference
< drawing::XShapes
> xLossTarget(
110 createGroupShape( m_xLogicTarget
, ObjectIdentifier::createClassifiedIdentifier(
111 OBJECTTYPE_DATA_STOCK_LOSS
, OUString() )));
112 uno::Reference
< drawing::XShapes
> xGainTarget(
113 createGroupShape( m_xLogicTarget
, ObjectIdentifier::createClassifiedIdentifier(
114 OBJECTTYPE_DATA_STOCK_GAIN
, OUString() )));
115 uno::Reference
< drawing::XShapes
> xTextTarget(
116 m_pShapeFactory
->createGroup2D( m_xFinalTarget
,OUString() ));
118 //---------------------------------------------
119 //check necessary here that different Y axis can not be stacked in the same group? ... hm?
121 bool bJapaneseStyle
=true;//@todo is this the correct default?
122 bool bShowFirst
= true;//is only important if bJapaneseStyle == false
123 tNameSequence aWhiteBox_Names
, aBlackBox_Names
;
124 tAnySequence aWhiteBox_Values
, aBlackBox_Values
;
127 if( m_xChartTypeModelProps
.is() )
129 m_xChartTypeModelProps
->getPropertyValue( "ShowFirst" ) >>= bShowFirst
;
131 uno::Reference
< beans::XPropertySet
> xWhiteDayProps(0);
132 uno::Reference
< beans::XPropertySet
> xBlackDayProps(0);
133 m_xChartTypeModelProps
->getPropertyValue( "Japanese" ) >>= bJapaneseStyle
;
134 m_xChartTypeModelProps
->getPropertyValue( "WhiteDay" ) >>= xWhiteDayProps
;
135 m_xChartTypeModelProps
->getPropertyValue( "BlackDay" ) >>= xBlackDayProps
;
137 tPropertyNameValueMap aWhiteBox_Map
;
138 PropertyMapper::getValueMap( aWhiteBox_Map
, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xWhiteDayProps
);
139 PropertyMapper::getMultiPropertyListsFromValueMap( aWhiteBox_Names
, aWhiteBox_Values
, aWhiteBox_Map
);
141 tPropertyNameValueMap aBlackBox_Map
;
142 PropertyMapper::getValueMap( aBlackBox_Map
, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xBlackDayProps
);
143 PropertyMapper::getMultiPropertyListsFromValueMap( aBlackBox_Names
, aBlackBox_Values
, aBlackBox_Map
);
146 catch( const uno::Exception
& e
)
148 ASSERT_EXCEPTION( e
);
151 //(@todo maybe different iteration for breaks in axis ?)
152 sal_Int32 nStartIndex
= 0;
153 sal_Int32 nEndIndex
= VSeriesPlotter::getPointCount();
154 double fLogicZ
= 1.5;//as defined
155 //=============================================================================
156 //iterate through all x values per indices
157 for( sal_Int32 nIndex
= nStartIndex
; nIndex
< nEndIndex
; nIndex
++ )
159 ::std::vector
< ::std::vector
< VDataSeriesGroup
> >::iterator aZSlotIter
= m_aZSlots
.begin();
160 const ::std::vector
< ::std::vector
< VDataSeriesGroup
> >::const_iterator aZSlotEnd
= m_aZSlots
.end();
161 //=============================================================================
162 for( sal_Int32 nZ
=0; aZSlotIter
!= aZSlotEnd
; ++aZSlotIter
, nZ
++ )
164 ::std::vector
< VDataSeriesGroup
>::iterator aXSlotIter
= aZSlotIter
->begin();
165 const ::std::vector
< VDataSeriesGroup
>::const_iterator aXSlotEnd
= aZSlotIter
->end();
167 sal_Int32 nAttachedAxisIndex
= 0;
168 BarPositionHelper
* pPosHelper
= m_pMainPosHelper
;
169 if( aXSlotIter
!= aXSlotEnd
)
171 nAttachedAxisIndex
= aXSlotIter
->getAttachedAxisIndexForFirstSeries();
172 //2ND_AXIS_IN_BARS so far one can assume to have the same plotter for each z slot
173 pPosHelper
= dynamic_cast<BarPositionHelper
*>(&( this->getPlottingPositionHelper( nAttachedAxisIndex
) ) );
175 pPosHelper
= m_pMainPosHelper
;
177 PlotterBase::m_pPosHelper
= pPosHelper
;
179 //update/create information for current group
180 pPosHelper
->updateSeriesCount( aZSlotIter
->size() );
181 //=============================================================================
182 //iterate through all x slots in this category
183 for( double fSlotX
=0; aXSlotIter
!= aXSlotEnd
; ++aXSlotIter
, fSlotX
+=1.0 )
185 ::std::vector
< VDataSeries
* >* pSeriesList
= &(aXSlotIter
->m_aSeriesVector
);
187 ::std::vector
< VDataSeries
* >::const_iterator aSeriesIter
= pSeriesList
->begin();
188 const ::std::vector
< VDataSeries
* >::const_iterator aSeriesEnd
= pSeriesList
->end();
189 //=============================================================================
190 //iterate through all series in this x slot
191 for( ; aSeriesIter
!= aSeriesEnd
; ++aSeriesIter
)
193 //collect data point information (logic coordinates, style ):
194 double fUnscaledX
= (*aSeriesIter
)->getXValue( nIndex
);
195 if( m_pExplicitCategoriesProvider
&& m_pExplicitCategoriesProvider
->isDateAxis() )
196 fUnscaledX
= DateHelper::RasterizeDateValue( fUnscaledX
, m_aNullDate
, m_nTimeResolution
);
197 if(fUnscaledX
<pPosHelper
->getLogicMinX() || fUnscaledX
>pPosHelper
->getLogicMaxX())
198 continue;//point not visible
199 double fScaledX
= pPosHelper
->getScaledSlotPos( fUnscaledX
, fSlotX
);
201 double fUnscaledY_First
= (*aSeriesIter
)->getY_First( nIndex
);
202 double fUnscaledY_Last
= (*aSeriesIter
)->getY_Last( nIndex
);
203 double fUnscaledY_Min
= (*aSeriesIter
)->getY_Min( nIndex
);
204 double fUnscaledY_Max
= (*aSeriesIter
)->getY_Max( nIndex
);
207 if(fUnscaledY_Last
<=fUnscaledY_First
)
209 std::swap(fUnscaledY_First
,fUnscaledY_Last
);
212 if(fUnscaledY_Max
<fUnscaledY_Min
)
213 std::swap(fUnscaledY_Min
,fUnscaledY_Max
);
214 //transformation 3) -> 4)
215 double fHalfScaledWidth
= pPosHelper
->getScaledSlotWidth()/2.0;
217 double fScaledY_First(fUnscaledY_First
);
218 double fScaledY_Last(fUnscaledY_Last
);
219 double fScaledY_Min(fUnscaledY_Min
);
220 double fScaledY_Max(fUnscaledY_Max
);
221 pPosHelper
->clipLogicValues( 0,&fScaledY_First
,0 );
222 pPosHelper
->clipLogicValues( 0,&fScaledY_Last
,0 );
223 pPosHelper
->clipLogicValues( 0,&fScaledY_Min
,0 );
224 pPosHelper
->clipLogicValues( 0,&fScaledY_Max
,0 );
225 pPosHelper
->doLogicScaling( 0,&fScaledY_First
,0 );
226 pPosHelper
->doLogicScaling( 0,&fScaledY_Last
,0 );
227 pPosHelper
->doLogicScaling( 0,&fScaledY_Min
,0 );
228 pPosHelper
->doLogicScaling( 0,&fScaledY_Max
,0 );
230 drawing::Position3D
aPosLeftFirst( pPosHelper
->transformScaledLogicToScene( fScaledX
-fHalfScaledWidth
, fScaledY_First
,0 ,true ) );
231 drawing::Position3D
aPosRightLast( pPosHelper
->transformScaledLogicToScene( fScaledX
+fHalfScaledWidth
, fScaledY_Last
,0 ,true ) );
232 drawing::Position3D
aPosMiddleFirst( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_First
,0 ,true ) );
233 drawing::Position3D
aPosMiddleLast( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_Last
,0 ,true ) );
234 drawing::Position3D
aPosMiddleMinimum( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_Min
,0 ,true ) );
235 drawing::Position3D
aPosMiddleMaximum( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_Max
,0 ,true ) );
237 uno::Reference
< drawing::XShapes
> xLossGainTarget( xGainTarget
);
239 xLossGainTarget
= xLossTarget
;
241 uno::Reference
< beans::XPropertySet
> xPointProp( (*aSeriesIter
)->getPropertiesOfPoint( nIndex
));
242 uno::Reference
< drawing::XShapes
> xPointGroupShape_Shapes(0);
244 OUString aPointCID
= ObjectIdentifier::createPointCID( (*aSeriesIter
)->getPointCID_Stub(), nIndex
);
245 uno::Reference
< drawing::XShapes
> xSeriesGroupShape_Shapes( getSeriesGroupShape(*aSeriesIter
, xSeriesTarget
) );
246 xPointGroupShape_Shapes
= createGroupShape(xSeriesGroupShape_Shapes
,aPointCID
);
249 //create min-max line
250 if( isValidPosition(aPosMiddleMinimum
) && isValidPosition(aPosMiddleMaximum
) )
252 uno::Reference
< drawing::XShape
> xShape(
253 m_xShapeFactory
->createInstance( "com.sun.star.drawing.PolyLineShape" ),
255 xPointGroupShape_Shapes
->add(xShape
);
256 uno::Reference
< beans::XPropertySet
> xProp( xShape
, uno::UNO_QUERY
);
259 drawing::PolyPolygonShape3D aPoly
;
260 sal_Int32 nLineIndex
=0;
261 AddPointToPoly( aPoly
, aPosMiddleMinimum
, nLineIndex
);
262 AddPointToPoly( aPoly
, aPosMiddleMaximum
, nLineIndex
);
263 xProp
->setPropertyValue( UNO_NAME_POLYPOLYGON
, uno::makeAny( PolyToPointSequence(aPoly
) ) );
265 this->setMappedProperties( xShape
, xPointProp
, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
268 //create first-last shape
269 if(bJapaneseStyle
&& isValidPosition(aPosLeftFirst
) && isValidPosition(aPosRightLast
) )
271 uno::Reference
< drawing::XShape
> xShape(
272 m_xShapeFactory
->createInstance( "com.sun.star.drawing.RectangleShape" ),
274 xLossGainTarget
->add(xShape
);
276 xShape
->setPosition( Position3DToAWTPoint( aPosLeftFirst
) );
277 drawing::Direction3D aDiff
= aPosRightLast
-aPosLeftFirst
;
278 awt::Size
aAWTSize( Direction3DToAWTSize( aDiff
));
279 // workaround for bug in drawing: if height is 0 the box gets infinitely large
280 if( aAWTSize
.Height
== 0 )
282 xShape
->setSize( aAWTSize
);
284 uno::Reference
< beans::XPropertySet
> xProp( xShape
, uno::UNO_QUERY
);
288 PropertyMapper::setMultiProperties( aBlackBox_Names
, aBlackBox_Values
, xProp
);
290 PropertyMapper::setMultiProperties( aWhiteBox_Names
, aWhiteBox_Values
, xProp
);
295 drawing::PolyPolygonShape3D aPoly
;
297 sal_Int32 nLineIndex
= 0;
298 if( bShowFirst
&& pPosHelper
->isLogicVisible( fUnscaledX
, fUnscaledY_First
,fLogicZ
)
299 && isValidPosition(aPosLeftFirst
) && isValidPosition(aPosMiddleFirst
) )
301 AddPointToPoly( aPoly
, aPosLeftFirst
, nLineIndex
);
302 AddPointToPoly( aPoly
, aPosMiddleFirst
, nLineIndex
++ );
304 if( pPosHelper
->isLogicVisible( fUnscaledX
, fUnscaledY_Last
,fLogicZ
)
305 && isValidPosition(aPosMiddleLast
) && isValidPosition(aPosRightLast
) )
307 AddPointToPoly( aPoly
, aPosMiddleLast
, nLineIndex
);
308 AddPointToPoly( aPoly
, aPosRightLast
, nLineIndex
);
311 if( aPoly
.SequenceX
.getLength() )
313 uno::Reference
< drawing::XShape
> xShape(
314 m_xShapeFactory
->createInstance( "com.sun.star.drawing.PolyLineShape" ),
316 xPointGroupShape_Shapes
->add(xShape
);
317 uno::Reference
< beans::XPropertySet
> xProp( xShape
, uno::UNO_QUERY
);
320 xProp
->setPropertyValue( UNO_NAME_POLYPOLYGON
, uno::makeAny( PolyToPointSequence(aPoly
) ) );
321 this->setMappedProperties( xShape
, xPointProp
, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
326 //create data point label
327 if( (**aSeriesIter
).getDataPointLabelIfLabel(nIndex
) )
329 if(isValidPosition(aPosMiddleFirst
))
330 this->createDataLabel( xTextTarget
, **aSeriesIter
, nIndex
331 , fUnscaledY_First
, 1.0, Position3DToAWTPoint(aPosMiddleFirst
), LABEL_ALIGN_LEFT_BOTTOM
);
332 if(isValidPosition(aPosMiddleLast
))
333 this->createDataLabel( xTextTarget
, **aSeriesIter
, nIndex
334 , fUnscaledY_Last
, 1.0, Position3DToAWTPoint(aPosMiddleLast
), LABEL_ALIGN_RIGHT_TOP
);
335 if(isValidPosition(aPosMiddleMinimum
))
336 this->createDataLabel( xTextTarget
, **aSeriesIter
, nIndex
337 , fUnscaledY_Min
, 1.0, Position3DToAWTPoint(aPosMiddleMinimum
), LABEL_ALIGN_BOTTOM
);
338 if(isValidPosition(aPosMiddleMaximum
))
339 this->createDataLabel( xTextTarget
, **aSeriesIter
, nIndex
340 , fUnscaledY_Max
, 1.0, Position3DToAWTPoint(aPosMiddleMaximum
), LABEL_ALIGN_TOP
);
342 }//next series in x slot (next y slot)
346 //=============================================================================
347 //=============================================================================
348 //=============================================================================
349 /* @todo remove series shapes if empty
350 //remove and delete point-group-shape if empty
351 if(!xSeriesGroupShape_Shapes->getCount())
353 (*aSeriesIter)->m_xShape.set(NULL);
354 m_xLogicTarget->remove(xSeriesGroupShape_Shape);
358 //remove and delete series-group-shape if empty
363 //.............................................................................
365 //.............................................................................
367 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */