Line data Source code
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 "ShapeFactory.hxx"
22 : #include "CommonConverters.hxx"
23 : #include "ObjectIdentifier.hxx"
24 : #include "LabelPositionHelper.hxx"
25 : #include "BarPositionHelper.hxx"
26 : #include "macros.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>
33 :
34 : namespace chart
35 : {
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;
40 :
41 6 : CandleStickChart::CandleStickChart( const uno::Reference<XChartType>& xChartTypeModel
42 : , sal_Int32 nDimensionCount )
43 : : VSeriesPlotter( xChartTypeModel, nDimensionCount )
44 6 : , m_pMainPosHelper( new BarPositionHelper() )
45 : {
46 6 : PlotterBase::m_pPosHelper = m_pMainPosHelper;
47 6 : VSeriesPlotter::m_pMainPosHelper = m_pMainPosHelper;
48 6 : }
49 :
50 18 : CandleStickChart::~CandleStickChart()
51 : {
52 6 : delete m_pMainPosHelper;
53 12 : }
54 :
55 : // MinimumAndMaximumSupplier
56 :
57 72 : bool CandleStickChart::isSeparateStackingForDifferentSigns( sal_Int32 /* nDimensionIndex */ )
58 : {
59 72 : return false;
60 : }
61 :
62 8 : LegendSymbolStyle CandleStickChart::getLegendSymbolStyle()
63 : {
64 8 : return LegendSymbolStyle_LINE;
65 : }
66 :
67 6 : drawing::Direction3D CandleStickChart::getPreferredDiagramAspectRatio() const
68 : {
69 6 : return drawing::Direction3D(-1,-1,-1);
70 : }
71 :
72 14 : void CandleStickChart::addSeries( VDataSeries* pSeries, sal_Int32 /* zSlot */, sal_Int32 xSlot, sal_Int32 ySlot )
73 : {
74 : //ignore y stacking for candle stick chart
75 14 : VSeriesPlotter::addSeries( pSeries, 0, xSlot, ySlot );
76 14 : }
77 :
78 6 : void CandleStickChart::createShapes()
79 : {
80 6 : if( m_aZSlots.begin() == m_aZSlots.end() ) //no series
81 0 : return;
82 :
83 6 : if( m_nDimension!=2 )
84 0 : return;
85 :
86 : OSL_ENSURE(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is(),"CandleStickChart is not proper initialized");
87 6 : if(!(m_pShapeFactory&&m_xLogicTarget.is()&&m_xFinalTarget.is()))
88 0 : return;
89 :
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)
93 :
94 : uno::Reference< drawing::XShapes > xSeriesTarget(
95 6 : createGroupShape( m_xLogicTarget,OUString() ));
96 : uno::Reference< drawing::XShapes > xLossTarget(
97 : createGroupShape( m_xLogicTarget, ObjectIdentifier::createClassifiedIdentifier(
98 12 : OBJECTTYPE_DATA_STOCK_LOSS, OUString() )));
99 : uno::Reference< drawing::XShapes > xGainTarget(
100 : createGroupShape( m_xLogicTarget, ObjectIdentifier::createClassifiedIdentifier(
101 12 : OBJECTTYPE_DATA_STOCK_GAIN, OUString() )));
102 : uno::Reference< drawing::XShapes > xTextTarget(
103 12 : m_pShapeFactory->createGroup2D( m_xFinalTarget,OUString() ));
104 :
105 : //check necessary here that different Y axis can not be stacked in the same group? ... hm?
106 :
107 6 : bool bJapaneseStyle=true;//@todo is this the correct default?
108 6 : bool bShowFirst = true;//is only important if bJapaneseStyle == false
109 12 : tNameSequence aWhiteBox_Names, aBlackBox_Names;
110 12 : tAnySequence aWhiteBox_Values, aBlackBox_Values;
111 : try
112 : {
113 6 : if( m_xChartTypeModelProps.is() )
114 : {
115 6 : m_xChartTypeModelProps->getPropertyValue( "ShowFirst" ) >>= bShowFirst;
116 :
117 6 : uno::Reference< beans::XPropertySet > xWhiteDayProps(0);
118 12 : uno::Reference< beans::XPropertySet > xBlackDayProps(0);
119 6 : m_xChartTypeModelProps->getPropertyValue( "Japanese" ) >>= bJapaneseStyle;
120 6 : m_xChartTypeModelProps->getPropertyValue( "WhiteDay" ) >>= xWhiteDayProps;
121 6 : m_xChartTypeModelProps->getPropertyValue( "BlackDay" ) >>= xBlackDayProps;
122 :
123 12 : tPropertyNameValueMap aWhiteBox_Map;
124 6 : PropertyMapper::getValueMap( aWhiteBox_Map, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xWhiteDayProps );
125 6 : PropertyMapper::getMultiPropertyListsFromValueMap( aWhiteBox_Names, aWhiteBox_Values, aWhiteBox_Map );
126 :
127 12 : tPropertyNameValueMap aBlackBox_Map;
128 6 : PropertyMapper::getValueMap( aBlackBox_Map, PropertyMapper::getPropertyNameMapForFillAndLineProperties(), xBlackDayProps );
129 12 : PropertyMapper::getMultiPropertyListsFromValueMap( aBlackBox_Names, aBlackBox_Values, aBlackBox_Map );
130 : }
131 : }
132 0 : catch( const uno::Exception& e )
133 : {
134 : ASSERT_EXCEPTION( e );
135 : }
136 :
137 : //(@todo maybe different iteration for breaks in axis ?)
138 6 : sal_Int32 nStartIndex = 0;
139 6 : sal_Int32 nEndIndex = VSeriesPlotter::getPointCount();
140 6 : double fLogicZ = 1.5;//as defined
141 : //iterate through all x values per indices
142 32 : for( sal_Int32 nIndex = nStartIndex; nIndex < nEndIndex; nIndex++ )
143 : {
144 26 : ::std::vector< ::std::vector< VDataSeriesGroup > >::iterator aZSlotIter = m_aZSlots.begin();
145 26 : const ::std::vector< ::std::vector< VDataSeriesGroup > >::const_iterator aZSlotEnd = m_aZSlots.end();
146 52 : for( sal_Int32 nZ=0; aZSlotIter != aZSlotEnd; ++aZSlotIter, nZ++ )
147 : {
148 26 : ::std::vector< VDataSeriesGroup >::iterator aXSlotIter = aZSlotIter->begin();
149 26 : const ::std::vector< VDataSeriesGroup >::const_iterator aXSlotEnd = aZSlotIter->end();
150 :
151 26 : sal_Int32 nAttachedAxisIndex = 0;
152 26 : BarPositionHelper* pPosHelper = m_pMainPosHelper;
153 26 : if( aXSlotIter != aXSlotEnd )
154 : {
155 26 : nAttachedAxisIndex = aXSlotIter->getAttachedAxisIndexForFirstSeries();
156 : //2ND_AXIS_IN_BARS so far one can assume to have the same plotter for each z slot
157 26 : pPosHelper = dynamic_cast<BarPositionHelper*>(&( this->getPlottingPositionHelper( nAttachedAxisIndex ) ) );
158 26 : if(!pPosHelper)
159 0 : pPosHelper = m_pMainPosHelper;
160 : }
161 26 : PlotterBase::m_pPosHelper = pPosHelper;
162 :
163 : //update/create information for current group
164 26 : pPosHelper->updateSeriesCount( aZSlotIter->size() );
165 : //iterate through all x slots in this category
166 76 : for( double fSlotX=0; aXSlotIter != aXSlotEnd; ++aXSlotIter, fSlotX+=1.0 )
167 : {
168 50 : ::std::vector< VDataSeries* >* pSeriesList = &(aXSlotIter->m_aSeriesVector);
169 :
170 50 : ::std::vector< VDataSeries* >::const_iterator aSeriesIter = pSeriesList->begin();
171 50 : const ::std::vector< VDataSeries* >::const_iterator aSeriesEnd = pSeriesList->end();
172 : //iterate through all series in this x slot
173 100 : for( ; aSeriesIter != aSeriesEnd; ++aSeriesIter )
174 : {
175 : //collect data point information (logic coordinates, style ):
176 50 : double fUnscaledX = (*aSeriesIter)->getXValue( nIndex );
177 50 : if( m_pExplicitCategoriesProvider && m_pExplicitCategoriesProvider->isDateAxis() )
178 0 : fUnscaledX = DateHelper::RasterizeDateValue( fUnscaledX, m_aNullDate, m_nTimeResolution );
179 50 : if(fUnscaledX<pPosHelper->getLogicMinX() || fUnscaledX>pPosHelper->getLogicMaxX())
180 0 : continue;//point not visible
181 50 : double fScaledX = pPosHelper->getScaledSlotPos( fUnscaledX, fSlotX );
182 :
183 50 : double fUnscaledY_First = (*aSeriesIter)->getY_First( nIndex );
184 50 : double fUnscaledY_Last = (*aSeriesIter)->getY_Last( nIndex );
185 50 : double fUnscaledY_Min = (*aSeriesIter)->getY_Min( nIndex );
186 50 : double fUnscaledY_Max = (*aSeriesIter)->getY_Max( nIndex );
187 :
188 50 : bool bBlack=false;
189 50 : if(fUnscaledY_Last<=fUnscaledY_First)
190 : {
191 8 : std::swap(fUnscaledY_First,fUnscaledY_Last);
192 8 : bBlack=true;
193 : }
194 50 : if(fUnscaledY_Max<fUnscaledY_Min)
195 20 : std::swap(fUnscaledY_Min,fUnscaledY_Max);
196 : //transformation 3) -> 4)
197 50 : double fHalfScaledWidth = pPosHelper->getScaledSlotWidth()/2.0;
198 :
199 50 : double fScaledY_First(fUnscaledY_First);
200 50 : double fScaledY_Last(fUnscaledY_Last);
201 50 : double fScaledY_Min(fUnscaledY_Min);
202 50 : double fScaledY_Max(fUnscaledY_Max);
203 50 : pPosHelper->clipLogicValues( 0,&fScaledY_First,0 );
204 50 : pPosHelper->clipLogicValues( 0,&fScaledY_Last,0 );
205 50 : pPosHelper->clipLogicValues( 0,&fScaledY_Min,0 );
206 50 : pPosHelper->clipLogicValues( 0,&fScaledY_Max,0 );
207 50 : pPosHelper->doLogicScaling( 0,&fScaledY_First,0 );
208 50 : pPosHelper->doLogicScaling( 0,&fScaledY_Last,0 );
209 50 : pPosHelper->doLogicScaling( 0,&fScaledY_Min,0 );
210 50 : pPosHelper->doLogicScaling( 0,&fScaledY_Max,0 );
211 :
212 50 : drawing::Position3D aPosLeftFirst( pPosHelper->transformScaledLogicToScene( fScaledX-fHalfScaledWidth, fScaledY_First ,0 ,true ) );
213 50 : drawing::Position3D aPosRightLast( pPosHelper->transformScaledLogicToScene( fScaledX+fHalfScaledWidth, fScaledY_Last ,0 ,true ) );
214 50 : drawing::Position3D aPosMiddleFirst( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_First ,0 ,true ) );
215 50 : drawing::Position3D aPosMiddleLast( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_Last ,0 ,true ) );
216 50 : drawing::Position3D aPosMiddleMinimum( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_Min ,0 ,true ) );
217 50 : drawing::Position3D aPosMiddleMaximum( pPosHelper->transformScaledLogicToScene( fScaledX, fScaledY_Max ,0 ,true ) );
218 :
219 50 : uno::Reference< drawing::XShapes > xLossGainTarget( xGainTarget );
220 50 : if(bBlack)
221 8 : xLossGainTarget = xLossTarget;
222 :
223 100 : uno::Reference< beans::XPropertySet > xPointProp( (*aSeriesIter)->getPropertiesOfPoint( nIndex ));
224 100 : uno::Reference< drawing::XShapes > xPointGroupShape_Shapes(0);
225 : {
226 50 : OUString aPointCID = ObjectIdentifier::createPointCID( (*aSeriesIter)->getPointCID_Stub(), nIndex );
227 100 : uno::Reference< drawing::XShapes > xSeriesGroupShape_Shapes( getSeriesGroupShape(*aSeriesIter, xSeriesTarget) );
228 100 : xPointGroupShape_Shapes = createGroupShape(xSeriesGroupShape_Shapes,aPointCID);
229 : }
230 :
231 : //create min-max line
232 50 : if( isValidPosition(aPosMiddleMinimum) && isValidPosition(aPosMiddleMaximum) )
233 : {
234 44 : drawing::PolyPolygonShape3D aPoly;
235 44 : sal_Int32 nLineIndex =0;
236 44 : AddPointToPoly( aPoly, aPosMiddleMinimum, nLineIndex);
237 44 : AddPointToPoly( aPoly, aPosMiddleMaximum, nLineIndex);
238 :
239 : uno::Reference< drawing::XShape > xShape =
240 : m_pShapeFactory->createLine2D( xPointGroupShape_Shapes,
241 88 : PolyToPointSequence(aPoly), NULL);
242 88 : setMappedProperties( xShape, xPointProp, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
243 : }
244 :
245 : //create first-last shape
246 50 : if(bJapaneseStyle && isValidPosition(aPosLeftFirst) && isValidPosition(aPosRightLast) )
247 : {
248 20 : drawing::Direction3D aDiff = aPosRightLast-aPosLeftFirst;
249 20 : awt::Size aAWTSize( Direction3DToAWTSize( aDiff ));
250 : // workaround for bug in drawing: if height is 0 the box gets infinitely large
251 20 : if( aAWTSize.Height == 0 )
252 0 : aAWTSize.Height = 1;
253 :
254 20 : tNameSequence aNames;
255 40 : tAnySequence aValues;
256 :
257 : uno::Reference< drawing::XShape > xShape =
258 : m_pShapeFactory->createRectangle( xLossGainTarget,
259 : aAWTSize, Position3DToAWTPoint( aPosLeftFirst ),
260 40 : aNames, aValues);
261 :
262 40 : uno::Reference< beans::XPropertySet > xProp( xShape, uno::UNO_QUERY );
263 20 : if(xProp.is())
264 : {
265 20 : if(bBlack)
266 8 : PropertyMapper::setMultiProperties( aBlackBox_Names, aBlackBox_Values, xProp );
267 : else
268 12 : PropertyMapper::setMultiProperties( aWhiteBox_Names, aWhiteBox_Values, xProp );
269 20 : }
270 : }
271 : else
272 : {
273 30 : drawing::PolyPolygonShape3D aPoly;
274 :
275 30 : sal_Int32 nLineIndex = 0;
276 30 : if( bShowFirst && pPosHelper->isLogicVisible( fUnscaledX, fUnscaledY_First ,fLogicZ )
277 30 : && isValidPosition(aPosLeftFirst) && isValidPosition(aPosMiddleFirst) )
278 : {
279 0 : AddPointToPoly( aPoly, aPosLeftFirst, nLineIndex );
280 0 : AddPointToPoly( aPoly, aPosMiddleFirst, nLineIndex++ );
281 : }
282 60 : if( pPosHelper->isLogicVisible( fUnscaledX, fUnscaledY_Last ,fLogicZ )
283 30 : && isValidPosition(aPosMiddleLast) && isValidPosition(aPosRightLast) )
284 : {
285 24 : AddPointToPoly( aPoly, aPosMiddleLast, nLineIndex );
286 24 : AddPointToPoly( aPoly, aPosRightLast, nLineIndex );
287 : }
288 :
289 30 : if( aPoly.SequenceX.getLength() )
290 : {
291 : uno::Reference< drawing::XShape > xShape =
292 : m_pShapeFactory->createLine2D( xPointGroupShape_Shapes,
293 24 : PolyToPointSequence(aPoly), NULL );
294 48 : uno::Reference< beans::XPropertySet > xProp( xShape, uno::UNO_QUERY );
295 24 : if(xProp.is())
296 : {
297 24 : setMappedProperties( xShape, xPointProp, PropertyMapper::getPropertyNameMapForLineSeriesProperties() );
298 24 : }
299 30 : }
300 : }
301 :
302 : //create data point label
303 50 : if( (**aSeriesIter).getDataPointLabelIfLabel(nIndex) )
304 : {
305 0 : if(isValidPosition(aPosMiddleFirst))
306 0 : createDataLabel( xTextTarget, **aSeriesIter, nIndex
307 0 : , fUnscaledY_First, 1.0, Position3DToAWTPoint(aPosMiddleFirst), LABEL_ALIGN_LEFT_BOTTOM );
308 0 : if(isValidPosition(aPosMiddleLast))
309 0 : createDataLabel( xTextTarget, **aSeriesIter, nIndex
310 0 : , fUnscaledY_Last, 1.0, Position3DToAWTPoint(aPosMiddleLast), LABEL_ALIGN_RIGHT_TOP );
311 0 : if(isValidPosition(aPosMiddleMinimum))
312 0 : createDataLabel( xTextTarget, **aSeriesIter, nIndex
313 0 : , fUnscaledY_Min, 1.0, Position3DToAWTPoint(aPosMiddleMinimum), LABEL_ALIGN_BOTTOM );
314 0 : if(isValidPosition(aPosMiddleMaximum))
315 0 : createDataLabel( xTextTarget, **aSeriesIter, nIndex
316 0 : , fUnscaledY_Max, 1.0, Position3DToAWTPoint(aPosMiddleMaximum), LABEL_ALIGN_TOP );
317 : }
318 50 : }//next series in x slot (next y slot)
319 : }//next x slot
320 : }//next z slot
321 6 : }//next category
322 : /* @todo remove series shapes if empty
323 : //remove and delete point-group-shape if empty
324 : if(!xSeriesGroupShape_Shapes->getCount())
325 : {
326 : (*aSeriesIter)->m_xShape.set(NULL);
327 : m_xLogicTarget->remove(xSeriesGroupShape_Shape);
328 : }
329 : */
330 :
331 : //remove and delete series-group-shape if empty
332 :
333 : //... todo
334 : }
335 :
336 : } //namespace chart
337 :
338 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|