Home
Search
C# Chart, Windows Forms (Series and Points)Use the Chart control from Windows Forms to display data in a graphical way.
Chart. This 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.
Getting started. First, a newer version of .NET is required—older versions will not have the Chart control available. Please open the Toolbox and drag the Chart item to the Form.
Here, 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.
Array In Form1_Load, we assign an array of strings (Series) and an array of integers (Points).
For In the for-loop, we add the strings to the Series collection and add the integers to the Points collections on those Series.
For
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 this page's screenshot.
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]); } } } }
Add note. 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 By calling Points.Add, you can set the values. We pass in elements from our array.
Code that saves chart to image, C#:
this.chart1.SaveImage("C:\\chart.png", ChartImageFormat.Png);
Palette. The Palette property on the Chart type can help with appearance. You can use a ChartColorPalette enum in an assignment expression.
Tip When you work with a Chart, try changing your Palette and experimenting with the ChartColorPalette constants.
SaveImage. 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. Try adding this statement.
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on 9/26/2022 (edit).
Home
© 2007-2022 sam allen.
see site info on the changelog.