Explore pie charts and interactivity in Swift Charts
Swift Charts has come full circle: Get ready to bake up pie and donut charts in your app with the latest improvements to the framework. Learn how to make your charts scrollable, explore the chart selection API for revealing additional details in your data, and find out how enabling additional interactivity can make your charts even more delightful.
♪ ♪ Richard: Hello, I'm Richard. Today, I'm thrilled to talk to you about some exciting new features in Swift Charts: Pie charts, selection, and scrolling. Let's start with pie charts. Swift Charts provides composable and customizable building blocks for you to create all kinds of data visualizations. Today, new to the family of Swift Charts are the delicious, beautiful pie charts. Pie charts show how a total value is made up by various categories, via simple, familiar shapes. For example, here's a chart visualizing the pancake sales data for my friends' food truck. Pie charts do not have axes, and they are great for casual settings where precision is not critical. With broad understanding of how wedges make up a full circle, they are intuitive for visualizing part-to-whole relationships. One important reason people like pie charts is because of their round, approachable shapes. You can create a pie chart using the mark-based composition syntax that you are already familiar with. Introducing a new mark type, SectorMark. A SectorMark represents a slice in the pie. It's positioned in polar space. Not this polar, but the polar coordinate system. The size of a sector is proportional to the value that it represents. Along the radius, you can customize the look of a sector. If I increase the inner radius, the pie chart becomes a donut chart. With SectorMark, you can easily build all kinds of pie charts and donut charts. Let me show you an example. Our friends' international pancake food truck business saw a big, revitalizing growth in their daily sales throughout the last year, selling six styles of pancakes. This summer, I took on another challenge to help them improve their sales app.
I start with a chart visualizing the best-selling styles of pancakes. For that, the app currently has a simple stacked bar chart. It's got a BarMark, with sales being stacked along the X dimension. As it's categorical data, the category is reflected as the foreground style of each stacked bar. This chart gets the job done, but let's turn it into a pie chart to take advantage of the available space on the screen and make the data stand out. All I need to do is to switch out BarMark and argument x for SectorMark and angle. It is that simple! With SectorMark, I use angle to represent the sales quantity. The angle values you provide for a pie chart are automatically normalized to a full circle. I can also apply some style customizations. angularInset can be set on a sector to create a gap between sectors. Here, I set the angular inset for sectors to 1.5 points, so the gap between two sectors is double the inset, 3 points wide. I also set a corner radius which gives me a nicely rounded pie crust. That already looks stunning with just a few lines of code. To try a different look, let's turn this chart into a donut chart.
I set the inner radius to be a ratio of the full radius of the pie. To me, the golden ratio looks just about right, but you might like a different thickness for your donut. Of all the sold pancakes, we have a clear winner, cachapa, that's currently displayed above the chart. But because donut charts are hollow in the center, I want to move the text to the center of the chart. I put the text in a chartBackground. I use some position calculations to make sure the text is centered in the donut hole.
Now that's a nifty-looking donut chart. So that's pie charts and donut charts. They are a great way to make an impression with your data, and they are absolutely stunning on a big screen. Next, I'd like to dive deep into some chart interactivity features, starting with selection. By enabling interactivity in your charts, you are progressively disclosing additional details. Interactivity encourages people to explore data naturally using various forms of input, such as touch.
Selection is a direct way for you to communicate with a chart, and Apple-designed charts, like the heart rate chart, are perfect examples of that. By selecting a point along an axis, the chart will reveal additional information. Let's bring this idea over to the pancake sales app.
One chart in the app visualizes the average sales of each day of the week in two cities.
I'll enable value selection on this chart to reveal the detailed sales numbers via a popover that shows the number of pancakes sold on the selected day.
This is how the chart is defined. Each city has a data series. Each element in the series has a day of the week and a sales count. The lines are then styled by city name. You may be already familiar with the chartOverlay modifier, which allows you to overlay a SwiftUI view to capture gestures. But in iOS 17, I can use the new chartXSelection modifier. It handles all the gesture recognition for me and stores the selected value to a binding.
The selection modifier gives me the raw date value along the X axis, so I define a computed property to match it to a data point in my line chart. Let's extend the chart to display a popover when a value is selected.
When a value is selected, I add a vertical rule mark as a selection indicator. I set its Z index to be lower than the default 0 value to make sure the rule mark stays behind the line marks. Now let's make a popover on top of the selection indicator. I can do this as an annotation using a custom SwiftUI view. Annotations are usually positioned within the chart, but in this case, the popover extends beyond the bounds of the chart. This is where I need an overflow resolution for annotations.
On the X axis, I make it fit to the chart such that the popover never moves past the horizontal bounds of the chart. On the Y axis, I disable overflow resolution so that the annotation can be just above the chart.
With a selection binding and a rule mark with annotation, I now have an interactive line chart. Swift Charts supports selection on macOS too, where hover gesture is the default for value selection.
Besides single value selection, a variant of this chart selection modifier allows you to select a range. On iOS, the default is a two-finger tap gesture, and on macOS, it's a drag gesture. Swift Charts also allows you to provide a custom gesture for selection, with ChartProxy helping you select a value based on gesture locations.
Beyond charts in X and Y coordinates, chart value selection works seamlessly with pie charts and donut charts. Tapping and highlighting sectors is a lot of fun.
So selection was all about revealing additional information in your chart. Another important part of interactivity is about navigating the data. Let's talk about scrolling.
I want to create a chart that visualizes daily pancake sales for a whole year. Fitting all 365 days on a screen would be unrealistic, so it has to be scrollable. To enable scrolling, I simply call the chartScrollableAxes modifier. With chartXVisibleDomain, I set a visible window of 30 days as a time interval. To be able to display the total pancake sales in the current visible domain, I use chartScrollPosition to store the current date to a binding. Now I can just take my finger and scroll.
Not only can I scroll the plot, the axis content scrolls with it, and it's buttery smooth. Scrolling can be customized in a few different ways. For example, I'd like scrolling to always snap to a date unit. This brings us to scroll behaviors. ScrollTargetBehavior is a new addition to SwiftUI and Swift Charts that allows you to align the scroll view content with values.
For the snapping behavior that I wanted, I set it to match the first hour of a day. majorAlignment customizes it one step further by defining the swiping behavior. Here, I set it to the first day of a month, so that when I page through a chart by swiping, I'll always land on the first of each month.
Scrollable charts are built on top of some of the latest enhancements to scroll views in SwiftUI. For more information, be sure to check out Beyond Scroll Views. Swift Charts provides you with endless possibilities to visualize data. Beyond charts in and X and Y coordinates, pie charts are now part of the family of APIs to create Apple-designed charts. Pie charts are simple yet powerful visualizations. They work the best when representing part-to-whole data relationships. Interactivity features like selection and scrolling enable a whole new dimension in your data visualizations, putting your user at the driver's seat as they explore data. Enjoy baking pie and donut charts. ♪ ♪