1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_chart2.hxx"
26 
27 #include "CandleStickChart.hxx"
28 #include "ShapeFactory.hxx"
29 #include "CommonConverters.hxx"
30 #include "ObjectIdentifier.hxx"
31 #include "LabelPositionHelper.hxx"
32 #include "BarPositionHelper.hxx"
33 #include "macros.hxx"
34 #include "VLegendSymbolFactory.hxx"
35 #include "FormattedStringHelper.hxx"
36 #include "DataSeriesHelper.hxx"
37 #include "DateHelper.hxx"
38 #include <tools/debug.hxx>
39 #include <rtl/math.hxx>
40 #include <editeng/unoprnms.hxx>
41 
42 //.............................................................................
43 namespace chart
44 {
45 //.............................................................................
46 using namespace ::com::sun::star;
47 using namespace ::rtl::math;
48 using namespace ::com::sun::star::chart2;
49 using ::com::sun::star::uno::Reference;
50 using ::rtl::OUString;
51 
52 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
55 
CandleStickChart(const uno::Reference<XChartType> & xChartTypeModel,sal_Int32 nDimensionCount)56 CandleStickChart::CandleStickChart( const uno::Reference<XChartType>& xChartTypeModel
57                                     , sal_Int32 nDimensionCount )
58         : VSeriesPlotter( xChartTypeModel, nDimensionCount )
59         , m_pMainPosHelper( new BarPositionHelper() )
60 {
61     PlotterBase::m_pPosHelper = m_pMainPosHelper;
62     VSeriesPlotter::m_pMainPosHelper = m_pMainPosHelper;
63 }
64 
~CandleStickChart()65 CandleStickChart::~CandleStickChart()
66 {
67     delete m_pMainPosHelper;
68 }
69 
70 //-------------------------------------------------------------------------
71 // MinimumAndMaximumSupplier
72 //-------------------------------------------------------------------------
73 
isSeperateStackingForDifferentSigns(sal_Int32)74 bool CandleStickChart::isSeperateStackingForDifferentSigns( sal_Int32 /* nDimensionIndex */ )
75 {
76     return false;
77 }
78 
79 //-----------------------------------------------------------------
80 //-----------------------------------------------------------------
81 //-----------------------------------------------------------------
82 
getLegendSymbolStyle()83 LegendSymbolStyle CandleStickChart::getLegendSymbolStyle()
84 {
85     return LegendSymbolStyle_LINE;
86 }
87 
88 //-----------------------------------------------------------------
89 // lang::XServiceInfo
90 //-----------------------------------------------------------------
91 /*
92 APPHELPER_XSERVICEINFO_IMPL(CandleStickChart,CHART2_VIEW_CANDLESTICKCHART_SERVICE_IMPLEMENTATION_NAME)
93 
94 	uno::Sequence< rtl::OUString > CandleStickChart
95 ::getSupportedServiceNames_Static()
96 {
97 	uno::Sequence< rtl::OUString > aSNS( 1 );
98 	aSNS.getArray()[ 0 ] = CHART2_VIEW_CANDLESTICKCHART_SERVICE_NAME;
99 	return aSNS;
100 }
101 */
102 
getPreferredDiagramAspectRatio() const103 drawing::Direction3D CandleStickChart::getPreferredDiagramAspectRatio() const
104 {
105     return drawing::Direction3D(-1,-1,-1);
106 }
107 
addSeries(VDataSeries * pSeries,sal_Int32,sal_Int32 xSlot,sal_Int32 ySlot)108 void CandleStickChart::addSeries( VDataSeries* pSeries, sal_Int32 /* zSlot */, sal_Int32 xSlot, sal_Int32 ySlot )
109 {
110     //ignore y stacking for candle stick chart
111     VSeriesPlotter::addSeries( pSeries, 0, xSlot, ySlot );
112 }
113 
createShapes()114 void CandleStickChart::createShapes()
115 {
116     if( m_aZSlots.begin() == m_aZSlots.end() ) //no series
117         return;
118 
119     if( m_nDimension!=2 )
120         return;
121 
122     DBG_ASSERT(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is(),"CandleStickChart is not proper initialized");
123     if(!(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is()))
124         return;
125 
126     //the text labels should be always on top of the other series shapes
127     //therefore create an own group for the texts to move them to front
128     //(because the text group is created after the series group the texts are displayed on top)
129 
130     uno::Reference< drawing::XShapes > xSeriesTarget(
131         createGroupShape( m_xLogicTarget,rtl::OUString() ));
132     uno::Reference< drawing::XShapes > xLossTarget(
133         createGroupShape( m_xLogicTarget, ObjectIdentifier::createClassifiedIdentifier(
134             OBJECTTYPE_DATA_STOCK_LOSS, rtl::OUString() )));
135     uno::Reference< drawing::XShapes > xGainTarget(
136         createGroupShape( m_xLogicTarget, ObjectIdentifier::createClassifiedIdentifier(
137             OBJECTTYPE_DATA_STOCK_GAIN, rtl::OUString() )));
138     uno::Reference< drawing::XShapes > xTextTarget(
139         m_pShapeFactory->createGroup2D( m_xFinalTarget,rtl::OUString() ));
140 
141     //---------------------------------------------
142     //check necessary here that different Y axis can not be stacked in the same group? ... hm?
143 
144     bool bJapaneseStyle=true;//@todo is this the correct default?
145     bool bShowFirst = true;//is only important if bJapaneseStyle == false
146     tNameSequence aWhiteBox_Names, aBlackBox_Names;
147     tAnySequence  aWhiteBox_Values, aBlackBox_Values;
148     try
149     {
150         if( m_xChartTypeModelProps.is() )
151         {
152             m_xChartTypeModelProps->getPropertyValue( C2U( "ShowFirst" ) ) >>= bShowFirst;
153 
154             uno::Reference< beans::XPropertySet > xWhiteDayProps(0);
155             uno::Reference< beans::XPropertySet > xBlackDayProps(0);
156             m_xChartTypeModelProps->getPropertyValue( C2U( "Japanese" ) ) >>= bJapaneseStyle;
157             m_xChartTypeModelProps->getPropertyValue( C2U( "WhiteDay" ) ) >>= xWhiteDayProps;
158             m_xChartTypeModelProps->getPropertyValue( C2U( "BlackDay" ) ) >>= xBlackDayProps;
159 
160             tPropertyNameValueMap aWhiteBox_Map;
161             PropertyMapper::getValueMap( aWhiteBox_Map, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xWhiteDayProps );
162             PropertyMapper::getMultiPropertyListsFromValueMap( aWhiteBox_Names, aWhiteBox_Values, aWhiteBox_Map );
163 
164             tPropertyNameValueMap aBlackBox_Map;
165             PropertyMapper::getValueMap( aBlackBox_Map, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xBlackDayProps );
166             PropertyMapper::getMultiPropertyListsFromValueMap( aBlackBox_Names, aBlackBox_Values, aBlackBox_Map );
167         }
168     }
169     catch( uno::Exception& e )
170 	{
171         ASSERT_EXCEPTION( e );
172     }
173 
174 	//(@todo maybe different iteration for breaks in axis ?)
175     sal_Int32 nStartIndex = 0;
176     sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
177     double fLogicZ = 1.5;//as defined
178 //=============================================================================
179     //iterate through all x values per indices
180     for( sal_Int32 nIndex = nStartIndex; nIndex < nEndIndex; nIndex++ )
181 	{
182         ::std::vector< ::std::vector< VDataSeriesGroup > >::iterator             aZSlotIter = m_aZSlots.begin();
183         const ::std::vector< ::std::vector< VDataSeriesGroup > >::const_iterator  aZSlotEnd = m_aZSlots.end();
184 //=============================================================================
185         for( sal_Int32 nZ=0; aZSlotIter != aZSlotEnd; aZSlotIter++, nZ++ )
186         {
187             ::std::vector< VDataSeriesGroup >::iterator             aXSlotIter = aZSlotIter->begin();
188             const ::std::vector< VDataSeriesGroup >::const_iterator aXSlotEnd = aZSlotIter->end();
189 
190             sal_Int32 nAttachedAxisIndex = 0;
191             BarPositionHelper* pPosHelper = m_pMainPosHelper;
192             if( aXSlotIter != aXSlotEnd )
193             {
194                 nAttachedAxisIndex = aXSlotIter->getAttachedAxisIndexForFirstSeries();
195                 //2ND_AXIS_IN_BARS so far one can assume to have the same plotter for each z slot
196                 pPosHelper = dynamic_cast<BarPositionHelper*>(&( this->getPlottingPositionHelper( nAttachedAxisIndex ) ) );
197                 if(!pPosHelper)
198                     pPosHelper = m_pMainPosHelper;
199             }
200             PlotterBase::m_pPosHelper = pPosHelper;
201 
202             //update/create information for current group
203             pPosHelper->updateSeriesCount( aZSlotIter->size() );
204 //=============================================================================
205             //iterate through all x slots in this category
206             for( double fSlotX=0; aXSlotIter != aXSlotEnd; aXSlotIter++, fSlotX+=1.0 )
207 	        {
208                 ::std::vector< VDataSeries* >* pSeriesList = &(aXSlotIter->m_aSeriesVector);
209 
210                 ::std::vector< VDataSeries* >::const_iterator       aSeriesIter = pSeriesList->begin();
211                 const ::std::vector< VDataSeries* >::const_iterator aSeriesEnd  = pSeriesList->end();
212                 aSeriesIter = pSeriesList->begin();
213     //=============================================================================
214                 //iterate through all series in this x slot
215 	            for( ; aSeriesIter != aSeriesEnd; aSeriesIter++ )
216                 {
217                     //collect data point information (logic coordinates, style ):
218                     double fUnscaledX = (*aSeriesIter)->getXValue( nIndex );
219                     if( m_pExplicitCategoriesProvider && m_pExplicitCategoriesProvider->isDateAxis() )
220                         fUnscaledX = DateHelper::RasterizeDateValue( fUnscaledX, m_aNullDate, m_nTimeResolution );
221                     if(fUnscaledX<pPosHelper->getLogicMinX() || fUnscaledX>pPosHelper->getLogicMaxX())
222                         continue;//point not visible
223                     double fScaledX = pPosHelper->getScaledSlotPos( fUnscaledX, fSlotX );
224 
225                     double fUnscaledY_First = (*aSeriesIter)->getY_First( nIndex );
226                     double fUnscaledY_Last = (*aSeriesIter)->getY_Last( nIndex );
227                     double fUnscaledY_Min = (*aSeriesIter)->getY_Min( nIndex );
228                     double fUnscaledY_Max = (*aSeriesIter)->getY_Max( nIndex );
229 
230                     bool bBlack=false;
231                     if(fUnscaledY_Last<=fUnscaledY_First)
232                     {
233                         std::swap(fUnscaledY_First,fUnscaledY_Last);
234                         bBlack=true;
235                     }
236                     if(fUnscaledY_Max<fUnscaledY_Min)
237                         std::swap(fUnscaledY_Min,fUnscaledY_Max);
238                     //transformation 3) -> 4)
239                     double fHalfScaledWidth = pPosHelper->getScaledSlotWidth()/2.0;
240 
241                     double fScaledY_First(fUnscaledY_First);
242                     double fScaledY_Last(fUnscaledY_Last);
243                     double fScaledY_Min(fUnscaledY_Min);
244                     double fScaledY_Max(fUnscaledY_Max);
245                     pPosHelper->clipLogicValues( 0,&fScaledY_First,0 );
246                     pPosHelper->clipLogicValues( 0,&fScaledY_Last,0 );
247                     pPosHelper->clipLogicValues( 0,&fScaledY_Min,0 );
248                     pPosHelper->clipLogicValues( 0,&fScaledY_Max,0 );
249                     pPosHelper->doLogicScaling( 0,&fScaledY_First,0 );
250                     pPosHelper->doLogicScaling( 0,&fScaledY_Last,0 );
251                     pPosHelper->doLogicScaling( 0,&fScaledY_Min,0 );
252                     pPosHelper->doLogicScaling( 0,&fScaledY_Max,0 );
253 
254                     drawing::Position3D aPosLeftFirst( pPosHelper->transformScaledLogicToScene( fScaledX-fHalfScaledWidth, fScaledY_First ,0 ,true ) );
255                     drawing::Position3D aPosRightLast( pPosHelper->transformScaledLogicToScene( fScaledX+fHalfScaledWidth, fScaledY_Last  ,0 ,true ) );
256                     drawing::Position3D aPosMiddleFirst( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_First ,0 ,true ) );
257                     drawing::Position3D aPosMiddleLast( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_Last  ,0 ,true ) );
258                     drawing::Position3D aPosMiddleMinimum( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_Min ,0 ,true ) );
259                     drawing::Position3D aPosMiddleMaximum( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_Max ,0 ,true ) );
260 
261                     uno::Reference< drawing::XShapes > xLossGainTarget( xGainTarget );
262                     if(bBlack)
263                         xLossGainTarget = xLossTarget;
264 
265                     uno::Reference< beans::XPropertySet > xPointProp( (*aSeriesIter)->getPropertiesOfPoint( nIndex ));
266                     uno::Reference< drawing::XShapes > xPointGroupShape_Shapes(0);
267                     {
268                         rtl::OUString aPointCID = ObjectIdentifier::createPointCID( (*aSeriesIter)->getPointCID_Stub(), nIndex );
269                         uno::Reference< drawing::XShapes > xSeriesGroupShape_Shapes( getSeriesGroupShape(*aSeriesIter, xSeriesTarget) );
270                         xPointGroupShape_Shapes = createGroupShape(xSeriesGroupShape_Shapes,aPointCID);
271                     }
272 
273                     //create min-max line
274                     if( isValidPosition(aPosMiddleMinimum) && isValidPosition(aPosMiddleMaximum) )
275                     {
276                         uno::Reference< drawing::XShape > xShape( m_xShapeFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
277                                 "com.sun.star.drawing.PolyLineShape" ) ) ), uno::UNO_QUERY );
278                         xPointGroupShape_Shapes->add(xShape);
279                         uno::Reference< beans::XPropertySet > xProp( xShape, uno::UNO_QUERY );
280                         if(xProp.is())
281                         {
282                             drawing::PolyPolygonShape3D aPoly;
283                             sal_Int32 nLineIndex =0;
284                             AddPointToPoly( aPoly, aPosMiddleMinimum, nLineIndex);
285                             AddPointToPoly( aPoly, aPosMiddleMaximum, nLineIndex);
286                             xProp->setPropertyValue( C2U( UNO_NAME_POLYPOLYGON ), uno::makeAny( PolyToPointSequence(aPoly) ) );
287                         }
288                         this->setMappedProperties( xShape, xPointProp, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
289                     }
290 
291                     //create first-last shape
292                     if(bJapaneseStyle && isValidPosition(aPosLeftFirst) && isValidPosition(aPosRightLast) )
293                     {
294                         uno::Reference< drawing::XShape > xShape( m_xShapeFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
295                                 "com.sun.star.drawing.RectangleShape" ) ) ), uno::UNO_QUERY );
296                         xLossGainTarget->add(xShape);
297 
298                         xShape->setPosition( Position3DToAWTPoint( aPosLeftFirst ) );
299                         drawing::Direction3D aDiff = aPosRightLast-aPosLeftFirst;
300                         awt::Size aAWTSize( Direction3DToAWTSize( aDiff ));
301                         // workaround for bug in drawing: if height is 0 the box gets infinitely large
302                         if( aAWTSize.Height == 0 )
303                             aAWTSize.Height = 1;
304                         xShape->setSize( aAWTSize );
305 
306                         uno::Reference< beans::XPropertySet > xProp( xShape, uno::UNO_QUERY );
307                         if(xProp.is())
308                         {
309                             if(bBlack)
310                                 PropertyMapper::setMultiProperties( aBlackBox_Names, aBlackBox_Values, xProp );
311                             else
312                                 PropertyMapper::setMultiProperties( aWhiteBox_Names, aWhiteBox_Values, xProp );
313                         }
314                     }
315                     else
316                     {
317                         drawing::PolyPolygonShape3D aPoly;
318 
319                         sal_Int32 nLineIndex = 0;
320                         if( bShowFirst &&  pPosHelper->isLogicVisible( fUnscaledX, fUnscaledY_First ,fLogicZ )
321                             && isValidPosition(aPosLeftFirst) && isValidPosition(aPosMiddleFirst) )
322                         {
323                             AddPointToPoly( aPoly, aPosLeftFirst, nLineIndex );
324                             AddPointToPoly( aPoly, aPosMiddleFirst, nLineIndex++ );
325                         }
326                         if( pPosHelper->isLogicVisible( fUnscaledX, fUnscaledY_Last ,fLogicZ )
327                             && isValidPosition(aPosMiddleLast) && isValidPosition(aPosRightLast) )
328                         {
329                             AddPointToPoly( aPoly, aPosMiddleLast, nLineIndex );
330                             AddPointToPoly( aPoly, aPosRightLast, nLineIndex );
331                         }
332 
333                         if( aPoly.SequenceX.getLength() )
334                         {
335                             uno::Reference< drawing::XShape > xShape( m_xShapeFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
336                                 "com.sun.star.drawing.PolyLineShape" ) ) ), uno::UNO_QUERY );
337                             xPointGroupShape_Shapes->add(xShape);
338                             uno::Reference< beans::XPropertySet > xProp( xShape, uno::UNO_QUERY );
339                             if(xProp.is())
340                             {
341                                 xProp->setPropertyValue( C2U( UNO_NAME_POLYPOLYGON ), uno::makeAny( PolyToPointSequence(aPoly) ) );
342                                 this->setMappedProperties( xShape, xPointProp, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
343                             }
344                         }
345                     }
346 
347                     //create data point label
348                     if( (**aSeriesIter).getDataPointLabelIfLabel(nIndex) )
349                     {
350                         if(isValidPosition(aPosMiddleFirst))
351                             this->createDataLabel( xTextTarget, **aSeriesIter, nIndex
352                                         , fUnscaledY_First, 1.0, Position3DToAWTPoint(aPosMiddleFirst), LABEL_ALIGN_LEFT_BOTTOM );
353                         if(isValidPosition(aPosMiddleLast))
354                             this->createDataLabel( xTextTarget, **aSeriesIter, nIndex
355                                         , fUnscaledY_Last, 1.0, Position3DToAWTPoint(aPosMiddleLast), LABEL_ALIGN_RIGHT_TOP );
356                         if(isValidPosition(aPosMiddleMinimum))
357                             this->createDataLabel( xTextTarget, **aSeriesIter, nIndex
358                                         , fUnscaledY_Min, 1.0, Position3DToAWTPoint(aPosMiddleMinimum), LABEL_ALIGN_BOTTOM );
359                         if(isValidPosition(aPosMiddleMaximum))
360                             this->createDataLabel( xTextTarget, **aSeriesIter, nIndex
361                                         , fUnscaledY_Max, 1.0, Position3DToAWTPoint(aPosMiddleMaximum), LABEL_ALIGN_TOP );
362                     }
363                 }//next series in x slot (next y slot)
364             }//next x slot
365         }//next z slot
366 	}//next category
367 //=============================================================================
368 //=============================================================================
369 //=============================================================================
370     /* @todo remove series shapes if empty
371     //remove and delete point-group-shape if empty
372     if(!xSeriesGroupShape_Shapes->getCount())
373     {
374         (*aSeriesIter)->m_xShape.set(NULL);
375         m_xLogicTarget->remove(xSeriesGroupShape_Shape);
376     }
377     */
378 
379 	//remove and delete series-group-shape if empty
380 
381     //... todo
382 }
383 
384 //.............................................................................
385 } //namespace chart
386 //.............................................................................
387