Articles in this section
Category / Section

How to create chart in VB .NET Windows Forms?

4 mins read

This KB article explains how to create a simple chart with title, legend, axes, and series in VB.Net Windows Forms.

Step 1:

Create a required DataModel for the chart.

Public Class Person
    Public Property Name As String
    Public Property Height As Double
End Class

 

Step 2:

Create a BindingList from the DataModel for the series datapoints.

Dim dataSource As BindingList(Of Person) = New BindingList(Of Person)
dataSource.Add(New Person With {.Name = "David", .Height = 180})
dataSource.Add(New Person With {.Name = "Michael", .Height = 170})
dataSource.Add(New Person With {.Name = "Steve", .Height = 160})
dataSource.Add(New Person With {.Name = "Joel", .Height = 182})

 

Step 3:

Initialize the chart with title by adding the ChartTitle instance to the Titles collection of the chart.

Dim Chart = New ChartControl()
Dim chartTitle = New ChartTitle With {.Text = "Height Report"}
Chart.Titles.Add(chartTitle)

 

Step 4:

Initialize the required axes by using ValueType property of PrimaryAxis and SecondaryAxis in the chart.

Chart.PrimaryXAxis.Title = "Name"
Chart.PrimaryXAxis.ValueType = ChartValueType.Category
 
Chart.PrimaryYAxis.Title = "Height"
Chart.PrimaryYAxis.ValueType = ChartValueType.Double

 

Step 5:

Create a data bind model for series by using CategoryAxisDataBindModel class and the defined BindingList. Then, mapping the respective data paths to CategoryName and YNames properties of data bind model.

Dim dataSeriesModel As CategoryAxisDataBindModel = New CategoryAxisDataBindModel(dataSource)
dataSeriesModel.CategoryName = "Name"
dataSeriesModel.YNames = New String() {"Height"} 

 

Step 6:

Define the required type of series by using the Type property of the series and set the datamodel created in the step 5 for the series by using the CategoryModel property. The Legend is generated by default and the legend label for corresponding series can be set by passing the required string in the series constructor.

Dim chartSeries As ChartSeries = New ChartSeries("Heights")
chartSeries.Type = ChartSeriesType.Column
chartSeries.CategoryModel = dataSeriesModel
Chart.Series.Add(chartSeries)

 

The following code snippet gives you the consolidated configuration of all the above codes in creating a simple chart.

Code snippet:

'Defining the ChartControl.
Dim Chart = New ChartControl()
 
'Defining the datasource for chart.
Dim dataSource As BindingList(Of Person) = New BindingList(Of Person)
dataSource.Add(New Person With {.Name = "David", .Height = 180})
dataSource.Add(New Person With {.Name = "Michael", .Height = 170})
dataSource.Add(New Person With {.Name = "Steve", .Height = 160})
dataSource.Add(New Person With {.Name = "Joel", .Height = 182})
 
Dim dataSeriesModel As CategoryAxisDataBindModel = New CategoryAxisDataBindModel(dataSource)
dataSeriesModel.CategoryName = "Name"
dataSeriesModel.YNames = New String() {"Height"}
 
'Setting the title for the chart.
Dim chartTitle = New ChartTitle With {.Text = "Height Report"}
Chart.Titles.Add(chartTitle)
 
'Defining the axes for the chart.
Chart.PrimaryXAxis.Title = "Name"
Chart.PrimaryXAxis.ValueType = ChartValueType.Category
 
Chart.PrimaryYAxis.Title = "Height"
Chart.PrimaryYAxis.ValueType = ChartValueType.Double
 
'Defining the series for the chart.
Dim chartSeries As ChartSeries = New ChartSeries("Heights")
chartSeries.Type = ChartSeriesType.Column
chartSeries.CategoryModel = dataSeriesModel
Chart.Series.Add(chartSeries)

 

See Also:

Create chart in WPF application using XAML

Create chart in C# WPF

Create pie chart in C# WPF

Create chart in VB .NET WPF

Create chart in Xamarin.Forms

Output

Create Simple Chart in Windows Forms VB.Net

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied