添加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, "绘图区域至少要有一条线!", "信息提示");
 
 }

效果图:

C# WinForm动态添加MSChart控件的更多相关文章

  1. 谨记给UpdatePanel中动态添加的控件赋ID

    原文:谨记给UpdatePanel中动态添加的控件赋ID 昨天下定决 心对上次做的布局编辑器控件加以改进,其中最主要变化的就是要完全使用ASP.NET AJAX!但是很遗憾,虽然耳闻已久,但目前对AS ...

  2. ASP.NET动态添加用户控件的方法

    本文实例讲述了ASP.NET动态添加用户控件的方法.分享给大家供大家参考.具体实现方法如下: 为了让用户控件能ASP.NET页面实现动态添加,首先写一个接口IGetUCable,这个接口有一个函数,返 ...

  3. 动态添加easyui 控件

    jquery提供了append,appendTo方法,可以动态添加静态的html文本,在easyui中,要动态添加easyui控件要怎么做呢,下面就来介绍动态添加easyui控件. 使用方法:和添加静 ...

  4. js动态添加file控件

    <html></head><script language="javascript" type="text/ecmascript" ...

  5. AX 用代码创建FORM动态加控件,重载动态添加的控件的方法。

    eg. 范例:class\RFIDReadWriteForm/Build方法. formRun.controlMethodOverload(true); formRun.controlMethodOv ...

  6. C# WinForm中添加用户控件

    转:https://blog.csdn.net/haelang/article/details/40681003 有的时候我们需要频繁使用一些系统默认工具的组合,那么就可以使用自定义用户控件. 起一个 ...

  7. duilib 修复CTreeViewUI控件动态添加子控件时,对是否显示判断不足的bug

    转载请说明出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42264947 这个bug我在仿酷狗开发日志里提到过,不过后来发现修复的不够 ...

  8. AngularJS:实现动态添加输入控件功能

    功能要求如下:1.    点击加号可以增加输入框.2.    点击减号可以减少输入框.3.    当输入框只有一个的时候,不能再减少输入框.效果图如下:只有一个输入框有多个输入框 要实现这个功能,可以 ...

  9. VB动态添加WebBrowser控件,并拦截弹出窗口(不用引用任何组件)

    新建空白窗体,然后粘帖下面代码: Option ExplicitPublic WithEvents br As VBControlExtender Private Sub br_ObjectEvent ...

随机推荐

  1. Linux下开发Windows平台运行的程序 - MinGW

    开源不乏神人,于是有了MinGW(Minimalist GNU for Windows),又称mingw32,是将GCC编译器和GNU Binutils一直到Win32平台下,包含一系列头文件.库和可 ...

  2. 改变当前shell工作目录

    执行脚本时候,只是在当前的shell下开了一个子进程,切换目录的操作只对该进程中相关后续指令有效,但改变不了父进程的目录. 解决方法: 法一: 用 source a.sh就行了. 法二: [fedor ...

  3. 从while(cin>>a)开始探讨cin

    1. 首先cin>>a返回的是左操作数,也就是返回cin. cin的条件状态中: cin.eof()    判断流是否到达文件的结束符 cin.fail()    判断IO操作是否失败 在 ...

  4. PHP运行错最有效解决办法Fatal error: Out of memory (allocated 786432) (tried to allocate 98304 bytes) in H:\freehost\zhengbao2\web\includes\lib_common.php on line 744

    原文 PHP运行错最有效解决办法Fatal error: Out of memory (allocated 6029312) Fatal error: Out of memory (allocated ...

  5. The Bip Buffer - The Circular Buffer with a Twist

    Introduction The Bip-Buffer is like a circular buffer, but slightly different. Instead of keeping on ...

  6. Selenium2学习-002-Selenium2 Web 元素定位及 XPath 编写演示示例

    此文主要对 Selenium2 的 Web 元素定位及 XPath 编写示例,敬请各位亲们参阅,共同探讨.若有不足之处,敬请各位大神指正,不胜感激! 通过 Firefox(火狐)浏览器的插件 Fire ...

  7. pages 元素(ASP.NET 设置架构)web.config 详解

    pages 元素(ASP.NET 设置架构)    buffer="[True|False]"   enableEventValidation="[True|False] ...

  8. iOS企业开发plist安装包实现

    第一步: 在使用MACBOOK导出ipa的时候,我们得到ipa的同时,还得到一份plist文件 看到我们导出的plist,需要注意的地方有两个已经用中文标注. 一个是URL,一个是bundle-ide ...

  9. iOS工程如何支持64-bit

    苹果在2014年10月20号发布了一条消息:从明年的二月一号开始,提交到App Store的应用必须支持64-bit.详细消息地址为:https://developer.apple.com/news/ ...

  10. C++学了这么多年,你也许不知道为什么类定义要放在.h文件,类实现放在cpp文件。它们如何关联?

    原文  http://blog.csdn.net/ithzhang/article/details/8119286 主题 C++  C++学了这么多年你知道为什么定义类时,类的定义放在.h文件中,而类 ...