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 a new Webform
  • 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
  1. body  
  2. {  
  3.    background#00688B;  
  4.     color#fff;  
  5.     font300 100.1% "Helvetica Neue" , Helvetica"Arial Unicode MS" , Arialsans-serif;  
  6. }  
  7. #holder  
  8. {  
  9.     height442px;  
  10.     left: 42%;  
  11.     margin-240px 0 0 -320px;  
  12.     positionabsolute;  
  13.     top: 57%;  
  14.     width842px;  
  15.     font-sizex-large;  
  16.     background-color#008000;  
  17. }  
  18. #duplicate {  
  19.     bottom: 0;  
  20.     font300 .7em "Helvetica Neue"Helvetica, ;  
  21.     positionabsolute;  
  22.     right: 1em;  
  23.     text-alignright;  
  24. }  
  25. #duplicate d {  
  26.     color#fff;  
d2.css scripting
  1. body {  
  2.     background#fff;  
  3.     color#000;  
  4.     font100.1% "Comic Sans MS", Lucida, Verdanasans-serif;  
  5. }  
  6. #holder {  
  7.     height480px;  
  8.     left: 50%;  
  9.     margin0 0 0 -320px;  
  10.     positionabsolute;  
  11.     top: 0;  
  12.     width640px;  
  13. }  
  14. #duplicate {  
  15.     bottom: 0;  
  16.     font-size: .7em;  
  17.     positionabsolute;  
  18.     right: 1em;  
  19.     text-alignright;  
Step 3: In this part, we need to work on some JavaScript. To fully understanding how the JavaScript works download the attached .rar file and run the GrowingPie application. The whole JavaScript looks as in the following.
  1. window.onload = function ()  
  2. {  
  3. var f = Color("holder");  
  4. f.customAttributes.segment = function (x, y, f, dee1, dee2)  
  5. var boolean = (dee2 - dee1) > 180,  
  6. clr = (dee2 - dee1) / 360;  
  7. dee1 = (dee1 % 360) * Math.PI / 180;  
  8. dee2 = (dee2 % 360) * Math.PI / 180;  
  9.  return  
  10.     {  
  11.         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"]],  
  12.         fill: "hsb(" + clr + ", .75, .8)"  
  13.     };  
  14. };  
  15. function animate(ms)  
  16. {  
  17.     var begin = 0,  
  18.         val;  
  19.     for (i = 0; i < ii; i++)  
  20.       {  
  21.         val = 360 / total * data[i];  
  22.         paths[i].animate({ segment: [200, 200, 150, start, start += val] }, ms || 1500, "bounce");  
  23.         paths[i].angle = begin - val / 2;  
  24.       }  
  25. }  
  26.    var data = [24, 92, 24, 52, 78, 99, 82, 27],  
  27.    paths = f.set(),  
  28.    total,  
  29.    begin,  
  30.    bg = f.circle(200, 200, 0).attr({ stroke: "#fff""stroke-width": 4 });  
  31.    data = data.sort(function (a, b) { return b - a; });  
  32.    total = 0;  
  33.    for (var i = 0, ii = data.length; i < ii; i++)  
  34.     {  
  35.       total += data[i];  
  36.     }  
  37.   begin = 0;  
  38.   for (i = 0; i < ii; i++)  
  39.    {  
  40.      var val = 360 / total * data[i];  
  41.      (function (i, val)  
  42.       {  
  43.         paths.push(f.path().attr({ segment: [200, 200, 1, begin, begin + val], stroke: "#fff" }).click(function () {  
  44.         total += data[i];  
  45.         data[i] *= 2;  
  46.         animate();  
  47.         }));  
  48.         })(i, val);  
  49.     begin += val;  
  50.     }  
  51.     bg.animate({ f: 151 }, 1000, "bounce");  
  52.     animate(1000);  
  53. }; 
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.
  1. <body>  
  2.     <center><h2>Click the section of Pie-chart</h2></center>  
  3.     <hr />  
  4.         <div id="holder"></div>  
  5. </body> 
Step 5 : The Complete code for the GrowingPie application:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Chartgrowingpie.aspx.cs" Inherits="GrowingPie._Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5. <meta charset="utf-8">  
  6.         <title></title>  
  7.         <link rel="stylesheet" href="d1.css" media="screen">  
  8.         <link rel="stylesheet" href="d2.css" media="print">  
  9.         <style media="screen">  
  10.             #holder {  
  11.                 height: 400px;  
  12.                 margin: -200px 0 0 -200px;  
  13.                 width: 400px;  
  14.             }  
  15.         </style>  
  16.         <script src="javascript.js"></script>  
  17.         <script type="text/javascript">    
  18.           From Step 3 JavaScript copy from there and paste it here.  
  19.        </script>  
  20.     </head>  
  21.     <body>  
  22.     <center><h2>Click the section of Pie-chart</h2></center>  
  23.     <hr />  
  24.         <div id="holder"></div>  
  25.    </body>  
  26. </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.
 
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 some useful resources
 


Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles