Learn MVC Using Angular Pie Chart

Introduction

In this article, we will learn MVC, using Angular Pie Chart, which is used for two types of UI, which are given below.

  • Angular Knob Pie Chart.
  • Angular Easy Pie Chart.

Angular Knob

Output 1
MVC

Create MVC Project

Open Visual Studio 2015.
MVC

Go to New menu > click New and then project. Now, it will open New Project Window.
MVC

You can select ASP.NET Web Application on Framework 4.6. Enter the name of the project in the Solution name text box, then click the OK button.

MVC

One more Window should appear. Select the MVC template in this popup and click the OK button. Now, you can start to play.

Configure Angular Knob

We will download Angular Knob plugin from here.

Features

  • It works based on canvas.
  • You can implement keyboard events.
  • You can change UI touches, as well as mouse and mouse wheel events.
  • Overloads an input element.

Open the _Layout.cshtml and refer the min.js file in this view page.

<script src="~/Plugin/angular-knob/src/angular-knob.js"></script>
<script src="~/Plugin/jquery-knob/dist/jquery.knob.min.js"></script>

Open the Angular module and name its injection of Knob, ui-knob, as shown below:

var uiroute = angularmodule('uiroute', ['ui.router', 'ui.knob']);

Open the HTML page and set the design of your chart using the knob element and attribute.

<knob knob-data="knobLoaderData1" -options="knobLoaderOptions1" knob-max="100"> </knob>

Set the values for the attribute. Open the “angular controller” files and hard code an input or you may get and bind the values from the Server side.

$scope.knobLoaderData1 = 80;
$scope.knobLoaderOptions1 = {
            width: '50%', 
            displayInput: false,
            fgColor: '#0f5889'
        };
You have some option for change the pie Chart UI 
$scope.knobLoaderOptions2 = {
            width: '50%', 
            displayInput: true,
            fgColor: '#09a564',
            readOnly: true,
            lineCap: 'round'
        };

        $scope.knobLoaderOptions3 = {
            width: '50%', 
            displayInput: true,
            fgColor: '#a50958',
            displayPrevious: true,
            thickness: 0.1
        };

        $scope.knobLoaderOptions4 = {
            width: '50%', 
            displayInput: true,
            fgColor: '#a50909',
            bgColor: '#a54a09',
            angleOffset: -125,
            angleArc: 250
        };

Easy Pie Chart

Output 2

MVC

Yes, we have one more choice – Easy Pie Chart. Download the plugin here.

Features

  • Highly customizable.
  • Resolution independent.
  • Works on all the modern devices (Even in IE7).

Add the Easy Pie Chart plugin _Layout.cs to the HTML.

<script src="~/Plugin/jquery.easy-pie-chart/dist/angular.easypiechart.min.js"></script>

Inject as “’easypiechart’” into the Angular module.

var uiroute = angular.module('uiroute', ['ui.router', 'ui.knob', 'easypiechart']);

List the HTML attributes as follows:

<div easypiechart="" options="pieOptions" percent="piePercent1" class="easypie">

Load Attribute values in the Angular Controller files.

$scope.piePercent1 = 75;
$scope.pieOptions = {
            animate: {
                duration: 700,
                enabled: true
            },
            barColor: '#0e74b2',	 
            scaleColor: false,
            lineWidth: 10,
            lineCap: 'circle'
        };

For the output, I have written random functions, as shown below.

$scope.randomize = function (type) {
            if (type === 'easy') {
                $scope.piePercent1 = random();
                $scope.piePercent2 = random();
                $scope.piePercent3 = random();
                $scope.piePercent4 = random();
            }
            if (type === 'knob') {
                $scope.knobLoaderData1 = random();
                $scope.knobLoaderData2 = random();
                $scope.knobLoaderData3 = random();
                $scope.knobLoaderData4 = random();
            }
        }

        function random() {
            return Math.floor((Math.random() * 100) + 1);
        }

Call the function in the HTML button, which is based on Pie Chart types.

<button type="button" ng-click="randomize('knob')" class="btn btn-sm btn-warning">Randomize Knob</button>
<button type="button" ng-click="randomize('easy')" class="btn btn-sm btn-danger">Randomize EasyPie</button>

A Side by Side Comparison

Output 3

MVC

Note

  • Knob “Default” Pie Chart works based on your mouse wheel and mouse click.

  • Knob “Angle Offset and Arc” Pie chart works based on your mouse wheel, mouse click, and the keyboard itself.

To download source, click here.

Conclusion

In this article, we have learned about MVC, using Angular Pie Chart. If you have any queries, please tell me through the comments sectionsince your comments are very valuable.

Happy Coding!