WinForms Chart Control in C#: Bar and Spline Chart Examples

The WinForms Chart control (namespace System.Windows.Forms.DataVisualization.Charting) draws column, bar, line, spline, pie and dozens of other chart types from a few lines of code. It started as a separate download for .NET Framework 3.5 SP1, became part of .NET Framework 4.0, and many older desktop apps still use it.

It is not part of .NET Core or .NET 5 and later. Microsoft published the source as an archived GitHub project but never shipped a supported package, so a new net8.0-windows WinForms project has no Chart control in its Toolbox. You have three options:

  • Stay on .NET Framework 4.8, where the control is still in the box (add a reference to System.Windows.Forms.DataVisualization).
  • Use a community port of the same control on .NET 8, such as the HIC.System.Windows.Forms.DataVisualization NuGet package. The namespace and API are unchanged, so the code below works as is.
  • Switch to an actively maintained charting library such as ScottPlot, LiveCharts2 or OxyPlot, which all have WinForms controls. For new projects, this is the better choice.

The examples below work on .NET Framework 4.8 and with the community port. They avoid newer C# syntax so they compile on both.

Create the chart

The designer can add a Chart for you on .NET Framework (Toolbox, Data group). With the community port the designer support is less reliable, so it is simpler to create the control in code:

using System;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

public partial class ChartForm : Form
{
    private readonly Chart chart = new Chart { Dock = DockStyle.Fill };

    public ChartForm()
    {
        InitializeComponent();

        chart.ChartAreas.Add(new ChartArea("Main"));
        chart.Legends.Add(new Legend("Legend"));
        Controls.Add(chart);

        Load += (sender, e) => ShowBarChart();
    }
}

A chart created in code has no chart area. Without the ChartAreas.Add line, series have nowhere to be drawn and the control stays empty. The designer adds one chart area and one legend automatically, which is why older examples never mention it.

Bar chart

private void ShowBarChart()
{
    chart.Series.Clear();
    chart.Titles.Clear();

    chart.Palette = ChartColorPalette.EarthTones;
    chart.Titles.Add("Animals");

    string[] animals = { "Cat", "Dog", "Bird", "Monkey" };
    int[] counts = { 2, 1, 7, 5 };

    for (int i = 0; i < animals.Length; i++)
    {
        Series series = chart.Series.Add(animals[i]);
        series.ChartType = SeriesChartType.Column;
        series.Points.Add(counts[i]);
    }
}
C# Bar Chart Example

Each animal is its own series with a single point, so each bar gets its own palette color and its own entry in the legend. If you would rather have one series with four labeled bars, bind both arrays to a single series:

Series series = chart.Series.Add("Count");
series.ChartType = SeriesChartType.Column;
series.IsValueShownAsLabel = true;
series.Points.DataBindXY(animals, counts);

SeriesChartType.Column draws vertical bars. SeriesChartType.Bar draws them horizontally, which reads better when the category names are long.

Note the Titles.Clear() call. Titles.Add appends, so calling the method twice without it stacks two identical titles on top of each other.

Spline chart

private void ShowSplineChart()
{
    chart.Series.Clear();
    chart.Titles.Clear();

    chart.Titles.Add("Total Income");

    Series series = chart.Series.Add("Total Income");
    series.ChartType = SeriesChartType.Spline;
    series.BorderWidth = 3;

    series.Points.AddXY("September", 100);
    series.Points.AddXY("October", 300);
    series.Points.AddXY("November", 800);
    series.Points.AddXY("December", 200);
    series.Points.AddXY("January", 600);
    series.Points.AddXY("February", 400);
}

When the X values are strings, the chart treats them as labels and places the points at positions 1, 2, 3 and so on, in the order you add them. For real dates, pass DateTime values and set series.XValueType = ChartValueType.Date, so the axis spacing reflects the actual time between points.

Saving a chart as an image

The control can render itself to a file without taking a screenshot:

chart.SaveImage("income.png", ChartImageFormat.Png);

The same bar chart with ScottPlot

ScottPlot (ScottPlot.WinForms package) targets current .NET and is MIT licensed. Drop a FormsPlot control on the form, then:

double[] counts = [2, 1, 7, 5];
string[] animals = ["Cat", "Dog", "Bird", "Monkey"];

formsPlot1.Plot.Add.Bars(counts);

ScottPlot.Tick[] ticks = animals.Select((name, i) => new ScottPlot.Tick(i, name)).ToArray();
formsPlot1.Plot.Axes.Bottom.TickGenerator = new ScottPlot.TickGenerators.NumericManual(ticks);

formsPlot1.Plot.Title("Animals");
formsPlot1.Refresh();

This is ScottPlot 5 syntax. ScottPlot changed its API substantially between versions 4 and 5, so check which version a sample was written for before copying it.

Related examples

7 thoughts on “WinForms Chart Control in C#: Bar and Spline Chart Examples”

  1. What if I’d like to add a spline chart with X and Y values from an excel sheet? How do i pull the data into the code you have here?

    Thanks in advance!

    Reply

Leave a Comment