Skip to main content

Ionic2 - How to use D3 JS


Note: Purpose for this post is just to show, how to correctly use D3 JS in Ionic2

During the UI/Mobile app development, we all would have encountered need to use data visualization components. D3 JS is one of such powerful tool, being used for same.

Using D3 in Ionic1 was pretty easy. Since Ionic2 has moved on typescript and node modules based development, developers has been facing difficulties about how to include any third party libraries. D3 is one of such libraries. Here in this post we will look how we can include D3 into our ionic2 application.

Install Typings

Typings is the simple way to manage and install TypeScript definitions. First we need to install typings, use given commnad in cmd/terminal:

npm install -g typings

Create Ionic2 blank application

use the below command to create blank ionic application
ionic start --v2 myApp blank

this command will create an empty ionic2 project, which will contain two components app and home. app is your main component. you can include as many as page like home and use ionic navcontroller to navigate between them. For this post we don't need to include any more pages. the folder structure will look like as shown:


navigate to the root folder myApp of your application into terminal/cmd, and search for typings of d3 using given command

typings search d3

This will list all the typings available for D3 js in npm, which will look something like this

The purpose for above command is just to check availability of typings for d3.

Now install d3 into your application using command
typings install d3 --save

once the d3 has been successfully installed. we are ready to use d3 into our application.

Open home.ts, which will look like something as shown


now all we have to do is import d3 into home.ts as shown
import * as d3 from 'd3';

That's it, everthing is ready. we can now use D3 using "d3" object, which is the name we have given it during import. we can now call any D3 function such as pie(), arc() using d3 object as shown d3.pie();

Cheers!! This was all we needed to begin our development for ionic2 using d3 js.

Github link

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...