Skip to content

Instantly share code, notes, and snippets.

@prafullakumar
Created September 14, 2020 04:09
Show Gist options
  • Save prafullakumar/f830fee6404dde3b5df653621a0e122a to your computer and use it in GitHub Desktop.
Save prafullakumar/f830fee6404dde3b5df653621a0e122a to your computer and use it in GitHub Desktop.
struct ChartCellModel: Identifiable {
let id = UUID()
let color: Color
let value: CGFloat
let name: String
}
final class ChartDataModel: ObservableObject {
var chartCellModel: [ChartCellModel]
var startingAngle = Angle(degrees: 0)
private var lastBarEndAngle = Angle(degrees: 0)
init(dataModel: [ChartCellModel]) {
chartCellModel = dataModel
}
var totalValue: CGFloat {
chartCellModel.reduce(CGFloat(0)) { (result, data) -> CGFloat in
result + data.value
}
}
func angle(for value: CGFloat) -> Angle {
if startingAngle != lastBarEndAngle {
startingAngle = lastBarEndAngle
}
lastBarEndAngle += Angle(degrees: Double(value / totalValue) * 360 )
print(lastBarEndAngle.degrees)
return lastBarEndAngle
}
}
let sample = [ ChartCellModel(color: Color.red, value: 123, name: "Math"),
ChartCellModel(color: Color.yellow, value: 233, name: "Physics"),
ChartCellModel(color: Color.pink, value: 73, name: "Chemistry"),
ChartCellModel(color: Color.blue, value: 731, name: "Litrature"),
ChartCellModel(color: Color.green, value: 51, name: "Art")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment