LibreOffice Module chart2 (master) 1
CandleStickChart.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "CandleStickChart.hxx"
21#include <ChartType.hxx>
22#include <ShapeFactory.hxx>
23#include <CommonConverters.hxx>
25#include <ObjectIdentifier.hxx>
26#include "BarPositionHelper.hxx"
27#include <DateHelper.hxx>
28#include <com/sun/star/beans/XPropertySet.hpp>
30#include <osl/diagnose.h>
31
32namespace chart
33{
34using namespace ::com::sun::star;
35using namespace ::com::sun::star::chart2;
36
38 , sal_Int32 nDimensionCount )
39 : VSeriesPlotter( xChartTypeModel, nDimensionCount )
40 , m_pMainPosHelper( new BarPositionHelper() )
41{
42 PlotterBase::m_pPosHelper = m_pMainPosHelper.get();
43 VSeriesPlotter::m_pMainPosHelper = m_pMainPosHelper.get();
44}
45
46CandleStickChart::~CandleStickChart()
47{
48}
49
50// MinimumAndMaximumSupplier
51
52bool CandleStickChart::isSeparateStackingForDifferentSigns( sal_Int32 /* nDimensionIndex */ )
53{
54 return false;
55}
56
57LegendSymbolStyle CandleStickChart::getLegendSymbolStyle()
58{
59 return LegendSymbolStyle::Line;
60}
61
62drawing::Direction3D CandleStickChart::getPreferredDiagramAspectRatio() const
63{
64 return drawing::Direction3D(-1,-1,-1);
65}
66
67void CandleStickChart::addSeries( std::unique_ptr<VDataSeries> pSeries, sal_Int32 /* zSlot */, sal_Int32 xSlot, sal_Int32 ySlot )
68{
69 //ignore y stacking for candle stick chart
70 VSeriesPlotter::addSeries( std::move(pSeries), 0, xSlot, ySlot );
71}
72
73void CandleStickChart::createShapes()
74{
75 if( m_aZSlots.empty() ) //no series
76 return;
77
78 if( m_nDimension!=2 )
79 return;
80
81 OSL_ENSURE(m_xLogicTarget.is()&&m_xFinalTarget.is(),"CandleStickChart is not proper initialized");
82 if(!(m_xLogicTarget.is()&&m_xFinalTarget.is()))
83 return;
84
85 //the text labels should be always on top of the other series shapes
86 //therefore create an own group for the texts to move them to front
87 //(because the text group is created after the series group the texts are displayed on top)
88
90 createGroupShape( m_xLogicTarget );
92 createGroupShape( m_xLogicTarget, ObjectIdentifier::createClassifiedIdentifier(
95 createGroupShape( m_xLogicTarget, ObjectIdentifier::createClassifiedIdentifier(
98 ShapeFactory::createGroup2D( m_xFinalTarget );
99
100 //check necessary here that different Y axis can not be stacked in the same group? ... hm?
101
102 bool bJapaneseStyle=true;//@todo is this the correct default?
103 bool bShowFirst = true;//is only important if bJapaneseStyle == false
104 tNameSequence aWhiteBox_Names, aBlackBox_Names;
105 tAnySequence aWhiteBox_Values, aBlackBox_Values;
106 try
107 {
108 if( m_xChartTypeModel.is() )
109 {
110 m_xChartTypeModel->getPropertyValue( "ShowFirst" ) >>= bShowFirst;
111
114 m_xChartTypeModel->getPropertyValue( "Japanese" ) >>= bJapaneseStyle;
115 m_xChartTypeModel->getPropertyValue( "WhiteDay" ) >>= xWhiteDayProps;
116 m_xChartTypeModel->getPropertyValue( "BlackDay" ) >>= xBlackDayProps;
117
118 tPropertyNameValueMap aWhiteBox_Map;
119 PropertyMapper::getValueMap( aWhiteBox_Map, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xWhiteDayProps );
120 PropertyMapper::getMultiPropertyListsFromValueMap( aWhiteBox_Names, aWhiteBox_Values, aWhiteBox_Map );
121
122 tPropertyNameValueMap aBlackBox_Map;
123 PropertyMapper::getValueMap( aBlackBox_Map, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xBlackDayProps );
124 PropertyMapper::getMultiPropertyListsFromValueMap( aBlackBox_Names, aBlackBox_Values, aBlackBox_Map );
125 }
126 }
127 catch( const uno::Exception& )
128 {
129 TOOLS_WARN_EXCEPTION("chart2", "" );
130 }
131
132 //(@todo maybe different iteration for breaks in axis ?)
133 sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
134 double fLogicZ = 1.5;//as defined
135 //iterate through all x values per indices
136 for( sal_Int32 nIndex = 0; nIndex < nEndIndex; nIndex++ )
137 {
138 for( auto const& rZSlot : m_aZSlots )
139 {
140 BarPositionHelper* pPosHelper = m_pMainPosHelper.get();
141 if( !rZSlot.empty() )
142 {
143 sal_Int32 nAttachedAxisIndex = rZSlot.front().getAttachedAxisIndexForFirstSeries();
144 //2ND_AXIS_IN_BARS so far one can assume to have the same plotter for each z slot
145 pPosHelper = dynamic_cast<BarPositionHelper*>(&( getPlottingPositionHelper( nAttachedAxisIndex ) ) );
146 if(!pPosHelper)
147 pPosHelper = m_pMainPosHelper.get();
148 }
149 PlotterBase::m_pPosHelper = pPosHelper;
150
151 //update/create information for current group
152 pPosHelper->updateSeriesCount( rZSlot.size() );
153 double fSlotX=0;
154 //iterate through all x slots in this category
155 for( auto const& rXSlot : rZSlot )
156 {
157 //iterate through all series in this x slot
158 for( std::unique_ptr<VDataSeries> const & pSeries : rXSlot.m_aSeriesVector )
159 {
160 //collect data point information (logic coordinates, style ):
161 double fUnscaledX = pSeries->getXValue( nIndex );
162 if( m_pExplicitCategoriesProvider && m_pExplicitCategoriesProvider->isDateAxis() )
163 fUnscaledX = DateHelper::RasterizeDateValue( fUnscaledX, m_aNullDate, m_nTimeResolution );
164 if(fUnscaledX<pPosHelper->getLogicMinX() || fUnscaledX>pPosHelper->getLogicMaxX())
165 continue;//point not visible
166 double fScaledX = pPosHelper->getScaledSlotPos( fUnscaledX, fSlotX );
167
168 double fUnscaledY_First = pSeries->getY_First( nIndex );
169 double fUnscaledY_Last = pSeries->getY_Last( nIndex );
170 double fUnscaledY_Min = pSeries->getY_Min( nIndex );
171 double fUnscaledY_Max = pSeries->getY_Max( nIndex );
172
173 bool bBlack=false;
174 if(fUnscaledY_Last<=fUnscaledY_First)
175 {
176 std::swap(fUnscaledY_First,fUnscaledY_Last);
177 bBlack=true;
178 }
179 if(fUnscaledY_Max<fUnscaledY_Min)
180 std::swap(fUnscaledY_Min,fUnscaledY_Max);
181 //transformation 3) -> 4)
182 double fHalfScaledWidth = pPosHelper->getScaledSlotWidth()/2.0;
183
184 double fScaledY_First(fUnscaledY_First);
185 double fScaledY_Last(fUnscaledY_Last);
186 double fScaledY_Min(fUnscaledY_Min);
187 double fScaledY_Max(fUnscaledY_Max);
188 pPosHelper->clipLogicValues( nullptr,&fScaledY_First,nullptr );
189 pPosHelper->clipLogicValues( nullptr,&fScaledY_Last,nullptr );
190 pPosHelper->clipLogicValues( nullptr,&fScaledY_Min,nullptr );
191 pPosHelper->clipLogicValues( nullptr,&fScaledY_Max,nullptr );
192 pPosHelper->doLogicScaling( nullptr,&fScaledY_First,nullptr );
193 pPosHelper->doLogicScaling( nullptr,&fScaledY_Last,nullptr );
194 pPosHelper->doLogicScaling( nullptr,&fScaledY_Min,nullptr );
195 pPosHelper->doLogicScaling( nullptr,&fScaledY_Max,nullptr );
196
197 drawing::Position3D aPosLeftFirst( pPosHelper->transformScaledLogicToScene( fScaledX-fHalfScaledWidth, fScaledY_First ,0 ,true ) );
198 drawing::Position3D aPosRightLast( pPosHelper->transformScaledLogicToScene( fScaledX+fHalfScaledWidth, fScaledY_Last ,0 ,true ) );
199 drawing::Position3D aPosMiddleFirst( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_First ,0 ,true ) );
200 drawing::Position3D aPosMiddleLast( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_Last ,0 ,true ) );
201 drawing::Position3D aPosMiddleMinimum( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_Min ,0 ,true ) );
202 drawing::Position3D aPosMiddleMaximum( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_Max ,0 ,true ) );
203
204 rtl::Reference<SvxShapeGroupAnyD> xLossGainTarget( xGainTarget );
205 if(bBlack)
206 xLossGainTarget = xLossTarget;
207
208 uno::Reference< beans::XPropertySet > xPointProp( pSeries->getPropertiesOfPoint( nIndex ));
209 rtl::Reference<SvxShapeGroupAnyD> xPointGroupShape_Shapes;
210 {
211 OUString aPointCID = ObjectIdentifier::createPointCID( pSeries->getPointCID_Stub(), nIndex );
212 rtl::Reference<SvxShapeGroupAnyD> xSeriesGroupShape_Shapes( getSeriesGroupShape(pSeries.get(), xSeriesTarget) );
213 xPointGroupShape_Shapes = createGroupShape(xSeriesGroupShape_Shapes,aPointCID);
214 }
215
216 //create min-max line
217 if( isValidPosition(aPosMiddleMinimum) && isValidPosition(aPosMiddleMaximum) )
218 {
219 std::vector<std::vector<css::drawing::Position3D>> aPoly
220 {
221 { aPosMiddleMinimum, aPosMiddleMaximum }
222 };
223
225 ShapeFactory::createLine2D( xPointGroupShape_Shapes, aPoly);
226 PropertyMapper::setMappedProperties( *xShape, xPointProp, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
227 }
228
229 //create first-last shape
230 if(bJapaneseStyle && isValidPosition(aPosLeftFirst) && isValidPosition(aPosRightLast) )
231 {
232 drawing::Direction3D aDiff = aPosRightLast-aPosLeftFirst;
233 awt::Size aAWTSize( Direction3DToAWTSize( aDiff ));
234 // workaround for bug in drawing: if height is 0 the box gets infinitely large
235 if( aAWTSize.Height == 0 )
236 aAWTSize.Height = 1;
237
238 tNameSequence aNames;
239 tAnySequence aValues;
240
242 ShapeFactory::createRectangle( xLossGainTarget,
243 aAWTSize, Position3DToAWTPoint( aPosLeftFirst ),
244 aNames, aValues);
245
246 if(bBlack)
247 PropertyMapper::setMultiProperties( aBlackBox_Names, aBlackBox_Values, *xShape );
248 else
249 PropertyMapper::setMultiProperties( aWhiteBox_Names, aWhiteBox_Values, *xShape );
250 }
251 else
252 {
253 std::vector<std::vector<css::drawing::Position3D>> aPoly;
254
255 sal_Int32 nLineIndex = 0;
256 if( bShowFirst && pPosHelper->isLogicVisible( fUnscaledX, fUnscaledY_First ,fLogicZ )
257 && isValidPosition(aPosLeftFirst) && isValidPosition(aPosMiddleFirst) )
258 {
259 AddPointToPoly( aPoly, aPosLeftFirst, nLineIndex );
260 AddPointToPoly( aPoly, aPosMiddleFirst, nLineIndex++ );
261 }
262 if( pPosHelper->isLogicVisible( fUnscaledX, fUnscaledY_Last ,fLogicZ )
263 && isValidPosition(aPosMiddleLast) && isValidPosition(aPosRightLast) )
264 {
265 AddPointToPoly( aPoly, aPosMiddleLast, nLineIndex );
266 AddPointToPoly( aPoly, aPosRightLast, nLineIndex );
267 }
268
269 if( !aPoly.empty() )
270 {
272 ShapeFactory::createLine2D( xPointGroupShape_Shapes, aPoly );
273 PropertyMapper::setMappedProperties( *xShape, xPointProp, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
274 }
275 }
276
277 //create data point label
278 if( pSeries->getDataPointLabelIfLabel(nIndex) )
279 {
280 if(isValidPosition(aPosMiddleFirst))
281 createDataLabel( xTextTarget, *pSeries, nIndex
282 , fUnscaledY_First, 1.0, Position3DToAWTPoint(aPosMiddleFirst), LABEL_ALIGN_LEFT_BOTTOM );
283 if(isValidPosition(aPosMiddleLast))
284 createDataLabel( xTextTarget, *pSeries, nIndex
285 , fUnscaledY_Last, 1.0, Position3DToAWTPoint(aPosMiddleLast), LABEL_ALIGN_RIGHT_TOP );
286 if(isValidPosition(aPosMiddleMinimum))
287 createDataLabel( xTextTarget, *pSeries, nIndex
288 , fUnscaledY_Min, 1.0, Position3DToAWTPoint(aPosMiddleMinimum), LABEL_ALIGN_BOTTOM );
289 if(isValidPosition(aPosMiddleMaximum))
290 createDataLabel( xTextTarget, *pSeries, nIndex
291 , fUnscaledY_Max, 1.0, Position3DToAWTPoint(aPosMiddleMaximum), LABEL_ALIGN_TOP );
292 }
293 }//next series in x slot (next y slot)
294 fSlotX+=1.0;
295 }//next x slot
296 }//next z slot
297 }//next category
298 /* @todo remove series shapes if empty
299 //remove and delete point-group-shape if empty
300 if(!xSeriesGroupShape_Shapes->getCount())
301 {
302 pSeries->m_xShape.set(NULL);
303 m_xLogicTarget->remove(xSeriesGroupShape_Shape);
304 }
305 */
306
307 //remove and delete series-group-shape if empty
308
309 //... todo
310}
311
312} //namespace chart
313
314/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual double getScaledSlotPos(double fCategoryX, double fSeriesNumber) const override
void updateSeriesCount(double fSeriesCount)
bool isLogicVisible(double fX, double fY, double fZ) const
void doLogicScaling(double *pX, double *pY, double *pZ) const
virtual css::drawing::Position3D transformScaledLogicToScene(double fX, double fY, double fZ, bool bClip) const
void clipLogicValues(double *pX, double *pY, double *pZ) const
#define TOOLS_WARN_EXCEPTION(area, stream)
float u
sal_Int32 nIndex
OOO_DLLPUBLIC_CHARTTOOLS css::awt::Point Position3DToAWTPoint(const css::drawing::Position3D &rPos)
Position3D --> awt::Point (2D)
@ OBJECTTYPE_DATA_STOCK_LOSS
@ OBJECTTYPE_DATA_STOCK_GAIN
OOO_DLLPUBLIC_CHARTTOOLS void AddPointToPoly(css::drawing::PolyPolygonShape3D &rPoly, const css::drawing::Position3D &rPos, sal_Int32 nSequenceIndex=0)
PolyPolygonShape3D + drawing::Position3D -> PolyPolygonShape3D.
css::uno::Sequence< OUString > tNameSequence
@ LABEL_ALIGN_RIGHT_TOP
@ LABEL_ALIGN_TOP
@ LABEL_ALIGN_LEFT_BOTTOM
@ LABEL_ALIGN_BOTTOM
css::uno::Sequence< css::uno::Any > tAnySequence
std::unordered_map< OUString, css::uno::Any > tPropertyNameValueMap
OOO_DLLPUBLIC_CHARTTOOLS css::awt::Size Direction3DToAWTSize(const css::drawing::Direction3D &rDirection)
Direction3D --> awt::Size (2D)