1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
23 #include <VSeriesPlotter.hxx>
24 #include <PlottingPositionHelper.hxx>
25 #include <basegfx/vector/b2ivector.hxx>
26 #include <com/sun/star/awt/Point.hpp>
27 #include <com/sun/star/chart2/PieChartSubType.hpp>
29 using namespace ::com::sun::star
;
30 using namespace ::com::sun::star::chart2
;
35 class PiePositionHelper
: public PolarPlottingPositionHelper
38 PiePositionHelper( double fAngleDegreeOffset
);
40 bool getInnerAndOuterRadius( double fCategoryX
, double& fLogicInnerRadius
, double& fLogicOuterRadius
, bool bUseRings
, double fMaxOffset
) const;
42 // Determine if the pie wedges are ordered clockwise (returns true) or
43 // counterclockwise (returns false)
44 bool clockwiseWedges() const;
47 //Distance between different category rings, seen relative to width of a ring:
48 double m_fRingDistance
; //>=0 m_fRingDistance=1 --> distance == width
51 enum class SubPieType
{
52 NONE
, // solo pie or donut
53 LEFT
, // left pie in pie-of-pie
54 RIGHT
// right pie in pie-of-pie
58 //=======================
59 // class PieDataSrcBase
60 //=======================
64 PieDataSrcBase() = default;
65 virtual ~PieDataSrcBase() = default;
67 // Number of data points for given pie subtype
68 virtual sal_Int32
getNPoints(const VDataSeries
* pSeries
,
69 enum SubPieType eType
) const = 0;
71 // Get the value for the given pie wedge, for the given subtype
72 virtual double getData(const VDataSeries
* pSeries
, sal_Int32 nPtIdx
,
73 enum SubPieType eType
) const = 0;
75 // Get the properties for the wedge and subtype
76 virtual uno::Reference
< beans::XPropertySet
> getProps(
77 const VDataSeries
* pSeries
, sal_Int32 nPtIdx
,
78 enum SubPieType eType
) const = 0;
81 //=======================
83 //=======================
84 class PieDataSrc
: public PieDataSrcBase
87 sal_Int32
getNPoints(const VDataSeries
* pSeries
,
88 enum SubPieType eType
) const;
90 double getData(const VDataSeries
* pSeries
, sal_Int32 nPtIdx
,
91 [[maybe_unused
]]enum SubPieType eType
) const;
93 virtual uno::Reference
< beans::XPropertySet
> getProps(
94 const VDataSeries
* pSeries
, sal_Int32 nPtIdx
,
95 enum SubPieType eType
) const;
98 //=======================
100 //=======================
101 class OfPieDataSrc
: public PieDataSrcBase
104 OfPieDataSrc(sal_Int32 nSplitPos
):
105 m_nSplitPos(nSplitPos
)
108 // Minimum sensible number of data points
109 static constexpr sal_Int32 minPoints
= 4;
111 sal_Int32
getNPoints(const VDataSeries
* pSeries
,
112 enum SubPieType eType
) const;
114 double getData(const VDataSeries
* pSeries
, sal_Int32 nPtIdx
,
115 enum SubPieType eType
) const;
117 virtual uno::Reference
< beans::XPropertySet
> getProps(
118 const VDataSeries
* pSeries
, sal_Int32 nPtIdx
,
119 enum SubPieType eType
) const;
124 //=======================
126 //=======================
127 class PieChart
: public VSeriesPlotter
134 PieChart( const rtl::Reference
< ::chart::ChartType
>& xChartTypeModel
135 , sal_Int32 nDimensionCount
, bool bExcludingPositioning
);
136 virtual ~PieChart() override
;
138 /** This method creates all shapes needed for representing the pie chart.
140 virtual void createShapes() override
;
141 virtual void rearrangeLabelToAvoidOverlapIfRequested( const css::awt::Size
& rPageSize
) override
;
143 virtual void setScales( std::vector
< ExplicitScaleData
>&& rScales
, bool bSwapXAndYAxis
) override
;
144 virtual void addSeries( std::unique_ptr
<VDataSeries
> pSeries
, sal_Int32 zSlot
, sal_Int32 xSlot
, sal_Int32 ySlot
) override
;
146 virtual css::drawing::Direction3D
getPreferredDiagramAspectRatio() const override
;
147 virtual bool shouldSnapRectToUsedArea() override
;
149 //MinimumAndMaximumSupplier
150 virtual double getMinimumX() override
;
151 virtual double getMaximumX() override
;
152 virtual double getMinimumYInRange( double fMinimumX
, double fMaximumX
, sal_Int32 nAxisIndex
) override
;
153 virtual double getMaximumYInRange( double fMinimumX
, double fMaximumX
, sal_Int32 nAxisIndex
) override
;
155 virtual bool isExpandBorderToIncrementRhythm( sal_Int32 nDimensionIndex
) override
;
156 virtual bool isExpandIfValuesCloseToBorder( sal_Int32 nDimensionIndex
) override
;
157 virtual bool isExpandWideValuesToZero( sal_Int32 nDimensionIndex
) override
;
158 virtual bool isExpandNarrowValuesTowardZero( sal_Int32 nDimensionIndex
) override
;
159 virtual bool isSeparateStackingForDifferentSigns( sal_Int32 nDimensionIndex
) override
;
162 rtl::Reference
<SvxShape
>
164 enum SubPieType eType
,
165 const rtl::Reference
<SvxShapeGroupAnyD
>& xTarget
,
166 const css::uno::Reference
<css::beans::XPropertySet
>& xObjectProperties
,
167 const ShapeParam
& rParam
,
168 const sal_Int32 nPointCount
,
169 const bool bConcentricExplosion
);
171 rtl::Reference
<SvxShape
> createBarDataPoint(
172 const rtl::Reference
<SvxShapeGroupAnyD
>& xTarget
,
173 const uno::Reference
<beans::XPropertySet
>& xObjectProperties
,
174 const ShapeParam
& rParam
,
175 double fBarSegBottom
, double fBarSegTop
);
176 /** This method creates a text shape for a label of a data point.
179 * where to append the new created text shape.
181 * the data series, the data point belongs to.
183 * the index of the data point the label is related to.
187 void createTextLabelShape(
188 const rtl::Reference
<SvxShapeGroupAnyD
>& xTextTarget
,
189 VDataSeries
& rSeries
, sal_Int32 nPointIndex
, ShapeParam
& rParam
,
190 enum SubPieType eType
);
192 /** Same as createTextLabelShape(), but for bar-of-pie bar charts.
194 void createBarLabelShape(
195 const rtl::Reference
<SvxShapeGroupAnyD
>& xTextTarget
,
196 VDataSeries
& rSeries
, sal_Int32 nPointIndex
,
197 double fBarBottom
, double fBarTop
, ShapeParam
& rParam
);
199 /** This method sets `m_fMaxOffset` to the maximum `Offset` property and
200 * returns it. There is a `Offset` property for each entry in a data
201 * series, moreover there exists a shared `Offset` property attached to
202 * the whole data series. The `Offset` property represents the
203 * relative distance offset of a slice from the pie center.
204 * The shared property is used for exploded pie chart, while the property
205 * attached to single data series entries is used for manual dragging of
207 * `m_fMaxOffset` is used by `PiePositionHelper::getInnerAndOuterRadius`.
208 * Note that only the `Offset` properties of the first (x slot) data series
209 * and its entries are utilized for computing the maximum offset.
211 double getMaxOffset();
212 bool detectLabelOverlapsAndMove(const css::awt::Size
& rPageSize
);//returns true when there might be more to do
213 void resetLabelPositionsToPreviousState();
215 bool tryMoveLabels( PieLabelInfo
const * pFirstBorder
, PieLabelInfo
const * pSecondBorder
216 , PieLabelInfo
* pCenter
, bool bSingleCenter
, bool& rbAlternativeMoveDirection
217 , const css::awt::Size
& rPageSize
);
219 bool performLabelBestFitInnerPlacement( ShapeParam
& rShapeParam
220 , PieLabelInfo
const & rPieLabelInfo
221 , double fRadiusScale
222 , const ::basegfx::B3DVector
& aShift
);
224 // A standalone pie, one pie in a pie-of-pie, or one ring of a donut
225 void createOneRing([[maybe_unused
]]enum SubPieType eType
228 , const rtl::Reference
<SvxShapeGroupAnyD
>& xSeriesTarget
229 , const rtl::Reference
<SvxShapeGroup
>& xTextTarget
230 , VDataSeries
* pSeries
231 , const PieDataSrcBase
*pDataSrc
232 , sal_Int32 n3DRelativeHeight
);
234 // A bar chart in a bar-of-pie
236 enum SubPieType eType
,
238 const rtl::Reference
<SvxShapeGroupAnyD
>& xSeriesTarget
,
239 const rtl::Reference
<SvxShapeGroup
>& xTextTarget
,
240 VDataSeries
* pSeries
,
241 const PieDataSrcBase
*pDataSrc
,
242 sal_Int32 n3DRelativeHeight
);
244 void getBarRect(css::awt::Point
*pPos
, css::awt::Size
*pSz
,
245 double fBarBottom
, double fBarTop
, const ShapeParam
& rParam
) const;
247 // Determine left endpoints of connecting lines. These will terminate either
248 // at the corners of the composite wedge (if the wedge is small enough), or
249 // tangent to the left pie circle (if the wedge is larger). The endpoints
250 // are at the returned values (xl0, +/-yl0).
251 static void leftConnEndpoints(double* xl0_p
, double* yl0_p
,
252 const PieDataSrcBase
*pDataSrc
,
253 const VDataSeries
*pSeries
,
254 const ShapeParam
&aParam
);
257 // Constants for of-pie charts. Some of these will want to become
258 // user-selectable values. TODO
260 // Radius scalings for left and right of-pie subcharts
261 static constexpr double m_fLeftScale
= 2.0/3;
262 static constexpr double m_fRightScale
= 1.0/3;
263 // Shifts left/right for of-pie subcharts
264 static constexpr double m_fLeftShift
= -0.75;
265 static constexpr double m_fRightShift
= 0.75;
266 // Height of bar-of-pie bar
267 static constexpr double m_fFullBarHeight
= 1.0;
268 // Bar-of-pie bar left side position
269 static constexpr double m_fBarLeft
= 0.75;
270 // Bar-of-pie bar right side position
271 static constexpr double m_fBarRight
= 1.25;
273 PiePositionHelper m_aPosHelper
;
276 bool m_bSizeExcludesLabelsAndExplodedSegments
;
277 ::css::chart2::PieChartSubType m_eSubType
;
278 // Number of entries in an of-pie composite wedge
284 bool moveAwayFrom( const PieLabelInfo
* pFix
, const css::awt::Size
& rPageSize
285 , bool bMoveHalfWay
, bool bMoveClockwise
);
287 rtl::Reference
< SvxShapeText
> xTextShape
;
288 rtl::Reference
< SvxShapeGroupAnyD
> xLabelGroupShape
;
289 ::basegfx::B2IVector aFirstPosition
;
290 ::basegfx::B2IVector aOuterPosition
;
291 ::basegfx::B2IVector aOrigin
;
293 bool bMovementAllowed
;
295 bool bShowLeaderLine
;
296 rtl::Reference
<SvxShapeGroupAnyD
> xTextTarget
;
297 PieLabelInfo
* pPrevious
;
299 css::awt::Point aPreviousPosition
;
302 std::vector
< PieLabelInfo
> m_aLabelInfoList
;
304 double m_fMaxOffset
; /// cached max offset value (init'ed to NaN)
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */