DrawGraphicsControl.xaml.cs 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection.Emit;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Animation;
  15. using System.Windows.Media.Imaging;
  16. namespace Venus_Themes.UserControls
  17. {
  18. /// <summary>
  19. /// DrawGraphicsControl.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class DrawGraphicsControl : UserControl
  22. {
  23. private double m_WavePlotX = 0;//x轴起始点
  24. private double m_WavePlotY = 0;//y轴起始点
  25. private double m_WavePlotWidth = 5000;//x轴长度
  26. private double m_WavePlotHeight = 100;//y轴长度
  27. private double m_WavePlotMinX = double.MaxValue;
  28. private double m_WavePlotMaxX = double.MinValue;
  29. private double m_WavePlotMinY = double.MaxValue;
  30. private double m_WavePlotMaxY = double.MinValue;
  31. private double m_StartDrawLineX = 0;
  32. private System.Drawing.Brush m_BrushText = System.Drawing.Brushes.Black;
  33. private System.Drawing.Brush m_BrushXYText = System.Drawing.Brushes.DarkRed;
  34. private System.Drawing.Pen m_PenLine;
  35. private System.Drawing.Pen m_PenLightGray;
  36. private System.Drawing.Pen m_PenBlue;
  37. private System.Drawing.Pen m_PenData;
  38. private System.Drawing.Pen m_PenSData;
  39. private System.Drawing.Pen m_PenASData;
  40. private System.Drawing.Pen[] m_PenCollencteions;
  41. private bool m_StartMouseMove = false;
  42. private bool m_MouseMove = false;
  43. private bool m_MouseEnter = false;
  44. private System.Windows.Point m_StartPoint;
  45. private System.Windows.Point m_EndPoint;
  46. private System.Windows.Point m_CurrentPoint;
  47. private float LineThickness = 2.0f;
  48. #region Property
  49. public PointCollection PlotDataPoints
  50. {
  51. get { return (PointCollection)GetValue(PlotDataProperty); }
  52. set { SetValue(PlotDataProperty, value); GraphicDraw(); }
  53. }
  54. /// <summary>
  55. /// Identifies the DrawGraphicsControl.PlotDataPoints attached property.
  56. /// </summary>
  57. public static readonly DependencyProperty PlotDataProperty =
  58. DependencyProperty.RegisterAttached("PlotDataPoints", typeof(PointCollection), typeof(DrawGraphicsControl));
  59. public PointCollection PlotSDataPoints
  60. {
  61. get { return (PointCollection)GetValue(PlotSDataProperty); }
  62. set { SetValue(PlotSDataProperty, value); GraphicDraw(); }
  63. }
  64. /// <summary>
  65. /// Identifies the DrawGraphicsControl.PlotSDataPoints attached property.
  66. /// </summary>
  67. public static readonly DependencyProperty PlotSDataProperty =
  68. DependencyProperty.RegisterAttached("PlotSDataPoints", typeof(PointCollection), typeof(DrawGraphicsControl));
  69. public PointCollection PlotASDataPoints
  70. {
  71. get { return (PointCollection)GetValue(PlotASDataProperty); }
  72. set { SetValue(PlotASDataProperty, value); GraphicDraw(); }
  73. }
  74. /// <summary>
  75. /// Identifies the DrawGraphicsControl.PlotASDataPoints attached property.
  76. /// </summary>
  77. public static readonly DependencyProperty PlotASDataProperty =
  78. DependencyProperty.RegisterAttached("PlotASDataPoints", typeof(PointCollection), typeof(DrawGraphicsControl));
  79. public List<PointCollection> PointCollections
  80. {
  81. get { return (List<PointCollection>)GetValue(PointCollectionsProperty); }
  82. set { SetValue(PointCollectionsProperty, value); GraphicDraw(); }
  83. }
  84. /// <summary>
  85. /// Identifies the DrawGraphicsControl.PointCollections attached property.
  86. /// </summary>
  87. public static readonly DependencyProperty PointCollectionsProperty =
  88. DependencyProperty.RegisterAttached("PointCollections", typeof(List<PointCollection>), typeof(DrawGraphicsControl));
  89. public PointCollection Points
  90. {
  91. get { return (PointCollection)GetValue(PointsProperty); }
  92. set { SetValue(PointsProperty, value); GraphicDraw(); }
  93. }
  94. /// <summary>
  95. /// Identifies the DrawGraphicsControl.Points attached property.
  96. /// </summary>
  97. public static readonly DependencyProperty PointsProperty =
  98. DependencyProperty.RegisterAttached("Points", typeof(PointCollection), typeof(DrawGraphicsControl));
  99. #region 起始坐标轴
  100. public double WavePlotOriginX
  101. {
  102. get { return (double)GetValue(WavePlotOriginXProperty); }
  103. set { SetValue(WavePlotOriginXProperty, value); }
  104. }
  105. public static readonly DependencyProperty WavePlotOriginXProperty =
  106. DependencyProperty.Register("WavePlotOriginX", typeof(double), typeof(DrawGraphicsControl), new PropertyMetadata(0.0));
  107. public double WavePlotOriginY
  108. {
  109. get { return (double)GetValue(WavePlotOriginYProperty); }
  110. set { SetValue(WavePlotOriginYProperty, value); }
  111. }
  112. public static readonly DependencyProperty WavePlotOriginYProperty =
  113. DependencyProperty.Register("WavePlotOriginY", typeof(double), typeof(DrawGraphicsControl), new PropertyMetadata(0.0));
  114. public double WavePlotOriginWidth
  115. {
  116. get { return (double)GetValue(WavePlotOriginWidthProperty); }
  117. set { SetValue(WavePlotOriginWidthProperty, value); }
  118. }
  119. public static readonly DependencyProperty WavePlotOriginWidthProperty =
  120. DependencyProperty.Register("WavePlotOriginWidth", typeof(double), typeof(DrawGraphicsControl), new PropertyMetadata(1000.0));
  121. public double WavePlotOriginHeight
  122. {
  123. get { return (double)GetValue(WavePlotOriginHeightProperty); }
  124. set { SetValue(WavePlotOriginHeightProperty, value); }
  125. }
  126. public static readonly DependencyProperty WavePlotOriginHeightProperty =
  127. DependencyProperty.Register("WavePlotOriginHeight", typeof(double), typeof(DrawGraphicsControl), new PropertyMetadata(1000.0));
  128. #endregion
  129. public bool IsHorizontalNavigationEnabled
  130. {
  131. get { return (bool)GetValue(IsHorizontalNavigationEnabledProperty); }
  132. set { SetValue(IsHorizontalNavigationEnabledProperty, value); }
  133. }
  134. /// <summary>Identifies <see cref="IsHorizontalNavigationEnabled"/> property</summary>
  135. public static readonly DependencyProperty IsHorizontalNavigationEnabledProperty =
  136. DependencyProperty.Register("IsHorizontalNavigationEnabled", typeof(bool), typeof(DrawGraphicsControl), new PropertyMetadata(true));
  137. public bool IsVerticalNavigationEnabled
  138. {
  139. get { return (bool)GetValue(IsVerticalNavigationEnabledProperty); }
  140. set { SetValue(IsVerticalNavigationEnabledProperty, value); }
  141. }
  142. /// <summary>Identifies <see cref="IsVerticalNavigationEnabled"/> property</summary>
  143. public static readonly DependencyProperty IsVerticalNavigationEnabledProperty =
  144. DependencyProperty.Register("IsVerticalNavigationEnabled", typeof(bool), typeof(DrawGraphicsControl), new PropertyMetadata(true));
  145. public bool IsHorizontalDateTimeAxis
  146. {
  147. get { return (bool)GetValue(IsHorizontalDateTimeAxisProperty); }
  148. set { SetValue(IsHorizontalDateTimeAxisProperty, value); }
  149. }
  150. /// <summary>Identifies <see cref="IsVerticalNavigationEnabled"/> property</summary>
  151. public static readonly DependencyProperty IsHorizontalDateTimeAxisProperty =
  152. DependencyProperty.Register("IsHorizontalDateTimeAxis", typeof(bool), typeof(DrawGraphicsControl), new PropertyMetadata(false));
  153. #endregion Property
  154. public DrawGraphicsControl()
  155. {
  156. InitializeComponent();
  157. m_PenLine = new System.Drawing.Pen(System.Drawing.Brushes.Black, 1);
  158. m_PenLightGray = new System.Drawing.Pen(System.Drawing.Brushes.DarkGray, 1);
  159. m_PenBlue = new System.Drawing.Pen(System.Drawing.Brushes.DarkBlue, 1);
  160. m_PenData = new System.Drawing.Pen(System.Drawing.Brushes.Green, 1);
  161. m_PenSData = new System.Drawing.Pen(System.Drawing.Brushes.Blue, 1);
  162. m_PenASData = new System.Drawing.Pen(System.Drawing.Brushes.Red, 1);
  163. m_PenCollencteions = new System.Drawing.Pen[]
  164. {
  165. new System.Drawing.Pen(System.Drawing.Brushes.Green,LineThickness),
  166. new System.Drawing.Pen(System.Drawing.Brushes.Red,LineThickness),
  167. new System.Drawing.Pen(System.Drawing.Brushes.Blue,LineThickness),
  168. new System.Drawing.Pen(System.Drawing.Brushes.Orange,LineThickness),
  169. new System.Drawing.Pen(System.Drawing.Brushes.Yellow,LineThickness),
  170. new System.Drawing.Pen(System.Drawing.Brushes.YellowGreen,LineThickness),
  171. new System.Drawing.Pen(System.Drawing.Brushes.AliceBlue,LineThickness),
  172. new System.Drawing.Pen(System.Drawing.Brushes.Chocolate,LineThickness),
  173. new System.Drawing.Pen(System.Drawing.Brushes.Cyan,LineThickness),
  174. new System.Drawing.Pen(System.Drawing.Brushes.DarkGreen,LineThickness),
  175. new System.Drawing.Pen(System.Drawing.Brushes.LightBlue,LineThickness),
  176. new System.Drawing.Pen(System.Drawing.Brushes.DarkBlue,LineThickness),
  177. new System.Drawing.Pen(System.Drawing.Brushes.Pink,LineThickness),
  178. new System.Drawing.Pen(System.Drawing.Brushes.DarkViolet,LineThickness),
  179. new System.Drawing.Pen(System.Drawing.Brushes.Cyan,LineThickness),
  180. new System.Drawing.Pen(System.Drawing.Brushes.HotPink,LineThickness),
  181. };
  182. IsHorizontalNavigationEnabled = false;
  183. IsVerticalNavigationEnabled = false;
  184. }
  185. #region Public
  186. public void ResetCoordinate()
  187. {
  188. m_WavePlotX = WavePlotOriginX;
  189. m_WavePlotY = WavePlotOriginY;
  190. m_WavePlotWidth = WavePlotOriginWidth;
  191. m_WavePlotHeight = WavePlotOriginHeight;
  192. GraphicDraw();
  193. }
  194. public void ResetControl()
  195. {
  196. GraphicDraw();
  197. }
  198. public void FitControl()
  199. {
  200. if (!double.IsNaN(m_WavePlotMinX) && !double.IsNaN(m_WavePlotMaxX) && !double.IsNaN(m_WavePlotMinY) && !double.IsNaN(m_WavePlotMaxY)
  201. && m_WavePlotMinX != double.MaxValue && m_WavePlotMaxX != double.MinValue && m_WavePlotMinY != double.MaxValue && m_WavePlotMaxY != double.MinValue)
  202. {
  203. m_WavePlotX = m_WavePlotMinX;
  204. m_WavePlotWidth = m_WavePlotMaxX - m_WavePlotMinX;
  205. m_WavePlotY = m_WavePlotMinY-1;
  206. m_WavePlotHeight = m_WavePlotMaxY - m_WavePlotMinY+2;
  207. GraphicDraw();
  208. }
  209. }
  210. public void FitControlWithoutDrawing()
  211. {
  212. m_WavePlotX = m_WavePlotMinX;
  213. m_WavePlotWidth = m_WavePlotMaxX - m_WavePlotMinX;
  214. m_WavePlotY = m_WavePlotMinY - 1;
  215. m_WavePlotHeight = m_WavePlotMaxY - m_WavePlotMinY + 2;
  216. }
  217. public void ClearPlot()
  218. {
  219. Points = new PointCollection();
  220. }
  221. public void ClearPlotPoints()
  222. {
  223. PointCollections = new List<PointCollection>();
  224. }
  225. public void Plot(IEnumerable x, IEnumerable y, int count = 0)
  226. {
  227. if (x == null || y == null) return;
  228. if (Points == null || count == 0) Points = new PointCollection();
  229. var enx = x.GetEnumerator();
  230. var eny = y.GetEnumerator();
  231. while (true)
  232. {
  233. var nx = enx.MoveNext();
  234. var ny = eny.MoveNext();
  235. if (nx && ny)
  236. Points.Add(new System.Windows.Point(Convert.ToSingle(enx.Current, CultureInfo.InvariantCulture),
  237. Convert.ToSingle(eny.Current, CultureInfo.InvariantCulture)));
  238. else if (!nx && !ny)
  239. break;
  240. else
  241. throw new ArgumentException("x and y have different lengthes");
  242. if (count > 0 && Points.Count > count) Points.RemoveAt(0);
  243. }
  244. }
  245. public void PlotY(IEnumerable y, int count = 0)
  246. {
  247. if (y == null) return;
  248. if (Points == null || count == 0) Points = new PointCollection();
  249. int x = 0;
  250. var en = y.GetEnumerator();
  251. while (en.MoveNext())
  252. {
  253. Points.Add(new System.Windows.Point(x++, Convert.ToSingle(en.Current, CultureInfo.InvariantCulture)));
  254. if (count > 0 && Points.Count > count) Points.RemoveAt(0);
  255. }
  256. }
  257. public void PlotPoints(IEnumerable x, IEnumerable y, int count = 0)
  258. {
  259. if (x == null || y == null)
  260. {
  261. PointCollections = new List<PointCollection>();
  262. return;
  263. }
  264. if (PointCollections == null || count == 0) PointCollections = new List<PointCollection>();
  265. var points = new PointCollection();
  266. var enx = x.GetEnumerator();
  267. var eny = y.GetEnumerator();
  268. while (true)
  269. {
  270. var nx = enx.MoveNext();
  271. var ny = eny.MoveNext();
  272. if (nx && ny)
  273. points.Add(new System.Windows.Point(Convert.ToSingle(enx.Current, CultureInfo.InvariantCulture),
  274. Convert.ToSingle(eny.Current, CultureInfo.InvariantCulture)));
  275. else if (!nx && !ny)
  276. break;
  277. else
  278. throw new ArgumentException("x and y have different lengthes");
  279. }
  280. PointCollections.Add(points);
  281. if (count > 0 && PointCollections.Count > count) PointCollections.RemoveAt(0);
  282. PointCollections = PointCollections;
  283. }
  284. public void PlotPointYs(IEnumerable y, int count = 0)
  285. {
  286. if (y == null) return;
  287. if (PointCollections == null || count == 0) PointCollections = new List<PointCollection>();
  288. var points = new PointCollection();
  289. int x = 0;
  290. var en = y.GetEnumerator();
  291. while (en.MoveNext())
  292. {
  293. points.Add(new System.Windows.Point(x++, Convert.ToSingle(en.Current, CultureInfo.InvariantCulture)));
  294. }
  295. PointCollections.Add(points);
  296. if (count > 0 && PointCollections.Count > count) PointCollections.RemoveAt(0);
  297. }
  298. public void PlotData(IEnumerable x, IEnumerable y)
  299. {
  300. if (x == null || y == null)
  301. {
  302. PlotDataPoints = new PointCollection();
  303. return;
  304. }
  305. var points = new PointCollection();
  306. var enx = x.GetEnumerator();
  307. var eny = y.GetEnumerator();
  308. while (true)
  309. {
  310. var nx = enx.MoveNext();
  311. var ny = eny.MoveNext();
  312. if (nx && ny)
  313. points.Add(new System.Windows.Point(Convert.ToDouble(enx.Current, CultureInfo.InvariantCulture),
  314. Convert.ToDouble(eny.Current, CultureInfo.InvariantCulture)));
  315. else if (!nx && !ny)
  316. break;
  317. else
  318. throw new ArgumentException("x and y have different lengthes");
  319. }
  320. PlotDataPoints = points;
  321. }
  322. public void PlotSData(IEnumerable x, IEnumerable y)
  323. {
  324. if (x == null || y == null)
  325. {
  326. PlotSDataPoints = new PointCollection();
  327. return;
  328. }
  329. var points = new PointCollection();
  330. var enx = x.GetEnumerator();
  331. var eny = y.GetEnumerator();
  332. while (true)
  333. {
  334. var nx = enx.MoveNext();
  335. var ny = eny.MoveNext();
  336. if (nx && ny)
  337. points.Add(new System.Windows.Point(Convert.ToDouble(enx.Current, CultureInfo.InvariantCulture),
  338. Convert.ToDouble(eny.Current, CultureInfo.InvariantCulture)));
  339. else if (!nx && !ny)
  340. break;
  341. else
  342. throw new ArgumentException("x and y have different lengthes");
  343. }
  344. PlotSDataPoints = points;
  345. }
  346. public void PlotASData(IEnumerable x, IEnumerable y)
  347. {
  348. if (x == null || y == null)
  349. {
  350. PlotASDataPoints = new PointCollection();
  351. return;
  352. }
  353. var points = new PointCollection();
  354. var enx = x.GetEnumerator();
  355. var eny = y.GetEnumerator();
  356. while (true)
  357. {
  358. var nx = enx.MoveNext();
  359. var ny = eny.MoveNext();
  360. if (nx && ny)
  361. points.Add(new System.Windows.Point(Convert.ToDouble(enx.Current, CultureInfo.InvariantCulture),
  362. Convert.ToDouble(eny.Current, CultureInfo.InvariantCulture)));
  363. else if (!nx && !ny)
  364. break;
  365. else
  366. throw new ArgumentException("x and y have different lengthes");
  367. }
  368. PlotASDataPoints = points;
  369. }
  370. /// <summary>
  371. /// 获取小数位数
  372. /// </summary>
  373. /// <param name="decimalV">小数</param>
  374. /// <returns></returns>
  375. public int GetNumberOfDecimalPlaces(double decimalV)
  376. {
  377. string[] temp = decimalV.ToString().Split('.');
  378. if (temp.Length == 2 && temp[1].Length > 0)
  379. {
  380. int index = temp[1].Length - 1;
  381. while (temp[1][index] == '0' && index-- > 0) ;
  382. return index + 1;
  383. }
  384. return 0;
  385. }
  386. #endregion Public
  387. #region Private Draw
  388. private void GraphicDraw()
  389. {
  390. if (Canvas_Main.ActualWidth <= 0 || Canvas_Main.ActualHeight <= 0) return;
  391. m_WavePlotMinX = double.MaxValue;
  392. m_WavePlotMaxX = double.MinValue;
  393. m_WavePlotMinY = double.MaxValue;
  394. m_WavePlotMaxY = double.MinValue;
  395. using (Bitmap bitmap = new Bitmap((int)Canvas_Main.ActualWidth, (int)Canvas_Main.ActualHeight))
  396. {
  397. Graphics graphics = Graphics.FromImage(bitmap);
  398. //纵坐标
  399. if (m_WavePlotHeight < Math.Pow(10, -4))
  400. {
  401. double Min = m_WavePlotY;
  402. double Max = m_WavePlotY + m_WavePlotHeight;
  403. double delta = (Max - Min) / 2;
  404. double center = (Max + Min) / 2;
  405. Min = center - delta * 1.2;
  406. Max = center + delta * 1.2;
  407. m_WavePlotY = Min;
  408. m_WavePlotHeight = Max - Min;
  409. }
  410. if (m_WavePlotHeight > Math.Pow(10, -4))
  411. {
  412. int IntervalTextY; int ExpCountY;
  413. GetIntervalValue(m_WavePlotY, m_WavePlotY + m_WavePlotHeight, out IntervalTextY, out ExpCountY);
  414. int StartIndexY = (int)(m_WavePlotY / (IntervalTextY * Math.Pow(10, ExpCountY)));
  415. int EndIndexY = (int)((m_WavePlotY + m_WavePlotHeight) / (IntervalTextY * Math.Pow(10, ExpCountY)));
  416. int DrawStringLength = 0;
  417. //寻找显示最长字符串
  418. for (int i = StartIndexY; i <= EndIndexY; i++)
  419. {
  420. if (DrawStringLength < ChangeDataToD((i * IntervalTextY * Math.Pow(10, ExpCountY)).ToString()).ToString().Length) DrawStringLength = ChangeDataToD((i * IntervalTextY * Math.Pow(10, ExpCountY)).ToString()).ToString().Length;
  421. }
  422. m_StartDrawLineX = (DrawStringLength + 5) * 5;
  423. for (int i = StartIndexY; i <= EndIndexY; i++)
  424. {
  425. if (i == StartIndexY && (i * IntervalTextY * Math.Pow(10, ExpCountY)) < m_WavePlotY) continue;
  426. if (i == EndIndexY && (i * IntervalTextY * Math.Pow(10, ExpCountY)) > m_WavePlotY + m_WavePlotHeight) continue;
  427. double Y = Canvas_Main.ActualHeight - 50 - ((Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight) * (i * IntervalTextY * Math.Pow(10, ExpCountY) - m_WavePlotY);
  428. if (Y < 0) Y = 0;
  429. if (Y > Canvas_Main.ActualHeight - 50) Y = Canvas_Main.ActualHeight - 50;
  430. graphics.DrawLine(m_PenLightGray, new PointF((float)m_StartDrawLineX, (float)Y), new PointF((float)(Canvas_Main.ActualWidth), (float)Y));
  431. graphics.DrawLine(m_PenLine, new PointF((float)m_StartDrawLineX, (float)Y), new PointF((float)m_StartDrawLineX - 10, (float)Y));
  432. graphics.DrawString(ChangeDataToD((i * IntervalTextY * Math.Pow(10, ExpCountY)).ToString()).ToString(), System.Drawing.SystemFonts.DefaultFont, m_BrushText, new PointF(0, (float)(Y - 5)));
  433. }
  434. }
  435. //横坐标
  436. if (m_WavePlotWidth < Math.Pow(10, -4))
  437. {
  438. double Min = m_WavePlotX;
  439. double Max = m_WavePlotX + m_WavePlotWidth;
  440. double delta = (Max - Min) / 2;
  441. double center = (Max + Min) / 2;
  442. Min = center - delta * 1.2;
  443. Max = center + delta * 1.2;
  444. m_WavePlotX = Min;
  445. m_WavePlotWidth = Max - Min;
  446. }
  447. if (m_WavePlotWidth > Math.Pow(10, -4))
  448. {
  449. int IntervalTextX; int ExpCountX;
  450. GetIntervalValue(m_WavePlotX, m_WavePlotX + m_WavePlotWidth, out IntervalTextX, out ExpCountX);
  451. int StartIndexX = (int)(m_WavePlotX / (IntervalTextX * Math.Pow(10, ExpCountX)));
  452. int EndIndexX = (int)((m_WavePlotX + m_WavePlotWidth) / (IntervalTextX * Math.Pow(10, ExpCountX)));
  453. for (int i = StartIndexX; i <= EndIndexX; i++)
  454. {
  455. if (i == StartIndexX && (i * IntervalTextX * Math.Pow(10, ExpCountX)) < m_WavePlotX) continue;
  456. if (i == EndIndexX && (i * IntervalTextX * Math.Pow(10, ExpCountX)) > m_WavePlotX + m_WavePlotWidth) continue;
  457. double X = m_StartDrawLineX + ((Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth) * (i * IntervalTextX * Math.Pow(10, ExpCountX) - m_WavePlotX);
  458. if (X < m_StartDrawLineX) X = m_StartDrawLineX;
  459. if (X > Canvas_Main.ActualWidth) X = Canvas_Main.ActualWidth;
  460. graphics.DrawLine(m_PenLightGray, new PointF((float)X, 0), new PointF((float)X, (float)Canvas_Main.ActualHeight - 50));
  461. graphics.DrawLine(m_PenLine, new PointF((float)X, (float)Canvas_Main.ActualHeight - 30), new PointF((float)X, (float)Canvas_Main.ActualHeight - 50));
  462. if (IsHorizontalDateTimeAxis)
  463. {
  464. graphics.DrawString(DateTime.FromOADate((double)ChangeDataToD((i * IntervalTextX * Math.Pow(10, ExpCountX)).ToString())).ToString("yyyy-MM-dd HH:mm:ss"), System.Drawing.SystemFonts.DefaultFont, m_BrushText, new PointF((float)X - 10, (float)Canvas_Main.ActualHeight - 30));
  465. }
  466. else
  467. {
  468. graphics.DrawString(ChangeDataToD((i * IntervalTextX * Math.Pow(10, ExpCountX)).ToString()).ToString(), System.Drawing.SystemFonts.DefaultFont, m_BrushText, new PointF((float)X - 10, (float)Canvas_Main.ActualHeight - 30));
  469. }
  470. }
  471. }
  472. //画边框
  473. graphics.DrawLine(m_PenLine, new PointF((float)m_StartDrawLineX, 0), new PointF((float)m_StartDrawLineX, (float)Canvas_Main.ActualHeight - 50));
  474. graphics.DrawLine(m_PenLine, new PointF((float)m_StartDrawLineX, 0), new PointF((float)(Canvas_Main.ActualWidth), 0));
  475. graphics.DrawLine(m_PenLine, new PointF((float)m_StartDrawLineX, (float)(Canvas_Main.ActualHeight - 50)), new PointF((float)Canvas_Main.ActualWidth, (float)(Canvas_Main.ActualHeight - 50)));
  476. graphics.DrawLine(m_PenLine, new PointF((float)Canvas_Main.ActualWidth - 1, 0), new PointF((float)Canvas_Main.ActualWidth - 1, (float)(Canvas_Main.ActualHeight - 50)));
  477. if (this.PlotDataPoints != null && this.PlotDataPoints.Count > 0)
  478. {
  479. List<PointF> points = new List<PointF>();
  480. for (int i = 0; i < PlotDataPoints.Count; i++)
  481. {
  482. if (PlotDataPoints[i].X >= m_WavePlotX && PlotDataPoints[i].X <= m_WavePlotX + m_WavePlotWidth)
  483. {
  484. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (PlotDataPoints[i].X - m_WavePlotX);
  485. if (StartX < m_StartDrawLineX || PlotDataPoints[i].X < m_WavePlotX) StartX = m_StartDrawLineX;
  486. if (StartX > Canvas_Main.ActualWidth || PlotDataPoints[i].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  487. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (PlotDataPoints[i].Y - m_WavePlotY);
  488. if (StartY < 0 || PlotDataPoints[i].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  489. if (StartY > Canvas_Main.ActualHeight - 50 || PlotDataPoints[i].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  490. points.Add(new PointF((float)StartX, (float)StartY));
  491. }
  492. if (m_WavePlotMinX == double.MaxValue || m_WavePlotMinX > PlotDataPoints[i].X) m_WavePlotMinX = PlotDataPoints[i].X;
  493. if (m_WavePlotMaxX == double.MinValue || m_WavePlotMaxX < PlotDataPoints[i].X) m_WavePlotMaxX = PlotDataPoints[i].X;
  494. if (m_WavePlotMinY == double.MaxValue || m_WavePlotMinY > PlotDataPoints[i].Y) m_WavePlotMinY = PlotDataPoints[i].Y;
  495. if (m_WavePlotMaxY == double.MinValue || m_WavePlotMaxY < PlotDataPoints[i].Y) m_WavePlotMaxY = PlotDataPoints[i].Y;
  496. }
  497. if (points.Count > 1 && points.Where(_ => float.IsNaN(_.X) || float.IsNaN(_.Y)).ToList<PointF>().Count == 0) graphics.DrawLines(m_PenData, points.ToArray());
  498. }
  499. if (this.PlotSDataPoints != null && this.PlotSDataPoints.Count > 0)
  500. {
  501. List<PointF> points = new List<PointF>();
  502. for (int i = 0; i < PlotSDataPoints.Count; i++)
  503. {
  504. if (PlotSDataPoints[i].X >= m_WavePlotX && PlotSDataPoints[i].X <= m_WavePlotX + m_WavePlotWidth)
  505. {
  506. double StartX = 50 + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (PlotSDataPoints[i].X - m_WavePlotX);
  507. if (StartX < m_StartDrawLineX || PlotSDataPoints[i].X < m_WavePlotX) StartX = m_StartDrawLineX;
  508. if (StartX > Canvas_Main.ActualWidth || PlotSDataPoints[i].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  509. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (PlotSDataPoints[i].Y - m_WavePlotY);
  510. if (StartY < 0 || PlotSDataPoints[i].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  511. if (StartY > Canvas_Main.ActualHeight - 50 || PlotSDataPoints[i].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  512. points.Add(new PointF((float)StartX, (float)StartY));
  513. }
  514. if (double.IsNaN(m_WavePlotMinX) || m_WavePlotMinX > PlotSDataPoints[i].X) m_WavePlotMinX = PlotSDataPoints[i].X;
  515. if (double.IsNaN(m_WavePlotMaxX) || m_WavePlotMaxX < PlotSDataPoints[i].X) m_WavePlotMaxX = PlotSDataPoints[i].X;
  516. if (double.IsNaN(m_WavePlotMinY) || m_WavePlotMinY > PlotSDataPoints[i].Y) m_WavePlotMinY = PlotSDataPoints[i].Y;
  517. if (double.IsNaN(m_WavePlotMaxY) || m_WavePlotMaxY < PlotSDataPoints[i].Y) m_WavePlotMaxY = PlotSDataPoints[i].Y;
  518. }
  519. if (points.Count > 1 && points.Where(_ => float.IsNaN(_.X) || float.IsNaN(_.Y)).ToList<PointF>().Count == 0) graphics.DrawLines(m_PenSData, points.ToArray());
  520. }
  521. if (this.PlotASDataPoints != null && this.PlotASDataPoints.Count > 0)
  522. {
  523. List<PointF> points = new List<PointF>();
  524. for (int i = 0; i < PlotASDataPoints.Count; i++)
  525. {
  526. if (PlotASDataPoints[i].X >= m_WavePlotX && PlotASDataPoints[i].X <= m_WavePlotX + m_WavePlotWidth)
  527. {
  528. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (PlotASDataPoints[i].X - m_WavePlotX);
  529. if (StartX < m_StartDrawLineX || PlotASDataPoints[i].X < m_WavePlotX) StartX = m_StartDrawLineX;
  530. if (StartX > Canvas_Main.ActualWidth || PlotASDataPoints[i].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  531. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (PlotASDataPoints[i].Y - m_WavePlotY);
  532. if (StartY < 0 || PlotASDataPoints[i].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  533. if (StartY > Canvas_Main.ActualHeight - 50 || PlotASDataPoints[i].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  534. points.Add(new PointF((float)StartX, (float)StartY));
  535. }
  536. if (m_WavePlotMinX == double.MaxValue || m_WavePlotMinX > PlotASDataPoints[i].X) m_WavePlotMinX = PlotASDataPoints[i].X;
  537. if (m_WavePlotMaxX == double.MinValue || m_WavePlotMaxX < PlotASDataPoints[i].X) m_WavePlotMaxX = PlotASDataPoints[i].X;
  538. if (m_WavePlotMinY == double.MaxValue || m_WavePlotMinY > PlotASDataPoints[i].Y) m_WavePlotMinY = PlotASDataPoints[i].Y;
  539. if (m_WavePlotMaxY == double.MinValue || m_WavePlotMaxY < PlotASDataPoints[i].Y) m_WavePlotMaxY = PlotASDataPoints[i].Y;
  540. }
  541. if (points.Count > 1 && points.Where(_ => float.IsNaN(_.X) || float.IsNaN(_.Y)).ToList<PointF>().Count == 0) graphics.DrawLines(m_PenASData, points.ToArray());
  542. }
  543. if (this.Points != null && this.Points.Count > 0)
  544. {
  545. List<PointF> points = new List<PointF>();
  546. for (int i = 0; i < Points.Count; i++)
  547. {
  548. if (Points[i].X >= m_WavePlotX && Points[i].X <= m_WavePlotX + m_WavePlotWidth)
  549. {
  550. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (Points[i].X - m_WavePlotX);
  551. if (StartX < m_StartDrawLineX || Points[i].X < m_WavePlotX) StartX = m_StartDrawLineX;
  552. if (StartX > Canvas_Main.ActualWidth || Points[i].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  553. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (Points[i].Y - m_WavePlotY);
  554. if (StartY < 0 || Points[i].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  555. if (StartY > Canvas_Main.ActualHeight - 50 || Points[i].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  556. points.Add(new PointF((float)StartX, (float)StartY));
  557. }
  558. if (m_WavePlotMinX == double.MaxValue || m_WavePlotMinX > Points[i].X) m_WavePlotMinX = Points[i].X;
  559. if (m_WavePlotMaxX == double.MinValue || m_WavePlotMaxX < Points[i].X) m_WavePlotMaxX = Points[i].X;
  560. if (m_WavePlotMinY == double.MaxValue || m_WavePlotMinY > Points[i].Y) m_WavePlotMinY = Points[i].Y;
  561. if (m_WavePlotMaxY == double.MinValue || m_WavePlotMaxY < Points[i].Y) m_WavePlotMaxY = Points[i].Y;
  562. }
  563. if (points.Count > 1 && points.Where(_ => float.IsNaN(_.X) || float.IsNaN(_.Y)).ToList<PointF>().Count == 0) graphics.DrawLines(m_PenData, points.ToArray());
  564. }
  565. if (this.PointCollections != null && this.PointCollections.Count > 0)
  566. {
  567. for (int i = 0; i < this.PointCollections.Count; i++)
  568. {
  569. List<PointF> points = new List<PointF>();
  570. for (int j = 0; j < this.PointCollections[i].Count; j++)
  571. {
  572. if (this.PointCollections[i][j].X >= m_WavePlotX && this.PointCollections[i][j].X <= m_WavePlotX + m_WavePlotWidth)
  573. {
  574. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (this.PointCollections[i][j].X - m_WavePlotX);
  575. if (StartX < m_StartDrawLineX || this.PointCollections[i][j].X < m_WavePlotX) StartX = m_StartDrawLineX;
  576. if (StartX > Canvas_Main.ActualWidth || this.PointCollections[i][j].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  577. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (this.PointCollections[i][j].Y - m_WavePlotY);
  578. if (StartY < 0 || this.PointCollections[i][j].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  579. if (StartY > Canvas_Main.ActualHeight - 50 || this.PointCollections[i][j].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  580. points.Add(new PointF((float)StartX, (float)StartY));
  581. }
  582. if (m_WavePlotMinX == double.MaxValue || m_WavePlotMinX > this.PointCollections[i][j].X) m_WavePlotMinX = this.PointCollections[i][j].X;
  583. if (m_WavePlotMaxX == double.MinValue || m_WavePlotMaxX < this.PointCollections[i][j].X) m_WavePlotMaxX = this.PointCollections[i][j].X;
  584. if (m_WavePlotMinY == double.MaxValue || m_WavePlotMinY > this.PointCollections[i][j].Y) m_WavePlotMinY = this.PointCollections[i][j].Y;
  585. if (m_WavePlotMaxY == double.MinValue || m_WavePlotMaxY < this.PointCollections[i][j].Y) m_WavePlotMaxY = this.PointCollections[i][j].Y;
  586. }
  587. if (points.Count > 1 && points.Where(_ => float.IsNaN(_.X) || float.IsNaN(_.Y)).ToList<PointF>().Count == 0)
  588. {
  589. graphics.DrawLines(m_PenCollencteions[i % 10], points.ToArray());
  590. }
  591. }
  592. }
  593. //框选
  594. if (m_StartMouseMove && m_MouseMove)
  595. {
  596. if (m_StartPoint != null && m_EndPoint != null)
  597. {
  598. int x = m_StartPoint.X > m_EndPoint.X ? (int)m_EndPoint.X : (int)m_StartPoint.X;
  599. int y = m_StartPoint.Y > m_EndPoint.Y ? (int)m_EndPoint.Y : (int)m_StartPoint.Y;
  600. int width = (int)Math.Abs(m_EndPoint.X - m_StartPoint.X);
  601. int height = (int)Math.Abs(m_EndPoint.Y - m_StartPoint.Y);
  602. System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(x, y, width, height);
  603. graphics.FillRectangle(System.Drawing.Brushes.LightGray, rectangle);
  604. graphics.DrawRectangle(new System.Drawing.Pen (System.Drawing.Color.Transparent), rectangle);
  605. }
  606. }
  607. else
  608. {
  609. //拾取坐标
  610. if (m_MouseEnter && m_CurrentPoint != null)
  611. {
  612. double X = m_WavePlotX + (m_CurrentPoint.X - m_StartDrawLineX) / ((this.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth);
  613. double Y = m_WavePlotY + (this.ActualHeight - 50 - m_CurrentPoint.Y) / ((this.ActualHeight - 50) / m_WavePlotHeight);
  614. if (this.PointCollections == null || this.PointCollections.Count <= 10)
  615. {
  616. graphics.DrawLine(m_PenBlue, new System.Drawing.PointF((float)m_StartDrawLineX, (float)m_CurrentPoint.Y), new PointF((float)Canvas_Main.ActualWidth, (float)m_CurrentPoint.Y));
  617. graphics.DrawLine(m_PenBlue, new System.Drawing.PointF((float)m_CurrentPoint.X, 0), new PointF((float)m_CurrentPoint.X, (float)Canvas_Main.ActualHeight - 50));
  618. //if (IsHorizontalDateTimeAxis)
  619. //{
  620. // graphics.DrawString(string.Format("X={0};Y={1}", DateTime.FromOADate(X), Y.ToString("0.00")), System.Drawing.SystemFonts.DefaultFont, m_BrushXYText, new PointF((float)m_CurrentPoint.X + 10, (float)m_CurrentPoint.Y + 10));
  621. //}
  622. //else
  623. //{
  624. // graphics.DrawString(string.Format("X={0};Y={1}", X.ToString("0.00"), Y.ToString("0.00")), System.Drawing.SystemFonts.DefaultFont, m_BrushXYText, new PointF((float)m_CurrentPoint.X + 10, (float)m_CurrentPoint.Y + 10));
  625. //}
  626. }
  627. //
  628. if (this.PlotDataPoints != null && this.PlotDataPoints.Count > 0)
  629. {
  630. if (X >= this.PlotDataPoints[0].X && X <= this.PlotDataPoints[this.PlotDataPoints.Count - 1].X)
  631. {
  632. //int index = this.PlotDataX.IndexOf((int)Math.Round(X, 0));
  633. var minDistance = this.PlotDataPoints.Min(x => Math.Abs(x.X - X));
  634. var closest = this.PlotDataPoints.First(x => Math.Abs(Math.Abs(x.X - X) - minDistance) < 0.0001f);
  635. int index = this.PlotDataPoints.IndexOf(closest);
  636. if (index >= 0)
  637. {
  638. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (PlotDataPoints[index].X - m_WavePlotX);
  639. if (StartX < m_StartDrawLineX || PlotDataPoints[index].X < m_WavePlotX) StartX = m_StartDrawLineX;
  640. if (StartX > Canvas_Main.ActualWidth || PlotDataPoints[index].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  641. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (PlotDataPoints[index].Y - m_WavePlotY);
  642. if (StartY < 0 || PlotDataPoints[index].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  643. if (StartY > Canvas_Main.ActualHeight - 50 || PlotDataPoints[index].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  644. if (!double.IsNaN(StartX) && !double.IsNaN(StartY))
  645. {
  646. System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle((int)StartX, (int)StartY, 5, 5);
  647. graphics.FillRectangle(System.Drawing.Brushes.Blue, rectangle);
  648. graphics.DrawEllipse(m_PenBlue, rectangle);
  649. if (IsHorizontalDateTimeAxis)
  650. {
  651. graphics.DrawString(string.Format("X={0};Y={1}", DateTime.FromOADate(PlotDataPoints[index].X), PlotDataPoints[index].Y.ToString("0.00")), System.Drawing.SystemFonts.DefaultFont, m_BrushXYText, new PointF((float)StartX + 10, (float)StartY + 10));
  652. }
  653. else
  654. {
  655. graphics.DrawString(string.Format("X={0};Y={1}", PlotDataPoints[index].X.ToString("0.00"), PlotDataPoints[index].Y.ToString("0.00")), System.Drawing.SystemFonts.DefaultFont, m_BrushXYText, new PointF((float)StartX + 10, (float)StartY + 10));
  656. }
  657. }
  658. }
  659. }
  660. }
  661. //
  662. if (this.PlotSDataPoints != null && this.PlotSDataPoints.Count > 0)
  663. {
  664. if (X >= this.PlotSDataPoints[0].X && X <= this.PlotSDataPoints[this.PlotSDataPoints.Count - 1].X)
  665. {
  666. //int index = this.PlotSDataX.IndexOf((int)Math.Round(X, 0));
  667. var minDistance = this.PlotSDataPoints.Min(x => Math.Abs(x.X - X));
  668. var closest = this.PlotSDataPoints.First(x => Math.Abs(Math.Abs(x.X - X) - minDistance) < 0.0001f);
  669. int index = this.PlotSDataPoints.IndexOf(closest);
  670. if (index >= 0)
  671. {
  672. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (PlotSDataPoints[index].X - m_WavePlotX);
  673. if (StartX < m_StartDrawLineX || PlotSDataPoints[index].X < m_WavePlotX) StartX = m_StartDrawLineX;
  674. if (StartX > Canvas_Main.ActualWidth || PlotSDataPoints[index].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  675. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (PlotSDataPoints[index].Y - m_WavePlotY);
  676. if (StartY < 0 || PlotSDataPoints[index].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  677. if (StartY > Canvas_Main.ActualHeight - 50 || PlotSDataPoints[index].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  678. if (!double.IsNaN(StartX) && !double.IsNaN(StartY))
  679. {
  680. System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle((int)StartX, (int)StartY, 5, 5);
  681. graphics.FillRectangle(System.Drawing.Brushes.Blue, rectangle);
  682. graphics.DrawEllipse(m_PenBlue, rectangle);
  683. graphics.DrawString(string.Format("X={0};Y={1}", PlotSDataPoints[index].X.ToString("0.00"), PlotSDataPoints[index].Y.ToString("0.00")), System.Drawing.SystemFonts.DefaultFont, m_BrushXYText, new PointF((float)StartX + 10, (float)StartY + 10));
  684. }
  685. }
  686. }
  687. }
  688. //
  689. if (this.PlotASDataPoints != null && this.PlotASDataPoints.Count > 0)
  690. {
  691. if (X >= this.PlotASDataPoints[0].X && X <= this.PlotASDataPoints[this.PlotASDataPoints.Count - 1].X)
  692. {
  693. //int index = this.PlotASDataX.IndexOf((int)Math.Round(X, 0));
  694. var minDistance = this.PlotASDataPoints.Min(x => Math.Abs(x.X - X));
  695. var closest = this.PlotASDataPoints.First(x => Math.Abs(x.X - X) == minDistance);
  696. int index = this.PlotASDataPoints.IndexOf(closest);
  697. if (index >= 0)
  698. {
  699. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (PlotASDataPoints[index].X - m_WavePlotX);
  700. if (StartX < m_StartDrawLineX || PlotASDataPoints[index].X < m_WavePlotX) StartX = m_StartDrawLineX;
  701. if (StartX > Canvas_Main.ActualWidth || PlotASDataPoints[index].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  702. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (PlotASDataPoints[index].Y - m_WavePlotY);
  703. if (StartY < 0 || PlotASDataPoints[index].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  704. if (StartY > Canvas_Main.ActualHeight - 50 || PlotASDataPoints[index].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  705. if (!double.IsNaN(StartX) && !double.IsNaN(StartY))
  706. {
  707. System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle((int)StartX, (int)StartY, 5, 5);
  708. graphics.FillRectangle(System.Drawing.Brushes.Blue, rectangle);
  709. graphics.DrawEllipse(m_PenBlue, rectangle);
  710. graphics.DrawString(string.Format("X={0};Y={1}", PlotASDataPoints[index].X.ToString("0.00"), PlotASDataPoints[index].Y.ToString("0.00")), System.Drawing.SystemFonts.DefaultFont, m_BrushXYText, new PointF((float)StartX + 10, (float)StartY + 10));
  711. }
  712. }
  713. }
  714. }
  715. //
  716. if (this.PointCollections != null && this.PointCollections.Count <= 10)
  717. {
  718. foreach (PointCollection point in this.PointCollections)
  719. {
  720. //if (X >= point[0].X && X <= point[point.Count - 1].X)
  721. {
  722. //int index = this.PlotASDataX.IndexOf((int)Math.Round(X, 0));
  723. var minDistance = point.Min(x => Math.Abs(x.X - X));
  724. var closest = point.First(x => Math.Abs(x.X - X) == minDistance);
  725. int index = point.IndexOf(closest);
  726. if (index >= 0)
  727. {
  728. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (point[index].X - m_WavePlotX);
  729. if (StartX < m_StartDrawLineX || point[index].X < m_WavePlotX) StartX = m_StartDrawLineX;
  730. if (StartX > Canvas_Main.ActualWidth || point[index].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  731. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (point[index].Y - m_WavePlotY);
  732. if (StartY < 0 || point[index].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  733. if (StartY > Canvas_Main.ActualHeight - 50 || point[index].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  734. if (!double.IsNaN(StartX) && !double.IsNaN(StartY))
  735. {
  736. System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle((int)StartX, (int)StartY, 5, 5);
  737. graphics.FillRectangle(System.Drawing.Brushes.Blue, rectangle);
  738. graphics.DrawEllipse(m_PenBlue, rectangle);
  739. if (IsHorizontalDateTimeAxis)
  740. {
  741. graphics.DrawString(string.Format("X={0};Y={1}", DateTime.FromOADate(point[index].X), point[index].Y.ToString("0.00")), new Font("Times New Roman", 15.0F), m_BrushXYText, new PointF((float)StartX + 10, (float)StartY + 10));
  742. }
  743. else
  744. {
  745. graphics.DrawString(string.Format("X={0};Y={1}", point[index].X.ToString("0.00"), point[index].Y.ToString("0.00")), new Font("Times New Roman", 15.0F), m_BrushXYText, new PointF((float)StartX + 10, (float)StartY + 10));
  746. }
  747. }
  748. }
  749. }
  750. }
  751. }
  752. }
  753. }
  754. ImageBrush_BK.ImageSource = BitmapToBitmapImage(bitmap);
  755. }
  756. this.InvalidateVisual();
  757. }
  758. #endregion Private Draw
  759. #region Private
  760. private void Canvas_Main_SizeChanged(object sender, SizeChangedEventArgs e)
  761. {
  762. GraphicDraw();
  763. }
  764. private void Canvas_Main_MouseWheel(object sender, MouseWheelEventArgs e)
  765. {
  766. double factor = e.Delta < 0 ? 1.2 : 1 / 1.2;
  767. if (IsVerticalNavigationEnabled)
  768. {
  769. double Min = m_WavePlotY;
  770. double Max = m_WavePlotY + m_WavePlotHeight;
  771. double delta = (Max - Min) / 2;
  772. double center = (Max + Min) / 2;
  773. Min = center - delta * factor;
  774. Max = center + delta * factor;
  775. m_WavePlotY = Min;
  776. m_WavePlotHeight = Max - Min;
  777. }
  778. if (IsHorizontalNavigationEnabled)
  779. {
  780. double Min = m_WavePlotX;
  781. double Max = m_WavePlotX + m_WavePlotWidth;
  782. double delta = (Max - Min) / 2;
  783. double center = (Max + Min) / 2;
  784. Min = center - delta * factor;
  785. Max = center + delta * factor;
  786. m_WavePlotX = Min;
  787. m_WavePlotWidth = Max - Min;
  788. }
  789. GraphicDraw();
  790. }
  791. private void Canvas_Main_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  792. {
  793. this.Cursor = Cursors.Hand;
  794. if (e.ClickCount == 2)
  795. {
  796. FitControl();
  797. }
  798. else
  799. {
  800. m_StartPoint = Mouse.GetPosition(e.Source as FrameworkElement);
  801. if (e.LeftButton == MouseButtonState.Pressed)
  802. {
  803. m_StartMouseMove = true;
  804. }//开始绘制矩形
  805. }
  806. }
  807. private void Canvas_Main_MouseMove(object sender, MouseEventArgs e)
  808. {
  809. m_EndPoint = Mouse.GetPosition(e.Source as FrameworkElement);
  810. m_CurrentPoint = Mouse.GetPosition(e.Source as FrameworkElement);
  811. if ((Keyboard.Modifiers == ModifierKeys.Control || m_MouseMove) && m_StartMouseMove)
  812. {
  813. m_MouseMove = true;
  814. GraphicDraw();
  815. }//移动矩形位置
  816. else if (m_StartMouseMove)
  817. {
  818. m_WavePlotX = m_WavePlotX - (m_EndPoint.X - m_StartPoint.X) / (this.ActualWidth - 50) * m_WavePlotWidth;
  819. m_WavePlotY = m_WavePlotY + (m_EndPoint.Y - m_StartPoint.Y) / (this.ActualHeight - 50) * m_WavePlotHeight;
  820. m_StartPoint = m_EndPoint;
  821. GraphicDraw();
  822. }
  823. else if (this.PointCollections == null || this.PointCollections.Count <= 10)
  824. {
  825. GraphicDraw();
  826. }
  827. }
  828. private void Canvas_Main_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  829. {
  830. this.Cursor = Cursors.Arrow;
  831. if (m_StartMouseMove && m_MouseMove && e.LeftButton == MouseButtonState.Released)
  832. {
  833. m_StartMouseMove = false;
  834. m_MouseMove = false;
  835. if (m_StartPoint.X == m_EndPoint.X || m_StartPoint.Y == m_EndPoint.Y) return;
  836. double x = m_StartPoint.X > m_EndPoint.X ? m_EndPoint.X : m_StartPoint.X;
  837. double y = m_StartPoint.Y < m_EndPoint.Y ? m_EndPoint.Y : m_StartPoint.Y;
  838. double width = Math.Abs(m_EndPoint.X - m_StartPoint.X);
  839. double height = Math.Abs(m_EndPoint.Y - m_StartPoint.Y);
  840. //设置坐标轴起始位置
  841. this.m_WavePlotX = this.m_WavePlotX + (x - m_StartDrawLineX) / ((this.ActualWidth - m_StartDrawLineX) / this.m_WavePlotWidth);
  842. this.m_WavePlotY = this.m_WavePlotY + (this.ActualHeight - 50 - y) / ((this.ActualHeight - 50) / this.m_WavePlotHeight);
  843. this.m_WavePlotWidth = this.m_WavePlotWidth * (width / (this.ActualWidth - m_StartDrawLineX));
  844. this.m_WavePlotHeight = this.m_WavePlotHeight * (height / (this.ActualHeight - 50));
  845. GraphicDraw();
  846. }//结束绘制矩形
  847. m_StartMouseMove = false;
  848. m_MouseMove = false;
  849. }
  850. private void Canvas_Main_MouseEnter(object sender, MouseEventArgs e)
  851. {
  852. if (this.PointCollections == null || this.PointCollections.Count <= 10)
  853. {
  854. m_MouseEnter = true;
  855. }
  856. }
  857. private void Canvas_Main_MouseLeave(object sender, MouseEventArgs e)
  858. {
  859. if (this.PointCollections == null || this.PointCollections.Count <= 10) m_MouseEnter = false;
  860. GraphicDraw();
  861. }
  862. private void Canvas_Main_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
  863. {
  864. m_WavePlotX = WavePlotOriginX;
  865. m_WavePlotY = WavePlotOriginY;
  866. m_WavePlotWidth = WavePlotOriginWidth;
  867. m_WavePlotHeight = WavePlotOriginHeight;
  868. ResetControl();
  869. }
  870. /// <summary>
  871. /// 获取坐标间隔
  872. /// </summary>
  873. /// <returns></returns>
  874. private void GetIntervalValue(double min, double max, out int IntervalValue, out int expCount)
  875. {
  876. IntervalValue = 0;
  877. expCount = 0;
  878. double ActualValue = Math.Abs(max - min);
  879. if (ActualValue < 1)
  880. {
  881. while (ActualValue < 1)
  882. {
  883. ActualValue *= 10;
  884. expCount -= 1;
  885. }
  886. }
  887. else
  888. {
  889. while (ActualValue >= 10)
  890. {
  891. ActualValue /= 10;
  892. expCount += 1;
  893. }
  894. }
  895. if (ActualValue <= 1.2)
  896. {
  897. IntervalValue = 1;
  898. expCount -= 1;
  899. }
  900. else if (ActualValue <= 3)
  901. {
  902. IntervalValue = 2;
  903. expCount -= 1;
  904. }
  905. else if (ActualValue <= 5)
  906. {
  907. IntervalValue = 5;
  908. expCount -= 1;
  909. }
  910. else
  911. {
  912. IntervalValue = 1;
  913. }
  914. }
  915. /// <summary>
  916. /// 字符串值中包含E等科学计数法
  917. /// </summary>
  918. /// <param name="strData"></param>
  919. /// <returns></returns>
  920. private Decimal ChangeDataToD(string strData)
  921. {
  922. Decimal dData = 0.0M;
  923. if (strData.ToString().Contains("E"))
  924. {
  925. dData = Decimal.Parse(strData, System.Globalization.NumberStyles.Float);
  926. }
  927. else
  928. {
  929. dData = Decimal.Parse(strData);
  930. }
  931. return dData;
  932. }
  933. /// <summary>
  934. /// Bitmap --> BitmapImage
  935. /// </summary>
  936. /// <param name="bitmap"></param>
  937. /// <returns></returns>
  938. private BitmapImage BitmapToBitmapImage(Bitmap bitmap)
  939. {
  940. using (MemoryStream stream = new MemoryStream())
  941. {
  942. bitmap.Save(stream, ImageFormat.Png); // 坑点:格式选Bmp时,不带透明度
  943. stream.Position = 0;
  944. BitmapImage result = new BitmapImage();
  945. result.BeginInit();
  946. // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."
  947. // Force the bitmap to load right now so we can dispose the stream.
  948. result.CacheOption = BitmapCacheOption.OnLoad;
  949. result.StreamSource = stream;
  950. result.EndInit();
  951. result.Freeze();
  952. return result;
  953. }
  954. }
  955. #endregion Private
  956. }
  957. }