Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / view / charttypes / CandleStickChart.cxx
blobe48489a61906778e0ac8219fc26099ebe3db6f3f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <sal/log.hxx>
29 #include <tools/diagnose_ex.h>
30 #include <osl/diagnose.h>
32 namespace chart
34 using namespace ::com::sun::star;
35 using namespace ::rtl::math;
36 using namespace ::com::sun::star::chart2;
38 CandleStickChart::CandleStickChart( const uno::Reference<XChartType>& xChartTypeModel
39 , sal_Int32 nDimensionCount )
40 : VSeriesPlotter( xChartTypeModel, nDimensionCount )
41 , m_pMainPosHelper( new BarPositionHelper() )
43 PlotterBase::m_pPosHelper = m_pMainPosHelper.get();
44 VSeriesPlotter::m_pMainPosHelper = m_pMainPosHelper.get();
47 CandleStickChart::~CandleStickChart()
51 // MinimumAndMaximumSupplier
53 bool CandleStickChart::isSeparateStackingForDifferentSigns( sal_Int32 /* nDimensionIndex */ )
55 return false;
58 LegendSymbolStyle CandleStickChart::getLegendSymbolStyle()
60 return LegendSymbolStyle::Line;
63 drawing::Direction3D CandleStickChart::getPreferredDiagramAspectRatio() const
65 return drawing::Direction3D(-1,-1,-1);
68 void CandleStickChart::addSeries( std::unique_ptr<VDataSeries> pSeries, sal_Int32 /* zSlot */, sal_Int32 xSlot, sal_Int32 ySlot )
70 //ignore y stacking for candle stick chart
71 VSeriesPlotter::addSeries( std::move(pSeries), 0, xSlot, ySlot );
74 void CandleStickChart::createShapes()
76 if( m_aZSlots.empty() ) //no series
77 return;
79 if( m_nDimension!=2 )
80 return;
82 OSL_ENSURE(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is(),"CandleStickChart is not proper initialized");
83 if(!(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is()))
84 return;
86 //the text labels should be always on top of the other series shapes
87 //therefore create an own group for the texts to move them to front
88 //(because the text group is created after the series group the texts are displayed on top)
90 uno::Reference< drawing::XShapes > xSeriesTarget(
91 createGroupShape( m_xLogicTarget ));
92 uno::Reference< drawing::XShapes > xLossTarget(
93 createGroupShape( m_xLogicTarget, ObjectIdentifier::createClassifiedIdentifier(
94 OBJECTTYPE_DATA_STOCK_LOSS, OUString() )));
95 uno::Reference< drawing::XShapes > xGainTarget(
96 createGroupShape( m_xLogicTarget, ObjectIdentifier::createClassifiedIdentifier(
97 OBJECTTYPE_DATA_STOCK_GAIN, OUString() )));
98 uno::Reference< drawing::XShapes > xTextTarget(
99 m_pShapeFactory->createGroup2D( m_xFinalTarget ));
101 //check necessary here that different Y axis can not be stacked in the same group? ... hm?
103 bool bJapaneseStyle=true;//@todo is this the correct default?
104 bool bShowFirst = true;//is only important if bJapaneseStyle == false
105 tNameSequence aWhiteBox_Names, aBlackBox_Names;
106 tAnySequence aWhiteBox_Values, aBlackBox_Values;
109 if( m_xChartTypeModelProps.is() )
111 m_xChartTypeModelProps->getPropertyValue( "ShowFirst" ) >>= bShowFirst;
113 uno::Reference< beans::XPropertySet > xWhiteDayProps;
114 uno::Reference< beans::XPropertySet > xBlackDayProps;
115 m_xChartTypeModelProps->getPropertyValue( "Japanese" ) >>= bJapaneseStyle;
116 m_xChartTypeModelProps->getPropertyValue( "WhiteDay" ) >>= xWhiteDayProps;
117 m_xChartTypeModelProps->getPropertyValue( "BlackDay" ) >>= xBlackDayProps;
119 tPropertyNameValueMap aWhiteBox_Map;
120 PropertyMapper::getValueMap( aWhiteBox_Map, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xWhiteDayProps );
121 PropertyMapper::getMultiPropertyListsFromValueMap( aWhiteBox_Names, aWhiteBox_Values, aWhiteBox_Map );
123 tPropertyNameValueMap aBlackBox_Map;
124 PropertyMapper::getValueMap( aBlackBox_Map, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xBlackDayProps );
125 PropertyMapper::getMultiPropertyListsFromValueMap( aBlackBox_Names, aBlackBox_Values, aBlackBox_Map );
128 catch( const uno::Exception& )
130 TOOLS_WARN_EXCEPTION("chart2", "" );
133 //(@todo maybe different iteration for breaks in axis ?)
134 sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
135 double fLogicZ = 1.5;//as defined
136 //iterate through all x values per indices
137 for( sal_Int32 nIndex = 0; nIndex < nEndIndex; nIndex++ )
139 for( auto const& rZSlot : m_aZSlots )
141 sal_Int32 nAttachedAxisIndex = 0;
142 BarPositionHelper* pPosHelper = m_pMainPosHelper.get();
143 if( !rZSlot.empty() )
145 nAttachedAxisIndex = rZSlot.front().getAttachedAxisIndexForFirstSeries();
146 //2ND_AXIS_IN_BARS so far one can assume to have the same plotter for each z slot
147 pPosHelper = dynamic_cast<BarPositionHelper*>(&( getPlottingPositionHelper( nAttachedAxisIndex ) ) );
148 if(!pPosHelper)
149 pPosHelper = m_pMainPosHelper.get();
151 PlotterBase::m_pPosHelper = pPosHelper;
153 //update/create information for current group
154 pPosHelper->updateSeriesCount( rZSlot.size() );
155 double fSlotX=0;
156 //iterate through all x slots in this category
157 for( auto const& rXSlot : rZSlot )
159 //iterate through all series in this x slot
160 for( std::unique_ptr<VDataSeries> const & pSeries : rXSlot.m_aSeriesVector )
162 //collect data point information (logic coordinates, style ):
163 double fUnscaledX = pSeries->getXValue( nIndex );
164 if( m_pExplicitCategoriesProvider && m_pExplicitCategoriesProvider->isDateAxis() )
165 fUnscaledX = DateHelper::RasterizeDateValue( fUnscaledX, m_aNullDate, m_nTimeResolution );
166 if(fUnscaledX<pPosHelper->getLogicMinX() || fUnscaledX>pPosHelper->getLogicMaxX())
167 continue;//point not visible
168 double fScaledX = pPosHelper->getScaledSlotPos( fUnscaledX, fSlotX );
170 double fUnscaledY_First = pSeries->getY_First( nIndex );
171 double fUnscaledY_Last = pSeries->getY_Last( nIndex );
172 double fUnscaledY_Min = pSeries->getY_Min( nIndex );
173 double fUnscaledY_Max = pSeries->getY_Max( nIndex );
175 bool bBlack=false;
176 if(fUnscaledY_Last<=fUnscaledY_First)
178 std::swap(fUnscaledY_First,fUnscaledY_Last);
179 bBlack=true;
181 if(fUnscaledY_Max<fUnscaledY_Min)
182 std::swap(fUnscaledY_Min,fUnscaledY_Max);
183 //transformation 3) -> 4)
184 double fHalfScaledWidth = pPosHelper->getScaledSlotWidth()/2.0;
186 double fScaledY_First(fUnscaledY_First);
187 double fScaledY_Last(fUnscaledY_Last);
188 double fScaledY_Min(fUnscaledY_Min);
189 double fScaledY_Max(fUnscaledY_Max);
190 pPosHelper->clipLogicValues( nullptr,&fScaledY_First,nullptr );
191 pPosHelper->clipLogicValues( nullptr,&fScaledY_Last,nullptr );
192 pPosHelper->clipLogicValues( nullptr,&fScaledY_Min,nullptr );
193 pPosHelper->clipLogicValues( nullptr,&fScaledY_Max,nullptr );
194 pPosHelper->doLogicScaling( nullptr,&fScaledY_First,nullptr );
195 pPosHelper->doLogicScaling( nullptr,&fScaledY_Last,nullptr );
196 pPosHelper->doLogicScaling( nullptr,&fScaledY_Min,nullptr );
197 pPosHelper->doLogicScaling( nullptr,&fScaledY_Max,nullptr );
199 drawing::Position3D aPosLeftFirst( pPosHelper->transformScaledLogicToScene( fScaledX-fHalfScaledWidth, fScaledY_First ,0 ,true ) );
200 drawing::Position3D aPosRightLast( pPosHelper->transformScaledLogicToScene( fScaledX+fHalfScaledWidth, fScaledY_Last ,0 ,true ) );
201 drawing::Position3D aPosMiddleFirst( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_First ,0 ,true ) );
202 drawing::Position3D aPosMiddleLast( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_Last ,0 ,true ) );
203 drawing::Position3D aPosMiddleMinimum( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_Min ,0 ,true ) );
204 drawing::Position3D aPosMiddleMaximum( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_Max ,0 ,true ) );
206 uno::Reference< drawing::XShapes > xLossGainTarget( xGainTarget );
207 if(bBlack)
208 xLossGainTarget = xLossTarget;
210 uno::Reference< beans::XPropertySet > xPointProp( pSeries->getPropertiesOfPoint( nIndex ));
211 uno::Reference< drawing::XShapes > xPointGroupShape_Shapes;
213 OUString aPointCID = ObjectIdentifier::createPointCID( pSeries->getPointCID_Stub(), nIndex );
214 uno::Reference< drawing::XShapes > xSeriesGroupShape_Shapes( getSeriesGroupShape(pSeries.get(), xSeriesTarget) );
215 xPointGroupShape_Shapes = createGroupShape(xSeriesGroupShape_Shapes,aPointCID);
218 //create min-max line
219 if( isValidPosition(aPosMiddleMinimum) && isValidPosition(aPosMiddleMaximum) )
221 drawing::PolyPolygonShape3D aPoly;
222 sal_Int32 nLineIndex =0;
223 AddPointToPoly( aPoly, aPosMiddleMinimum, nLineIndex);
224 AddPointToPoly( aPoly, aPosMiddleMaximum, nLineIndex);
226 uno::Reference< drawing::XShape > xShape =
227 m_pShapeFactory->createLine2D( xPointGroupShape_Shapes,
228 PolyToPointSequence(aPoly));
229 setMappedProperties( xShape, xPointProp, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
232 //create first-last shape
233 if(bJapaneseStyle && isValidPosition(aPosLeftFirst) && isValidPosition(aPosRightLast) )
235 drawing::Direction3D aDiff = aPosRightLast-aPosLeftFirst;
236 awt::Size aAWTSize( Direction3DToAWTSize( aDiff ));
237 // workaround for bug in drawing: if height is 0 the box gets infinitely large
238 if( aAWTSize.Height == 0 )
239 aAWTSize.Height = 1;
241 tNameSequence aNames;
242 tAnySequence aValues;
244 uno::Reference< drawing::XShape > xShape =
245 m_pShapeFactory->createRectangle( xLossGainTarget,
246 aAWTSize, Position3DToAWTPoint( aPosLeftFirst ),
247 aNames, aValues);
249 uno::Reference< beans::XPropertySet > xProp( xShape, uno::UNO_QUERY );
250 if(xProp.is())
252 if(bBlack)
253 PropertyMapper::setMultiProperties( aBlackBox_Names, aBlackBox_Values, xProp );
254 else
255 PropertyMapper::setMultiProperties( aWhiteBox_Names, aWhiteBox_Values, xProp );
258 else
260 drawing::PolyPolygonShape3D aPoly;
262 sal_Int32 nLineIndex = 0;
263 if( bShowFirst && pPosHelper->isLogicVisible( fUnscaledX, fUnscaledY_First ,fLogicZ )
264 && isValidPosition(aPosLeftFirst) && isValidPosition(aPosMiddleFirst) )
266 AddPointToPoly( aPoly, aPosLeftFirst, nLineIndex );
267 AddPointToPoly( aPoly, aPosMiddleFirst, nLineIndex++ );
269 if( pPosHelper->isLogicVisible( fUnscaledX, fUnscaledY_Last ,fLogicZ )
270 && isValidPosition(aPosMiddleLast) && isValidPosition(aPosRightLast) )
272 AddPointToPoly( aPoly, aPosMiddleLast, nLineIndex );
273 AddPointToPoly( aPoly, aPosRightLast, nLineIndex );
276 if( aPoly.SequenceX.hasElements() )
278 uno::Reference< drawing::XShape > xShape =
279 m_pShapeFactory->createLine2D( xPointGroupShape_Shapes,
280 PolyToPointSequence(aPoly) );
281 uno::Reference< beans::XPropertySet > xProp( xShape, uno::UNO_QUERY );
282 if(xProp.is())
284 setMappedProperties( xShape, xPointProp, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
289 //create data point label
290 if( pSeries->getDataPointLabelIfLabel(nIndex) )
292 if(isValidPosition(aPosMiddleFirst))
293 createDataLabel( xTextTarget, *pSeries, nIndex
294 , fUnscaledY_First, 1.0, Position3DToAWTPoint(aPosMiddleFirst), LABEL_ALIGN_LEFT_BOTTOM );
295 if(isValidPosition(aPosMiddleLast))
296 createDataLabel( xTextTarget, *pSeries, nIndex
297 , fUnscaledY_Last, 1.0, Position3DToAWTPoint(aPosMiddleLast), LABEL_ALIGN_RIGHT_TOP );
298 if(isValidPosition(aPosMiddleMinimum))
299 createDataLabel( xTextTarget, *pSeries, nIndex
300 , fUnscaledY_Min, 1.0, Position3DToAWTPoint(aPosMiddleMinimum), LABEL_ALIGN_BOTTOM );
301 if(isValidPosition(aPosMiddleMaximum))
302 createDataLabel( xTextTarget, *pSeries, nIndex
303 , fUnscaledY_Max, 1.0, Position3DToAWTPoint(aPosMiddleMaximum), LABEL_ALIGN_TOP );
305 }//next series in x slot (next y slot)
306 fSlotX+=1.0;
307 }//next x slot
308 }//next z slot
309 }//next category
310 /* @todo remove series shapes if empty
311 //remove and delete point-group-shape if empty
312 if(!xSeriesGroupShape_Shapes->getCount())
314 pSeries->m_xShape.set(NULL);
315 m_xLogicTarget->remove(xSeriesGroupShape_Shape);
319 //remove and delete series-group-shape if empty
321 //... todo
324 } //namespace chart
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */