.net中,微软给我们提供了画图类(system.drawing.imaging),在该类中画图的基本功能都有。比如:直线、折线、矩形、多边形、椭圆形、扇形、曲线等等,因此一般的图形都可以直接通过代码画出来。接下来介绍一些画图函数:
Bitmap bmap=new Bitmap(500,500) /定义图像大小;
bmap.Save(stream,imagecodecinfo) /将图像保存到指定的输出流;
Graphics gph /定义或创建gdi绘图对像;
PointF cpt /定义二维平面中x,y坐标;
DrawString(string,font,brush,ponitf) /用指定的brush和font对像在指定的矩形或点绘制指定的字符串;
DrawLine(pen,ponit,ponit) /用指定的笔(pen)对像绘制指定两点之间直线;
DrawPolygon(pen,ponit[]) /用指定的笔(pen)对像绘制指定多边形,比如三角形,四边形等等;
FillPolygon(brush,ponit[]) /用指定的刷子(brush)对像填充指定的多边形;
DrawEllipse(pen,x,y,width,height) /用指定的笔绘制一个边框定义的椭圆;
FillEllipse(brush,x,y,width,height) /用指定的刷子填充一个边框定义的椭圆;
DrawRectangle(pen,x,y,width,height) /用指定的笔绘制一个指定坐标点、宽度、高度的矩形;
DrawPie(pen,x,y,width,height,startangle,sweepangle) /用指定的笔绘制一个指定坐标点、宽度、高度以及两条射线组成的扇形;

如果你在Form中绘图的话,不论是不是采用的双缓存,都会看到图片在更新的时候都会不断地闪烁,解决方法就是在这个窗体的构造函数中增加以下三行代码:

请在构造函数里面底下加上如下几行:

SetStyle(ControlStyles.UserPaint, true);

SetStyle(ControlStyles.AllPaintingInWmPaint, true);   //   禁止擦除背景.

SetStyle(ControlStyles.DoubleBuffer, true);   //   双缓冲

参数说明:

UserPaint

如果为true,控件将自行绘制,而不是通过操作系统来绘制。此样式仅适用于派生自   Control的类。

AllPaintingInWmPaint

如果为true,控件将忽略 WM_ERASEBKGND窗口消息以减少闪烁。仅当UserPaint   位设置为true时,才应当应用该样式。

DoubleBuffer

如果为true,则绘制在缓冲区中进行,完成后将结果输出到屏幕上。双重缓冲区可防止由控件重绘引起的闪烁。要完全启用双重缓冲,还必须将UserPaint和AllPaintingInWmPaint样式位设置为   true。

  1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.Drawing;
