How to create a chart in VB .NET Windows Forms?
This KB article explains how to create a simple chart with a title, legend, axes, and series in VB.NET using the Syncfusion® WinForms Chart.
Step 1:
Create a required DataModel for the chart.
Public Class Person
Public Property Name As String
Public Property Height As Double
End ClassStep 2:
Create a BindingList from the DataModel for 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 = "Joel", .Height = 182})
dataSource.Add(New Person With {.Name = "Michael", .Height = 170})
dataSource.Add(New Person With {.Name = "Steve", .Height = 160})Step 3:
Initialize the chart with a 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 the ValueType property of PrimaryXAxis and PrimaryYAxis in the chart.
Chart.PrimaryXAxis.Title = "Name"
Chart.PrimaryXAxis.ValueType = ChartValueType.Category
Chart.PrimaryYAxis.Title = "Height"
Chart.PrimaryYAxis.ValueType = ChartValueType.DoubleStep 5:
Create a data bind model for series by using CategoryAxisDataBindModel class and the defined BindingList. Then, map the respective data paths to the 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 using the Type property of the series and set the data model 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 into the series constructor.
'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 = "Joel", .Height = 182})
dataSource.Add(New Person With {.Name = "Michael", .Height = 170})
dataSource.Add(New Person With {.Name = "Steve", .Height = 160})
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)
Conclusion
I hope you enjoyed learning about how to create a 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 and documentation, and how to quickly get started with configuration specifications. You can also explore our WinForms Chart examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!