Created
April 25, 2021 22:21
-
-
Save ilamanov/2155e96cdf60c8755d02969de9c2cac9 to your computer and use it in GitHub Desktop.
Creating SwiftPieChart: PieChartView init
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct PieChartView: View { | |
public let values: [Double] | |
public var colors: [Color] | |
var slices: [PieSliceData] { | |
let sum = values.reduce(0, +) | |
var endDeg: Double = 0 | |
var tempSlices: [PieSliceData] = [] | |
for (i, value) in values.enumerated() { | |
let degrees: Double = value * 360 / sum | |
tempSlices.append(PieSliceData(startAngle: Angle(degrees: endDeg), endAngle: Angle(degrees: endDeg + degrees), text: String(format: "%.0f%%", value * 100 / sum), color: self.colors[i])) | |
endDeg += degrees | |
} | |
return tempSlices | |
} | |
var body: some View { | |
Text("Hello, World!") | |
} | |
} | |
struct PieChartView_Previews: PreviewProvider { | |
static var previews: some View { | |
PieChartView(values: [1300, 500, 300], colors: [Color.blue, Color.green, Color.orange]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment