Windows Forms: Chart / Graph in C#

By FoxLearn 3/30/2019 11:21:29 PM   17.64K
Create Chart/Graph include bar, line, pie chart with SQL Server database using C#

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "ChartExample" and then click OK

chart c#Step 2: Create an Entity Framework Model First, then add the Revenue table to your Model

entity framework

Step 3: Design your form as below

chart c#

Step 4: Add code to handle your form as below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void btnLoad_Click(object sender, EventArgs e)
        {
            using(ChartEntities db = new ChartEntities())
            {
                //Load data to chart control
                chartRevenue.DataSource = db.Revenues.ToList();
                chartRevenue.Series["Revenue"].XValueMember = "Year";
                chartRevenue.Series["Revenue"].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
                chartRevenue.Series["Revenue"].YValueMembers = "Total";
                chartRevenue.Series["Revenue"].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //chartSalary.Series["Salary"].Points.AddXY("Peter", 1000);
            //chartSalary.Series["Salary"].Points.AddXY("John", 5000);
            //chartSalary.Series["Salary"].Points.AddXY("Tan", 1500);
            //chartSalary.Series["Salary"].Points.AddXY("Lucy", 7000);
            chartSalary.Series["Salary"].Points.Add(1000);
            chartSalary.Series["Salary"].Points[0].Color = Color.Red;
            chartSalary.Series["Salary"].Points[0].AxisLabel = "Peter";
            chartSalary.Series["Salary"].Points[0].LegendText = "Peter";
            chartSalary.Series["Salary"].Points[0].Label = "1000";
            //Init data
            chartSalary.Series["Salary"].Points.Add(5000);
            chartSalary.Series["Salary"].Points[1].Color = Color.Green;
            chartSalary.Series["Salary"].Points[1].AxisLabel = "John";
            chartSalary.Series["Salary"].Points[1].LegendText = "John";
            chartSalary.Series["Salary"].Points[1].Label = "5000";
            //
            chartSalary.Series["Salary"].Points.Add(1500);
            chartSalary.Series["Salary"].Points[2].Color = Color.Yellow;
            chartSalary.Series["Salary"].Points[2].AxisLabel = "Tan";
            chartSalary.Series["Salary"].Points[2].LegendText = "Tan";
            chartSalary.Series["Salary"].Points[2].Label = "1500";
            //
            chartSalary.Series["Salary"].Points.Add(7000);
            chartSalary.Series["Salary"].Points[3].Color = Color.Blue;
            chartSalary.Series["Salary"].Points[3].AxisLabel = "Lucy";
            chartSalary.Series["Salary"].Points[3].LegendText = "Lucy";
            chartSalary.Series["Salary"].Points[3].Label = "7000";
        }
    }
}

VIDEO TUTORIALS