C#:Line Chart code sample with Chart Control

Published on:
Last updated:

This post is also available in: 日本語 (Japanese)

This post is about drawing fastLine graph with C# by using chart control of .NET Framework.
When you use chart control with C#, I think there are often use List and Dictionary.
So, it is recommended that you also refer to article C#:Group duplicate elements of List to Dictionary Key.

In the sample code, the title of the graph is defined by string legend.
And the type of the graph is defined by SeriesChartType.
Please refer to Microsoft Docs for other chart types.

string legend = "Sample FastLine Chart";
chart1.Series.Clear(); //Initialize Graph
chart1.Series.Add(legend); //Add Graph
chart1.Series[legend].ChartType = SeriesChartType.FastLine; //Select Graph Type
chart1.Series[legend].LegendText = legend; //Specify Graph Legend
// Set Chart Data
string[] xValues = new string{"aaa", "bbb", "ccc"};
double[] yValues = new double{10.21, 20.22, 30.23};
// Assigning Data Values to the Chart
for (int i = 0; i < xValues.Length; i++)
	chart1.Series[legend].Points.AddXY(xValues[i], yValues[i]);
No tags for this post.

Kuniyoshi Takemoto is the founder of Amelt.net LLC, and editor of this blog(www.amelt.net).Learn more and follow me on LinkedIn.