Skip to content

skw398/SwiftUI-PieChart

Repository files navigation

SwiftUI-PieChart

Swift5.7+ iOS13+

Easily draw pie charts with SwiftUI.

スクリーンショット 2023-04-16 22 52 36

Examples

import PieChart
    var fruits: [(name: String, count: Int, color: Color)] = [
        ("apple", 10, .red),
        ("orange", 9, .orange),
        ("banana", 8, .yellow)
    ]    

1.

    PieChart(
        values: fruits.map(\.count)
    )

2.

    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
        )
    }

3.

    PieChart(
        fruits,
        backgroundColor: .black
    ) {
        PieChart.Item(
            value: $0.count,
            color: $0.color
        )
    }

4.

   PieChart(
       values: fruits.map(\.count),
       configuration: PieChart.Configuration(space: 0.5) // 0~1.0
   )

5.

   PieChart(
       values: fruits.map(\.count),
       configuration: PieChart.Configuration(hole: 0.6) // 0~1.0
   )

6.

   PieChart(
       values: fruits.map(\.count),
       configuration: PieChart.Configuration(space: 0.5, hole: 0.6)
   )

7.

   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)

8.

    PieChart(
        values: [Int](repeating: 1, count: 7),
        backgroundColor: .gray,
        configuration: PieChart.Configuration(
            pieSizeRatio: 0.5
        )
    )
    .border(.black, width: 1)
    .frame(width: 150, height: 150)

9.

    // 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)

About

Easily draw pie charts with SwiftUI.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages