Lets get moving

Depending on the type of game you are looking to make, sometimes we need to adjust the background. All we simply have to do it switch to our main camera and under clear flags, we change it to solid…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Create a simple pie chart with SwiftUI

Display data in pie chart the easiest way

When we want to display statistical information, it is better to display data in graphic forms like charts and graphs. Pie chart is the most common of these because it is simple and easy to understand. You can compare data, analyze information very quickly, and emphasize your points easily by using different colors to highlight a slide in the chart. But this article is not talking about pros and cons of pie chart or when to use it, it is about helping you create a pie chart in SwiftUI

To begin, create a brand new SwiftUI project, then create a new file called DataItem.swift this object is to keep each piece of information (one slide) that we have to display in the chart (the whole pie)

So, based on this data, we have to create data for each slide in the chart called SlideData.swift

From here, we have to create data for the whole pie calledPieChartData.swift, in this file, we will generate data for the whole pie from and array of double values, or from an array of SlideData

To explain the code used to calculate data for each slide a bit: the screen coordinates on mobile is like in the picture below, (0,0) is the top left corner, the first slide usually starts at -90°, so the first slide has the angle of -90°+ α, the second slide (red) has the angle of -90°+ α + β and so on

x-y axis on device

From the data of each slide, we can draw the slide view of the pie chart by creating a Path starting from the center of the pie chart view calculated by geometry proxy combined with arc’s angle from the startAngle to the endAngle

Then create the whole pie chart

So if I want to add annotation label (for example 8%) to the red slide at the red dot (at 0.7 radius from the center of the circle). It’s just pure math calculation here. First I will find the center point on the slide arc, which is (𝑥0, y0), by using trigonometric functions for provided data (alpha, and radius). And to make it simple, I just do it for the first quadrant, because when the arc center point is on different quadrant, its coordinate is just a combination of ±𝑥0 and ±y0. Then, the coordinate for the annotation label of this slide will be (0.7𝑥0, 0.7y0)

You can also use this concept to move one or a few slides a little bit farther from the center of the pie to emphasize that piece of data (like the 3rd pie chart in the top image)

Add a small animation when displaying the chart will make it more friendly and more eye-catching, here I use spring animation, combining with the index of the slide to make each slide appear a little bit later than the previous one.

Now, you can create a nice pie chart to display data in your application, but sometimes, it’s better to use different type of charts/graphs (bar graph for example), I will save that for another article.

Add a comment

Related posts:

5 tips voor teams die plotseling thuis werken

Op dit moment kunnen we allemaal wel wat extra hulp gebruiken. Covid-19 heeft het nieuws overgenomen, de aandelenmarkten storten in en supermarkten worden geplunderd. Overheden van over de hele…

Relatable Content

Last year I released a self published volume of comics I made for VICE, it’s an assortment of various topics that cover big things like workers rights and conspiracy theory nuts to little things like…

Some words of wisdom that speaks to me

As I was about to start writing this post, I asked my sister what word of wisdom I have spoken to her and you will not believe what she said. She said that I told her to be selfish. Imagine! Me oh…