5 using System.Drawing.Drawing2D;
6 using System.Windows.Forms;
7 using System.Windows.Forms.DataVisualization.Charting;
8
9
10 namespace WindowsFormsApp2
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17 }
18 private Queue<double> dataQueue = new Queue<double>();//把Queue<double>看成一个类型 int[] a=new int [8]
19 List<int> lis = new List<int>();
20 private void InitChart()
21 {
22 Chart[] ch = new Chart[1] { chart1 };
23 for (int i = 0; i < 1; i++)
24 {
25 ch[i].ChartAreas.Clear();
26 ChartArea chartArea1 = new ChartArea("C1");
27 ch[i].ChartAreas.Add(chartArea1);
28 //定义存储和显示点的容器
29 ch[i].Series.Clear();
30 Series series1 = new Series("S1");
31 series1.ChartArea = "C1";
32 ch[i].Series.Add(series1);
33
34 ch[i].ChartAreas[0].AxisY.IsStartedFromZero = false;
35 ch[i].Legends[0].Enabled = false;
36
37 ch[i].ChartAreas[0].AxisX.Interval = 1;
38 ch[i].ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
39 ch[i].ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
40 //设置标题
41 ch[i].Titles.Clear();
42 ch[i].Titles.Add("S01");
43 ch[i].Titles[0].Text = "通道" + (i + 1) + " 折线图显示";
44 ch[i].Titles[0].ForeColor = Color.RoyalBlue;
45 ch[i].Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
46 //设置图表显示样式
47 ch[i].Series[0].Color = Color.Red;
48 //this.chart1.Titles[0].Text = string.Format("{0}折线图显示", );
49 ch[i].Series[0].ChartType = SeriesChartType.FastLine;
50 ch[i].Series[0].Points.Clear();
51 }
52 }
53
54 private void button1_Click(object sender, EventArgs e)
55 {
56
57 List<string> xData = new List<string>() ;
58 for (int i = 0; i < 90; i++)
59 {
60 xData.Add("A" + i.ToString());
61 }
62
63 List<int> yData = new List<int>() ;
64 for (int i = 0; i < 90; i++)
65 {
66 yData.Add(Convert.ToInt32 (Math.Sin(i)));
67 }
68 Stopwatch sw = new Stopwatch();
69 sw.Start();
70 chart1.Series[0]["PieLabelStyle"] = "Outside";//将文字移到外侧
71 chart1.Series[0]["PieLineColor"] = "Black";//绘制黑色的连线。
72 chart1.Series[0].Points.DataBindXY(xData, yData);
73 sw.Stop();
74 label1.Text = sw.ElapsedMilliseconds.ToString("0000");
75 }
76
77
78 private void Form1_Load(object sender, EventArgs e)
79 {
80 //双缓冲参考https://blog.csdn.net/kasama1953/article/details/51637617
81 this.DoubleBuffered = true;//设置本窗体
82 SetStyle(ControlStyles.UserPaint, true);
83 SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
84 SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
85
86 InitChart();
87 for (int i = 0; i < 10000; i++)
88 dataQueue.Enqueue(i+1);
89 for (int i = 0; i < 10000; i++)
90 {
91 lis.Add(i);
92 //if (i == 5)
93 // lis.RemoveAt(0);
94 }
95 }
96
97
98 private void button2_Click(object sender, EventArgs e)
99 {
100 Stopwatch sw = new Stopwatch();
101 sw.Start();
102 for (int i = 0; i < lis.Count; i++)
103 chart2.Series[0].Points.AddXY((i + 1), lis[i]);
104 sw.Stop();
105 label1.Text = sw.ElapsedMilliseconds.ToString("0000");
106 }
107
108
109 private void button3_Click(object sender, EventArgs e)
110 {
111 Stopwatch sw = new Stopwatch();
112 sw.Start();
113 string[] month = new string[12] { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" };
114 float[] d = new float[12] { 20.5f, 60, 10.8f, 15.6f, 30, 70.9f, 50.3f, 30.7f, 70, 50.4f, 30.8f, 20 };
115 //画图初始化
116 Bitmap bmap = new Bitmap(500, 500);
117 Graphics gph = Graphics.FromImage(bmap);
118 gph.Clear(Color.White);
119 PointF cpt = new PointF(40, 420);//中心点
120 PointF[] xpt = new PointF[3] { new PointF(cpt.Y + 15, cpt.Y), new PointF(cpt.Y, cpt.Y - 8), new PointF(cpt.Y, cpt.Y + 8) };//x轴三角形
121 PointF[] ypt = new PointF[3] { new PointF(cpt.X, cpt.X - 15), new PointF(cpt.X - 8, cpt.X), new PointF(cpt.X + 8, cpt.X) };//y轴三角形
122 gph.DrawString("某工厂某产品月生产量图表", new Font("宋体", 14), Brushes.Black, new PointF(cpt.X + 60, cpt.X));//图表标题
123 //画x轴
124 gph.DrawLine(Pens.Black, cpt.X, cpt.Y, cpt.Y, cpt.Y);
125 gph.DrawPolygon(Pens.Black, xpt);
126 gph.FillPolygon(new SolidBrush(Color.Black), xpt);
127 gph.DrawString("月份", new Font("宋体", 12), Brushes.Black, new PointF(cpt.Y + 10, cpt.Y + 10));
128 //画y轴
129 gph.DrawLine(Pens.Black, cpt.X, cpt.Y, cpt.X, cpt.X);
130 gph.DrawPolygon(Pens.Black, ypt);
131 gph.FillPolygon(new SolidBrush(Color.Black), ypt);
132 gph.DrawString("单位(万)", new Font("宋体", 12), Brushes.Black, new PointF(0, 7));
133 for (int i = 1; i <= 12; i++)
134 {
135 //画y轴刻度
136 if (i < 11)
137 {
138 gph.DrawString((i * 10).ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X - 30, cpt.Y - i * 30 - 6));
139 gph.DrawLine(Pens.Black, cpt.X - 3, cpt.Y - i * 30, cpt.X, cpt.Y - i * 30);
140 }
141 //画x轴项目
142 gph.DrawString(month[i - 1].Substring(0, 1), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30 - 5, cpt.Y + 5));
143 gph.DrawString(month[i - 1].Substring(1, 1), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30 - 5, cpt.Y + 20));
144 if (month[i - 1].Length > 2) gph.DrawString(month[i - 1].Substring(2, 1), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30 - 5, cpt.Y + 35));
145 //画点
146 gph.DrawEllipse(Pens.Black, cpt.X + i * 30 - 1.5f, cpt.Y - d[i - 1] * 3 - 1.5f, 3, 3);
147 gph.FillEllipse(new SolidBrush(Color.Black), cpt.X + i * 30 - 1.5f, cpt.Y - d[i - 1] * 3 - 1.5f, 3, 3);
148 //画数值
149 gph.DrawString(d[i - 1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cpt.X + i * 30, cpt.Y - d[i - 1] * 3));
150 //画折线
151 if (i > 1) gph.DrawLine(Pens.Red, cpt.X + (i - 1) * 30, cpt.Y - d[i - 2] * 3, cpt.X + i * 30, cpt.Y - d[i - 1] * 3);
152 }
153 //保存输出图片
154 //bmap.Save(Response.OutputStream, ImageFormat.Gif);
155 pictureBox1.Image = bmap;
156 sw.Stop();
157
158 label1.Text = sw.ElapsedMilliseconds.ToString("0000");
159
160 }
161
162 private void Form1_Paint(object sender, PaintEventArgs e)
163 {
164
165 }
166 }
167 }

