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