Working With Growing Pie-chart Using HTML 5

Introduction

In this article we have a very interesting thing related to design. We will learn how to use JavaScript, CSS and HTML to make a pie-chart that grows when you click on any section of it.

Here we will use some JavaScript and some styles along with the HTML code. Just go through the steps to see how to create this application.

Let's see how the GrowingPie application can be created. To do so go through the following steps.

Step 1 : Open a HTML editor or Visual Studio.

sd.gif

Open File menu ->select new ->Choose Website then.

0000.jpg

This is where we will create the HTML5 application.

  • Go to Solution Explorer
  • Right-click on the Application name
  • Select Add-->add new item
  • Now in the window that opens, select an HTML page or new Web form
  • Rename it to Chartgrowingpie.aspx

gc3.gif

Step 2 : In this section we will create the style for the media and create the .css on the media screen. Put the given script in the Head section of the HTML or in between the <head>--</head> tags.

Here two CSS files are used for design purposes; one is for the section div, named d1.css and the other d2.css for the whole body of the web page.

d1.css scripting

body
{
   background: #00688B;
    color: #fff;
    font: 300 100.1% "Helvetica Neue" , Helvetica, "Arial Unicode MS" , Arial, sans-serif;
}
#holder
{
    height: 442px;
    left: 42%;
    margin: -240px 0 0 -320px;
    position: absolute;
    top: 57%;
    width: 842px;
    font-size: x-large;
    background-color: #008000;
}
#duplicate {
    bottom: 0;
    font: 300 .7em "Helvetica Neue", Helvetica, ;
    position: absolute;
    right: 1em;
    text-align: right;
}
#duplicate d {
    color: #fff;
}


d2.css scripting.

body {
    background: #fff;
    color: #000;
    font: 100.1% "Comic Sans MS", Lucida, Verdana, sans-serif;
}
#holder {
    height: 480px;
    left: 50%;
    margin: 0 0 0 -320px;
    position: absolute;
    top: 0;
    width: 640px;
}
#duplicate {
    bottom: 0;
    font-size: .7em;
    position: absolute;
    right: 1em;
    text-align: right;
}

Step 3 : In this part we need to work on some JavaScript. For fully understanding how the JavaScript works download the attached .rar file and run the GrowingPie application.

The whole JavaScript looks as in the following.

window.onload = function ()
              {
                var f = Color("holder");
                f.customAttributes.segment = function (x, y, f, dee1, dee2)
                var boolean = (dee2 - dee1) > 180,
                clr = (dee2 - dee1) / 360;
                dee1 = (dee1 % 360) * Math.PI / 180;
                dee2 = (dee2 % 360) * Math.PI / 180;
                 return
                    {
                        path: [["M", x, y], ["l", f * Math.cos(dee1), f * Math.sin(dee1)], ["A", f, f, 0, +boolean, 1, x + f * Math.cos(dee2), y + f * Math.sin(dee2)], ["z"]],
                        fill: "hsb(" + clr + ", .75, .8)"
                    };
               };
                function animate(ms)
                {
                    var begin = 0,
                        val;
                    for (i = 0; i < ii; i++)
                      {
                        val = 360 / total * data[i];
                        paths[i].animate({ segment: [200, 200, 150, start, start += val] }, ms || 1500, "bounce");
                        paths[i].angle = begin - val / 2;
                      }
                }
                   var data = [24, 92, 24, 52, 78, 99, 82, 27],
                   paths = f.set(),
                   total,
                   begin,
                   bg = f.circle(200, 200, 0).attr({ stroke: "#fff", "stroke-width": 4 });
                   data = data.sort(function (a, b) { return b - a; });
                   total = 0;
                   for (var i = 0, ii = data.length; i < ii; i++)
                    {
                      total += data[i];
                    }
                  begin = 0;
                  for (i = 0; i < ii; i++)
                   {
                     var val = 360 / total * data[i];
                     (function (i, val)
                      {
                        paths.push(f.path().attr({ segment: [200, 200, 1, begin, begin + val], stroke: "#fff" }).click(function () {
                        total += data[i];
                        data[i] *= 2;
                        animate();
                        }));
                        })(i, val);
                    begin += val;
                    }
                    bg.animate({ f: 151 }, 1000, "bounce");
                    animate(1000);
               };

Step 4 : In this section we are going to become familiar with the body part of HTML scripting. Replace this script from the body section of the Chartgrowingpie.aspx page. Here we pass a #holder in the div id that is defined in the d1.css file.

<body>
    <center><h2>Click the section of Pie-chart</h2></center>
    <hr />
        <div id="holder"></div>
   </body>


Step 5 : The Complete code for the
GrowingPie application:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Chartgrowingpie.aspx.cs" Inherits="GrowingPie._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8">
        <title></title>
        <link rel="stylesheet" href="d1.css" media="screen">
        <link rel="stylesheet" href="d2.css" media="print">
        <style media="screen">
            #holder {
                height: 400px;
                margin: -200px 0 0 -200px;
                width: 400px;
            }
        </style>
        <script src="javascript.js"></script>
        <script type="text/javascript">  
         
From Step 3 JavaScript copy from there and paste it here.
       </script>
    </head>
    <body>
    <center><h2>Click the section of Pie-chart</h2></center>
    <hr />
        <div id="holder"></div>
   </body>
</html>

Step 6 : Output Press F5

Note :
For the accurate output of HTML5 applications, you must have the Google Chrome browser in your PC. You will see here
a pie-chart that grows when you click on any section of the pie-chart.

gc.gif 

After continuing to click on the same section again and again you will see the section enlarges as shown in the figure.

gc1.gif

After continuing to click on the same section again and again you will see the section enlarges as shown in the figure.

gc4.gif

Here are the some useful resources

Working With Pie Chart in HTML5
Pie Chart in WPF
Pie Chart in Silverlight
Creating Exploded Pie Chart Having Click Through Functionality in C#
QC DATA PULLER using C# 

Up Next
    Ebook Download
    View all
    Learn
    View all