首页 > 代码库 > C# WinForm动态添加MSChart控件

C# WinForm动态添加MSChart控件


添加mschart.dll动态链接库
添加引用
System.Windows.Forms.DataVisualization
 
 

 MSChart控件作为方便的用户数据展示控件,可以方便的使用控件提供的形状和展示形式展示数据,早Web应用中用的比较多,这几天一直在做一个基于Winform的CS结构的演示程序,用到了MSChart,由于一直也不太熟悉MSChart,又是动态自定义添加,所以一点一点的摸索着做起来,动态添加自定义的MSChart到WinForm程序中,上代码:

1、创建一条曲线形式的Chart

System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();

//定义一个chart
 System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();

//定义一个绘图区域
 System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();

//定义一个数据列
 chartArea1.Name = "ChartArea1";

//其实没有必要,可以使用chart1。ChartAreas[0]就可以了
 chart1.ChartAreas.Add(chartArea1);

//完成Chart和chartArea的关联
 //System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
 //legend1.Name = "图标";
 //chart1.Legends.Add(legend1);
 chart1.Name = "chart1";
 series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;

//设置线性
 Random rd = new Random();
 double[] num = new double[20];

 for (int i = 0; i < 20; i++)
 {
 int valuey = rd.Next(20, 100);
 DataPoint point = new DataPoint((i + 1), valuey);
 series1.Points.Add(point);
 }

//产生点的坐标
 //chart1.Titles[0].Text = "";
 
 series1.ChartArea = "ChartArea1";
 chartArea1.AxisX.Title = "日期";
 chartArea1.AxisY.Title = "值";
 chartArea1.AxisX.Interval = 1;
 chartArea1.AxisY.Interval = 5;
 chartArea1.AxisY.Minimum = 20;
 series1.Legend = "图标";
 series1.Name = "Series1";
 chart1.Text = "测试";
 chart1.Size = new System.Drawing.Size(700, 500);
 //chart1.Location = new System.Drawing.Point(50,120);
 series1.Color = Color.Blue;
 chart1.Text = "ceshi";
 //chart1.Titles[0].Text = "fff";
 chart1.Series.Add(series1);
 //这一句很重要,缺少的话绘图区域将显示空白
 //chart1.SizeChanged += new System.EventHandler(DoSizeChanged);
 //chart1.AllowDrop = true;
 chart1.BackColor = Color.FromArgb(243, 223, 193);

//设置chart背景颜色
 chartArea1.BackColor = Color.FromArgb(243, 223, 193);

//设置c绘图区域背景颜色
 series1.BorderWidth = 2;
 series1.IsValueShownAsLabel = true;

//是否显示Y的值

 //this.groupBox9.Controls.Add(chart1);
 this.panel21.Controls.Add(chart1);
 chart1.Visible = true;
 //this.label10.Visible = true;
 //this.label10.Text = "【" + tn.Name + "】图";
 chart1.Titles.Add("【" + tn.Name + "】图");
 //为Chart1添加标题
 chartArea1.AxisX.IsMarginVisible = true;

//是否显示X轴两端的空白

2、在上述chart的基础上添加一条线

Control[] controls = this.groupBox9.Controls.Find("chart1",true);

//找到已经有的Chart1
 System.Windows.Forms.DataVisualization.Charting.Chart ch = (System.Windows.Forms.DataVisualization.Charting.Chart)controls[0];
 System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();

//新定义一个数据项
 Random rd=new Random();
 for (int i = 0; i < ch.Series[0].Points.Count; i++)
 {
 int valuey=rd.Next(20,100);
 DataPoint point = new DataPoint((i + 1), valuey);
 series2.Points.Add(point);
 }
 series2.Color = Color.FromArgb(rd.Next(100, 255), rd.Next(0, 150), rd.Next(0, 255));
 series2.BorderWidth = 2;
 series2.ChartType = ((System.Windows.Forms.DataVisualization.Charting.Chart)this.panel21.Controls[0]).Series[0].ChartType;

//定义线性和原有的线条形状一致
 series2.IsValueShownAsLabel = true;
 ch.Series.Add(series2);

//添加数据列

3、减少一条曲线

Control[] controls = this.groupBox9.Controls.Find("chart1", true);
 System.Windows.Forms.DataVisualization.Charting.Chart ch = (System.Windows.Forms.DataVisualization.Charting.Chart)controls[0];
 if (ch.Series.Count>1)
 {
 //MessageBox.Show(this, "去掉一条线!", "信息提示");
 int i = ch.Series.Count - 1;
 while (i>1)
 {
 if (ch.Series[i].Points.Count>0)
 {
 break;
 }
 i -=1;
 }

 ch.Series[i].Points.Clear();
 this.toolStripStatusLabel2.Text = "减少对比曲线完成!";
 }
 else
 {
 MessageBox.Show(this, "绘图区域至少要有一条线!", "信息提示");
 
 }

效果图: