Create Stunning Charts In EmberJS Web Application

Create charts in Ember JS

EmberJS is a JavaScript based front-end framework that helps developers:

  • build websites with rich and complex user interactions
  • manage the complexity in modern web applications.

The open-source framework also comes with an integrated development toolkit that enables rapid developmental iterations. Ember projects are created and managed through the command line build tool Ember CLI.

Key features of Ember.JS

  • EmberJS implements the Model View Controller (MVC) architecture for an application; the structure is auto-generated every time a new EmberJS application is created using the CLI.
  • EmberJS incorporates the commonly followed programming idioms and best practices, thus enabling developers to come up with scalable single-page web applications.
  • Although it was primarily designed to be a framework for the web, EmberJS can also be used for creating mobile and desktop applications.
  • EmberJS has a pluggable architecture that aids in enhancing and extending the capabilities of the framework.
  • The EmberJS framework maintains backward compatibility even as it continues to evolve.

Basic concepts related to Ember.JS

Routes – In Ember, a URL is used to represent the application’s state. A route object, corresponding to each URL, is used to control what is visible to the user. Routes are useful for redirecting towards the several route templates within the same application by hitting the URL appending the route names.

Models – A model contains data about an application’s state. Each model is associated with a route.

Templates – To ease the setup and configuration of the MVC architecture, EmberJS comes with templates written in Handlebars that help define what the user interface looks like.

Components – The component controls the behaviour of the user interface and consists of two parts:

HBS – a template that defines the UI of the page

Controller – a source file written in JavaScript that defines the behavior of the UI

In this article, we are using components to modify a chart that is rendered in the UI template file. These components can also be reused for UI visualization in other templates.

 

About FusionCharts

FusionCharts Suite XT is a JavaScript based charting solution that runs on desktop and mobile platforms. The various charts and maps that are a part of the charting library help in simplifying data analysis and data visualization. The library also includes several basic and advanced configuration options that help to add value to the data being showcased.

Prerequisites

Before we proceed with seeing how you can integrate FusionCharts with an EmberJS application, you need to:

Before we proceed with seeing how you can integrate FusionCharts with an EmberJS application, you need to:

  • Have a basic understanding of HTML, CSS, JavaScript, and Document Object Model (DOM)
  • Download and install NodeJS (NPM) to make use of the Command Line Interface (CLI)
  • Install Ember CLI via NPM (Installation details in the steps below)
  • Download FusionCharts

Steps to render a chart using an EmberJS component within an EmberJS web app

Step 1

Install NodeJS (if not installed already)

Step 2

Install Ember CLI.
To do this:
Go the directory in which you want to setup your project.
Execute the following command from the command prompt:

npm install -g ember-cli

After EmberJS is installed successfully, use the following command to check the version details:

ember -v

Step 3

Create a new Ember Project, named TestEmber, using the following command:

ember new TestEmber

When the project is created, the file structure for the Ember project is automatically generated in the working directory.

The file structure of the Ember project, TestEmber, looks as shown in the image below:

Step 4

Run the project.
To start running the project:
Start the server using the following command:

ember serve
Type the url localhost: 4200 in your browser (because 4200 is the default port being used).
Your project is running successfully if you can see the Ember landing page, as shown in the image below:

Step 5

Install FusionCharts via NPM in the TestEmber project directory, using the following command:

npm install fusioncharts

The node_modules folder will now have the entire FusionCharts library within it.

Step 6

Include the required JavaScript files from node_modules into the TestEmber project.
To do this, include the following import statements in the ember-cli-build.js  file:

A screenshot of the ember-cli-build.js file, after including the above set of commands, is shown in the image below:

Step 7
Create routes in the TestEmber project.

To generate a “route” in the TestEmber project use the following command :

ember g route ChartList/chart2
This will create a route file, chart2.js, under the routes folder structure. In addition, a template file, chart2.hbs, is also created under the templates folder structure.

The .js file is the route handler file, which defines what should happen when the route template is loaded.

The .hbs file is a Handlebars template file that contains static HTML to generate the application UI and dynamic content (scripts) for rendering charts using FusionCharts. Click here to read more about handlebars.

The directory structure of the TestEmber project now looks as shown in the image below:

Step 8
Create the component for the TestEmber project using the following command:

ember g component chart2-comp
Upon execution of the above command, the directory structure of the TestEmber project will be modified to include the components.

The modified directory structure looks as shown in the image below:

Step 9
Define the UI and its behavior.

The image below shows the intended UI visualization for this sample:

Based on this visualization, you need to:

  • Create charts and a button corresponding to each chart. Click the button to render the chart.
  • Provide a text area where the user can enter the data source for a custom chart. Add a button to render the chart when clicked.

To define this UI, copy the following code and paste it into the chart2-comp.hbs file:

A screenshot of the chart2-comp.hbs file, after including the above code, is shown in the image below:

When you click the button for a chart type, the corresponding chart will be rendered based on the actions defined in the chart2-comp.js file in the App/Components folder.

Similarly, when the data source is entered in the text area field and the corresponding button is clicked, the data source is fetched and passed to the chart2-comp.js file, via the action specified for the button.

To define this behavior of the UI, copy the following code and paste it in the chart2-comp.js file:

A screenshot of the chart2-comp.js file, after including the above code, is shown in the image below:

Note:
The actions for buttons must be defined in the component file (chart2-comp.js). This can be seen in the image above.
The component name must have a hyphen (-) in it. This is the nomenclature followed by Ember in order to identify a component.

Step 10
Components, once created, can be reused in several different templates. We will include the component created above in the routes template that was created in Step 7.

To do this, add the following code to the chart2.hbs file:
{{chart2-comp}}

A screenshot of the chart2.hbs file, after including the above code, is shown in the image below:

Step 11
Save all the files and run the project.

To run the project:

Start the server using the following command:
ember serve

Type the url localhost: 4200 in your browser (because 4200 is the default port being used).

Then hit the URL – localhost:4200/ChartList/chart2

Output
Your output should look as seen in the image below:

If you followed the steps closely, you’ll get output as shown above. If you still face problems, you can view the source code.

Troubleshooting:
If you are unable to see the chart, check that:
*the chart ID is unique for all charts rendered on the same page or it will result in a JavaScript error.
*the fusioncharts.js and other related library files are properly included (especially if the chart does not show up at all)
*the path to the included files is correct and the files reside there
*the chart alias names are correctly spelt.

Leave a Comment