Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[issue: #4414 ] Added: minAngleForSlices #5136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions Source/Charts/Charts/PieChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -249,24 +252,40 @@ open class PieChartView: PieRadarChartViewBase
{
_drawAngles = [CGFloat]()
_absoluteAngles = [CGFloat]()
var minAngles = [CGFloat]()

guard let data = data else { return }

let entryCount = data.entryCount

_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
Expand All @@ -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.
Expand Down Expand Up @@ -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
{
Expand Down
5 changes: 5 additions & 0 deletions Tests/ChartsTests/PieChartTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}