TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Chart: Windows Forms

This C# tutorial demonstrates the Chart control. It requires Windows Forms.

Chart. The Chart control visualizes your data.

It displays data in your Windows Forms program as a bar graph or chart. With Chart you can quickly display your data in a colorful graphic controlled by C# code.

Start. First, you must be using a newer version of the .NET Framework. Older versions will not have the Chart control available. Please open the Toolbox and drag the Chart item to the Form.

Next: Select Properties after right-clicking on the Chart. You can add Series and Titles this way.

And: For our example, remove the default Series1 that was added by Visual Studio.

Example. Now we can use the Chart. In the next step, we will use the Form1_Load event handler to initialize the Chart we just added. To add Form1_Load, double-click on the Form window in Visual Studio.

In Form1_Load, we assign an array of strings (Series) and an array of integers (Points). In the for-loop, we add the strings to the Series collection and add the integers to the Points collections on those Series.

For

Example that sets Chart control up: C#

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
	public Form1()
	{
	    InitializeComponent();
	}

	private void Form1_Load(object sender, EventArgs e)
	{
	    // Data arrays.
	    string[] seriesArray = { "Cats", "Dogs" };
	    int[] pointsArray = { 1, 2 };

	    // Set palette.
	    this.chart1.Palette = ChartColorPalette.SeaGreen;

	    // Set title.
	    this.chart1.Titles.Add("Pets");

	    // Add series.
	    for (int i = 0; i < seriesArray.Length; i++)
	    {
		// Add series.
		Series series = this.chart1.Series.Add(seriesArray[i]);

		// Add point.
		series.Points.Add(pointsArray[i]);
	    }
	}
    }
}

Tip: The loop results in two Series: a Cats series with a Point of 1, and a Dogs series with a Point of 2.

Info: These are reflected in a bar graph, which is shown in the screenshot above.

When you call Series.Add, you can pass a string which becomes the name of the Series. A Series object is returned by Add. Then you can assign a local variable to the result of Add and access the Points collection on the Series.

And: By calling Points.Add, you can set the values. We pass in elements from our array.

Palette. Some computer programmers may not be artistically inclined. The Palette property on the Chart type can help with this. You can use a ChartColorPalette enumerated constant in an assignment expression.

Tip: When you work with a Chart, try changing your Palette and experimenting with the ChartColorPalette constants.

ChartColorPalette enumerated constants: C#

ChartColorPalette.Berry
ChartColorPalette.Bright
ChartColorPalette.BrightPastel
ChartColorPalette.Chocolate
ChartColorPalette.EarthTones
ChartColorPalette.Excel
ChartColorPalette.Fire
ChartColorPalette.GrayScale
ChartColorPalette.Light
ChartColorPalette.None
ChartColorPalette.Pastel
ChartColorPalette.SeaGreen
ChartColorPalette.SemiTransparent

SaveImage. Sometimes you will need to save your chart to an image file. The SaveImage method takes the graphical representation of your chart as it appears on the screen and writes it to a file or Stream you specify.

Note: In the above example, try adding this statement and then check out the chart.png file.

Code that saves chart to image: C#

this.chart1.SaveImage("C:\\chart.png", ChartImageFormat.Png);

 

Summary. We looked at the Chart type in the Windows Forms library. With Chart, you can automatically generate graphs based on data. These bar graphs can then be used in a variety of places or displayed directly in a Windows Forms program.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf