Category: Blog, iOS, Development

Beautiful charts in Swift

Beautiful charts in Swif

Charts are a lovely way to present complicated data sets. Everything seems to be easier when you present it in on a chart. One picture is worth of thousand words and the same can be said about charts.

Unfortunately charts libraries are often not-so-easy or (and that’s the worst) after multiple hours spent with it you see that’s something is impossible with that one. Probably one of the most popular is CorePlot. For me that was an overkill. I believe you can make beautiful chart with it, but it takes hours or even days. On the other hand, iOS-Charts gives beautiful charts without spending whole week on it. Out of the box your charts seem to be cool. In addition it has so many properties letting you configure a chart the way you want – it’s really powerful. Also it’s written in modern Swift.

Furthermore it can be considered as a port of great MPAndroidChart, meaning that you can easily achieve the same effect on Android OS too. That makes iOS-Charts the best choice.

Before you take iOS-Charts for a spin, let’s see how to use it. In fact iOS-Charts has only one big disadvantage – it’s not well documented. But thanks to the fact it’s really similar to Android library, MPAndroidChart Wiki is really helpful.

First, create a new iOS project. Remember to not name the project ‘Charts’, because that would make conflict with module names. I named my ‘ChartsDemo’. Choose Swift as programming language and add pod 'Charts' to your Podfile.

Two things. First, if you don’t know much about CocoaPods or even about creating projects, you can find more info in one of our previous tutorials, like this one. Second, you may think it’s weird that pod named Charts is connected with library named iOS-Charts. As far as I know with the next release of the library it’s going to be changed and repository’s name would be the same as pod’s name. Woohoo!

Your Podfile should look like the one below.

Okay, you’ve got empty project with installed pod. Let’s start then.

Charts lets you make line, bar, pie, scatter, bubble and radar charts. Everything you want. But before we start, we need some data that can be presented on a chart. If you don’t have any, I’ve prepared data generator that I use in this tutorial.

This code is self-explanatory. Class func data gives sample i.e. sales data with profit for each month. That’s a perfect fit for bar or line chart, so let’s do those!

Open storyboard and add in the first view controller new UIView. In Identity Inspector change its class to BarChartView (class names are really simple and if you want a line chart then a proper class is named just LineChartView), and then pin the view with margin equals zero to all edges of the superview.

Zrzut ekranu 2016-03-22 o 14.04.57

We need an outlet to this new view, so make one ctrl-dragging this to ViewController.swift. I named mine just barChart. Your Xcode should notice really fast that BarChartView class is a one big mystery for it. Just add import Charts at the top of your file. You view controller’s class can look like this:

Now we should give the chart some data to present. I do all the steps in viewDidLoad like so:

It should work now. In my case after running the app it looked like this:

Simulator Screen Shot 22.03.2016, 15.12.53

Not bad for first attempt, but a little boring. Let’s polish it right now.

There’re multiple things that we can easily fix. First, we don’t need ‘Description’ in the right bottom corner. To remove it we need just this:

Unfortunately although Charts is written in Swift, it doesn’t use all available semantic. descriptionText is not an optional, so it can’t be niled. But an empty string does the job.

Second, there is no legend and/or values with month’s names. How would I know that these 12 bars are representing one year instead of twelve years or days? We have to add some labels. It’s simple and requires exactly one line of code:

But if you run the app right now you can notice that only every second label is shown. That’s because Charts have an algorithm that detect how many labels can be shown to make them readable. Take a shot and show every single legend element this way:

Next thing, we precisely know min and max values that can be received from DataGenerator. Why not add them to the setup to make the chart scaling more accurate?

The bars are definitely not happy or even colorful. Why not paint them with some bright colors?

Probably you have noticed that single chart can present multiple chart data sets (that’s exactly why I showed you BarChartData initializer with dataSets instead of also available dataSet parameter). That’s why we can color each data set separately. We can give an array of colors or use one of predefined colors from ChartColorTemplates enum.

As I said we’ve got only one chart data set, so there is no need for the legend at the bottom. Again it’s just one line of code:

Out of the box, iOS-Charts lets you pinch-to-zoom on a graph. If you want to disable it, then of course it’s possible. It can be enabled separately for each axis, but the code below disables both of them including double tap to zoom.

Another nice option is choosing bars by tapping them. Then you can show some kind of balloon with value or description. We don’t need that in our case, so selection can be disabled.

It looks like right axis is unnecessary here. The chart is really simple and there is no need to duplicate the left one. Also we don’t need vertical grid lines. Let’s hide them right now.

Last, but not least. What about a nice animation making first impression even better? Animations often are a pain in the neck. But not with Charts! Add this line just after setting bar chart’s data.

That’s all. Not so hard, huh? Take a look at the final effect we achieved here.

light.mov

Of course that’s not everything that Charts lets you do. Check this library out with it’s numerous charts types and take a look at hundreds of properties that let you customize charts that you create.

Thank you for reading. Have a happy Easter and feel free to comment!

About the author

Piotr Sochalewski

Piotr Sochalewski

Droids On Roids iOS Developer