Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / view / inc / VSeriesPlotter.hxx
blob7b799eb9c4bfabb0ef8d5c7c931d4256a96fb5b8
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 .
19 #ifndef INCLUDED_CHART2_SOURCE_VIEW_INC_VSERIESPLOTTER_HXX
20 #define INCLUDED_CHART2_SOURCE_VIEW_INC_VSERIESPLOTTER_HXX
22 #include <memory>
23 #include "PlotterBase.hxx"
24 #include "VDataSeries.hxx"
25 #include "LabelAlignment.hxx"
26 #include "MinimumAndMaximumSupplier.hxx"
27 #include "LegendEntryProvider.hxx"
28 #include <com/sun/star/drawing/Direction3D.hpp>
30 namespace com { namespace sun { namespace star { namespace awt { struct Point; } } } }
31 namespace com { namespace sun { namespace star { namespace chart2 { class XChartType; } } } }
34 namespace chart { class ExplicitCategoriesProvider; }
35 namespace chart { struct ExplicitScaleData; }
36 namespace chart { class ChartModel; }
38 namespace com { namespace sun { namespace star {
39 namespace util {
40 class XNumberFormatsSupplier;
42 namespace chart2 {
43 class XColorScheme;
44 class XRegressionCurveCalculator;
46 }}}
48 namespace chart {
50 class NumberFormatterWrapper;
52 class AxesNumberFormats
54 public:
55 AxesNumberFormats() {};
57 void setFormat( sal_Int32 nFormatKey, sal_Int32 nDimIndex, sal_Int32 nAxisIndex )
59 m_aNumberFormatMap[tFullAxisIndex(nDimIndex,nAxisIndex)] = nFormatKey;
61 bool hasFormat( sal_Int32 nDimIndex, sal_Int32 nAxisIndex ) const
63 return (m_aNumberFormatMap.find(tFullAxisIndex(nDimIndex,nAxisIndex)) !=m_aNumberFormatMap.end());
65 sal_Int32 getFormat( sal_Int32 nDimIndex, sal_Int32 nAxisIndex ) const
67 tNumberFormatMap::const_iterator aIt = m_aNumberFormatMap.find(tFullAxisIndex(nDimIndex,nAxisIndex));
68 if( aIt !=m_aNumberFormatMap.end() )
69 return aIt->second;
70 return 0;
73 private:
74 typedef std::pair< sal_Int32, sal_Int32 > tFullAxisIndex;
75 typedef std::map< tFullAxisIndex, sal_Int32 > tNumberFormatMap;
76 tNumberFormatMap m_aNumberFormatMap;
79 /**
80 * A list of series that have the same CoordinateSystem. They are used to be
81 * plotted maybe in a stacked manner by a plotter.
83 class VDataSeriesGroup final
85 public:
86 VDataSeriesGroup() = delete;
87 VDataSeriesGroup( std::unique_ptr<VDataSeries> pSeries );
88 VDataSeriesGroup(VDataSeriesGroup&&) noexcept;
89 ~VDataSeriesGroup();
91 void addSeries( std::unique_ptr<VDataSeries> pSeries );//takes ownership of pSeries
92 sal_Int32 getSeriesCount() const;
93 void deleteSeries();
95 sal_Int32 getPointCount() const;
96 sal_Int32 getAttachedAxisIndexForFirstSeries() const;
98 void getMinimumAndMaximiumX( double& rfMinimum, double& rfMaximum ) const;
99 void getMinimumAndMaximiumYInContinuousXRange( double& rfMinY, double& rfMaxY, double fMinX, double fMaxX, sal_Int32 nAxisIndex ) const;
101 void calculateYMinAndMaxForCategory( sal_Int32 nCategoryIndex
102 , bool bSeparateStackingForDifferentSigns
103 , double& rfMinimumY, double& rfMaximumY, sal_Int32 nAxisIndex );
104 void calculateYMinAndMaxForCategoryRange( sal_Int32 nStartCategoryIndex, sal_Int32 nEndCategoryIndex
105 , bool bSeparateStackingForDifferentSigns
106 , double& rfMinimumY, double& rfMaximumY, sal_Int32 nAxisIndex );
108 std::vector< std::unique_ptr<VDataSeries> > m_aSeriesVector;
110 private:
111 //cached values
112 struct CachedYValues
114 CachedYValues();
116 bool m_bValuesDirty;
117 double m_fMinimumY;
118 double m_fMaximumY;
121 mutable bool m_bMaxPointCountDirty;
122 mutable sal_Int32 m_nMaxPointCount;
123 typedef std::map< sal_Int32, CachedYValues > tCachedYValuesPerAxisIndexMap;
124 mutable std::vector< tCachedYValuesPerAxisIndexMap > m_aListOfCachedYValues;
127 class VSeriesPlotter : public PlotterBase, public MinimumAndMaximumSupplier, public LegendEntryProvider
129 public:
130 VSeriesPlotter() = delete;
132 virtual ~VSeriesPlotter() override;
135 * A new series can be positioned relative to other series in a chart.
136 * This positioning has two dimensions. First a series can be placed
137 * next to each other on the category axis. This position is indicated by xSlot.
138 * Second a series can be stacked on top of another. This position is indicated by ySlot.
139 * The positions are counted from 0 on.
140 * xSlot < 0 : append the series to already existing x series
141 * xSlot > occupied : append the series to already existing x series
143 * If the xSlot is already occupied the given ySlot decides what should happen:
144 * ySlot < -1 : move all existing series in the xSlot to next slot
145 * ySlot == -1 : stack on top at given x position
146 * ySlot == already occupied : insert at given y and x position
147 * ySlot > occupied : stack on top at given x position
149 virtual void addSeries( std::unique_ptr<VDataSeries> pSeries, sal_Int32 zSlot, sal_Int32 xSlot, sal_Int32 ySlot );
151 /** a value <= 0 for a directions means that this direction can be stretched arbitrary
153 virtual css::drawing::Direction3D getPreferredDiagramAspectRatio() const;
155 /** this enables you to handle series on the same x axis with different y axis
156 the property AttachedAxisIndex at a dataseries indicates which value scale is to use
157 (0==AttachedAxisIndex or a not set AttachedAxisIndex property indicates that this series should be scaled at the main y-axis;
158 1==AttachedAxisIndex indicates that the series should be scaled at the first secondary axis if there is any otherwise at the main y axis
159 and so on.
160 The parameter nAxisIndex matches this DataSeries property 'AttachedAxisIndex'.
161 nAxisIndex must be greater than 0. nAxisIndex==1 refers to the first secondary axis.
164 @throws css::uno::RuntimeException
167 void addSecondaryValueScale( const ExplicitScaleData& rScale, sal_Int32 nAxisIndex );
169 // MinimumAndMaximumSupplier
171 virtual double getMinimumX() override;
172 virtual double getMaximumX() override;
174 virtual double getMinimumYInRange( double fMinimumX, double fMaximumX, sal_Int32 nAxisIndex ) override;
175 virtual double getMaximumYInRange( double fMinimumX, double fMaximumX, sal_Int32 nAxisIndex ) override;
177 virtual double getMinimumZ() override;
178 virtual double getMaximumZ() override;
180 virtual bool isExpandBorderToIncrementRhythm( sal_Int32 nDimensionIndex ) override;
181 virtual bool isExpandIfValuesCloseToBorder( sal_Int32 nDimensionIndex ) override;
182 virtual bool isExpandWideValuesToZero( sal_Int32 nDimensionIndex ) override;
183 virtual bool isExpandNarrowValuesTowardZero( sal_Int32 nDimensionIndex ) override;
184 virtual bool isSeparateStackingForDifferentSigns( sal_Int32 nDimensionIndex ) override;
186 virtual long calculateTimeResolutionOnXAxis() override;
187 virtual void setTimeResolutionOnXAxis( long nTimeResolution, const Date& rNullDate ) override;
189 void getMinimumAndMaximiumX( double& rfMinimum, double& rfMaximum ) const;
190 void getMinimumAndMaximiumYInContinuousXRange( double& rfMinY, double& rfMaxY, double fMinX, double fMaxX, sal_Int32 nAxisIndex ) const;
193 // Methods for handling legends and legend entries.
195 virtual std::vector< ViewLegendEntry > createLegendEntries(
196 const css::awt::Size& rEntryKeyAspectRatio,
197 css::chart::ChartLegendExpansion eLegendExpansion,
198 const css::uno::Reference< css::beans::XPropertySet >& xTextProperties,
199 const css::uno::Reference< css::drawing::XShapes >& xTarget,
200 const css::uno::Reference< css::lang::XMultiServiceFactory >& xShapeFactory,
201 const css::uno::Reference< css::uno::XComponentContext >& xContext,
202 ChartModel& rModel
203 ) override;
205 virtual LegendSymbolStyle getLegendSymbolStyle();
206 virtual css::awt::Size getPreferredLegendKeyAspectRatio() override;
208 virtual css::uno::Any getExplicitSymbol( const VDataSeries& rSeries, sal_Int32 nPointIndex/*-1 for series symbol*/ );
210 css::uno::Reference< css::drawing::XShape > createLegendSymbolForSeries(
211 const css::awt::Size& rEntryKeyAspectRatio
212 , const VDataSeries& rSeries
213 , const css::uno::Reference< css::drawing::XShapes >& xTarget
214 , const css::uno::Reference< css::lang::XMultiServiceFactory >& xShapeFactory );
216 css::uno::Reference< css::drawing::XShape > createLegendSymbolForPoint(
217 const css::awt::Size& rEntryKeyAspectRatio
218 , const VDataSeries& rSeries
219 , sal_Int32 nPointIndex
220 , const css::uno::Reference< css::drawing::XShapes >& xTarget
221 , const css::uno::Reference< css::lang::XMultiServiceFactory >& xShapeFactory );
223 std::vector< ViewLegendEntry > createLegendEntriesForSeries(
224 const css::awt::Size& rEntryKeyAspectRatio,
225 const VDataSeries& rSeries,
226 const css::uno::Reference< css::beans::XPropertySet >& xTextProperties,
227 const css::uno::Reference< css::drawing::XShapes >& xTarget,
228 const css::uno::Reference< css::lang::XMultiServiceFactory >& xShapeFactory,
229 const css::uno::Reference< css::uno::XComponentContext >& xContext
232 std::vector< VDataSeries* > getAllSeries();
234 // This method creates a series plotter of the requested type; e.g. : return new PieChart...
235 static VSeriesPlotter* createSeriesPlotter( const css::uno::Reference< css::chart2::XChartType >& xChartTypeModel
236 , sal_Int32 nDimensionCount
237 , bool bExcludingPositioning /*for pie and donut charts labels and exploded segments are excluded from the given size*/);
239 sal_Int32 getPointCount() const;
241 // Methods for number formats and color schemes
243 void setNumberFormatsSupplier( const css::uno::Reference< css::util::XNumberFormatsSupplier > & xNumFmtSupplier );
244 void setAxesNumberFormats( const AxesNumberFormats& rAxesNumberFormats ) { m_aAxesNumberFormats = rAxesNumberFormats; };
246 void setColorScheme( const css::uno::Reference< css::chart2::XColorScheme >& xColorScheme );
248 void setExplicitCategoriesProvider( ExplicitCategoriesProvider* pExplicitCategoriesProvider );
250 //get series names for the z axis labels
251 css::uno::Sequence< OUString > getSeriesNames() const;
253 void setPageReferenceSize( const css::awt::Size & rPageRefSize );
254 //better performance for big data
255 void setCoordinateSystemResolution( const css::uno::Sequence< sal_Int32 >& rCoordinateSystemResolution );
256 bool PointsWereSkipped() const { return m_bPointsWereSkipped;}
258 //return the depth for a logic 1
259 double getTransformedDepth() const;
261 void releaseShapes();
263 virtual void rearrangeLabelToAvoidOverlapIfRequested( const css::awt::Size& rPageSize );
265 bool WantToPlotInFrontOfAxisLine();
266 virtual bool shouldSnapRectToUsedArea();
268 protected:
270 VSeriesPlotter( const css::uno::Reference< css::chart2::XChartType >& xChartTypeModel
271 , sal_Int32 nDimensionCount
272 , bool bCategoryXAxis=true );
274 // Methods for group shapes.
276 css::uno::Reference< css::drawing::XShapes >
277 getSeriesGroupShape( VDataSeries* pDataSeries
278 , const css:: uno::Reference< css::drawing::XShapes >& xTarget );
280 //the following group shapes will be created as children of SeriesGroupShape on demand
281 //they can be used to assure that some parts of a series shape are always in front of others (e.g. symbols in front of lines)
282 //parameter xTarget will be used as parent for the series group shape
283 css::uno::Reference< css::drawing::XShapes >
284 getSeriesGroupShapeFrontChild( VDataSeries* pDataSeries
285 , const css:: uno::Reference< css::drawing::XShapes >& xTarget );
286 css::uno::Reference< css::drawing::XShapes >
287 getSeriesGroupShapeBackChild( VDataSeries* pDataSeries
288 , const css:: uno::Reference< css::drawing::XShapes >& xTarget );
290 /// This method creates a 2D group shape for containing all text shapes
291 /// needed for this series; the group is added to the text target;
292 css::uno::Reference< css::drawing::XShapes >
293 getLabelsGroupShape( VDataSeries& rDataSeries
294 , const css:: uno::Reference< css::drawing::XShapes >& xTarget );
296 css::uno::Reference< css::drawing::XShapes >
297 getErrorBarsGroupShape( VDataSeries& rDataSeries
298 , const css:: uno::Reference< css::drawing::XShapes >& xTarget, bool bYError );
300 /** This method creates a text shape for a label related to a data point
301 * and append it to the root text shape group (xTarget).
303 * @param xTarget
304 * the main root text shape group.
305 * @param rDataSeries
306 * the data series, the data point belongs to.
307 * @param nPointIndex
308 * the index of the data point the label is related to.
309 * @param fValue
310 * the value of the data point.
311 * @param fSumValue
312 * the sum of all data point values in the data series.
313 * @param rScreenPosition2D
314 * the anchor point position for the label.
315 * @param eAlignment
316 * the required alignment of the label.
317 * @param offset
318 * an optional offset depending on the label alignment.
319 * @param nTextWidth
320 * the maximum width of a text label (used for text wrapping).
322 * @return
323 * a reference to the created text shape.
325 css::uno::Reference< css::drawing::XShape >
326 createDataLabel( const css::uno::Reference< css::drawing::XShapes >& xTarget
327 , VDataSeries& rDataSeries
328 , sal_Int32 nPointIndex
329 , double fValue
330 , double fSumValue
331 , const css::awt::Point& rScreenPosition2D
332 , LabelAlignment eAlignment
333 , sal_Int32 nOffset=0
334 , sal_Int32 nTextWidth = 0 );
336 /// This method returns a text string representation of the passed numeric
337 /// value by exploiting a NumberFormatterWrapper object.
338 OUString getLabelTextForValue( VDataSeries const & rDataSeries
339 , sal_Int32 nPointIndex
340 , double fValue
341 , bool bAsPercentage );
343 /** creates two T-shaped error bars in both directions (up/down or
344 left/right depending on the bVertical parameter)
346 @param rPos
347 logic coordinates
349 @param xErrorBarProperties
350 the XPropertySet returned by the DataPoint-property "ErrorBarX" or
351 "ErrorBarY".
353 @param nIndex
354 the index of the data point in rData for which the calculation is
355 done.
357 @param bVertical
358 for y-error bars this is true, for x-error-bars it is false.
360 void createErrorBar(
361 const css::uno::Reference< css::drawing::XShapes >& xTarget
362 , const css::drawing::Position3D & rPos
363 , const css::uno::Reference< css::beans::XPropertySet > & xErrorBarProperties
364 , const VDataSeries& rVDataSeries
365 , sal_Int32 nIndex
366 , bool bVertical
367 , const double* pfScaledLogicX
370 void createErrorRectangle(
371 const css::drawing::Position3D& rUnscaledLogicPosition
372 , VDataSeries& rVDataSeries
373 , sal_Int32 nIndex
374 , const css::uno::Reference< css::drawing::XShapes >& rTarget
375 , bool bUseXErrorData
376 , bool bUseYErrorData
379 void addErrorBorder(
380 const css::drawing::Position3D& rPos0
381 , const css::drawing::Position3D& rPos1
382 , const css::uno::Reference< css::drawing::XShapes >& rTarget
383 , const css::uno::Reference< css::beans::XPropertySet >& rErrorBorderProp );
385 void createErrorBar_X( const css::drawing::Position3D& rUnscaledLogicPosition
386 , VDataSeries& rVDataSeries, sal_Int32 nPointIndex
387 , const css::uno::Reference< css::drawing::XShapes >& xTarget );
389 void createErrorBar_Y( const css::drawing::Position3D& rUnscaledLogicPosition
390 , VDataSeries& rVDataSeries, sal_Int32 nPointIndex
391 , const css::uno::Reference< css::drawing::XShapes >& xTarget
392 , double const * pfScaledLogicX );
394 void createRegressionCurvesShapes( VDataSeries const & rVDataSeries
395 , const css::uno::Reference< css::drawing::XShapes >& xTarget
396 , const css::uno::Reference< css::drawing::XShapes >& xEquationTarget
397 , bool bMaySkipPointsInRegressionCalculation );
399 void createRegressionCurveEquationShapes( const OUString & rEquationCID
400 , const css::uno::Reference< css::beans::XPropertySet > & xEquationProperties
401 , const css::uno::Reference< css::drawing::XShapes >& xEquationTarget
402 , const css::uno::Reference< css::chart2::XRegressionCurveCalculator > & xRegressionCurveCalculator
403 , css::awt::Point aDefaultPos );
405 static void setMappedProperties(
406 const css::uno::Reference< css::drawing::XShape >& xTarget
407 , const css::uno::Reference< css::beans::XPropertySet >& xSource
408 , const tPropertyNameMap& rMap
409 , tPropertyNameValueMap const * pOverwriteMap=nullptr );
411 virtual PlottingPositionHelper& getPlottingPositionHelper( sal_Int32 nAxisIndex ) const;//nAxisIndex indicates whether the position belongs to the main axis ( nAxisIndex==0 ) or secondary axis ( nAxisIndex==1 )
413 VDataSeries* getFirstSeries() const;
415 OUString getCategoryName( sal_Int32 nPointIndex ) const;
417 protected:
418 PlottingPositionHelper* m_pMainPosHelper;
420 css::uno::Reference< css::chart2::XChartType > m_xChartTypeModel;
421 css::uno::Reference< css::beans::XPropertySet > m_xChartTypeModelProps;
423 std::vector< std::vector< VDataSeriesGroup > > m_aZSlots;
425 bool m_bCategoryXAxis;//true->xvalues are indices (this would not be necessary if series for category chart wouldn't have x-values)
426 long m_nTimeResolution;
427 Date m_aNullDate;
429 std::unique_ptr< NumberFormatterWrapper > m_apNumberFormatterWrapper;
430 AxesNumberFormats m_aAxesNumberFormats;//direct numberformats on axes, if empty ask the data series instead
432 css::uno::Reference< css::chart2::XColorScheme > m_xColorScheme;
434 ExplicitCategoriesProvider* m_pExplicitCategoriesProvider;
436 //better performance for big data
437 css::uno::Sequence< sal_Int32 > m_aCoordinateSystemResolution;
438 bool m_bPointsWereSkipped;
440 private:
441 typedef std::map< sal_Int32 , ExplicitScaleData > tSecondaryValueScales;
442 tSecondaryValueScales m_aSecondaryValueScales;
444 typedef std::map< sal_Int32 , std::unique_ptr<PlottingPositionHelper> > tSecondaryPosHelperMap;
445 mutable tSecondaryPosHelperMap m_aSecondaryPosHelperMap;
446 css::awt::Size m_aPageReferenceSize;
449 } //namespace chart
450 #endif
452 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */