From 1300e76111b870d15b6d7c8429b76abfaed77f6d Mon Sep 17 00:00:00 2001 From: oshio07 Date: Wed, 7 Dec 2022 09:25:02 +0900 Subject: [PATCH] refactor --- Sources/PieChart/Config.swift | 14 +++----------- Sources/PieChart/PieChart.swift | 26 ++++++-------------------- 2 files changed, 9 insertions(+), 31 deletions(-) diff --git a/Sources/PieChart/Config.swift b/Sources/PieChart/Config.swift index ef5a6bd..2c25a11 100644 --- a/Sources/PieChart/Config.swift +++ b/Sources/PieChart/Config.swift @@ -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) } } diff --git a/Sources/PieChart/PieChart.swift b/Sources/PieChart/PieChart.swift index e224af1..d316349 100644 --- a/Sources/PieChart/PieChart.swift +++ b/Sources/PieChart/PieChart.swift @@ -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 @@ -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 @@ -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