Implement a Graph from ASCII Data to Chart Windows Form Control using VB.NET

The example below is a simple way on how to Plot a Graph in Chart Windows Form Control using VB.Net.

Implement Graph on Chart Windows Form Control

Requirements:
1. Microsoft Visual Studio (In my case, I'm using Visual Studio 2010 Express Edition).

To Begin with:
1. Start your Microsoft Visual Basic 2010 Express.
2. In the New Project window, Choose "Windows Form Application" and name it to whatever you like or something like "Plot Graph" then Click "OK".
3. You must now be able to see the Form. On the toolbox Add the following controls onto the form and update its properties.
     a. Form: Name:=frmGraph
     b. (1) Chart: Name:=Chart1
     c. (1) Textbox: Name:=txtData, Multiline:=True, Text:=30;52;57;57;68;93;129;173;209;232;240;232;217;196;169;141;116;98;86;80;78;76;76;79;81;83;86;91;95;97;95;93;95;99;103;105;106;107;110;116;120;122;123;124;127;132;137;139;137;136;132;126;124;122;117;113;110;105;97
     d. (1) Button: Name:=btnPlot, Text:=&Plot

*Then arrange and re-size the controls as you can see below. But you can still orient it to whatever you want.

Form Controls Orientation

4. Now Press F7 to view the code editor window.

Code Blocks:

:namespace - We need to declare the namespace containing the methods and properties of the Chart Windows Form Control in order to use its properties and methods.

Imports System.Windows.Forms.DataVisualization.Charting

:frmGraph_Load() - When the form is loaded we do not want to see the Gridlines behind the plot so we'll disable it by setting the MajorGrid Property of the ChartAreas X and Y Axis Enabled to False and set it background to transparent.

    Private Sub frmGraph_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With Chart1.ChartAreas(0)
            .AxisX.MajorGrid.Enabled = False
            .AxisY.MajorGrid.Enabled = False
            .AxisX.MajorGrid.LineColor = Color.Coral
            .AxisY.MajorGrid.LineColor = Color.Coral
            .BackColor = Color.Transparent
        End With
    End Sub

:btnPlot_Click() - When the Plot button is clicked, we declare an Array and 2 collections of series values.
        CD = An array collection that holds the data we split from the data Textbox.       
        s1 = Is our first series collection
        s2 = is our second series collection (Both have the same values since we only need to plot the Points and a Line.

    Private Sub btnPlot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlot.Click
        Dim CD As Array
        Dim s1 As New Series
        Dim s2 As New Series
        Dim xLab As Long

        'GET data from the textbox
        CD = Split(txtData.Text, ";", -1)

        Chart1.Series.Clear()

        s1.Name = "Series 1"
        s1.ChartType = SeriesChartType.FastPoint
        s2.Name = "Series 2"
        s2.ChartType = SeriesChartType.FastLine

        xLab = 1
        'loop through each array and set series values
        For Each d In CD
            s1.Points.AddXY(xLab, d)
            s2.Points.AddXY(xLab, d)
            xLab = xLab + 1
        Next

        'Add update the chart with the series values
        Chart1.Series.Add(s1)
        Chart1.Series.Add(s2)

    End Sub

Full Code:

Imports System.Windows.Forms.DataVisualization.Charting

Public Class frmGraph

    Private Sub frmGraph_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With Chart1.ChartAreas(0)
            .AxisX.MajorGrid.Enabled = False
            .AxisY.MajorGrid.Enabled = False
            .AxisX.MajorGrid.LineColor = Color.Coral
            .AxisY.MajorGrid.LineColor = Color.Coral
            .BackColor = Color.Transparent
        End With
    End Sub
    
    Private Sub btnPlot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlot.Click
        Dim CD As Array
        Dim s1 As New Series
        Dim s2 As New Series
        Dim xLab As Long

        'GET data from the textbox
        CD = Split(txtData.Text, ";", -1)

        Chart1.Series.Clear()

        s1.Name = "Series 1"
        s1.ChartType = SeriesChartType.FastPoint
        s2.Name = "Series 2"
        s2.ChartType = SeriesChartType.FastLine

        xLab = 1
        'loop through each array and set series values
        For Each d In CD
            s1.Points.AddXY(xLab, d)
            s2.Points.AddXY(xLab, d)
            xLab = xLab + 1
        Next

        'Add update the chart with the series values
        Chart1.Series.Add(s1)
        Chart1.Series.Add(s2)

    End Sub
End Class

5. Finally, build and run the project.

Comments

Popular posts from this blog

How to Create a Configuration.INI Files in VB6

How to Make Windows Form Transparent using Visual Basic 6/VB6

How to Set Windows Form Always on Top of Other Applications in VB6