winform的chart控件初探

有了chart控件,画图变得很简单了。打开属性Series可以任意设置,画多条线可以添加series,而其中的ChartType可以控制画line、pie或者还是chart,还有SaveImage可以直接保存图片,非常方便。

再推荐一个连接:http://www.cnblogs.com/gaoweipeng/archive/2010/04/06/1704879.html

using System;

using System.Data;

using System.Test;

using System.Drawing;

using System.Drawing.Imaging;

using System.Windows.Forms;

namespace chartdemo
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //chart1.Series.
            DataTable dt = default(DataTable);
            dt = CreateDataTable();

            //设置图表的数据源
            chart1.DataSource = dt;

            //设置图表Y轴对应项
            chart1.Series[0].YValueMembers = "Volume1";
            chart1.Series[1].YValueMembers = "Volume2";

            //设置图表X轴对应项
            chart1.Series[0].XValueMember = "Date";

            //绑定数据

            chart1.DataBind();

            //保存图片

            chart1.SaveImage(@"e:\1.bmp ", ImageFormat.Bmp);

        }

        private DataTable CreateDataTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("Date");
            dt.Columns.Add("Volume1");
            dt.Columns.Add("Volume2");

            DataRow dr;
            dr = dt.NewRow();
            dr["Date"] = "Jan";
            dr["Volume1"] = 3731;
            dr["Volume2"] = 4101;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Date"] = "Feb";
            dr["Volume1"] = 6024;
            dr["Volume2"] = 4324;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Date"] = "Mar";
            dr["Volume1"] = 4935;
            dr["Volume2"] = 2935;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Date"] = "Apr";
            dr["Volume1"] = 4466;
            dr["Volume2"] = 5644;
            dt.Rows.Add(dr);
            return dt;
        }
    }
}

你可能感兴趣的:(winform)