SurChart.cs
13.4 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace Asa.Face
{
/// <summary>
/// 图表控件
/// </summary>
[DefaultProperty("Text")]
[DefaultEvent("Click")]
public partial class SurChart : BaseCtl
{
private RectangleF _chartArea = new RectangleF(); //图表区域
private RectangleF _textArea = new RectangleF();
private List<Curve> _gridCurve = new List<Curve>();
private RectangleF[] _xTextRect; //X轴刻度位置
private string[] _xText; //X轴刻度的文本
private RectangleF _xTitleRect; //X轴标题位置
private string _xTitle; //X轴标题的文本
private Line[] _xGrid; //X轴网格线
private float _xGridWidth; //X轴网格宽度
private float _xValueWidth; //X轴实际点的宽度
private int _xStep; //X轴网格间隔
private int _xMaxValue = 1; //X轴最大值
private int _xCount; //X轴显示的数量
private RectangleF[] _yTextRect; //Y轴刻度位置
private string[] _yText; //Y轴刻度的文本
private RectangleF _yTitleRect; //Y轴标题位置
private string _yTitle; //Y轴标题的文本
private Line[] _yGrid; //Y轴网格线
private float _yGridHeight; //Y轴网格高度
private float _yValueHeight; //X轴实际点的高度
private float _yStep; //Y轴网格间隔
private float _yMaxValue = 0; //Y轴最大值
private int _yCount; //X轴显示的数量
private static readonly Font AXIS_FONT = new Font("宋体", 9f);
private static readonly Pen AXIS_PEN = new Pen(Color.FromArgb(70, 70, 70), 1);
//private static readonly Pen CURVE = new Pen(Common.BLUE_COLOR, 1);
/// <summary>
/// 图表控件
/// </summary>
public SurChart()
{
InitializeComponent();
_xTitle = "X";
_yTitle = "Y";
InitX(10);
InitY(10);
}
//==========属性==========
/// <summary>
/// X轴标题
/// </summary>
[Browsable(true), Category(""), Description("X轴标题"), DefaultValue("X")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string XAxisTitle
{
set
{
_xTitle = value;
CalcSize();
Refresh();
}
get => _xTitle;
}
/// <summary>
/// Y轴标题
/// </summary>
[Browsable(true), Category(""), Description("Y轴标题"), DefaultValue("Y")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string YAxisTitle
{
set
{
_yTitle = value;
CalcSize();
Refresh();
}
get => _yTitle;
}
/// <summary>
/// X轴显示总数
/// </summary>
[Browsable(true), Category(""), Description("X轴显示总数"), DefaultValue(10)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int XAxisCount
{
set
{
InitX(value);
CalcSize();
Refresh();
}
get => _xCount;
}
/// <summary>
/// Y轴显示总数
/// </summary>
[Browsable(true), Category(""), Description("Y轴显示总数"), DefaultValue(10)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int YAxisCount
{
set
{
InitY(value);
CalcSize();
Refresh();
}
get => _yCount;
}
//==========方法==========
/// <summary>
/// 添加曲线
/// </summary>
/// <returns></returns>
public int AddCurve()
{
_gridCurve.Add(new Curve());
return _gridCurve.Count - 1;
}
/// <summary>
/// 添加曲线
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public int AddCurve(Color color)
{
if (color.IsEmpty)
_gridCurve.Add(new Curve());
else
_gridCurve.Add(new Curve(color));
return _gridCurve.Count - 1;
}
/// <summary>
/// 添加XY坐标
/// </summary>
/// <param name="index"></param>
/// <param name="x"></param>
/// <param name="y"></param>
public void AddXY(int index, int x, float y)
{
if (index < 0 || index >= _gridCurve.Count) return;
_gridCurve[index].Point.Add(new GridPoint(x, y));
if (x > _xMaxValue) _xMaxValue = x;
if (y > _yMaxValue) _yMaxValue = y;
CalcSize();
Refresh();
}
/// <summary>
/// 添加XY数组坐标
/// </summary>
/// <param name="index"></param>
/// <param name="x"></param>
/// <param name="y"></param>
public void AddXY(int index, int[] x, float[] y)
{
if (index < 0 || index >= _gridCurve.Count) return;
int xx = 0;
float yy = 0;
for (int i = 0; i < x.Length; i++)
{
_gridCurve[index].Point.Add(new GridPoint(x[i], y[i]));
if (x[i] > xx) xx = x[i];
if (y[i] > yy) yy = y[i];
}
if (xx > _xMaxValue) _xMaxValue = xx;
if (yy > _yMaxValue) _yMaxValue = yy;
CalcSize();
Refresh();
}
/// <summary>
/// 清除所有曲线
/// </summary>
public void ClearAll()
{
_gridCurve.Clear();
_xMaxValue = 1;
_yMaxValue = 0;
CalcSize();
Refresh();
}
/// <summary>
/// 清除指定曲线
/// </summary>
/// <param name="index"></param>
public void Clear(int index)
{
if (index < 0 || index >= _gridCurve.Count) return;
_gridCurve.RemoveAt(index);
if (_gridCurve.Count == 0)
{
_xMaxValue = 1;
_yMaxValue = 0;
}
CalcSize();
Refresh();
}
//==========私有==========
/// <summary>
/// 计算位置大小
/// </summary>
protected override void CalcSize()
{
base.CalcSize();
if (_xGrid == null || _yGrid == null) return;
if (_xCount == 0 || _yCount == 0) return;
float left = 70;
float bottom = 50;
float top = 40;
float right = 20;
SizeF sf;
Graphics g = CreateGraphics();
sf = g.MeasureString(Text, Font);
_textArea = new RectangleF((Width - sf.Width) / 2f, BorderWidth + (top - sf.Height) / 2f, sf.Width, sf.Height);
_chartArea = new RectangleF(left, top, Width - left - right, Height - top - bottom);
sf = g.MeasureString(_xTitle, Font);
_xTitleRect = new RectangleF(_chartArea.X + (_chartArea.Width - sf.Width) / 2, Height - sf.Height - 5, sf.Width, sf.Height);
sf = g.MeasureString(_yTitle, Font);
_yTitleRect = new RectangleF(-(_chartArea.Height - sf.Width) / 2 - sf.Width - _chartArea.Y, 5, sf.Width, sf.Height);
_xStep = Convert.ToInt32(Math.Ceiling((float)_xMaxValue / _xCount));
_xGridWidth = _chartArea.Width / _xCount;
_xValueWidth = _chartArea.Width / (_xStep * _xCount);
for (int i = 0; i < _xCount; i++)
{
float ww = _xGridWidth * (i + 1);
_xGrid[i] = new Line();
_xGrid[i].Head = new PointF(_chartArea.X + ww, _chartArea.Y);
_xGrid[i].End = new PointF(_chartArea.X + ww, _chartArea.Bottom);
_xText[i] = (_xStep * (i + 1)).ToString();
sf = g.MeasureString(_xText[i], AXIS_FONT);
_xTextRect[i] = new RectangleF(_chartArea.X + ww - sf.Width / 2, _chartArea.Bottom + 5, sf.Width, sf.Height);
}
_yStep = _yMaxValue / _yCount;
_yGridHeight = _chartArea.Height / _yCount;
_yValueHeight = _chartArea.Height / (_yStep * _yCount);
for (int i = 0; i < _yCount; i++)
{
float hh = _yGridHeight * (i + 1);
_yGrid[i] = new Line();
_yGrid[i].Head = new PointF(_chartArea.X, _chartArea.Bottom - hh);
_yGrid[i].End = new PointF(_chartArea.Right, _chartArea.Bottom - hh);
_yText[i] = string.Format("{0:0.0000}", _yStep * (i + 1));
sf = g.MeasureString(_yText[i], AXIS_FONT);
_yTextRect[i] = new RectangleF(_chartArea.X - sf.Width, _chartArea.Bottom - hh - sf.Height / 2, sf.Width, sf.Height);
}
for (int i = 0; i < _gridCurve.Count; i++)
{
PointF[] item = new PointF[_gridCurve[i].Point.Count];
for (int j = 0; j < item.Length; j++)
item[j] = new PointF(_chartArea.X + _gridCurve[i].Point[j].X * _xValueWidth, _chartArea.Bottom - _gridCurve[i].Point[j].Y * _yValueHeight);
_gridCurve[i].Pixel = item;
}
}
private void InitX(int n)
{
_xCount = n;
_xGrid = new Line[n];
_xTextRect = new RectangleF[n];
_xText = new string[n];
}
private void InitY(int n)
{
_yCount = n;
_yGrid = new Line[n];
_yTextRect = new RectangleF[n];
_yText = new string[n];
}
private void SurChart_Paint(object sender, PaintEventArgs e)
{
BufferedGraphicsContext current = BufferedGraphicsManager.Current;
BufferedGraphics bg = current.Allocate(e.Graphics, e.ClipRectangle);
Graphics g = bg.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.Clear(Common.BACK_COLOR); //背景
//控件边框
if (Inside && Enabled)
{
if (BorderWidth > 0)
g.DrawRectangle(BorderInsidePen, BorderRect);
}
else
{
if (BorderWidth > 0)
g.DrawRectangle(BorderOutsidePen, BorderRect);
}
//X轴
if (_xGrid != null)
{
for (int i = 0; i < _xGrid.Length; i++)
{
g.DrawLine(AXIS_PEN, _xGrid[i].Head, _xGrid[i].End);
g.DrawString(_xText[i], AXIS_FONT, Common.FORE_BRUSH, _xTextRect[i]);
}
}
//Y轴
if (_yGrid != null)
{
for (int i = 0; i < _yGrid.Length; i++)
{
g.DrawLine(AXIS_PEN, _yGrid[i].Head, _yGrid[i].End);
g.DrawString(_yText[i], AXIS_FONT, Common.FORE_BRUSH, _yTextRect[i]);
}
}
//曲线
//if (_gridPixel.Length > 1)
// g.DrawLines(CURVE, _gridPixel);
for (int i = 0; i < _gridCurve.Count; i++)
{
if (_gridCurve[i].Pixel == null) continue;
g.DrawLines(_gridCurve[i].Pen, _gridCurve[i].Pixel);
}
g.DrawString(Text, Font, Common.FORE_BRUSH, _textArea);
SizeF sf = g.MeasureString("0", Font);
g.DrawString("0", Font, Common.FORE_BRUSH, _chartArea.X - sf.Width, _chartArea.Bottom + 5);
g.DrawRectangle(AXIS_PEN, _chartArea.X, _chartArea.Y, _chartArea.Width, _chartArea.Height);
g.DrawString(_xTitle, Font, Common.FORE_BRUSH, _xTitleRect);
g.RotateTransform(-90);
g.DrawString(_yTitle, Font, Common.FORE_BRUSH, _yTitleRect);
g.ResetTransform();
bg.Render();
bg.Dispose();
}
private void SurChart_Load(object sender, EventArgs e)
{
InitX(10);
InitY(10);
CalcSize();
}
private class Line
{
public PointF Head;
public PointF End;
}
private class GridPoint
{
public int X;
public float Y;
public GridPoint(int x, float y)
{
X = x;
Y = y;
}
}
private class Curve
{
public List<GridPoint> Point = new List<GridPoint>();
public PointF[] Pixel = null;
public Pen Pen = new Pen(Common.BLUE_COLOR, 1);
public Curve()
{ }
public Curve(Color color)
{
Pen = new Pen(color, 1);
}
}
}
}