Home All Groups Group Topic Archive Search About
Author
20 Sep 2006 3:42 PM
Rohan
Hello I need major help,
I am trying to make a plane to create a graph,  I have been working on
it for a day now  and I can't figure it out, the problem is that one
of the drawstring doesn't write the number under the lines and one of
the line, for some reason, it is not proportional !!
Thank you,

------------------------Start User control--------------------------

Imports System.ComponentModel
Imports System.Drawing
Public Class graph

    Public Structure Xcord
        Public x As Integer
        Public xMin As Integer
        Public xMax As Integer
    End Structure

    Public xGridRes As Integer = 1
    Public yGridRes As Integer = 1

    Dim Points() As PointF

    Public y As Integer
    Dim xcordnate As Xcord
    <Category("Graph"), Browsable(True), Description("Set xScale"),
DefaultValue(5)> _
    Public Property xScale() As Integer
        Get
            Return xGridRes
        End Get
        Set(ByVal value As Integer)
            xGridRes = value
        End Set
    End Property
    <Category("Graph"), Browsable(True), Description("Set yScale"),
DefaultValue(5)> _
    Public Property yScale() As Integer
        Get
            Return yGridRes
        End Get
        Set(ByVal value As Integer)
            yGridRes = value
        End Set
    End Property

    Private Sub graph_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim xPen As New Pen(Color.Blue)
        Dim PrimaryLine As New Pen(Color.Blue, 2)
        Dim xcounter As Integer = 0
        Dim WinRect As Rectangle = New Rectangle(0, 0, Me.Width - 1,
Me.Height - 1)
        Dim Margin As Integer = 25
        Dim XLabelCounter As Integer = xGridRes
        Dim YLabelCounter As Integer = yGridRes
        Dim xRes, yRes As Integer
        xRes = Me.Height / xGridRes
        yRes = Me.Width / yGridRes
        Dim Width As Integer = Me.Width
        Dim Height As Integer = Me.Height

        e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        'Create Boarder Rect
        e.Graphics.DrawRectangle(Pens.Black, WinRect)

        'Create Primary lines
        e.Graphics.DrawLine(PrimaryLine, Margin, Height - Margin,
Margin, 0)
        e.Graphics.DrawLine(PrimaryLine, Margin, Height - Margin,
Width, Height - Margin)

        'Create Horizontal lines,
        Dim xLabel As Integer

        xLabel = (Height - Margin) / XLabelCounter

        For xcounter = (Height - Margin) To 0 Step -xRes
            xLabel = xLabel - System.Math.Abs(XLabelCounter)
            e.Graphics.DrawLine(xPen, Margin, xcounter, Width,
xcounter)
            e.Graphics.DrawString(CStr(xLabel), New Font("Verdana", 5),
Brushes.AliceBlue, xcounter + Margin, Height - Margin)
        Next

        'Create Vertical lines !

        xLabel = (Width - Margin) / YLabelCounter
        For xcounter = (Width - Margin) To 0 Step -yRes
            xLabel = xLabel + System.Math.Abs(YLabelCounter)
            e.Graphics.DrawLine(xPen, Margin + xcounter, 0, xcounter +
Margin, Height - Margin)
            e.Graphics.DrawString(CStr(xLabel), New Font("Verdana", 5),
Brushes.AliceBlue, 10, xcounter - (Height / 2))
        Next

        e.Graphics.Flush()

    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
End Class

Author
20 Sep 2006 5:06 PM
IdleBrain
Rohanji,
I suggest you to use this graph control instead:
http://zedgraph.org/wiki/index.php?title=Main_Page

Its one of the best controls ever developed.

Hope it helps.