This is something that I had been meaning to add to Html5StarterAppWithSwift for a long time and I was finally able to achieve that a couple of weeks ago. In this post, I will talk about how to visualise data in an iOS app using using web based html5 and JQuery based charting library. In summary, this post focuses on visualising data in iOS app using methods similar to web.
Background
Now that I am involved in making iOS apps and given my background it seemed natural for me to apply my charting knowledge to making iOS apps…and that is the story behind this post.
Visualising data in a native iOS app
So what’s the charting solution?
![]() |
My aim here was simply to show how and what can be done. So have a look at the screenshot on the right.
Here we are drawing a chart that shows the muesli bar consumption on weekdays.
Solution details
In this section, I will try to delve into the different aspects of this solution.
Chartist.js why not morris.js?
Initially I thought I should use morris.js for this too, however morris.js depends on jQuery and for this repo, I have not had the need to use jQuery yet. I had to find charting solution that does not depend on jQuery and in my search I came across this excellent SitePoint article, which listed the 15 best Javascript charting libraries, through which I discovered chartist.js.
Code for the visualisation
I would recommend going through the getting started page to get a feel or know more about how chartist draws charts. What I have done in my solution is, draw a simple bar chart using the sample code on the getting started page of Chartist.js.
Remember, the goal here was to simply demonstrate via a simple example. Anyway the only thing that I have done differently is how data is fetched for the chart. For my solution, the data comes from a service called TestData which has the forCharts method.
This is the code for the service,
.factory('TestData', function() {
var chartTestData = {
labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
series: [
[4, 2, 2.5, 0.5,3]
]
};
return {
forCharts: function() {
return chartTestData;
},
}
});
For the controller,
.controller('ChartsCtrl', function($scope,TestData){
var data = TestData.forCharts();
var options = {
width: 300,
height: 200
};
new Chartist.Bar('.ct-chart', data, options);
})
For the template,
<ion-view title="Html5 Charts">
<div style="margin-top:50px;margin-left:5px;" >
<h5> Muesli bars eaten per day </h5>
<div class="ct-chart ct-golden-section" id="chart1"></div>
</div>
</ion-view>
As I said I have intentionally kept things quite simple in order to convey the message about Html5 based charting in a native iOS app.
Conclusion
In addition to hard-coding the data in the service, there are two other ways to fetch data for this sort of app,
- Using $http service of AngularJS
- The Swift backend
So if you would like to see a demonstration of any of the above 2 techniques please leave a comment or create an issue here…or better, if you think you can do that, then by all means, fork the repo and create a pull request.
As usual, if you find any of my posts useful support me by buying or even trying one of my apps on the App Store.
Also, if you can leave a review on the App Store or Google Play Store, that would help too.

0 Comments