------------------------------------------------------------------------

如果需要查看更多文章,请微信搜索  csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! !

C# 生成chart图表的三种方式的更多相关文章

  1. Android 生成LayoutInflater的三种方式

    通俗的说,inflate就相当于将一个xml中定义的布局找出来. 因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组 ...

  2. python 全栈开发,Day94(Promise,箭头函数,Django REST framework,生成json数据三种方式,serializers,Postman使用,外部python脚本调用django)

    昨日内容回顾 1. 内容回顾 1. VueX VueX分三部分 1. state 2. mutations 3. actions 存放数据 修改数据的唯一方式 异步操作 修改state中数据的步骤: ...

  3. 根据服务端生成的WSDL文件创建客户端支持代码的三种方式

    第一种:使用wsimport是JDK自带的工具,来生成 生成java客户端代码常使用的命令参数说明: 参数 说明 -p 定义客户端生成类的包名称 -s 指定客户端执行类的源文件存放目录 -d 指定客户 ...

  4. spring生成EntityManagerFactory的三种方式

    spring生成EntityManagerFactory的三种方式 1.LocalEntityManagerFactoryBean只是简单环境中使用.它使用JPA PersistenceProvide ...

  5. 监视EntityFramework中的sql流转你需要知道的三种方式Log,SqlServerProfile, EFProfile

    大家在学习entityframework的时候,都知道那linq写的叫一个爽,再也不用区分不同RDMS的sql版本差异了,但是呢,高效率带来了差灵活性,我们 无法控制sql的生成策略,所以必须不要让自 ...

  6. 【整理】Linux下中文检索引擎coreseek4安装,以及PHP使用sphinx的三种方式(sphinxapi,sphinx的php扩展,SphinxSe作为mysql存储引擎)

          一,软件准备 coreseek4.1 (包含coreseek测试版和mmseg最新版本,以及测试数据包[内置中文分词与搜索.单字切分.mysql数据源.python数据源.RT实时索引等测 ...

  7. Spark部署三种方式介绍:YARN模式、Standalone模式、HA模式

    参考自:Spark部署三种方式介绍:YARN模式.Standalone模式.HA模式http://www.aboutyun.com/forum.php?mod=viewthread&tid=7 ...

  8. js学习-DOM之动态创建元素的三种方式、插入元素、onkeydown与onkeyup两个事件整理

    动态创建元素的三种方式: 第一种: Document.write(); <body> <input type="button" id="btn" ...

  9. Linux就这个范儿 第15章 七种武器 linux 同步IO: sync、fsync与fdatasync Linux中的内存大页面huge page/large page David Cutler Linux读写内存数据的三种方式

    Linux就这个范儿 第15章 七种武器  linux 同步IO: sync.fsync与fdatasync   Linux中的内存大页面huge page/large page  David Cut ...

随机推荐

  1. java基础语法(二)

    一.运算符 算数运算符 算数运算符用在数学表达式中,它们的作用和在数学中的作用一样. 操作符 描述 例子 + 两数相加 1+1=2 - 两数相减 2-1=1 * 两数相乘 1*1=1 / 两数相除 1 ...

  2. Myeclipse 连接数据库(jdbc)

    1.找到DataBase Explorer,如下图所示: 2.点击下图红框内图标,new 3.进入下图界面 如果是JDBC驱动按下图配置: driver name自己起 url一定要注意:jdbc:m ...

  3. Q200510-02-02: 重复的DNA序列 SQL解法

    重复的DNA序列所有 DNA 都由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:“ACGAATTCCG”.在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助. 编写一个函数来 ...

  4. centos开放指定端口

    1.开启防火墙      systemctl start firewalld 2.开放指定端口       firewall-cmd --zone=public --add-port=1935/tcp ...

  5. leetcode刷题-82.删除排序链表中的重复元素 II

    题目 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 示例 1: 输入: 1->2->3->3->4->4->5输出: 1- ...

  6. Linux:基础命令三

    一.软链接 相当于windows中的快捷方式,为了方便用户在使用时更快找到 ln -s /application/appche2.2.0/  /application/appche       注意: ...

  7. Springboot中WebMvcConfigurer接口详解

    Springboot 使用越来越多,企业的基本框架,到Springcloud分布式,可以说无论面试还是平常技术学习,一说到spring几乎就就代替了Java,可以说spring,springboot的 ...

  8. iscroll5 滚动条根据内容高度自动显示隐藏及强制横屏时方向错位

    横竖屏方向错位: move: function (e) { if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) { r ...

  9. JVM垃圾收集机制

    JVM垃圾回收机制是java程序员必须要了解的知识,对于程序调优具有很大的帮助(同时也是大厂面试必问题). 要了解垃圾回收机制,主要从三个方面: (1)垃圾回收面向的对象是谁? (2)垃圾回收算法有哪 ...

  10. 关于KeePass基于csv格式的批量导入与导出

    在KeePass的导出选项中,有一个KeePass CSV(1.x),导出后格式如下: "Account","Login Name","Passwor ...