DrawGraphicsControl.xaml.cs 60 KB

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