How to create chart in VB .NET Windows Forms Chart?
This KB article explains how to create a simple chart with title, legend, axes, and series in VB.Net WinForms Chart feature tour.
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)
I hope you enjoyed learning about how to create chart in VB .NET Windows Forms Chart.
You can refer to our WinForms Chart feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms Chart Documentation to understand how to manipulate data.
For current customers you can check out on our Winforms components from the License and Download page. If you are new to Syncfusion, you can try our 30-day free trial to check out our Winforms GridGroupingControl and other WinForms components.
If you have any queries or require clarifications, please let us know in the comment section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!