C# Create a Piechart for the specified Hard Disk Drive Utilization

By Perry

This application can be used to generate a piechart for the hard disk drive utilization. You can provide any drive as input and it will generate the output folder wise utilization. You can see the given output. The code is lengthy so I have provided individual pieces of the code functions.

The namespaces used:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using System.Data;

The structure used:

struct MyDirTotalSize
        {
             public string dirPath;
             public long totalSize;
             public int parent;
        }
struct MyAngleStruct
        {
             public string folderpath;
             public float myAngle;
             public long mySize;
        }

The below function is responsible for the creating angle for each folder path:

private void GeneratePieChartAngle(string path)
        {
            MyAngleStruct angStructObj;
            angStructObj.myAngle = 0;
            MyDirTotalSize eff;
             long opteller = 0, compsize = 0;
             int startingPoint = -1, startingPoint2;
             for (startingPoint2 = 0; startingPoint2 < SizeDir.Count; startingPoint2++)
            {
                eff = (MyDirTotalSize) (SizeDir[startingPoint2]);
                 if (path != eff.dirPath) continue;
                compsize += eff.totalSize;
                angStructObj.folderpath = eff.dirPath;
                angStructObj.mySize = eff.totalSize;
                AngleS.Add(angStructObj);
                startingPoint = eff.parent;
             }

             foreach (MyDirTotalSize ef in SizeDir)
             {
                 if (ef.dirPath.CompareTo(path) == 0 || startingPoint < 0) continue;
                 if (!ef.dirPath.StartsWith(path)) continue;
                compsize += ef.totalSize;
                startingPoint2 = ef.parent;
                opteller += ef.totalSize;
                 if (startingPoint2 > (startingPoint + 1)) continue;
                angStructObj.folderpath = ef.dirPath;
                angStructObj.mySize = opteller;
                AngleS.Add(angStructObj);
                opteller = 0; //ef.size;
            }
            startingPoint = AngleS.Count;
             for (startingPoint2 = 0; startingPoint2 < startingPoint; startingPoint2++)
            {
                angStructObj = (MyAngleStruct) (AngleS[startingPoint2]);
                AngleS.RemoveAt(startingPoint2);
                angStructObj.myAngle = (angStructObj.mySize/(float) compsize)*360;
                AngleS.Insert(startingPoint2, angStructObj);
            }

        }
        
Get the total size of given directory folder:

private void GetDirveDirTotalSize(string path, int index)
        {

            MyDirTotalSize total1, total2;
            DirectoryInfo[] dirs;
            DirectoryInfo dir = new DirectoryInfo(path);
            dirs = dir.GetDirectories();
             foreach (DirectoryInfo run in dirs)
            {
                nAantalDirs = index + 1;
                GetDirveDirTotalSize(run.FullName, nAantalDirs);
            }
            total1.dirPath = path;
             long totalFileSizeInFolder=0;
            FileInfo[] files;
            DirectoryInfo file = new DirectoryInfo(path);
             files=file.GetFiles();
             foreach(FileInfo run in files)
               totalFileSizeInFolder+=run.Length;
            total1.totalSize = totalFileSizeInFolder;
            total1.parent = index;
            SizeDir.Add(total1);
            total2 = (MyDirTotalSize) SizeDir[(SizeDir.Count - 1)];
            nAantalDirs = total2.parent;
        }

Draw the piechart angles from starting to ending position and you are done.

private void DrawPitotal1romAngles()
        {
            float realAngle=0F;
            Random rnd = new Random();
             Red=Green=Blue=0;
             pas.Clear(BackColor);
             foreach(MyAngleStruct total1 in AngleS)
             {
                 pas.FillPie(brus,rRect,realAngle,total1.myAngle);realAngle+=total1.myAngle;
                 //Generate random color for drawing
                 int myRed = myGreen = myBlue = Random() % 250;
                brus.Color = System.Drawing.Color.FromArgb(myRed,myGreen,myBlue);
             }
         }


Output:





Popularity  (853 Views)
Create New Account
Article Discussion: C# : Create the piechart for the specified Hard Disk Drive Utilization
Perry posted at Saturday, December 06, 2008 2:57 AM
reply
Its my complete article copied there. I will inform Rob about this.
Perry replied to Stella Pandian at Tuesday, November 09, 2010 6:34 AM
end of post
reply