Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Simple Graph Control Source

[Image: capture-1.gif]

Please note it only updates when you update a position! so if your not updating points it wont draw a new line

Code
//***Credits***
//Name: Jordan
//Alias: Coding4Noobs
//Website: www.emissarysolutions.org
//Application: eGraph
//Description: A simple graph control
//Version: 1.0
//***End Of Credits***
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Drawing.Design;
namespace E_Graph
{
    class EGraph : Control
    {
        public int[] Points = { 0, 0, 0, 0 };
        public int[] Points2 = { 0, 0, 0, 0 };
        public Color bgColor = Color.FromArgb(55, 55, 55);
        public int Spacing = 20;
        public bool EnableStatistics = false;
        private int Highest = 0;
        private int Total = 0;
        private int Total2 = 0;
        private int Highest2 = 0;
        private PictureBox Pic;
        public EGraph()
        {
            Pic = new PictureBox();
            this.Controls.Add(Pic);
            Pic.Dock = DockStyle.Fill;
            Pic.SizeMode = PictureBoxSizeMode.StretchImage;
            //nothing yet
            // DrawGraph();
        }
        /// <summary>
        /// Updates the next point on your graph
        /// </summary>
        /// <param name="newPoint">The new point</param>
        public void UpdatePoint(int newPoint, int newpoint2)
        {
            try
            {
                Points[0] = Points[1];
                Points[1] = Points[2];
                Points[2] = Points[3];
                Points[3] = newPoint;
                Points2[0] = Points2[1];
                Points2[1] = Points2[2];
                Points2[2] = Points2[3];
                Points2[3] = newpoint2;
                //if (newPoint > Highest)
                //{
                //    Highest = newPoint;
                //}
                //if (newpoint2 > Highest2)
                //{
                //    Highest2 = newpoint2;
                //}

                Highest = newPoint;
                Highest2 = newpoint2;
                Total2 += newpoint2;
                Total += newPoint;
                DrawGraph();
            }
            catch { }
        }
        //Draws the graph on the control
        public void DrawGraph()
        {
            new System.Threading.Thread(() =>
                {
                    try
                    {
                        Spacing = this.Width / 3;
                        Bitmap bmp = new Bitmap(this.Size.Width, this.Size.Height);
                        Graphics g = this.CreateGraphics();
                        g = Graphics.FromImage(bmp);
                        g.Clear(bgColor);
                        for (int i = 0; i < this.Width; i += 10)
                        {
                            g.DrawLine(Pens.DimGray, new Point(i, 0), new Point(i, this.Height));
                        }
                        for (int e = 0; e < this.Height; e += 10)
                        {
                            g.DrawLine(Pens.DimGray, new Point(0, e), new Point(this.Width, e));
                        }
                        if (EnableStatistics)
                        {
                            g.DrawString("Income /sec: " + Highest, new Font(DefaultFont.FontFamily, 8, FontStyle.Regular), Brushes.Red, new Point(4, this.Height - 16));
                            g.DrawString("Total Incomming: " + this.Total, new Font(DefaultFont.FontFamily, 8, FontStyle.Regular), Brushes.Red, new Point(4, this.Height - 32));
                            g.DrawString("Outgoing /sec: " + this.Highest2, new Font(DefaultFont.FontFamily, 8, FontStyle.Regular), Brushes.Blue, new Point(4, this.Height - 48));
                            g.DrawString("Total Out: " + this.Total2, new Font(DefaultFont.FontFamily, 8, FontStyle.Regular), Brushes.Blue, new Point(4, this.Height - 64));
                        }
                        g.DrawLine(Pens.Red, new Point(0, this.Height - Points[0]), new Point(Spacing, this.Height - Points[1]));
                        g.DrawLine(Pens.Red, new Point(Spacing, this.Height - Points[1]), new Point(Spacing * 2, this.Height - Points[2]));
                        g.DrawLine(Pens.Red, new Point(Spacing * 2, this.Height - Points[2]), new Point(Spacing * 3, this.Height - Points[3]));
                        Points[0] = Points[1];
                        Points[1] = Points[2];
                        Points[2] = Points[3];
                        g.DrawLine(Pens.Blue, new Point(0, this.Height - Points2[0]), new Point(Spacing, this.Height - Points2[1]));
                        g.DrawLine(Pens.Blue, new Point(Spacing, this.Height - Points2[1]), new Point(Spacing * 2, this.Height - Points2[2]));
                        g.DrawLine(Pens.Blue, new Point(Spacing * 2, this.Height - Points2[2]), new Point(Spacing * 3, this.Height - Points2[3]));
                        Points2[0] = Points2[1];
                        Points2[1] = Points2[2];
                        Points2[2] = Points2[3];
                        g.Dispose();
                        Pic.Image = bmp;
                    }
                    catch { }
                }).Start();
        }
    }
}

Usage:

Enable Statistic:
Code
eGraph1.EnableStatistics = true;
                eGraph1.DrawGraph();

Draw new Point:
Code
eGraph1.UpdatePoint(point1, point2);
Ahh I released something like this a while ago: https://hackforums.net/showthread.php?tid=699827

we use a similar method ^^
(Jun 9, 2012 01:22 PM)Modesty Wrote:  Ahh I released something like this a while ago: https://hackforums.net/showthread.php?tid=699827

we use a similar method ^^

nice didn't even see that, i through that together last night for {ES}. one of my customers suggested i do a statistics tab :P
(Jun 9, 2012 01:37 PM)Coding4Noobs Wrote:  nice didn't even see that, i through that together last night for {ES}. one of my customers suggested i do a statistics tab :P

Yeah aha we had it for clientmesh 1.0, but realised it soon became useless because it didn't have that much of a purpose lul
This is pretty cool, doubt I'll ever be using something like this tho :P


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Legends] by DystortionFeb 11, 2026 04:09 PM 4 154 Feb 11, 2026 04:09 PM
Last Post: Dystortion
[Python] by MiguelSanchezJan 30, 2022 01:36 AM 2 127 Jan 30, 2022 01:36 AM
Last Post: MiguelSanchez
[C] by georgeziozasDec 20, 2020 02:00 PM 0 142 Dec 20, 2020 02:00 PM
Last Post: georgeziozas