Swift5.7+
iOS13+
Easily draw pie charts with SwiftUI.
import PieChart
var fruits: [(name: String, count: Int, color: Color)] = [
("apple", 10, .red),
("orange", 9, .orange),
("banana", 8, .yellow)
]
PieChart(
values: fruits.map(\.count)
)
PieChart(
values: fruits.map(\.count),
colors: [.red, .orange, .yellow]
)
or
// Another instantiation
// All the same parameters can be used.
PieChart(fruits) { fruit in
PieChart.Item(
value: fruit.count,
color: fruit.color
)
}
PieChart(
fruits,
backgroundColor: .black
) {
PieChart.Item(
value: $0.count,
color: $0.color
)
}
PieChart(
values: fruits.map(\.count),
configuration: PieChart.Configuration(space: 0.5) // 0~1.0
)
PieChart(
values: fruits.map(\.count),
configuration: PieChart.Configuration(hole: 0.6) // 0~1.0
)
PieChart(
values: fruits.map(\.count),
configuration: PieChart.Configuration(space: 0.5, hole: 0.6)
)
PieChart(
values: [Int](repeating: 1, count: 7),
backgroundColor: .gray,
configuration: PieChart.Configuration(
pieSizeRatio: 1 // 0~1.0 Default 0.8
)
)
.border(.black, width: 1)
.frame(width: 75, height: 75)
PieChart(
values: [Int](repeating: 1, count: 7),
backgroundColor: .gray,
configuration: PieChart.Configuration(
pieSizeRatio: 0.5
)
)
.border(.black, width: 1)
.frame(width: 150, height: 150)
// All parameters
PieChart(
values: [Int](repeating: 1, count: 7),
colors: [.red, .orange, .yellow, .green, .cyan, .blue, .purple],
backgroundColor: .black,
configuration: PieChart.Configuration(
space: 0.3, hole: 0.5, pieSizeRatio: 0.7
)
)
.cornerRadius(30)
.frame(width: 150, height: 150)