diff --git a/Source/Charts/Charts/PieChartView.swift b/Source/Charts/Charts/PieChartView.swift index 704c066bdf..a7c1bdcaf3 100644 --- a/Source/Charts/Charts/PieChartView.swift +++ b/Source/Charts/Charts/PieChartView.swift @@ -72,6 +72,9 @@ open class PieChartView: PieRadarChartViewBase /// maximum angle for this pie private var _maxAngle: CGFloat = 360.0 + /// mininum angle for slices + private var _minAngleForSlices: CGFloat = 0.0 + public override init(frame: CGRect) { super.init(frame: frame) @@ -249,6 +252,7 @@ open class PieChartView: PieRadarChartViewBase { _drawAngles = [CGFloat]() _absoluteAngles = [CGFloat]() + var minAngles = [CGFloat]() guard let data = data else { return } @@ -256,17 +260,32 @@ open class PieChartView: PieRadarChartViewBase _drawAngles.reserveCapacity(entryCount) _absoluteAngles.reserveCapacity(entryCount) + minAngles.reserveCapacity(entryCount) let yValueSum = (data as! PieChartData).yValueSum + let hasMinAngle = _minAngleForSlices != .zero && (CGFloat(entryCount) * _minAngleForSlices) <= _maxAngle var cnt = 0 + var offset = 0.0 + var diff = 0.0 for set in data { for j in 0 ..< set.entryCount { guard let e = set.entryForIndex(j) else { continue } + let drawAngle = calcAngle(value: abs(e.y), yValueSum: yValueSum) + if hasMinAngle { + let temp = drawAngle - _minAngleForSlices + if temp <= 0 { + minAngles.append(_minAngleForSlices) + offset += -temp + } else { + minAngles.append(drawAngle) + diff += temp + } + } _drawAngles.append(calcAngle(value: abs(e.y), yValueSum: yValueSum)) if cnt == 0 @@ -281,6 +300,20 @@ open class PieChartView: PieRadarChartViewBase cnt += 1 } } + + if hasMinAngle { + for i in 0..<entryCount { + minAngles[i] -= (minAngles[i] - _minAngleForSlices) / diff * offset + + if i == 0 { + _absoluteAngles[0] = minAngles[0] + } else { + _absoluteAngles[i] = _absoluteAngles[i - 1] + minAngles[i] + } + } + + _drawAngles = minAngles + } } /// Checks if the given index is set to be highlighted. @@ -665,6 +698,28 @@ open class PieChartView: PieRadarChartViewBase } } + /// Minimum angle to draw slices, + /// this only works if there is enough space for all slices to have the minimum angel + /// + /// **default**: 0.0 + @objc open var minAngleForSlices: CGFloat + { + get + { + return _minAngleForSlices + } + set + { + _minAngleForSlices = newValue + + if _minAngleForSlices > (_maxAngle / 2) { + _minAngleForSlices = _maxAngle / 2 + } else if _minAngleForSlices < 0 { + _minAngleForSlices = .zero + } + } + } + /// smallest pie slice angle that will have a label drawn in degrees, 0 by default @objc open var sliceTextDrawingThreshold: CGFloat = 0.0 { diff --git a/Tests/ChartsTests/PieChartTests.swift b/Tests/ChartsTests/PieChartTests.swift index a33c3ebae7..0a57781212 100644 --- a/Tests/ChartsTests/PieChartTests.swift +++ b/Tests/ChartsTests/PieChartTests.swift @@ -76,4 +76,9 @@ class PieChartTests: XCTestCase { chart.highlightValue(x: 1.0, dataSetIndex: 0, callDelegate: false) assertChartSnapshot(matching: chart) } + + func testMinAngleForSlices() { + chart.minAngleForSlices = 2 + assertChartSnapshot(matching: chart) + } }