Skip to main content

Ionic - Using Chart.js

In the world of data, visualization plays the important role. It helps the business to identify the various patterns. More and more of data visualization and data scientist professional are being required. With great demand in visualization, great tools and technologies are emerging too.

In this post we are will be looking upon Chart.js with Ionic. It's an open source javascript library, which provides some predefined charts. Developers can easily integrate these charts into their web application. Syntax and integration of Chart.js is also very simple.

We assume that you have already installed ionic on your system, if not please follow the steps given on ionic documentation. Let's first create an ionic application
ionic start ChartJsDemo blank
Now our application is created, let's install Chart.js into our application
npm install chart.js --save
Now we are ready to incorporate Chart.js into our application.  To import Chart.js into our application, all we need to do is import it as other npm modules.

import { Chart } from 'chart.js';

Now let's create a page, which will show an example of using bar chart. Let's create a page structure as shown in below image

Let's see the code for each file 

bar.chart.html


  
    
    
      Bar Chart in Chart.js
    
  



  


Since Chart.js is dependent on canvas technologies, which we will be using within our typescript file to render the bar chart.

bar.chart.ts

import { Component, ViewChild, OnInit } from '@angular/core';
import { Chart } from 'chart.js';

@Component({
    selector: 'bar-chart',
    templateUrl: 'bar.chart.html'
})
export class BarChartPage implements OnInit {
    @ViewChild('barchart') barchart: any;

    ngOnInit(): void {
        console.log(this.barchart.nativeElement);
        let ctx = this.barchart.nativeElement.getContext('2d');
        let barChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: ["2000", "2005", "2010", "2015", "2016", "2017"],
                datasets: [{
                    label: 'Yearly % Change in India\'s Population',
                    data: [1.20, 1.20, 1.27, 1.47, 1.67, 1.86],
                    backgroundColor: [
                        'rgba(255, 99, 132, 0.2)',
                        'rgba(54, 162, 235, 0.2)',
                        'rgba(255, 206, 86, 0.2)',
                        'rgba(75, 192, 192, 0.2)',
                        'rgba(153, 102, 255, 0.2)',
                        'rgba(255, 159, 64, 0.2)'
                    ],
                    borderColor: [
                        'rgba(255,99,132,1)',
                        'rgba(54, 162, 235, 1)',
                        'rgba(255, 206, 86, 1)',
                        'rgba(75, 192, 192, 1)',
                        'rgba(153, 102, 255, 1)',
                        'rgba(255, 159, 64, 1)'
                    ],
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero: true
                        }
                    }]
                }
            }
        });
    }
}

The result of  the above code will look like as shown below.


The full example source for the demo can be found on github. In the github source code we, have also included an example of pie-chart too for reference purpose.

Comments

Popular posts from this blog

Ionic2 - Secure Storage in mobile application

Securing communication between mobile application and back-end server is crucial. We should authenticate the requests sent from mobile applications before serving them. Authentication may require username and password for the user, which most of the developers store into mobile local-storage.  Storing sensitive information in local-Storage is a very bad practice, instead you should use other options such as JWT tokens or Social providers. To get more information look to Security for Cordova mobile applications . An token is also an sensitive information, which we need to store somewhere, so what shall we do? Thank God cordova have such large community support, we will look for one of such plugin which can help us here. Before using plugin, lets first create the blank ionic2 application ionic start --v2 SecureStorageExample blank navigate to the directory of your application  and add the secure storage plugin using the below command ionic plugin add cordov...

Ionic2 - Hammer JS Example

Gesture event support is also one of the reason behind popularity of mobile applications. Capturing gesture events can be easily done in case of native mobile applications, since all the mobile platform expose gesture events in native code. Since Hybrid mobile applications are built over html and  javascript,  html don't support gesture events. There could be scenario's, where you wants to provide support for your hybrid mobile application. In such cases, we can use Hammer.JS  . Angular version 2 also provides support for Hammer.JS, about the same we will look into this post. Let's say we want to design a simple box, which we can rotate in any direction as shown Here we are trying to rotate the box in any direction by using Gesture events. To perform rotation we need to use two fingers. Let's see how we can implement the same in Ionic. Let's create a blank application using command ionic start hammerjsExample blank --v2 Now let's add Hammer.JS ...

Ionic2 - Internationalization using ng2-translate

To target target people from different countries, i18n support is required for your mobile app . Here in India where there are so many local languages, i18n support in mobile apps is must. In This blog, we will look how we can achieve i18n in ionic2 apps. We all know ionic2 is based on angular version 2. New angular version has it's own internationalization(i18n) approach , but sadly this approach is not yet supported in ionic2. Ionic team suggests to use ng2-translate for the internationalization in ionic2. Let's begin with it Create a blank application in ionic2 ionic start --v2 i18Ionic blank navigate to folder i18Ionic in terminal/cmd and install ng2-translate npm package in your ionic project npm install ng2-translate --save Once the npm package is installed, we are ready to configure our application for i18n support. in ng2-translate, we need to create json specific to the languages which we want to support in our code. In this example we will s...