Click here to Skip to main content
15,896,111 members
Articles / Web Development / ASP.NET

Graph Library

Rate me:
Please Sign up or sign in to vote.
4.84/5 (43 votes)
10 Apr 2008CPOL3 min read 141.8K   7K   155  
This article addresses the construction of a simple graph library using C#.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Anoop.Graph;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace GraphV1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void ui_btn_Show_Click(object sender, EventArgs e)
        {
           
            string [] keyValue=new string[ui_lbx_DataKey.Items.Count];
            float[] values = new float[ui_lbx_DataKey.Items.Count] ;
            for (int i = 0; i < ui_lbx_DataKey.Items.Count; i++)
            {
                keyValue[i] = ui_lbx_DataKey.Items[i].ToString();
                values[i] = float.Parse(ui_lbx_DataValue.Items[i].ToString());
            }
            DrawGraph bargraph = new DrawGraph(keyValue, values, ui_txt_xlabel.Text, ui_txt_ylabel.Text, "Courier", 255);
            p1.Image = bargraph.DrawBarGraph();
            p2.Image = bargraph.Draw3dBarGraph();
            p3.Image = bargraph.DrawPieGraph();
            p4.Image = bargraph.Draw3DPieGraph();
            float [] newValues=new float[]{5,10,6,3,9,7};
            string[] newXValues = new string[] { "2000", "2001", "2002", "2003", "2004", "2005"};
            p6.Image = bargraph.DrawBarLineGraph(newValues,newXValues);


            DrawGraph bargraph1 = new DrawGraph(keyValue, values, ui_txt_xlabel.Text, ui_txt_ylabel.Text, "Courier", 255);
            p5.Image = bargraph1.DrawLineGraph();
            

             
        }

        private void ui_btn_Add_Click(object sender, EventArgs e)
        {
            ui_lbx_DataKey.Items.Add(ui_txt_DataKey.Text);
            ui_lbx_DataValue.Items.Add(ui_txt_DataValue.Text);
            ui_txt_DataKey.Text = string.Empty;
            ui_txt_DataValue.Text = string.Empty;
        }

        private void ui_btn_Clear_Click(object sender, EventArgs e)
        {
            ui_lbx_DataKey.Items.Clear();
            ui_lbx_DataValue.Items.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Bitmap b = new Bitmap(300, 300);
            Graphics objGraphics=Graphics.FromImage(b);
            int iLoop, iLoop2;
			
            // Create location and size of ellipse.
			int x  = 50;
			int y  = 120;
			int width  = 200;
			int height = 100;
			
            // Create start and sweep angles.
			int startAngle = 0;
            		
            SolidBrush objBrush = new SolidBrush(Color.Aqua);
			Random rand = new Random();
            int sweepAngle = rand.Next(45); 

			objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
			
			//Loop through 180 back around to 315 degress so it gets drawn correctly.
            for (iLoop = 0; iLoop <= 315; iLoop += sweepAngle)
            {
                startAngle = (iLoop + 180) % 360;

                objBrush.Color = Color.FromArgb(rand.Next(255), rand.Next(255), rand.Next(255));
                // On degrees from 0 to 180 draw 10 Hatched brush slices to show depth
                if ((startAngle < 135) || (startAngle == 180))
                {
                    for (iLoop2 = 0; iLoop2 < 10; iLoop2++)
                        objGraphics.FillPie(new HatchBrush(HatchStyle.Percent50, objBrush.Color), x, y + iLoop2, width, height, startAngle, sweepAngle);

                }
                // Displace this pie slice from pie.
                //if (startAngle == 135)
                //{
                //    // Show Depth
                //    for (iLoop2 = 0; iLoop2 < 10; iLoop2++)
                //        objGraphics.FillPie(new HatchBrush(HatchStyle.Percent50, objBrush.Color), x - 30, y + iLoop2 + 15, width, height, startAngle, sweepAngle);

                //    objGraphics.FillPie(objBrush, x - 30, y + 15, width, height, startAngle, sweepAngle);
                //}
                //else // Draw normally
                    objGraphics.FillPie(objBrush, x, y, width, height, startAngle, sweepAngle);

                sweepAngle = rand.Next(45);
            }

            p5.Image = b;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            //    DrawLineGraph()

            //p5.Image = objgraph;

           
        }

        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
Anoop Unnikrishnan is associated with a CMM Level 5 Company. He has done his Bachelor of Engineering in Information Science. His certifications include OCA,IBM SOA Associate, MCAD, MCTS and MCPD. Currently persuing MBA.

He is working on .NET since first Beta versions. He has also published his book "Start Programming with C#".

Grab a copy from www.pothi.com/pothi/book/anoop-unnikrishnan-start-programming-c

Anoop can be reached : anoopukrish@gmail.com

Comments and Discussions