clsDrawChart.cs
25.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
///包含用于定义常用值和引用数据类型、事件和事件处理程序、接口、属性和处理异常的基础类和基类。其他类提供支持下列操作的服务:数据类型转换,方法参数操作,数学计算,远程和本地程序调用,应用程序环境管理以及对托管和非托管应用程序的监管
using System;
///包含定义各种对象集合(如列表、队列、位数组、哈希表和字典)的接口和类。
using System.Collections;
///包含定义泛型集合的接口和类;泛型集合允许用户创建强类型的集合,这种集合在类型安全和性能上均优于非泛型强类型集合
using System.Collections.Generic;
///提供用于实现组件和控件的运行时和设计时行为的类。此命名空间包括用于属性和类型转换器的实现、数据源绑定和组件授权的基类和接口
using System.ComponentModel;
///命名空间提供支持使用语言集成查询 (LINQ) 进行查询的类和接口
using System.Linq;
///包含表示 ASCII、Unicode、UTF-7 和 UTF-8 字符编码的类;用于在字符块和字节块之间相互转换的抽象基类;以及不需要创建字符串的中间实例就可以操作和格式化字符串对象的帮助器类
using System.Text;
///包含用于创建基于 Windows 的应用程序的类,这些应用程序可以充分利用 Microsoft Windows 操作系统中的丰富用户界面功能
using System.Windows.Forms;
///这个名称空间提供了一系列的类来对COM对象进行操作 一般用到其中的DllImport,它用来调用windows中一些DLL的函数(Windows API),或调用自己用c++写的DLL中的函数
using System.Runtime.InteropServices;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
using System.Net;
///提供对 GDI+ 基本图形功能的访问。System.Drawing.Drawing2D、System.Drawing.Imaging 和 System.Drawing.Text 命名空间中提供了更高级的功能
using System.Drawing;
using System.Drawing.Imaging;
using System.CodeDom;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Security.Cryptography;
namespace MyClassLib
{
public class clsDrawChart
{
private static AChart chart = new AChart();
private static Object[] ChartColor = { Color.Green, Color.Blue, Color.Red, Color.Orange, Color.Cyan, Color.Purple, Color.Coral, Color.Chocolate, Color.Gray, Color.Gold, Color.Lavender, Color.Linen, Color.Magenta, Color.Moccasin, Color.Navy, Color.Olive, Color.Peru, Color.Plum, Color.Purple, Color.Salmon, Color.Sienna, Color.Silver, Color.Tan, Color.Tomato, Color.Violet, Color.Turquoise, Color.Transparent };
/// <summary>
/// 填充饼图、直方图、曲线图到容器里
/// </summary>
/// <param name="chartTitle">标题</param>
/// <param name="control">容器(Panel,Form,TabPage)</param>
/// <param name="dataSet">对Table[0]进行操作,饼图取最前两列,第一列为名字,第二列为值。单数据直方图取最前两列,第一列为横轴每列名称,第二列为值。多数据直方图第一列为横轴父项名称,然后依次取前一列为横轴每列名称,后一列为值。曲线图取第一列为横轴每列名称,往后每列都代表一条曲线,列名为曲线名称。</param>
/// <param name="chartType">图表类型</param>
/// <param name="minNumber">刻度最小值,此参数对饼图无效</param>
/// <param name="maxNumber">刻度最大值,此参数对饼图无效</param>
/// <param name="scale">刻度值,此参数对饼图无效</param>
/// <param name="unit">值的单位,此参数对饼图无效</param>
public static void DrawingChart(string chartTitle, Control control, DataSet dataset, ChartType chartType, int minNumber, int maxNumber, int scale, string unit)
{
DrawingChartInclude(chartTitle, control, dataset, chartType, minNumber, maxNumber, scale, unit);
}
/// <summary>
/// 填充饼图到容器里
/// </summary>
/// <param name="chartTitle">标题</param>
/// <param name="control">容器(Panel,Form,TabPage)</param>
/// <param name="dataSet">对Table[0]进行操作,取最前两列,第一列为名字,第二列为值</param>
public static void DrawingChart(string chartTitle, Control control, DataSet dataSet)
{
DrawingChartInclude(chartTitle, control, dataSet, ChartType.Piegraph, 0, 0, 0, "");
}
private static void DrawingChartInclude(string chartTitle, Control control, DataSet dataSet, ChartType chartType, int minNumber, int maxNumber, int scale, string unit)
{
if (control.Height < 230 || control.Width < 230)
{
MessageBox.Show("容器宽度或长度设置过小!(宽度和长度必须>=230)");
return;
}
if (control.GetType().Name.Equals("Form") || control.GetType().Name.Equals("TabPage") || control.GetType().Name.Equals("GroupBox") || control.GetType().Name.Equals("Panel"))
{
for (int i = 0; i < chart.Count; i++)
{
if (control == (Control)chart[i].crtObj)
{
chart.RemoveChart(i);
break;
}
}
chart.AddChart(chartTitle, control, chartType, dataSet, minNumber, maxNumber, scale, unit);
control.Paint += new PaintEventHandler(control_Paint);
control.Disposed += new EventHandler(control_Disposed);
control.Refresh();
}
else
{
MessageBox.Show(("该容器不是Form,TabPage,GroupBox,Panel类型!"));
return;
}
}
private static void control_Paint(object sender, PaintEventArgs e)
{
try
{
//chart.Count 是表示需要绘制图表上实例的个数
for (int i = 0; i < chart.Count; i++)
{
if (chart[i].crtType == ChartType.None)
{
break;
}
else if (chart[i].crtObj != sender)
{
continue;
}
int x = 0;
int y = 0;
#region
//step1:确定绘制图表的绘制区域 绘制区域-100 是因为标题Title占用了
int diameter = Math.Min(((Control)chart[i].crtObj).Height, ((Control)chart[i].crtObj).Width) - 100;
//step2:确定绘制标题Title的起点。 如果绘制区域的宽度大于高度
if (((Control)chart[i].crtObj).Width >= ((Control)chart[i].crtObj).Height)
{
x = (((Control)chart[i].crtObj).Width - ((Control)chart[i].crtObj).Height) / 2 + 50;
y = 50;
}
else
{
x = 50;
y = (((Control)chart[i].crtObj).Height - ((Control)chart[i].crtObj).Width) / 2 + 50;
}
#endregion
//step3:绘制标题Title的边框
Pen line = new Pen(Color.Black, 2);
SolidBrush linesb = new SolidBrush(Color.DarkGreen);
Rectangle rect = new Rectangle(x, y, diameter, diameter);
//step4:绘制标题的文字
Pen title = new Pen(Color.Blue, 2);
SolidBrush B = new SolidBrush(Color.Blue);
StringFormat Ll = new StringFormat();
Ll.Alignment = System.Drawing.StringAlignment.Near;
int height = ((Control)chart[i].crtObj).Height - 20;
RectangleF RectTitle = new RectangleF(5, 5, diameter, diameter);
e.Graphics.DrawString(chart[i].crtTitle, new Font("Arial", 14), B, RectTitle, Ll);
#region 饼图
if (chart[i].crtType == ChartType.Piegraph)
{
float total = 0;
float fontWidth = 0;
//step1: 确定绘制饼图的总量和字体的宽度 (取最大的字体宽度)
for (int j = 0; j < chart[i].crtDataSet.Tables[0].Rows.Count; j++)
{
total += (float)Convert.ToDouble(chart[i].crtDataSet.Tables[0].Rows[j][1].ToString());
if (fontWidth < e.Graphics.MeasureString(chart[i].crtDataSet.Tables[0].Rows[j][0].ToString(), new Font("Arial", 10)).Width)
{
fontWidth = e.Graphics.MeasureString(chart[i].crtDataSet.Tables[0].Rows[j][0].ToString(), new Font("Arial", 10)).Width;
}
}
//step2:绘制饼图的各个对象
float next = 0;
for (int j = 0; j < chart[i].crtDataSet.Tables[0].Rows.Count; j++)
{
//step3:选择画刷的颜色
SolidBrush sb = new SolidBrush((Color)ChartColor[j % ChartColor.Length]);
//step4:确定绘制的角度
float tmp = (float)(Convert.ToDouble(chart[i].crtDataSet.Tables[0].Rows[j][1].ToString()) / total * 100 * 3.6);
//step5:填充饼图
e.Graphics.FillPie(sb, rect, next, tmp);
//step6:绘制该对象的标题
RectTitle = new RectangleF(5, height, diameter, diameter);
B = new SolidBrush(Color.Black);
e.Graphics.DrawString(chart[i].crtDataSet.Tables[0].Rows[j][0].ToString(), new Font("Arial", 10), B, RectTitle, Ll);
//step7:绘制该对象的颜色表示示例矩形
RectTitle = new RectangleF(5 + (int)fontWidth + 2, height, 10, 12);
e.Graphics.FillRectangle(sb, RectTitle);
//step8:绘制该对象的颜色表示示例矩形
RectTitle = new RectangleF(x + diameter + 2, height, 10, 12);
e.Graphics.FillRectangle(sb, RectTitle);
//step9:绘制该对象的所占总量的百分比
RectTitle = new RectangleF(x + diameter + 14, height, diameter, diameter);
e.Graphics.DrawString(Convert.ToString(Math.Round((Convert.ToDouble(chart[i].crtDataSet.Tables[0].Rows[j][1].ToString()) / total * 100), 0)) + "%", new Font("Arial", 10), B, RectTitle, Ll);
//step10: 确定下一个对象的绘制开始角度
height -= 20;
next += tmp;
}
}
#endregion
#region 直方图
else if (chart[i].crtType == ChartType.Histogram)
{
//step1:绘制一条直线即---Y轴
if (x >= 100)
{
x = 100;
}
else if (y >= 100)
{
y = 100;
}
e.Graphics.DrawLine(line, x, y, x, y + diameter);
//step2:绘制的区间高度
height = y + diameter - 12;
//step3:计算绘制的刻度区间有多少个
int scaleBS = diameter / ((chart[i].crtMaxNumber - chart[i].crtMinNumber) / chart[i].crtScale);
//step4:计算字体的高度
float fontWidth = e.Graphics.MeasureString(chart[i].crtMaxNumber.ToString(), new Font("Arial", 10)).Width;
//step5:绘制Y轴的刻度值
for (int j = chart[i].crtMinNumber; j <= chart[i].crtMaxNumber; j += chart[i].crtScale)
{
RectangleF RectScaleHeightLine = new RectangleF(x - fontWidth - 1, height, diameter, diameter);
e.Graphics.DrawString(j.ToString(), new Font("Arial", 10), linesb, RectScaleHeightLine, Ll);
height -= scaleBS;
}
//step6:确认直方图的绘制高度
height = y + diameter;
int width = x + 10;
//step7:确认直方图有多少个对象
for (int j = 0; j < chart[i].crtDataSet.Tables[0].Rows.Count; j++)
{
if (chart[i].crtDataSet.Tables[0].Columns.Count <= 2)
{
SolidBrush sb = new SolidBrush((Color)ChartColor[j % ChartColor.Length]);
RectangleF RectScale;
//绘制项目名称
if (j % 3 == 0)
{
RectScale = new RectangleF(width, height, diameter, diameter);
}
else if (j % 3 == 1)
{
RectScale = new RectangleF(width, height + 15, diameter, diameter);
}
else
{
RectScale = new RectangleF(width, height + 30, diameter, diameter);
}
e.Graphics.DrawString(chart[i].crtDataSet.Tables[0].Rows[j][0].ToString(), new Font("Arial", 8), sb, RectScale, Ll);
double aa = Convert.ToDouble(chart[i].crtDataSet.Tables[0].Rows[j][1].ToString()) / chart[i].crtScale * scaleBS;
RectScale = new RectangleF(width, y + diameter - (int)aa, 20, (int)aa);
e.Graphics.FillRectangle(sb, RectScale);
RectScale = new RectangleF(width, y + diameter - (int)aa - 15, diameter, diameter);
e.Graphics.DrawString(chart[i].crtDataSet.Tables[0].Rows[j][1].ToString() + chart[i].crtUnit, new Font("Arial", 9), sb, RectScale);
}
else
{
int sbnum = 0;
RectangleF RectScale = new RectangleF(width, height + 24, diameter, diameter);
e.Graphics.DrawString(chart[i].crtDataSet.Tables[0].Rows[j][0].ToString(), new Font("Arial", 10), B, RectScale, Ll);
for (int k = 1; k < chart[i].crtDataSet.Tables[0].Columns.Count; k += 2)
{
SolidBrush sb = new SolidBrush((Color)ChartColor[sbnum % ChartColor.Length]);
sbnum++;
RectScale = new RectangleF(width, height + (sbnum % 2) * 12, diameter, diameter);
e.Graphics.DrawString(chart[i].crtDataSet.Tables[0].Rows[j][k].ToString(), new Font("Arial", 8), sb, RectScale, Ll);
double aa = Convert.ToDouble(chart[i].crtDataSet.Tables[0].Rows[j][k + 1].ToString()) / chart[i].crtScale * scaleBS;
RectScale = new RectangleF(width, y + diameter - (int)aa, 20, (int)aa);
e.Graphics.FillRectangle(sb, RectScale);
RectScale = new RectangleF(width, y + diameter - (int)aa - 15, diameter, diameter);
e.Graphics.DrawString(chart[i].crtDataSet.Tables[0].Rows[j][k + 1].ToString() + chart[i].crtUnit, new Font("Arial", 8), sb, RectScale);
width += 30;
}
}
if ((fontWidth + e.Graphics.MeasureString(chart[i].crtUnit, new Font("Arial", 8)).Width) >= 30)
{
//width += (int)fontWidth + (int)e.Graphics.MeasureString(chart[i].crtUnit, new Font("Arial", 8)).Width + 10;
width += (int)fontWidth + (int)e.Graphics.MeasureString(chart[i].crtUnit, new Font("Arial", 8)).Width + 5;
}
else
{
//间距
width += 30;
}
}
e.Graphics.DrawLine(line, x, y + diameter, width, y + diameter);
}
#endregion
#region 曲线图
else if (chart[i].crtType == ChartType.Polygram)
{
int width = x;
int scaleBS = diameter / ((chart[i].crtMaxNumber - chart[i].crtMinNumber) / chart[i].crtScale);
int scaleWidthBS = diameter / chart[i].crtDataSet.Tables[0].Rows.Count;
height = y + diameter - ((chart[i].crtMaxNumber - chart[i].crtMinNumber) / chart[i].crtScale) * scaleBS;
float fontWidth = e.Graphics.MeasureString(chart[i].crtMaxNumber.ToString(), new Font("Arial", 10)).Width;
for (int j = 0; j <= chart[i].crtDataSet.Tables[0].Rows.Count; j++)
{
e.Graphics.DrawLine(line, width, height, width, y + diameter);
width += scaleWidthBS;
}
height = y + diameter - 12;
for (int j = chart[i].crtMinNumber; j <= chart[i].crtMaxNumber; j += chart[i].crtScale)
{
e.Graphics.DrawLine(line, x, height + 12, x + scaleWidthBS * chart[i].crtDataSet.Tables[0].Rows.Count, height + 12);
RectangleF RectScaleHeightLine = new RectangleF(x - fontWidth - 1, height, diameter, diameter);
e.Graphics.DrawString(j.ToString(), new Font("Arial", 10), linesb, RectScaleHeightLine, Ll);
height -= scaleBS;
}
width = x;
height = y + diameter;
double nowheight = 0;
for (int j = 0; j < chart[i].crtDataSet.Tables[0].Rows.Count; j++)
{
height = y + diameter;
SolidBrush textsb = new SolidBrush(Color.Black);
RectangleF rectText = new RectangleF(width + scaleWidthBS, height + 2 + (j % 2) * 12, diameter, diameter);
e.Graphics.DrawString(chart[i].crtDataSet.Tables[0].Rows[j][0].ToString(), new Font("Arial", 10), textsb, rectText);
//
for (int k = 1; k < chart[i].crtDataSet.Tables[0].Columns.Count; k++)
{
Pen polygramLine = new Pen((Color)ChartColor[(k - 1) % ChartColor.Length]);
SolidBrush valueSB = new SolidBrush((Color)ChartColor[(k - 1) % ChartColor.Length]);
double aa = y + diameter;
if (j > 0)
{
aa = y + diameter - Convert.ToDouble(chart[i].crtDataSet.Tables[0].Rows[j - 1][k].ToString()) / chart[i].crtScale * scaleBS;
}
nowheight = y + diameter - Convert.ToDouble(chart[i].crtDataSet.Tables[0].Rows[j][k].ToString()) / chart[i].crtScale * scaleBS;
e.Graphics.DrawLine(polygramLine, width, (int)aa, width + scaleWidthBS, (int)nowheight);
RectangleF RectScale = new RectangleF(width + scaleWidthBS, (int)nowheight - 12, diameter, diameter);
e.Graphics.DrawString(chart[i].crtDataSet.Tables[0].Rows[j][k].ToString() + chart[i].crtUnit, new Font("Arial", 10), valueSB, RectScale);
}
width += scaleWidthBS;
}
fontWidth = 0;
for (int j = 1; j < chart[i].crtDataSet.Tables[0].Columns.Count; j++)
{
if (fontWidth < e.Graphics.MeasureString(chart[i].crtDataSet.Tables[0].Columns[j].ColumnName.ToString(), new Font("Arial", 10)).Width)
{
fontWidth = e.Graphics.MeasureString(chart[i].crtDataSet.Tables[0].Columns[j].ColumnName.ToString(), new Font("Arial", 10)).Width;
}
}
height = y + diameter;
for (int j = 1; j < chart[i].crtDataSet.Tables[0].Columns.Count; j++)
{
RectTitle = new RectangleF(5, height, diameter, diameter);
B = new SolidBrush(Color.Black);
SolidBrush valueSB = new SolidBrush((Color)ChartColor[(j - 1) % ChartColor.Length]);
e.Graphics.DrawString(chart[i].crtDataSet.Tables[0].Columns[j].ColumnName.ToString(), new Font("Arial", 10), B, RectTitle, Ll);
RectTitle = new RectangleF(5 + (int)fontWidth + 2, height, 15, 12);
e.Graphics.FillRectangle(valueSB, RectTitle);
height -= 20;
}
}
#endregion
break;
}
}
catch
{
return;
}
}
private static void control_Disposed(object sender, EventArgs e)
{
for (int i = 0; i < chart.Count; i++)
{
if ((Control)sender == (Control)chart[i].crtObj)
{
chart.RemoveChart(i);
break;
}
}
}
}
public class pChart
{
public Object crtObj;
public ChartType crtType;
public DataSet crtDataSet;
public string crtTitle = "";
public int crtMinNumber = 0;
public int crtMaxNumber = 0;
public int crtScale = 0;
public string crtUnit = "";
public pChart(string chartTitle, Object chartObject, ChartType chartType, DataSet chartDataSet, int minNumber, int maxNumber, int scale, string unit)
{
crtObj = chartObject;
crtType = chartType;
crtDataSet = chartDataSet;
crtTitle = chartTitle;
crtMinNumber = minNumber;
crtMaxNumber = maxNumber;
crtScale = scale;
crtUnit = unit;
}
}
public class AChart : System.Collections.CollectionBase
{
public AChart()
{ }
/// <summary>
/// 新增实例方法
/// </summary>
/// <param name="chartTitle"></param>
/// <param name="chartObject"></param>
/// <param name="chartType"></param>
/// <param name="chartDataSet"></param>
/// <param name="minNumber"></param>
/// <param name="maxNumber"></param>
/// <param name="scale"></param>
/// <param name="unit"></param>
public void AddChart(string chartTitle, Object chartObject, ChartType chartType, DataSet chartDataSet, int minNumber, int maxNumber, int scale, string unit)
{
List.Add(new pChart(chartTitle, chartObject, chartType, chartDataSet, minNumber, maxNumber, scale, unit));
}
public void RemoveChart(int index)
{
if (index > Count - 1 || index < 0)
{
MessageBox.Show("Index not valid!");
}
else
{
List.RemoveAt(index);
}
}
public AChart Item
{
get
{
return this;
}
}
[System.Runtime.CompilerServices.IndexerName("item")]
public pChart this[int index]
{
get
{
return (pChart)List[index];
}
}
}
/// <summary>
/// 图表类型
/// </summary>
public enum ChartType
{
/// <summary>
/// 直方图
/// </summary>
Histogram,
/// <summary>
/// 饼图
/// </summary>
Piegraph,
/// <summary>
/// 曲线图
/// </summary>
Polygram,
/// <summary>
/// None
/// </summary>
None
}
}