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"
27 #include "VLegendSymbolFactory.hxx"
28 #include "FormattedStringHelper.hxx"
29 #include "DataSeriesHelper.hxx"
30 #include "DateHelper.hxx"
31 #include <rtl/math.hxx>
32 #include <editeng/unoprnms.hxx>
36 using namespace ::com::sun::star
;
37 using namespace ::rtl::math
;
38 using namespace ::com::sun::star::chart2
;
39 using ::com::sun::star::uno::Reference
;
41 CandleStickChart::CandleStickChart( const uno::Reference
<XChartType
>& xChartTypeModel
42 , sal_Int32 nDimensionCount
)
43 : VSeriesPlotter( xChartTypeModel
, nDimensionCount
)
44 , m_pMainPosHelper( new BarPositionHelper() )
46 PlotterBase::m_pPosHelper
= m_pMainPosHelper
;
47 VSeriesPlotter::m_pMainPosHelper
= m_pMainPosHelper
;
50 CandleStickChart::~CandleStickChart()
52 delete m_pMainPosHelper
;
55 // MinimumAndMaximumSupplier
57 bool CandleStickChart::isSeparateStackingForDifferentSigns( sal_Int32
/* nDimensionIndex */ )
62 LegendSymbolStyle
CandleStickChart::getLegendSymbolStyle()
64 return LegendSymbolStyle_LINE
;
67 drawing::Direction3D
CandleStickChart::getPreferredDiagramAspectRatio() const
69 return drawing::Direction3D(-1,-1,-1);
72 void CandleStickChart::addSeries( VDataSeries
* pSeries
, sal_Int32
/* zSlot */, sal_Int32 xSlot
, sal_Int32 ySlot
)
74 //ignore y stacking for candle stick chart
75 VSeriesPlotter::addSeries( pSeries
, 0, xSlot
, ySlot
);
78 void CandleStickChart::createShapes()
80 if( m_aZSlots
.begin() == m_aZSlots
.end() ) //no series
86 OSL_ENSURE(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is(),"CandleStickChart is not proper initialized");
87 if(!(m_pShapeFactory
&&m_xLogicTarget
.is()&&m_xFinalTarget
.is()))
90 //the text labels should be always on top of the other series shapes
91 //therefore create an own group for the texts to move them to front
92 //(because the text group is created after the series group the texts are displayed on top)
94 uno::Reference
< drawing::XShapes
> xSeriesTarget(
95 createGroupShape( m_xLogicTarget
,OUString() ));
96 uno::Reference
< drawing::XShapes
> xLossTarget(
97 createGroupShape( m_xLogicTarget
, ObjectIdentifier::createClassifiedIdentifier(
98 OBJECTTYPE_DATA_STOCK_LOSS
, OUString() )));
99 uno::Reference
< drawing::XShapes
> xGainTarget(
100 createGroupShape( m_xLogicTarget
, ObjectIdentifier::createClassifiedIdentifier(
101 OBJECTTYPE_DATA_STOCK_GAIN
, OUString() )));
102 uno::Reference
< drawing::XShapes
> xTextTarget(
103 m_pShapeFactory
->createGroup2D( m_xFinalTarget
,OUString() ));
105 //check necessary here that different Y axis can not be stacked in the same group? ... hm?
107 bool bJapaneseStyle
=true;//@todo is this the correct default?
108 bool bShowFirst
= true;//is only important if bJapaneseStyle == false
109 tNameSequence aWhiteBox_Names
, aBlackBox_Names
;
110 tAnySequence aWhiteBox_Values
, aBlackBox_Values
;
113 if( m_xChartTypeModelProps
.is() )
115 m_xChartTypeModelProps
->getPropertyValue( "ShowFirst" ) >>= bShowFirst
;
117 uno::Reference
< beans::XPropertySet
> xWhiteDayProps(0);
118 uno::Reference
< beans::XPropertySet
> xBlackDayProps(0);
119 m_xChartTypeModelProps
->getPropertyValue( "Japanese" ) >>= bJapaneseStyle
;
120 m_xChartTypeModelProps
->getPropertyValue( "WhiteDay" ) >>= xWhiteDayProps
;
121 m_xChartTypeModelProps
->getPropertyValue( "BlackDay" ) >>= xBlackDayProps
;
123 tPropertyNameValueMap aWhiteBox_Map
;
124 PropertyMapper::getValueMap( aWhiteBox_Map
, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xWhiteDayProps
);
125 PropertyMapper::getMultiPropertyListsFromValueMap( aWhiteBox_Names
, aWhiteBox_Values
, aWhiteBox_Map
);
127 tPropertyNameValueMap aBlackBox_Map
;
128 PropertyMapper::getValueMap( aBlackBox_Map
, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xBlackDayProps
);
129 PropertyMapper::getMultiPropertyListsFromValueMap( aBlackBox_Names
, aBlackBox_Values
, aBlackBox_Map
);
132 catch( const uno::Exception
& e
)
134 ASSERT_EXCEPTION( e
);
137 //(@todo maybe different iteration for breaks in axis ?)
138 sal_Int32 nStartIndex
= 0;
139 sal_Int32 nEndIndex
= VSeriesPlotter::getPointCount();
140 double fLogicZ
= 1.5;//as defined
141 //iterate through all x values per indices
142 for( sal_Int32 nIndex
= nStartIndex
; nIndex
< nEndIndex
; nIndex
++ )
144 ::std::vector
< ::std::vector
< VDataSeriesGroup
> >::iterator aZSlotIter
= m_aZSlots
.begin();
145 const ::std::vector
< ::std::vector
< VDataSeriesGroup
> >::const_iterator aZSlotEnd
= m_aZSlots
.end();
146 for( sal_Int32 nZ
=0; aZSlotIter
!= aZSlotEnd
; ++aZSlotIter
, nZ
++ )
148 ::std::vector
< VDataSeriesGroup
>::iterator aXSlotIter
= aZSlotIter
->begin();
149 const ::std::vector
< VDataSeriesGroup
>::const_iterator aXSlotEnd
= aZSlotIter
->end();
151 sal_Int32 nAttachedAxisIndex
= 0;
152 BarPositionHelper
* pPosHelper
= m_pMainPosHelper
;
153 if( aXSlotIter
!= aXSlotEnd
)
155 nAttachedAxisIndex
= aXSlotIter
->getAttachedAxisIndexForFirstSeries();
156 //2ND_AXIS_IN_BARS so far one can assume to have the same plotter for each z slot
157 pPosHelper
= dynamic_cast<BarPositionHelper
*>(&( this->getPlottingPositionHelper( nAttachedAxisIndex
) ) );
159 pPosHelper
= m_pMainPosHelper
;
161 PlotterBase::m_pPosHelper
= pPosHelper
;
163 //update/create information for current group
164 pPosHelper
->updateSeriesCount( aZSlotIter
->size() );
165 //iterate through all x slots in this category
166 for( double fSlotX
=0; aXSlotIter
!= aXSlotEnd
; ++aXSlotIter
, fSlotX
+=1.0 )
168 ::std::vector
< VDataSeries
* >* pSeriesList
= &(aXSlotIter
->m_aSeriesVector
);
170 ::std::vector
< VDataSeries
* >::const_iterator aSeriesIter
= pSeriesList
->begin();
171 const ::std::vector
< VDataSeries
* >::const_iterator aSeriesEnd
= pSeriesList
->end();
172 //iterate through all series in this x slot
173 for( ; aSeriesIter
!= aSeriesEnd
; ++aSeriesIter
)
175 //collect data point information (logic coordinates, style ):
176 double fUnscaledX
= (*aSeriesIter
)->getXValue( nIndex
);
177 if( m_pExplicitCategoriesProvider
&& m_pExplicitCategoriesProvider
->isDateAxis() )
178 fUnscaledX
= DateHelper::RasterizeDateValue( fUnscaledX
, m_aNullDate
, m_nTimeResolution
);
179 if(fUnscaledX
<pPosHelper
->getLogicMinX() || fUnscaledX
>pPosHelper
->getLogicMaxX())
180 continue;//point not visible
181 double fScaledX
= pPosHelper
->getScaledSlotPos( fUnscaledX
, fSlotX
);
183 double fUnscaledY_First
= (*aSeriesIter
)->getY_First( nIndex
);
184 double fUnscaledY_Last
= (*aSeriesIter
)->getY_Last( nIndex
);
185 double fUnscaledY_Min
= (*aSeriesIter
)->getY_Min( nIndex
);
186 double fUnscaledY_Max
= (*aSeriesIter
)->getY_Max( nIndex
);
189 if(fUnscaledY_Last
<=fUnscaledY_First
)
191 std::swap(fUnscaledY_First
,fUnscaledY_Last
);
194 if(fUnscaledY_Max
<fUnscaledY_Min
)
195 std::swap(fUnscaledY_Min
,fUnscaledY_Max
);
196 //transformation 3) -> 4)
197 double fHalfScaledWidth
= pPosHelper
->getScaledSlotWidth()/2.0;
199 double fScaledY_First(fUnscaledY_First
);
200 double fScaledY_Last(fUnscaledY_Last
);
201 double fScaledY_Min(fUnscaledY_Min
);
202 double fScaledY_Max(fUnscaledY_Max
);
203 pPosHelper
->clipLogicValues( 0,&fScaledY_First
,0 );
204 pPosHelper
->clipLogicValues( 0,&fScaledY_Last
,0 );
205 pPosHelper
->clipLogicValues( 0,&fScaledY_Min
,0 );
206 pPosHelper
->clipLogicValues( 0,&fScaledY_Max
,0 );
207 pPosHelper
->doLogicScaling( 0,&fScaledY_First
,0 );
208 pPosHelper
->doLogicScaling( 0,&fScaledY_Last
,0 );
209 pPosHelper
->doLogicScaling( 0,&fScaledY_Min
,0 );
210 pPosHelper
->doLogicScaling( 0,&fScaledY_Max
,0 );
212 drawing::Position3D
aPosLeftFirst( pPosHelper
->transformScaledLogicToScene( fScaledX
-fHalfScaledWidth
, fScaledY_First
,0 ,true ) );
213 drawing::Position3D
aPosRightLast( pPosHelper
->transformScaledLogicToScene( fScaledX
+fHalfScaledWidth
, fScaledY_Last
,0 ,true ) );
214 drawing::Position3D
aPosMiddleFirst( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_First
,0 ,true ) );
215 drawing::Position3D
aPosMiddleLast( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_Last
,0 ,true ) );
216 drawing::Position3D
aPosMiddleMinimum( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_Min
,0 ,true ) );
217 drawing::Position3D
aPosMiddleMaximum( pPosHelper
->transformScaledLogicToScene( fScaledX
, fScaledY_Max
,0 ,true ) );
219 uno::Reference
< drawing::XShapes
> xLossGainTarget( xGainTarget
);
221 xLossGainTarget
= xLossTarget
;
223 uno::Reference
< beans::XPropertySet
> xPointProp( (*aSeriesIter
)->getPropertiesOfPoint( nIndex
));
224 uno::Reference
< drawing::XShapes
> xPointGroupShape_Shapes(0);
226 OUString aPointCID
= ObjectIdentifier::createPointCID( (*aSeriesIter
)->getPointCID_Stub(), nIndex
);
227 uno::Reference
< drawing::XShapes
> xSeriesGroupShape_Shapes( getSeriesGroupShape(*aSeriesIter
, xSeriesTarget
) );
228 xPointGroupShape_Shapes
= createGroupShape(xSeriesGroupShape_Shapes
,aPointCID
);
231 //create min-max line
232 if( isValidPosition(aPosMiddleMinimum
) && isValidPosition(aPosMiddleMaximum
) )
234 drawing::PolyPolygonShape3D aPoly
;
235 sal_Int32 nLineIndex
=0;
236 AddPointToPoly( aPoly
, aPosMiddleMinimum
, nLineIndex
);
237 AddPointToPoly( aPoly
, aPosMiddleMaximum
, nLineIndex
);
239 uno::Reference
< drawing::XShape
> xShape
=
240 m_pShapeFactory
->createLine2D( xPointGroupShape_Shapes
,
241 PolyToPointSequence(aPoly
), NULL
);
242 setMappedProperties( xShape
, xPointProp
, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
245 //create first-last shape
246 if(bJapaneseStyle
&& isValidPosition(aPosLeftFirst
) && isValidPosition(aPosRightLast
) )
248 drawing::Direction3D aDiff
= aPosRightLast
-aPosLeftFirst
;
249 awt::Size
aAWTSize( Direction3DToAWTSize( aDiff
));
250 // workaround for bug in drawing: if height is 0 the box gets infinitely large
251 if( aAWTSize
.Height
== 0 )
254 tNameSequence aNames
;
255 tAnySequence aValues
;
257 uno::Reference
< drawing::XShape
> xShape
=
258 m_pShapeFactory
->createRectangle( xLossGainTarget
,
259 aAWTSize
, Position3DToAWTPoint( aPosLeftFirst
),
262 uno::Reference
< beans::XPropertySet
> xProp( xShape
, uno::UNO_QUERY
);
266 PropertyMapper::setMultiProperties( aBlackBox_Names
, aBlackBox_Values
, xProp
);
268 PropertyMapper::setMultiProperties( aWhiteBox_Names
, aWhiteBox_Values
, xProp
);
273 drawing::PolyPolygonShape3D aPoly
;
275 sal_Int32 nLineIndex
= 0;
276 if( bShowFirst
&& pPosHelper
->isLogicVisible( fUnscaledX
, fUnscaledY_First
,fLogicZ
)
277 && isValidPosition(aPosLeftFirst
) && isValidPosition(aPosMiddleFirst
) )
279 AddPointToPoly( aPoly
, aPosLeftFirst
, nLineIndex
);
280 AddPointToPoly( aPoly
, aPosMiddleFirst
, nLineIndex
++ );
282 if( pPosHelper
->isLogicVisible( fUnscaledX
, fUnscaledY_Last
,fLogicZ
)
283 && isValidPosition(aPosMiddleLast
) && isValidPosition(aPosRightLast
) )
285 AddPointToPoly( aPoly
, aPosMiddleLast
, nLineIndex
);
286 AddPointToPoly( aPoly
, aPosRightLast
, nLineIndex
);
289 if( aPoly
.SequenceX
.getLength() )
291 uno::Reference
< drawing::XShape
> xShape
=
292 m_pShapeFactory
->createLine2D( xPointGroupShape_Shapes
,
293 PolyToPointSequence(aPoly
), NULL
);
294 uno::Reference
< beans::XPropertySet
> xProp( xShape
, uno::UNO_QUERY
);
297 setMappedProperties( xShape
, xPointProp
, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
302 //create data point label
303 if( (**aSeriesIter
).getDataPointLabelIfLabel(nIndex
) )
305 if(isValidPosition(aPosMiddleFirst
))
306 createDataLabel( xTextTarget
, **aSeriesIter
, nIndex
307 , fUnscaledY_First
, 1.0, Position3DToAWTPoint(aPosMiddleFirst
), LABEL_ALIGN_LEFT_BOTTOM
);
308 if(isValidPosition(aPosMiddleLast
))
309 createDataLabel( xTextTarget
, **aSeriesIter
, nIndex
310 , fUnscaledY_Last
, 1.0, Position3DToAWTPoint(aPosMiddleLast
), LABEL_ALIGN_RIGHT_TOP
);
311 if(isValidPosition(aPosMiddleMinimum
))
312 createDataLabel( xTextTarget
, **aSeriesIter
, nIndex
313 , fUnscaledY_Min
, 1.0, Position3DToAWTPoint(aPosMiddleMinimum
), LABEL_ALIGN_BOTTOM
);
314 if(isValidPosition(aPosMiddleMaximum
))
315 createDataLabel( xTextTarget
, **aSeriesIter
, nIndex
316 , fUnscaledY_Max
, 1.0, Position3DToAWTPoint(aPosMiddleMaximum
), LABEL_ALIGN_TOP
);
318 }//next series in x slot (next y slot)
322 /* @todo remove series shapes if empty
323 //remove and delete point-group-shape if empty
324 if(!xSeriesGroupShape_Shapes->getCount())
326 (*aSeriesIter)->m_xShape.set(NULL);
327 m_xLogicTarget->remove(xSeriesGroupShape_Shape);
331 //remove and delete series-group-shape if empty
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */