C# 生成chart图表的三种方式
.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图表的三种方式的更多相关文章
- Android 生成LayoutInflater的三种方式
通俗的说,inflate就相当于将一个xml中定义的布局找出来. 因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组 ...
- python 全栈开发,Day94(Promise,箭头函数,Django REST framework,生成json数据三种方式,serializers,Postman使用,外部python脚本调用django)
昨日内容回顾 1. 内容回顾 1. VueX VueX分三部分 1. state 2. mutations 3. actions 存放数据 修改数据的唯一方式 异步操作 修改state中数据的步骤: ...
- 根据服务端生成的WSDL文件创建客户端支持代码的三种方式
第一种:使用wsimport是JDK自带的工具,来生成 生成java客户端代码常使用的命令参数说明: 参数 说明 -p 定义客户端生成类的包名称 -s 指定客户端执行类的源文件存放目录 -d 指定客户 ...
- spring生成EntityManagerFactory的三种方式
spring生成EntityManagerFactory的三种方式 1.LocalEntityManagerFactoryBean只是简单环境中使用.它使用JPA PersistenceProvide ...
- 监视EntityFramework中的sql流转你需要知道的三种方式Log,SqlServerProfile, EFProfile
大家在学习entityframework的时候,都知道那linq写的叫一个爽,再也不用区分不同RDMS的sql版本差异了,但是呢,高效率带来了差灵活性,我们 无法控制sql的生成策略,所以必须不要让自 ...
- 【整理】Linux下中文检索引擎coreseek4安装,以及PHP使用sphinx的三种方式(sphinxapi,sphinx的php扩展,SphinxSe作为mysql存储引擎)
一,软件准备 coreseek4.1 (包含coreseek测试版和mmseg最新版本,以及测试数据包[内置中文分词与搜索.单字切分.mysql数据源.python数据源.RT实时索引等测 ...
- Spark部署三种方式介绍:YARN模式、Standalone模式、HA模式
参考自:Spark部署三种方式介绍:YARN模式.Standalone模式.HA模式http://www.aboutyun.com/forum.php?mod=viewthread&tid=7 ...
- js学习-DOM之动态创建元素的三种方式、插入元素、onkeydown与onkeyup两个事件整理
动态创建元素的三种方式: 第一种: Document.write(); <body> <input type="button" id="btn" ...
- 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 ...
随机推荐
- OpenJ_Bailian - 2995-登山(两遍最长上升子序列+枚举顶点)
五一到了,PKU-ACM队组织大家去登山观光,队员们发现山上一个有N个景点,并且决定按照顺序来浏览这些景点,即每次所浏览景点的编号都要大于前一个浏览景点的编号.同时队员们还有另一个登山习惯,就是不连续 ...
- MySQL通过POIN数据类型查询指定范围内数据
情况一: 数据库:只有point类型的location字段 实体类:有经纬度字段(double).originLoction字段(存放string类型的数据库location字段:POINT(123. ...
- 痞子衡嵌入式:IVT里的不同entry设置可能会造成i.MXRT1xxx系列启动App后发生异常跑飞
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是IVT里的不同entry设置可能会造成i.MXRT1xxx系列启动App后发生异常跑飞问题的分析解决经验. 事情缘起恩智浦官方论坛上的一 ...
- IT行业程序开发如何
学习程序开发怎么样,由于软件开发涉及到的知识结构比较丰富,所以学习软件开发通常需要一个系统的学习过程.如果未来要想专业从事软件开发工作,那么可以按照以下步骤学习软件开发技术: 第一:从计算机操作系统开 ...
- 分别用canvas和css3的transform做出钟表的效果
两种方式实际上在js上的原理都是一样的.都是获取时间对象,再获取时间对象的时分秒,时分秒乘以其旋转一刻度(一秒.一分.一小时)对应的角度.css3中要赋值于transform:rotate(角度),c ...
- DASH流媒体MPD中的segmentTemplate
SegmentTemplate利用MPD中的属性代入公式计算可以得到相关通配符的数值,来提供给客户端进行相关地址解析.相较于segmentList,使用 SegmentTemplate 的方式,能够很 ...
- Node.js 从零开发 web server博客项目[日志]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- adrci清理日志
adrci> show home adrci> set home diag/rdbms/mesp/MESP adrci> help purge adrci> purge -ag ...
- 结构体排序中sort的自定义函数cmp()
水题链接 #include<iostream> #include<cstdio> #include<algorithm> using namespace std; ...
- Windows10数字权利永久激活教程
很多人用Windows10系统,但是没有办法激活,这个教程一定会让你永久激活windows10系统(并非ksm) 打开设置,查看是否激活 如果激活的话,先退掉秘钥,在Windows power ...