Pie chart in AngularJS using ChartJS

Pie chart in AngularJS using ChartJS

A set of data are better understood when represented graphically, there are various ways to represent data, out which Pie Chart is one of them. The chart can be implemented using AngularJS.

AngularJS uses Chart.js as a dependency to create given chart, which imparts responsiveness and provides various other flexibility, which we’ll see them going further. Other implementation of Chart are

  1. Doughnut Chart
  2. Bar Chart
  3. Horizontal Bar Chart
  4. Line Chart

Environment Set:

Dependencies:

  1. angular.min.js
  2. angular-chart.min.js
  3. chart.js

These files need to be included in page as-

<script src="angular.min.js"></script>
<script src="Chart.min.js"></script>
<script src="angular-chart.js"></script>

After including the files in page, one must add chart.js in module dependency, as shown below

angular.module('pieChart', ['chart.js']);

Directives of AngularJS charts:

[table “” not found /]

Implementation of Pie chart:

Let’s see the implementation of Pie Chart:

index.html

<html ng-app="angularChartApp">

<head>
    <title>AngularJS Chart</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
</head>

<body class="container-fluid ">
<div class="panel panel-success">
    <div class="panel-heading">
        <div class="panel-title text-center">AngularJS Charts</div>
    </div>
    <div class="panel-body container-fluid">
        <section>
            <div class="row well">
                <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
                    <section ng-controller="angularPieChartController" class="">
                        <div class="row lead">
                            <div class="col-md-12 text-center">Pie Chart</div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <canvas id="pie" class="chart chart-pie" chart-data="data" 
                                        chart-labels="labels" chart-options="optionsPie" 
                                        chart-colors="colorsPie" chart-options="optionsPie" 
                                        chart-click="clickme" chart-hover="hoverme" 
                                        chart-dataset-override="PieDataSetOverride">
                                </canvas>
                            </div>
                        </div>
                    </section>
                </div>
            </div>
        </section>
    </div>
</div>
</body>
<script src="Controllers/Chart.js"></script>
<script src="Controllers/angular-chart.min.js"></script>
<script src="Controllers/chartController.js"></script>

</html>

chartController.js

var app = angular.module('angularChartApp', ['chart.js']); 

app.controller('angularPieChartController', function ($scope) {
    $scope.labels = ["Internet bill", "School fee", "House budget"];
    $scope.data = [100,300,700];
    //Make sure to use color codes, instead of color name.
    $scope.colorsPie = ['#90EE90', '#FF6600', '#8080FF']; 
    //PieDataSetOverride is used to draw lines to display the labels

    $scope.PieDataSetOverride = [{ yAxisID: 'y-axis-1' }]; //y-axis-1 is the ID defined in scales under options.

    $scope.optionsPie = {
        legend: { display: true },
        responsive: true,  // set to false to remove responsiveness. Default responsive value is true.
        scales: {
            yAxes: [
              {
                  id: 'y-axis-1',
                  type: 'linear',
                  display: true,
                  position: 'left'
              }]
        }
    }

    $scope.clickme = function($event){
        alert("You've clicked upon "+$event[0]._view.label);
    }

    $scope.hoverme = function ($event) {
        alert("You hovered over " + $event[0]._view.label);
    }       
});

Output:

Pie Chart

We can clearly see how a Pie chart is rendered. Depending upon requirement we can make successive changes. There are few thing one should keep note while making changes

  • chart-colors: This directive is not mandatory, when removed a color is automatically assigned to each section. However if used then one need to ensure to use only color code but not names.

Stay tuned for more updates!

Leave a Reply

Your email address will not be published. Required fields are marked *