Getting Started with Windows Forms Chart

18 Nov 201813 minutes to read

This section explains the steps required to populate the Windows Forms Chart control with data and configure a title, data labels, a legend, tooltips, and a built-in skin. This section covers only the minimal features that you need to learn to get started with the chart.

Add chart reference

Refer to the Control Dependencies section to learn about the assemblies or NuGet packages required to use the Chart control in a Windows Forms application.

For information about installing NuGet packages, refer to Install NuGet packages in a Windows Forms application.

Creating application with chart control

This section explains how to add the chart control through the designer and code.

Adding control via designer

The chart control can be added through the designer by following the given steps:

Step 1: Create a new Windows Forms Application in Visual Studio.

Step 2: Drag and drop the ChartControl from the toolbox into the designer page.

Chart Started

Chart Started

Step 3: After dropping the chart, the ChartWizard will be opened. You can set up the chart configuration during design-time.

Chart Started

Step 4: The appearance and behavior of the chart can be controlled by setting the appropriate properties in the Properties window.

For example, align the legend at the top by changing the LegendPosition property.

Chart Started

Adding control via code

To add the chart control manually in C#, follow the given steps:

Step 1: Create a new Windows Forms Application in Visual Studio.

Step 2: Add the following required assembly references to the project:

  • Syncfusion.Chart.Windows.dll
  • Syncfusion.Chart.Base.dll
  • Syncfusion.Chart.Windows
  • Syncfusion.Shared.Base

Step 3: Include the namespace Syncfusion.Windows.Forms.Chart.

using Syncfusion.Windows.Forms.Chart;
Imports Syncfusion.Windows.Forms.Chart

Step 4: Create the ChartControl control instance and add it to the form.

ChartControl chartControl = new ChartControl();
chartControl.Dock = DockStyle.Fill;
this.Controls.Add(chartControl);
Dim chartControl As ChartControl = New ChartControl
chartControl.Dock = DockStyle.Fill
Me.Controls.Add(chartControl)

Populate chart with data

This section visualizes product sales data using a BindingList.

Define a data model

Define a data model that represents a sales data point.

public class SalesData
{
    private string year;
    private double sales;

    public string Year
    {
        get { return year; }
        set { year = value; }
    }

    public double Sales
    {
        get { return sales; }
        set { sales = value; }
    }

    public SalesData(string year, double sales)
    {
        this.Year = year;
        this.Sales = sales;
    }
}
Public Class SalesData

    Private xValue As String
    Private yValue As Double

    Public Property Year As String
        Get
            Return xValue
        End Get
        Set(ByVal value As String)
            xValue = value
        End Set
    End Property

    Public Property Sales As Double
        Get
            Return yValue
        End Get
        Set(ByVal value As Double)
            yValue = value
        End Set
    End Property

    Public Sub New(ByVal year As String, ByVal sales As Double)
        Me.Year = year
        Me.Sales = sales
    End Sub

End Class

Create a data source

Create a BindingList and populate it with instances of the SalesData model.

BindingList<SalesData> dataSource = new BindingList<SalesData>();

dataSource.Add(new SalesData("1999", 3));
dataSource.Add(new SalesData("2000", 7));
dataSource.Add(new SalesData("2001", 12));
dataSource.Add(new SalesData("2002", 18));
dataSource.Add(new SalesData("2003", 22));
dataSource.Add(new SalesData("2004", 30));
dataSource.Add(new SalesData("2005", 40));
dataSource.Add(new SalesData("2006", 50));
dataSource.Add(new SalesData("2007", 65));
dataSource.Add(new SalesData("2008", 75));
Dim dataSource As New BindingList(Of SalesData)()

dataSource.Add(New SalesData("1999", 3))
dataSource.Add(New SalesData("2000", 7))
dataSource.Add(New SalesData("2001", 12))
dataSource.Add(New SalesData("2002", 18))
dataSource.Add(New SalesData("2003", 22))
dataSource.Add(New SalesData("2004", 30))
dataSource.Add(New SalesData("2005", 40))
dataSource.Add(New SalesData("2006", 50))
dataSource.Add(New SalesData("2007", 65))
dataSource.Add(New SalesData("2008", 75))

Create the data binding model

Create a CategoryAxisDataBindModel using the data source. Use the CategoryName and YNames properties to map the category and Y-value fields.

CategoryAxisDataBindModel dataSeriesModel = new CategoryAxisDataBindModel(dataSource);
dataSeriesModel.CategoryName = "Year";
dataSeriesModel.YNames = new string[] { "Sales" };
Dim dataSeriesModel As New CategoryAxisDataBindModel(dataSource)
dataSeriesModel.CategoryName = "Year"
dataSeriesModel.YNames = New String() {"Sales"}

