Explore pie charts and interactivity in Swift Charts


Pie charts

  • Pie charts are new to Swift Charts
  • They don’t have axises and don’t really show precision – so great for casual, intuitive analysis.
  • You can dd a sector mark – you can customize the look- increasing the radius – you get a donut charts
  • The code for a pie chart is pretty easy –

Chart(data, id: \.name) { element in

SectorMark (

angle: .value(“Sales), element.sales)

)

.foregroundStyle(by: .value(“Name”, element.name))

}

  • That’s it.  By using Sector mark, you end up with the PieChart, you can add other properties and modifiers, via .innerRadius, .angularInset, and .cornerRadius

Selection

  • This is interactivity for your chart.  Check out heart rate chart by apple.
  • Using valueSelection chartXSelection(value: $selector) modeifier allows you to capture the selection information and then provide additional information like a popover or annotation, you can use DragGestures to do a selection range

Scrolling

  • Navigating the data, just add .chartScrollableAxes a set a domain of .chartXVisibleDomain, and you provide a .chartScrollPosition as a binding, to say what part of the data to show in the scroll section.  Adding .chartScrollTargetBehavior allows you to snap to boundaries when things scroll out of the view.