.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. 良许被百万大V安排得服服帖帖,还跟美女小姐姐合影了……

    大家好,我是良许. 很多人问我说,良许,你在工作之余还花这么多时间精力去写公众号运营自媒体,到底是为了什么? 其实原因很简单,就是想做个副业,万一到了 35 岁真的失业了,我至少还有另外一份收入,不至 ...

  2. 安装和配置SQL

    安装和配置SQL 在终端输入 npm i mysql命令安装SQL(加上-g全局安装) 配置SQL // 1.导入mysql模块 const mysql = require("mysql&q ...

  3. Zabbix template for Microsoft SQL Server总结

      Zabbix template for Microsoft SQL Server介绍   这里介绍Zabbix下监控Microsoft SQL Server数据库非常好用的一个模板,模板名为&qu ...

  4. 20190923-03Linux时间日期类 000 011

    1.基本语法 date [OPTION]... [+FORMAT] 2.选项说明 表1-20 选项 功能 -d<时间字符串> 显示指定的“时间字符串”表示的时间,而非当前时间 -s< ...

  5. 关于json序列化相关代码

    自己写的一个 /// <summary> /// 序列化JSON,返回string /// </summary> /// <param name="dt&quo ...

  6. matlab中如何定义函数

    首先建立M文件或直接点击(File/New/Function)建立函数文件,其中函数文件的格式是: function [输出变量] = 函数名称(输入变量) % 注释 % 函数体 如下所示,是编写的一 ...

  7. php第六天-UNIX时间戳/格式化时间,程序错误发送的领域

    ###0x01 PHP的错误处理 1.1 错误报告级别 PHP程序的错误发生一般归属于下列三个领域: 语法错误: 语法错误最常见,并且也容易修复.如:代码中遗漏一个分号.这类错误会阻止脚本的执行. 运 ...

  8. Redis可视化工具推荐

    前言 Redis可视化工具目前好用的免费的几乎难以寻迹,百度能搜索到的推荐比较多的是Redis Desktop Manager 官网地址:https://redisdesktop.com/pricin ...

  9. C语言实现数据机构链表的基本操作(从键盘输入生成链表、读取数组生成链表)

    利用头插法实现逆置 下面简单介绍一下,算法思想结合图示看 算法思想:"删除"头结点与链表其他结点的原有联系(即将头结点的指针置空),再逐个插入逆置链表的表头(即"头插&q ...

  10. kafk学习笔记(一)

    kafka消费模式 1.点对点模式:消费者主动拉取消息,消费之后删除数据. 2.发布/订阅模式:如果生产者推给消费者,可能会有些消费者消费比较慢,直接爆炸.或者有些消费者消费很快,资源浪费:一般是消费 ...