Sep 25, 2010 09:00 AM
(This post was last modified: Sep 26, 2010 01:46 PM by Modesty.)
What this does:
Know those graphs you get, like on the task manager showing CPU usage history, the horizontally scrolling ones which scroll to the left, each time a new point is plotted on the graph? Well heres how I managed to create something similar to that in VB.NET. unfortunately it uses timers, this is just a proof of concept.
The code is really self explanatory, and is not targetted at beginner users.
Place a Picturebox (reccommended size 400 width, 200 height, set the back color to black) and a Button on your form. The picturebox is where the graph will be displayed. Now add a timer with an interval of 1000ms.
Go to codeview, and enter these variables, just under "Public Class Form1"
Double click button1, and add this code:
Now double click the timer and add this code
Now press debug and watch it rawr :)
Screeny:
[img]http://i56.[censored].com/2nktppl.gif[/img]
thanks to Classical for making the gif^^
Note: The code contains some specifics to the size of my picturebox, such as the positions at which the points will be drawn. This was put intentionally to prevent newbs trying this out without having prior knowledge, or not wanting to learn. For the more experienced members, remmeber to adjust the array 'xs' so that the points shown fit in your picturebox.
Download Full project: http://www.mediafire.com/?5g26z39kr5hk412
Project was created in VS2010, so if you have 2008, just create a new project, delete the form1, and drag in the form1.vb in the download into your solution provider and it should add it.
Know those graphs you get, like on the task manager showing CPU usage history, the horizontally scrolling ones which scroll to the left, each time a new point is plotted on the graph? Well heres how I managed to create something similar to that in VB.NET. unfortunately it uses timers, this is just a proof of concept.
The code is really self explanatory, and is not targetted at beginner users.
Place a Picturebox (reccommended size 400 width, 200 height, set the back color to black) and a Button on your form. The picturebox is where the graph will be displayed. Now add a timer with an interval of 1000ms.
Go to codeview, and enter these variables, just under "Public Class Form1"
Code
Dim whichpoint As Integer = 1
Dim xs() As String = {0, 50, 100, 150, 200, 250, 300, 350} ' an array containing the positions at which each new point will be made
Dim ys() As String = {0, 1, 1, 1, 1, 1, 1, 1} ' starting y coords, set at 0Code
timer1.start()Now double click the timer and add this code
Code
If whichpoint <= xs.Length - 1 Then
Dim myPen As New System.Drawing.Pen(Brushes.Green, 3)
Dim formGraphics As System.Drawing.Graphics
formGraphics = PictureBox1.CreateGraphics()
formGraphics.DrawLine(myPen, CInt(xs(whichpoint - 1)), PictureBox1.Height - CInt(ys(whichpoint - 1)), CInt(xs(whichpoint)), PictureBox1.Height - CInt(ys(whichpoint)))
whichpoint += 1
Else
PictureBox1.Refresh() 'refresh picturebox to remove old graph
whichpoint = 1
Timer1.Stop()
Dim r As New Random() 'create a new random point that is going to be drawn
ys = {ys(1), ys(2), ys(3), ys(4), ys(5), ys(6), ys(7), r.Next(20, 150)} ' here we remove the first point of the old graph, add a new point to the end (this
'is currently a random number) and we redraw the graph as shown below. Then the timer is started again and this loops round,
'each time recreating the old graph withouit the first point and adding on a point.
Dim myPen As New System.Drawing.Pen(Brushes.Green, 3)
Dim formGraphics As System.Drawing.Graphics
formGraphics = PictureBox1.CreateGraphics()
formGraphics.DrawLine(myPen, CInt(xs(whichpoint - 1)), PictureBox1.Height - CInt(ys(whichpoint - 1)), CInt(xs(whichpoint)),PictureBox1.Height - CInt(ys(whichpoint)))
whichpoint += 1
formGraphics.DrawLine(myPen, CInt(xs(whichpoint - 1)), PictureBox1.Height - CInt(ys(whichpoint - 1)), CInt(xs(whichpoint)), PictureBox1.Height - CInt(ys(whichpoint)))
whichpoint += 1
formGraphics.DrawLine(myPen, CInt(xs(whichpoint - 1)), PictureBox1.Height - CInt(ys(whichpoint - 1)), CInt(xs(whichpoint)), PictureBox1.Height - CInt(ys(whichpoint)))
whichpoint += 1
formGraphics.DrawLine(myPen, CInt(xs(whichpoint - 1)), PictureBox1.Height - CInt(ys(whichpoint - 1)), CInt(xs(whichpoint)), PictureBox1.Height - CInt(ys(whichpoint)))
whichpoint += 1
formGraphics.DrawLine(myPen, CInt(xs(whichpoint - 1)), PictureBox1.Height - CInt(ys(whichpoint - 1)), CInt(xs(whichpoint)), PictureBox1.Height - CInt(ys(whichpoint)))
whichpoint += 1
formGraphics.DrawLine(myPen, CInt(xs(whichpoint - 1)), PictureBox1.Height - CInt(ys(whichpoint - 1)), CInt(xs(whichpoint)), PictureBox1.Height - CInt(ys(whichpoint)))
whichpoint += 1
formGraphics.DrawLine(myPen, CInt(xs(whichpoint - 1)), PictureBox1.Height - CInt(ys(whichpoint - 1)), CInt(xs(whichpoint)), PictureBox1.Height - CInt(ys(whichpoint)))
whichpoint += 1
Timer1.Start()
End IfNow press debug and watch it rawr :)
Screeny:
[img]http://i56.[censored].com/2nktppl.gif[/img]
thanks to Classical for making the gif^^
Note: The code contains some specifics to the size of my picturebox, such as the positions at which the points will be drawn. This was put intentionally to prevent newbs trying this out without having prior knowledge, or not wanting to learn. For the more experienced members, remmeber to adjust the array 'xs' so that the points shown fit in your picturebox.
Download Full project: http://www.mediafire.com/?5g26z39kr5hk412
Project was created in VS2010, so if you have 2008, just create a new project, delete the form1, and drag in the form1.vb in the download into your solution provider and it should add it.

