Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
skw398 committed Dec 7, 2022
1 parent 3e9efcb commit 1300e76
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 31 deletions.
14 changes: 3 additions & 11 deletions Sources/PieChart/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,8 @@ public struct Config {
hole: Double = 0,
pieSizeRatio: Double = 0.8
) {
self.pieSizeRatio = convertTo0to1Value(pieSizeRatio)
self.lineWidthMultiplier = convertTo0to1Value(space) / 10
self.holeSizeRatio = convertTo0to1Value(hole)

func convertTo0to1Value(_ value: Double) -> Double {
switch value {
case ..<0: return 0
case 1...: return 1
default: return value
}
}
self.pieSizeRatio = min(max(pieSizeRatio, 0), 1)
self.lineWidthMultiplier = min(max(space, 0), 1) / 10
self.holeSizeRatio = min(max(hole, 0), 1)
}
}
26 changes: 6 additions & 20 deletions Sources/PieChart/PieChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,10 @@ public struct PieChart: View {

public var body: some View {
GeometryReader { geometry in
let angles: [CGFloat] = {
let totalValue = values.reduce(0, +)
var angle = -90.0
var angels: [CGFloat] = [angle]
values.forEach {
angle += Double($0 / totalValue * 360)
angels += [angle]
}
return angels
}()
let totalValue = values.reduce(0, +)
let angles = values.reduce(into: [-90.0]) { (angles, value) in
angles.append(angles.last! + value / totalValue * 360)
}
let shorterSideLength: CGFloat = min(geometry.size.width, geometry.size.height)
let center: CGPoint = .init(x: geometry.size.width / 2, y: geometry.size.height / 2)
let lineWidth: CGFloat = shorterSideLength * pieSizeRatio * lineWidthMultiplier
Expand Down Expand Up @@ -88,11 +82,7 @@ public extension PieChart {
config: Config = .init()
) {
self.values = values.map { Double($0) }
if colors.isEmpty {
self.colors = defaultColors
} else {
self.colors = colors
}
self.colors = colors.isEmpty ? defaultColors : colors
self.pieSizeRatio = config.pieSizeRatio
self.lineWidthMultiplier = config.lineWidthMultiplier
self.holeSizeRatio = config.holeSizeRatio
Expand All @@ -106,11 +96,7 @@ public extension PieChart {
config: Config = .init()
) {
self.values = values.map { Double($0) }
if colors.isEmpty {
self.colors = defaultColors
} else {
self.colors = colors
}
self.colors = colors.isEmpty ? defaultColors : colors
self.pieSizeRatio = config.pieSizeRatio
self.lineWidthMultiplier = config.lineWidthMultiplier
self.holeSizeRatio = config.holeSizeRatio
Expand Down

0 comments on commit 1300e76

Please sign in to comment.