[SwiftUI] Swift Charts / 차트

2024. 2. 29. 01:13🍏/UI

Swift Charts

Construct and customize charts on every Apple platform.

iOS 16 버전 부터 등장한 chart 입니다.

charts

 

Swift Charts | Apple Developer Documentation

Construct and customize charts on every Apple platform.

developer.apple.com

 

Hello Swift Charts - WWDC22 - Videos - Apple Developer

Say hello to Swift Charts — a flexible framework that helps you create charts entirely in SwiftUI that look and feel right at home on all...

developer.apple.com

SwiftChartsExample Code 입니다. 

 

 

Visualizing your app’s data | Apple Developer Documentation

Build complex and interactive charts using Swift Charts.

developer.apple.com


 

 

 

SwiftChartsExample Code는 WWDC 2023 아래 링크에서 자세한 설명을 들을 수 있습니다.

 

Visualizing your app’s data | Apple Developer Documentation

Build complex and interactive charts using Swift Charts.

developer.apple.com

 

ChartsExample의 Overview입니다.

Total Sales는 BarMark로 이루어져있고 (상단)

Most Sold Style은 SectorMark (중단)

LineMark와  PointMark (하단)



 

Stacked bar chart 

Chart(data, id: \.name) { element in
  BarMark(
    x: .value("Sales", element.sales),
    stacking: .normalized
  )
  .foregroundStyle(by: .value("Name", element.name))
}
.chartXAxis(.hidden)

 

Pie chart

Chart(data, id: \.name) { element in
  SectorMark(
    angle: .value("Sales", element.sales),
    angularInset: 1.5
  )
  .foregroundStyle(by: .value("Name", element.name))
}

 

Pie chart with angular inset

Chart(data, id: \.name) { element in
  SectorMark(
    angle: .value("Sales", element.sales),
    angularInset: 1.5
  )
  .foregroundStyle(by: .value("Name", element.name))
}

 

Donut chart

Chart(data, id: \.name) { element in
  SectorMark(
    angle: .value("Sales", element.sales),
    innerRadius: .ratio(0.618),
    angularInset: 1.5
  )
  .cornerRadius(5)
  .foregroundStyle(by: .value("Name", element.name))
}

 

Donut chart with text in center

Chart(data, id: \.name) { element in
  SectorMark(
    angle: .value("Sales", element.sales),
    innerRadius: .ratio(0.618),
    angularInset: 1.5
  )
  .cornerRadius(5)
  .foregroundStyle(by: .value("Name", element.name))
}
.chartBackground { chartProxy in
  GeometryReader { geometry in
    let frame = geometry[chartProxy.plotAreaFrame]
    VStack {
      Text("Most Sold Style")
        .font(.callout)
        .foregroundStyle(.secondary)
      Text(mostSold)
        .font(.title2.bold())
        .foregroundColor(.primary)
    }
    .position(x: frame.midX, y: frame.midY)
  }
}

 

Line chart

struct LocationDetailsChart: View {
  ...

  var body: some View {
    Chart {
      ForEach(data) { series in
        ForEach(series.sales, id: \.day) { element in
          LineMark(
            x: .value("Day", element.day, unit: .day),
            y: .value("Sales", element.sales)
          )
        }
        .foregroundStyle(by: .value("City", series.city))
        .symbol(by: .value("City", series.city))
        .interpolationMethod(.catmullRom)
      }
    }
    ...
  }
}

 

 

 

'🍏 > UI' 카테고리의 다른 글

[Swift] ActivityKit / WidgetKit / Live Activity / Dynamic Island  (0) 2024.03.18