{"id":12948,"date":"2017-07-17T14:35:14","date_gmt":"2017-07-17T14:35:14","guid":{"rendered":"http:\/\/www.highcharts.com\/blog\/?p=12948"},"modified":"2026-01-12T09:35:27","modified_gmt":"2026-01-12T09:35:27","slug":"setting-chart-ionic-app-using-highcharts","status":"publish","type":"post","link":"https:\/\/www.highcharts.com\/blog\/integration\/setting-chart-ionic-app-using-highcharts\/","title":{"rendered":"Setting up a chart in an Ionic App using Highcharts"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><b><i>For native iOS apps, check out the new <a href=\"https:\/\/www.highcharts.com\/wrappers\/\">Highcharts iOS wrapper<\/a><\/i><\/b>.<\/p>\n<p>Ionic has grown spectacularly in the last two years; from using AngularJS initially to Angular 4 and now TypeScript in <a href=\"http:\/\/ionicframework.com\/\" target=\"_blank\" rel=\"noopener\">Ionic Framework (version 3)<\/a>. I am a big fan of Ionic.<\/p>\n<p>My other favorite JavaScript library is Highcharts (no surprise there). Like most Highcharts fans, I love how easy it is to create responsive and interactive charts. That is why I decided to set up a project to combine my two favorite libraries. Will it blend? Let\u2019s give it a shot!<\/p>\n<p>First thing first: install Ionic by typing the following commands in the command prompt or terminal. Actually, the first thing is to install the latest version of NodeJs, if you haven\u2019t done so already. Download NodeJS from nodejs.org.<br \/>\n<code>npm install ionic -g<br \/>\nnpm install cordova -g<\/code><\/p>\n<p>Install also Cordova as Ionic Apps require Cordova. (Ok, this is technically the second first thing to do\u2026)<br \/>\nCordova wraps your HTML, SCSS, and TS based Web App into a Native Android or iOS App, which is often a good option.<br \/>\nFinally, let\u2019s create our first Ionic App:<br \/>\n<code>ionic start  blank<\/code><code><\/code><br \/>\nReplace with the name of your choice to create an app. For this article, I use the name HighChartsIonic. \u201cblank\u201d is a template name to create a blank Ionic App with minimal pre-written code.<br \/>\n<code>ionic start HighChartsIonic<\/code><\/p>\n<p>Once this command finishes its execution, a new folder with the name HighChartsIonic is created. Open that folder in the text editor of your choice; I use VS Code as a text editor, as it allows me to run the app in the browser. (You may naturally use any other IDE of your choice). Open the terminal from the view menu or press Ctrl + ` (backtick) and write the following command:<br \/>\n<code>ionic serve <\/code><\/p>\n<p>This command builds and launches the app in the browser. To get a more mobile-like view in the browser use \u2013l flag to launch the app in the lab mode.<br \/>\n<code>ionic serve \u2013l <\/code><\/p>\n<p>At this point of the project you should see something like this in the browser:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-12955 size-full\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112047\/Ionic-App-using-Highcharts-1.png\" alt=\"\" width=\"1362\" height=\"689\" srcset=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112047\/Ionic-App-using-Highcharts-1.png 1362w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112047\/Ionic-App-using-Highcharts-1-560x283.png 560w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112047\/Ionic-App-using-Highcharts-1-768x389.png 768w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112047\/Ionic-App-using-Highcharts-1-760x384.png 760w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112047\/Ionic-App-using-Highcharts-1-360x182.png 360w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112047\/Ionic-App-using-Highcharts-1-1160x587.png 1160w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112047\/Ionic-App-using-Highcharts-1-30x15.png 30w\" sizes=\"auto, (max-width: 1362px) 100vw, 1362px\" \/><\/p>\n<p>This is a preview of how the application will look like on a real mobile device. Let\u2019s bring in Highcharts and create a chart on this page. Get back to VS Code, and in the terminal press Ctrl + C to stop the Ionic Development Server. Type in the following commands to install Highcharts.<br \/>\n<code>npm install highcharts \u2013save<\/code><\/p>\n<p>The result will look like:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-12973 size-full\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17131251\/Ionic-App-using-Highcharts-61.png\" alt=\"\" width=\"1635\" height=\"373\" srcset=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17131251\/Ionic-App-using-Highcharts-61.png 1635w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17131251\/Ionic-App-using-Highcharts-61-560x128.png 560w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17131251\/Ionic-App-using-Highcharts-61-768x175.png 768w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17131251\/Ionic-App-using-Highcharts-61-760x173.png 760w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17131251\/Ionic-App-using-Highcharts-61-360x82.png 360w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17131251\/Ionic-App-using-Highcharts-61-1160x265.png 1160w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17131251\/Ionic-App-using-Highcharts-61-30x7.png 30w\" sizes=\"auto, (max-width: 1635px) 100vw, 1635px\" \/><\/p>\n<p>The message on the screen is information that Highcharts module has been installed successfully (notice the <code>text<\/code> in this color).<br \/>\nOpen the file <code>.\/src\/pages\/home\/home.html<\/code> and replace everything inside the ion-content tag with a div like this.<\/p>\n<pre>&lt;div id=\"container\" style=\"display: block;\"\t&gt;&lt;\/div&gt;<\/pre>\n<p>This div is a container to hold the chart. I write the code in <code>home.ts<\/code> file. The <code>home.html<\/code> and <code>home.ts<\/code> are the files in charge of creating the home page in the app.<br \/>\nIn home.ts, on the top, import the <code>highcharts<\/code> module first.<\/p>\n<pre>import * as HighCharts from 'highcharts';<\/pre>\n<p>Next, create a function called <code>ionViewDidLoad()<\/code> inside the <code>HomePage<\/code> class, just after the constructor. The file should look like this:<\/p>\n<pre>import {\r\n  Component\r\n} from '@angular\/core';\r\nimport {\r\n  NavController\r\n} from 'ionic-angular';\r\nimport * as HighCharts from 'highcharts';\r\n\r\n@Component({\r\n  selector: 'page-home',\r\n  templateUrl: 'home.html'\r\n})\r\nexport class HomePage {\r\n\r\n  constructor(public navCtrl: NavController) {}\r\n\r\n  ionViewDidLoad() {}\r\n}\r\n<\/pre>\n<p>The <code>ionViewDidLoad<\/code> is a special function which is executed after the View has been initialized and loaded; this makes sure to avoid any errors during the components\u2019 access in the view. Create a new <code>HighCharts.chart<\/code> object in the <code>ionViewDidLoad<\/code>:<\/p>\n<pre>var myChart = HighCharts.chart('container', {\r\n  chart: {\r\n    type: 'bar'\r\n  },\r\n  title: {\r\n    text: 'Fruit Consumption'\r\n  },\r\n  xAxis: {\r\n    categories: ['Apples', 'Bananas', 'Oranges']\r\n  },\r\n  yAxis: {\r\n    title: {\r\n      text: 'Fruit eaten'\r\n    }\r\n  },\r\n  series: [{\r\n    name: 'Jane',\r\n    data: [1, 0, 4]\r\n  }, {\r\n    name: 'John',\r\n    data: [5, 7, 3]\r\n  }]\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p>Save the file, and in the terminal type ionic serve \u2013l to run the app in the browser in the lab mode. The app should look like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-12953 size-full\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112043\/Ionic-App-using-Highcharts-3.png\" alt=\"\" width=\"1358\" height=\"686\" srcset=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112043\/Ionic-App-using-Highcharts-3.png 1358w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112043\/Ionic-App-using-Highcharts-3-560x283.png 560w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112043\/Ionic-App-using-Highcharts-3-768x388.png 768w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112043\/Ionic-App-using-Highcharts-3-760x384.png 760w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112043\/Ionic-App-using-Highcharts-3-360x182.png 360w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112043\/Ionic-App-using-Highcharts-3-1160x586.png 1160w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112043\/Ionic-App-using-Highcharts-3-30x15.png 30w\" sizes=\"auto, (max-width: 1358px) 100vw, 1358px\" \/><\/p>\n<p>The charts are now visible on the homepage of the Ionic 3 App. Let\u2019s play around with the chart object a little bit and see how it modifies the charts in the app.<br \/>\nChange the <code>chart.type<\/code> property in the JSON object to a <code>line<\/code> and save the file. The browser will reload the app in a few seconds as soon as the file is saved. The result should look like:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-12952 size-full\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112042\/Ionic-App-using-Highcharts-4.png\" alt=\"\" width=\"1359\" height=\"683\" srcset=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112042\/Ionic-App-using-Highcharts-4.png 1359w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112042\/Ionic-App-using-Highcharts-4-560x281.png 560w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112042\/Ionic-App-using-Highcharts-4-768x386.png 768w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112042\/Ionic-App-using-Highcharts-4-760x382.png 760w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112042\/Ionic-App-using-Highcharts-4-360x181.png 360w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112042\/Ionic-App-using-Highcharts-4-1160x583.png 1160w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112042\/Ionic-App-using-Highcharts-4-30x15.png 30w\" sizes=\"auto, (max-width: 1359px) 100vw, 1359px\" \/><\/p>\n<p>Awesome, isn\u2019t it? Let\u2019s try one more thing. Change the chart\u2019s type to <code>spline<\/code> and save again. Let the magic happens.<\/p>\n<p>I can also generate the graph series data using a function. For now, I use a for-loop to generate random dummy data, but in a real app, the data could come from an API, web service, or after calculations.<br \/>\nReplace the existing <code>data<\/code> objects of both the series with the data object:<\/p>\n<pre>data: (function() {\r\n  var data = [];\r\n\r\n  for (let i = 0; i & lt; = 5; i += 1) {\r\n    data.push({\r\n      x: i,\r\n      y: Math.floor(Math.random() * 10) + 0\r\n    });\r\n  }\r\n  return data;\r\n}())\r\n<\/pre>\n<p>This<br \/>\nfunction generates an array of five random integer elements between 0 and 10. The < code > ionViewDidLoad < \/code> should now look like this: <\/p>\n<pre>data: (function() {\r\n  var data = [];\r\n\r\n  for (let i = 0; i & lt; = 5; i += 1) {\r\n    data.push({\r\n      x: i,\r\n      y: Math.floor(Math.random() * 10) + 0\r\n    });\r\n  }\r\n  return data;\r\n}())\r\nionViewDidLoad() {\r\n  var myChart = HighCharts.chart('container', {\r\n    chart: {\r\n      type: 'spline'\r\n    },\r\n    title: {\r\n      text: 'Fruit Consumption'\r\n    },\r\n    xAxis: {\r\n      categories: ['Apples', 'Bananas', 'Oranges']\r\n    },\r\n    yAxis: {\r\n      title: {\r\n        text: 'Fruit eaten'\r\n      }\r\n    },\r\n    series: [{\r\n      name: 'Jane',\r\n      data: (function() {\r\n        var data = [];\r\n        for (let i = 0; i & lt; = 5; i += 1) {\r\n          data.push({\r\n            x: i,\r\n            y: Math.floor(Math.random() * 10) + 0\r\n          });\r\n        }\r\n        return data;\r\n      }())\r\n    }, {\r\n      name: 'John',\r\n      data: (function() {\r\n        var data = [];\r\n        for (let i = 0; i & lt; = 5; i += 1) {\r\n          data.push({\r\n            x: i,\r\n            y: Math.floor(Math.random() * 10) + 0\r\n          });\r\n        }\r\n        return data;\r\n      }())\r\n    }]\r\n  });\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Save the file and let the app reload in the browser. You will get random graphs like the ones shown below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-12951 size-full\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112040\/Ionic-App-using-Highcharts-5.png\" alt=\"\" width=\"1361\" height=\"755\" srcset=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112040\/Ionic-App-using-Highcharts-5.png 1361w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112040\/Ionic-App-using-Highcharts-5-560x311.png 560w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112040\/Ionic-App-using-Highcharts-5-768x426.png 768w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112040\/Ionic-App-using-Highcharts-5-760x422.png 760w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112040\/Ionic-App-using-Highcharts-5-360x200.png 360w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112040\/Ionic-App-using-Highcharts-5-1160x643.png 1160w, https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2017\/07\/17112040\/Ionic-App-using-Highcharts-5-30x17.png 30w\" sizes=\"auto, (max-width: 1361px) 100vw, 1361px\" \/><\/p>\n<p>Feel free to play with the code and try out new properties and functions. Check out <a href=\"https:\/\/www.highcharts.com\/docs\" target=\"_blank\" rel=\"noopener\">the Official Highcharts Docs<\/a> to learn more about Highcharts.<br \/>\nI enjoyed this project, and I hope it will help you to set up great charts. I would love to hear your comments and experiences.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to build wonderful charts for your mobile apps using Highcharts and IONIC framework.<\/p>\n","protected":false},"author":214,"featured_media":13173,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"meta_title":"","meta_description":"","hc_selected_options":[],"footnotes":""},"categories":[1105],"tags":[1094],"coauthors":[697],"class_list":["post-12948","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-integration","tag-highcharts-core"],"_links":{"self":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/12948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/users\/214"}],"replies":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/comments?post=12948"}],"version-history":[{"count":1,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/12948\/revisions"}],"predecessor-version":[{"id":29098,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/12948\/revisions\/29098"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media\/13173"}],"wp:attachment":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media?parent=12948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/categories?post=12948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/tags?post=12948"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/coauthors?post=12948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}