Site icon FusionBrew – The FusionCharts Blog

How to Create Stunning Charts in Polymer Applications

charts in Polymer JS Application
Polymer is a JavaScript library that assists in the development of custom components for a web page or web application. Web components are primitives at the lowest level. Because Polymer is built with web components, these components assist you in defining your own HTML elements. They also give you more flexibility by allowing you to define custom components. The defined custom components are also used in web pages in the same way that any other HTML component is, by using custom tags. In this post, you’ll learn about Polymer JS’s key features, how to create stunning Live Charts with Fusioncharts in Polymer Applications, and how to troubleshoot.

Key Features of Polymer JS

Polymer focuses on allowing you to create rich, powerful, reusable web components by defining custom elements. These could be used in web applications built using frameworks like Angular or Ember. Custom elements can be created using the dom-module element provided by Polymer. Custom element definition comprises of: Some key features of Polymer JS include:

Constructor

Registering an element associates a class with a custom element name. The class has a constructor that instantiates the associated custom element. Alternatively, document.createElement() can also be used to create and instantiate the element.

Populated with Internal DOM Within Each Instance

Shadow DOM provides a local, encapsulated DOM tree for your element. The Polymer can automatically create and populate a shadow tree for your element from a DOM template.

Styled with Internal Defaults or Externally

The <style></style> tags can be used as usual in the shadow Dom template of the custom elements. FusionCharts, being a JavaScript-based charting solution can be rendered in Polymer applications. We will define a custom element and register it to a class. The local DOM template of the custom element is holding the FusionCharts chart instance. The custom element will be used in another web page or DOM to render the chart. This custom element is reusable for multiple instances.

Prerequisites

Before you integrate FusionCharts with a Polymer JS application, you need to have a basic understanding of HTML, CSS, JavaScript, and Document Object Model (DOM).

Set the Environment

Before getting into the steps to render the FusionCharts Polymer JS application, let’s first set the environment in our local machine. Some of the basic requirements include:

Step 1

Step 2

This will create a bower.json file in the project working directory.

Step 3

Install Polymer using Bower in the project working directory. Execute the command as shown below: bower install --save Polymer/polymer This will install the Polymer JS in the bower_components folder within the TestPolymer project directory. Once you are done with the installation of Polymer JS, cross-check whether it has been perfectly installed: polymer help

Step 4

We are all set with the environment and ready to render FusionCharts in the Polymer JS application.

Polymer Custom-Element Within a Polymer JS Application

Step 1

Create an index.html file in the TestPolymer directory. Once done follow the steps mentioned below :      
<link rel="import" href="bower_components/polymer/polymer.html"></script>
Once we are done with the four steps mentioned above, our index.html file is ready with its code implementation.

Step 2

We are all set with our index.html file. Also, we have already mentioned the source code and the page content using the polymer-fusioncharts custom element. Now, let’s create the custom element we added previously: An optional <template> tag can also be added to the custom element if you want to provide any additional content to the file.
<template>
    <style>
    </style>
    <h4 align="center">Rendering the chart using the Polymer custom element(containing the chart)</h4>
  </template>
With this, the polymer-fusioncharts.html custom element is ready to be used in index.html. Therefore, you can import and add <polymer-fusioncharts></polymer-fusioncharts> which we have already discussed while creating the index.html file.

Step 3

The project is ready but to run the application. However, you have to start the Polymer server using the following command in the terminal: polymer serve The above command will start the server. To get the output on the screen, hit the URL displayed in the Cmder. Check out the final output after hitting the URL (http://127.0.0.1:8081): If you find any difficulty in rendering the chart or you see any error in your code, click here. You will be able to download the complete source code of the sample project we created for this tutorial.

Troubleshooting

If you can’t see the chart, check that:
Exit mobile version