c#利用Chart 画图

c#利用Chart 画图

添加画图组件
在这里插入图片描述
编写代码

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;
using System.Windows.Forms.DataVisualization.Charting;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            // 设置曲线的样式

            Series series = chart1.Series[0];

            // 画样条曲线(Spline)
            series.ChartType = SeriesChartType.Spline;
            //series.ChartType = SeriesChartType.Point;

            // 线宽2个像素
            series.BorderWidth = 2;

            // 线的颜色:红色
            series.Color = System.Drawing.Color.Red;

            // 图示上的文字
            series.LegendText = "演示曲线";

            // 准备数据
            float[] values = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 };

            // 在chart中显示数据
            int x = 0;
            foreach (float v in values)
            {
                series.Points.AddXY(x, v);
                x++;
            }

            // 设置显示范围
            ChartArea chartArea = chart1.ChartAreas[0];
            chartArea.AxisX.Minimum = 0;
            chartArea.AxisX.Maximum = 10;
            chartArea.AxisY.Minimum = 0d;
            chartArea.AxisY.Maximum = 1.1d;
        }
    }
}