Bind the data to the chart series

Create a ChartSeries and assign the data-binding model to its CategoryModel property. Then, add the series to the chart.

ChartSeries chartSeries = new ChartSeries("Sales");
chartSeries.CategoryModel = dataSeriesModel;
chartControl.Series.Add(chartSeries);
Dim chartSeries As New ChartSeries("Sales")
chartSeries.CategoryModel = dataSeriesModel
chartControl.Series.Add(chartSeries)

Configure the category axis

To display categorical data, set the ValueType property of the PrimaryXAxis to Category.
You can also define titles for the primary X and Y axes by using their Title properties.

chartControl.PrimaryXAxis.ValueType = ChartValueType.Category;
chartControl.PrimaryXAxis.Title = "Year";
chartControl.PrimaryYAxis.Title = "Sales(Millions)";
chartControl.PrimaryXAxis.ValueType = ChartValueType.Category
chartControl.PrimaryXAxis.Title = "Year"
chartControl.PrimaryYAxis.Title = "Sales(Millions)"

Apply a skin to the chart control

To improve the appearance of the chart, apply the Metro skin to the ChartControl as follows.

chartControl.Skins = Skins.Metro;
chartControl.Skins = Skins.Metro

The following screenshot depicts the view when you run the project in your form.

Chart Started

Add chart title

A title provides users with quick information about the data plotted in the chart. You can add a title to the chart by using the Text property of ChartTitle.

ChartTitle title = new ChartTitle();
title.Text = "Product Sales";
chartControl.Titles.Add(title);
Dim title As ChartTitle = New ChartTitle()
title.Text = "Product Sales"
chartControl.Titles.Add(title)

Chart Started

Enable legend

The Legend is enabled or disabled by using the Visible property. By default, the legend is enabled in the chart.

ChartSeries chartSeries = new ChartSeries("Sales");
chartControl.Legend.Visible = true;
chartControl.LegendAlignment = ChartAlignment.Center;
chartControl.Legend.Position = ChartDock.Top; 
chartControl.LegendsPlacement = ChartPlacement.Outside;
Dim chartSeries As ChartSeries = New ChartSeries("Sales")
chartControl.Legend.Visible = true
chartControl.LegendAlignment = ChartAlignment.Center
chartControl.Legend.Position = ChartDock.Top
chartControl.LegendsPlacement = ChartPlacement.Outside

Chart Started

Enable data labels

You can add data labels to the chart to improve readability by enabling the DisplayText property of the Style in ChartSeries.

chartSeries.Style.DisplayText = true;
chartSeries.Style.TextOrientation = ChartTextOrientation.Up;
chartSeries.Style.DisplayText = true
chartSeries.Style.TextOrientation = ChartTextOrientation.Up

Chart Started

Enable tooltip

Tooltip is used to show detailed information about a data point. You can enable tooltip by using the ShowToolTips property.

chartControl.ShowToolTips = true;
chartControl.Tooltip.BackgroundColor = new BrushInfo(Color.White);
chartControl.Tooltip.BorderStyle = BorderStyle.FixedSingle;
chartControl.Tooltip.Font = new Font("Segoe UI", 10);
chartSeries.PrepareStyle += ChartSeries_PrepareStyle;

private void ChartSeries_PrepareStyle(object sender, ChartPrepareStyleInfoEventArgs args)
{
     ChartSeries series = sender as ChartSeries;
     int index = args.Index;
     ChartPoint point = series.Points[index];
     args.Style.ToolTip = "Product : " + point.Category + "\nSales : " + point.YValues[0];
	 args.Handled = true;
}
chartControl.ShowToolTips = True
chartControl.Tooltip.BackgroundColor = New BrushInfo(Color.White)
chartControl.Tooltip.BorderStyle = BorderStyle.FixedSingle
chartControl.Tooltip.Font = New Font("Segoe UI", 10)
AddHandler chartSeries.PrepareStyle, AddressOf ChartSeries_PrepareStyle

Private Sub ChartSeries_PrepareStyle(ByVal sender As Object, ByVal args As ChartPrepareStyleInfoEventArgs)
    Dim series As ChartSeries = CType(sender, ChartSeries)
    Dim index As Integer = args.Index
    Dim point As ChartPoint = series.Points(index)
    args.Style.ToolTip = "Product : " & point.Category & vbLf & "Sales : " & point.YValues(0)
    args.Handled = True
End Sub

Chart Started

See also

You can find the complete getting started sample from this link.

You can also explore our WinForms Chart example that shows how to render various chart types as well as how to easily configure with built-in support for creating stunning visual effects.