DrawGraphicsControl.xaml.cs 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. public 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. //public void GraphicDrawplotsally()
  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. // if (m_WavePlotHeight < Math.Pow(10, -4))
  399. // {
  400. // double Min = m_WavePlotY;
  401. // double Max = m_WavePlotY + m_WavePlotHeight;
  402. // double delta = (Max - Min) / 2;
  403. // double center = (Max + Min) / 2;
  404. // Min = center - delta * 1.2;
  405. // Max = center + delta * 1.2;
  406. // m_WavePlotY = Min;
  407. // m_WavePlotHeight = Max - Min;
  408. // }
  409. // }
  410. //}
  411. private void GraphicDraw()
  412. {
  413. if (Canvas_Main.ActualWidth <= 0 || Canvas_Main.ActualHeight <= 0) return;
  414. m_WavePlotMinX = double.MaxValue;
  415. m_WavePlotMaxX = double.MinValue;
  416. m_WavePlotMinY = double.MaxValue;
  417. m_WavePlotMaxY = double.MinValue;
  418. using (Bitmap bitmap = new Bitmap((int)Canvas_Main.ActualWidth, (int)Canvas_Main.ActualHeight))
  419. {
  420. Graphics graphics = Graphics.FromImage(bitmap);
  421. //纵坐标
  422. if (m_WavePlotHeight < Math.Pow(10, -4))
  423. {
  424. double Min = m_WavePlotY;
  425. double Max = m_WavePlotY + m_WavePlotHeight;
  426. double delta = (Max - Min) / 2;
  427. double center = (Max + Min) / 2;
  428. Min = center - delta * 1.2;
  429. Max = center + delta * 1.2;
  430. m_WavePlotY = Min;
  431. m_WavePlotHeight = Max - Min;
  432. }
  433. if (m_WavePlotHeight > Math.Pow(10, -4))
  434. {
  435. int IntervalTextY; int ExpCountY;
  436. GetIntervalValue(m_WavePlotY, m_WavePlotY + m_WavePlotHeight, out IntervalTextY, out ExpCountY);
  437. int StartIndexY = (int)(m_WavePlotY / (IntervalTextY * Math.Pow(10, ExpCountY)));
  438. int EndIndexY = (int)((m_WavePlotY + m_WavePlotHeight) / (IntervalTextY * Math.Pow(10, ExpCountY)));
  439. int DrawStringLength = 0;
  440. //寻找显示最长字符串
  441. for (int i = StartIndexY; i <= EndIndexY; i++)
  442. {
  443. if (DrawStringLength < ChangeDataToD((i * IntervalTextY * Math.Pow(10, ExpCountY)).ToString()).ToString().Length) DrawStringLength = ChangeDataToD((i * IntervalTextY * Math.Pow(10, ExpCountY)).ToString()).ToString().Length;
  444. }
  445. m_StartDrawLineX = (DrawStringLength + 5) * 5;
  446. for (int i = StartIndexY; i <= EndIndexY; i++)
  447. {
  448. if (i == StartIndexY && (i * IntervalTextY * Math.Pow(10, ExpCountY)) < m_WavePlotY) continue;
  449. if (i == EndIndexY && (i * IntervalTextY * Math.Pow(10, ExpCountY)) > m_WavePlotY + m_WavePlotHeight) continue;
  450. double Y = Canvas_Main.ActualHeight - 50 - ((Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight) * (i * IntervalTextY * Math.Pow(10, ExpCountY) - m_WavePlotY);
  451. if (Y < 0) Y = 0;
  452. if (Y > Canvas_Main.ActualHeight - 50) Y = Canvas_Main.ActualHeight - 50;
  453. graphics.DrawLine(m_PenLightGray, new PointF((float)m_StartDrawLineX, (float)Y), new PointF((float)(Canvas_Main.ActualWidth), (float)Y));
  454. graphics.DrawLine(m_PenLine, new PointF((float)m_StartDrawLineX, (float)Y), new PointF((float)m_StartDrawLineX - 10, (float)Y));
  455. graphics.DrawString(ChangeDataToD((i * IntervalTextY * Math.Pow(10, ExpCountY)).ToString()).ToString(), System.Drawing.SystemFonts.DefaultFont, m_BrushText, new PointF(0, (float)(Y - 5)));
  456. }
  457. }
  458. //横坐标
  459. if (m_WavePlotWidth < Math.Pow(10, -4))
  460. {
  461. double Min = m_WavePlotX;
  462. double Max = m_WavePlotX + m_WavePlotWidth;
  463. double delta = (Max - Min) / 2;
  464. double center = (Max + Min) / 2;
  465. Min = center - delta * 1.2;
  466. Max = center + delta * 1.2;
  467. m_WavePlotX = Min;
  468. m_WavePlotWidth = Max - Min;
  469. }
  470. if (m_WavePlotWidth > Math.Pow(10, -4))
  471. {
  472. int IntervalTextX; int ExpCountX;
  473. GetIntervalValue(m_WavePlotX, m_WavePlotX + m_WavePlotWidth, out IntervalTextX, out ExpCountX);
  474. int StartIndexX = (int)(m_WavePlotX / (IntervalTextX * Math.Pow(10, ExpCountX)));
  475. int EndIndexX = (int)((m_WavePlotX + m_WavePlotWidth) / (IntervalTextX * Math.Pow(10, ExpCountX)));
  476. for (int i = StartIndexX; i <= EndIndexX; i++)
  477. {
  478. if (i == StartIndexX && (i * IntervalTextX * Math.Pow(10, ExpCountX)) < m_WavePlotX) continue;
  479. if (i == EndIndexX && (i * IntervalTextX * Math.Pow(10, ExpCountX)) > m_WavePlotX + m_WavePlotWidth) continue;
  480. double X = m_StartDrawLineX + ((Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth) * (i * IntervalTextX * Math.Pow(10, ExpCountX) - m_WavePlotX);
  481. if (X < m_StartDrawLineX) X = m_StartDrawLineX;
  482. if (X > Canvas_Main.ActualWidth) X = Canvas_Main.ActualWidth;
  483. graphics.DrawLine(m_PenLightGray, new PointF((float)X, 0), new PointF((float)X, (float)Canvas_Main.ActualHeight - 50));
  484. graphics.DrawLine(m_PenLine, new PointF((float)X, (float)Canvas_Main.ActualHeight - 30), new PointF((float)X, (float)Canvas_Main.ActualHeight - 50));
  485. if (IsHorizontalDateTimeAxis)
  486. {
  487. 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));
  488. }
  489. else
  490. {
  491. 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));
  492. }
  493. }
  494. }
  495. //画边框
  496. graphics.DrawLine(m_PenLine, new PointF((float)m_StartDrawLineX, 0), new PointF((float)m_StartDrawLineX, (float)Canvas_Main.ActualHeight - 50));
  497. graphics.DrawLine(m_PenLine, new PointF((float)m_StartDrawLineX, 0), new PointF((float)(Canvas_Main.ActualWidth), 0));
  498. 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)));
  499. graphics.DrawLine(m_PenLine, new PointF((float)Canvas_Main.ActualWidth - 1, 0), new PointF((float)Canvas_Main.ActualWidth - 1, (float)(Canvas_Main.ActualHeight - 50)));
  500. if (this.PlotDataPoints != null && this.PlotDataPoints.Count > 0)
  501. {
  502. List<PointF> points = new List<PointF>();
  503. for (int i = 0; i < PlotDataPoints.Count; i++)
  504. {
  505. if (PlotDataPoints[i].X >= m_WavePlotX && PlotDataPoints[i].X <= m_WavePlotX + m_WavePlotWidth)
  506. {
  507. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (PlotDataPoints[i].X - m_WavePlotX);
  508. if (StartX < m_StartDrawLineX || PlotDataPoints[i].X < m_WavePlotX) StartX = m_StartDrawLineX;
  509. if (StartX > Canvas_Main.ActualWidth || PlotDataPoints[i].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  510. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (PlotDataPoints[i].Y - m_WavePlotY);
  511. if (StartY < 0 || PlotDataPoints[i].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  512. if (StartY > Canvas_Main.ActualHeight - 50 || PlotDataPoints[i].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  513. points.Add(new PointF((float)StartX, (float)StartY));
  514. }
  515. if (m_WavePlotMinX == double.MaxValue || m_WavePlotMinX > PlotDataPoints[i].X) m_WavePlotMinX = PlotDataPoints[i].X;
  516. if (m_WavePlotMaxX == double.MinValue || m_WavePlotMaxX < PlotDataPoints[i].X) m_WavePlotMaxX = PlotDataPoints[i].X;
  517. if (m_WavePlotMinY == double.MaxValue || m_WavePlotMinY > PlotDataPoints[i].Y) m_WavePlotMinY = PlotDataPoints[i].Y;
  518. if (m_WavePlotMaxY == double.MinValue || m_WavePlotMaxY < PlotDataPoints[i].Y) m_WavePlotMaxY = PlotDataPoints[i].Y;
  519. }
  520. if (points.Count > 1 && points.Where(_ => float.IsNaN(_.X) || float.IsNaN(_.Y)).ToList<PointF>().Count == 0) graphics.DrawLines(m_PenData, points.ToArray());
  521. }
  522. if (this.PlotSDataPoints != null && this.PlotSDataPoints.Count > 0)
  523. {
  524. List<PointF> points = new List<PointF>();
  525. for (int i = 0; i < PlotSDataPoints.Count; i++)
  526. {
  527. if (PlotSDataPoints[i].X >= m_WavePlotX && PlotSDataPoints[i].X <= m_WavePlotX + m_WavePlotWidth)
  528. {
  529. double StartX = 50 + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (PlotSDataPoints[i].X - m_WavePlotX);
  530. if (StartX < m_StartDrawLineX || PlotSDataPoints[i].X < m_WavePlotX) StartX = m_StartDrawLineX;
  531. if (StartX > Canvas_Main.ActualWidth || PlotSDataPoints[i].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  532. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (PlotSDataPoints[i].Y - m_WavePlotY);
  533. if (StartY < 0 || PlotSDataPoints[i].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  534. if (StartY > Canvas_Main.ActualHeight - 50 || PlotSDataPoints[i].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  535. points.Add(new PointF((float)StartX, (float)StartY));
  536. }
  537. if (double.IsNaN(m_WavePlotMinX) || m_WavePlotMinX > PlotSDataPoints[i].X) m_WavePlotMinX = PlotSDataPoints[i].X;
  538. if (double.IsNaN(m_WavePlotMaxX) || m_WavePlotMaxX < PlotSDataPoints[i].X) m_WavePlotMaxX = PlotSDataPoints[i].X;
  539. if (double.IsNaN(m_WavePlotMinY) || m_WavePlotMinY > PlotSDataPoints[i].Y) m_WavePlotMinY = PlotSDataPoints[i].Y;
  540. if (double.IsNaN(m_WavePlotMaxY) || m_WavePlotMaxY < PlotSDataPoints[i].Y) m_WavePlotMaxY = PlotSDataPoints[i].Y;
  541. }
  542. if (points.Count > 1 && points.Where(_ => float.IsNaN(_.X) || float.IsNaN(_.Y)).ToList<PointF>().Count == 0) graphics.DrawLines(m_PenSData, points.ToArray());
  543. }
  544. if (this.PlotASDataPoints != null && this.PlotASDataPoints.Count > 0)
  545. {
  546. List<PointF> points = new List<PointF>();
  547. for (int i = 0; i < PlotASDataPoints.Count; i++)
  548. {
  549. if (PlotASDataPoints[i].X >= m_WavePlotX && PlotASDataPoints[i].X <= m_WavePlotX + m_WavePlotWidth)
  550. {
  551. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (PlotASDataPoints[i].X - m_WavePlotX);
  552. if (StartX < m_StartDrawLineX || PlotASDataPoints[i].X < m_WavePlotX) StartX = m_StartDrawLineX;
  553. if (StartX > Canvas_Main.ActualWidth || PlotASDataPoints[i].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  554. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (PlotASDataPoints[i].Y - m_WavePlotY);
  555. if (StartY < 0 || PlotASDataPoints[i].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  556. if (StartY > Canvas_Main.ActualHeight - 50 || PlotASDataPoints[i].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  557. points.Add(new PointF((float)StartX, (float)StartY));
  558. }
  559. if (m_WavePlotMinX == double.MaxValue || m_WavePlotMinX > PlotASDataPoints[i].X) m_WavePlotMinX = PlotASDataPoints[i].X;
  560. if (m_WavePlotMaxX == double.MinValue || m_WavePlotMaxX < PlotASDataPoints[i].X) m_WavePlotMaxX = PlotASDataPoints[i].X;
  561. if (m_WavePlotMinY == double.MaxValue || m_WavePlotMinY > PlotASDataPoints[i].Y) m_WavePlotMinY = PlotASDataPoints[i].Y;
  562. if (m_WavePlotMaxY == double.MinValue || m_WavePlotMaxY < PlotASDataPoints[i].Y) m_WavePlotMaxY = PlotASDataPoints[i].Y;
  563. }
  564. if (points.Count > 1 && points.Where(_ => float.IsNaN(_.X) || float.IsNaN(_.Y)).ToList<PointF>().Count == 0) graphics.DrawLines(m_PenASData, points.ToArray());
  565. }
  566. if (this.Points != null && this.Points.Count > 0)
  567. {
  568. List<PointF> points = new List<PointF>();
  569. for (int i = 0; i < Points.Count; i++)
  570. {
  571. if (Points[i].X >= m_WavePlotX && Points[i].X <= m_WavePlotX + m_WavePlotWidth)
  572. {
  573. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (Points[i].X - m_WavePlotX);
  574. if (StartX < m_StartDrawLineX || Points[i].X < m_WavePlotX) StartX = m_StartDrawLineX;
  575. if (StartX > Canvas_Main.ActualWidth || Points[i].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  576. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (Points[i].Y - m_WavePlotY);
  577. if (StartY < 0 || Points[i].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  578. if (StartY > Canvas_Main.ActualHeight - 50 || Points[i].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  579. points.Add(new PointF((float)StartX, (float)StartY));
  580. }
  581. if (m_WavePlotMinX == double.MaxValue || m_WavePlotMinX > Points[i].X) m_WavePlotMinX = Points[i].X;
  582. if (m_WavePlotMaxX == double.MinValue || m_WavePlotMaxX < Points[i].X) m_WavePlotMaxX = Points[i].X;
  583. if (m_WavePlotMinY == double.MaxValue || m_WavePlotMinY > Points[i].Y) m_WavePlotMinY = Points[i].Y;
  584. if (m_WavePlotMaxY == double.MinValue || m_WavePlotMaxY < Points[i].Y) m_WavePlotMaxY = Points[i].Y;
  585. }
  586. if (points.Count > 1 && points.Where(_ => float.IsNaN(_.X) || float.IsNaN(_.Y)).ToList<PointF>().Count == 0) graphics.DrawLines(m_PenData, points.ToArray());
  587. }
  588. if (this.PointCollections != null && this.PointCollections.Count > 0)
  589. {
  590. for (int i = 0; i < this.PointCollections.Count; i++)
  591. {
  592. List<PointF> points = new List<PointF>();
  593. for (int j = 0; j < this.PointCollections[i].Count; j++)
  594. {
  595. if (this.PointCollections[i][j].X >= m_WavePlotX && this.PointCollections[i][j].X <= m_WavePlotX + m_WavePlotWidth)
  596. {
  597. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (this.PointCollections[i][j].X - m_WavePlotX);
  598. if (StartX < m_StartDrawLineX || this.PointCollections[i][j].X < m_WavePlotX) StartX = m_StartDrawLineX;
  599. if (StartX > Canvas_Main.ActualWidth || this.PointCollections[i][j].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  600. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (this.PointCollections[i][j].Y - m_WavePlotY);
  601. if (StartY < 0 || this.PointCollections[i][j].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  602. if (StartY > Canvas_Main.ActualHeight - 50 || this.PointCollections[i][j].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  603. points.Add(new PointF((float)StartX, (float)StartY));
  604. }
  605. if (m_WavePlotMinX == double.MaxValue || m_WavePlotMinX > this.PointCollections[i][j].X) m_WavePlotMinX = this.PointCollections[i][j].X;
  606. if (m_WavePlotMaxX == double.MinValue || m_WavePlotMaxX < this.PointCollections[i][j].X) m_WavePlotMaxX = this.PointCollections[i][j].X;
  607. if (m_WavePlotMinY == double.MaxValue || m_WavePlotMinY > this.PointCollections[i][j].Y) m_WavePlotMinY = this.PointCollections[i][j].Y;
  608. if (m_WavePlotMaxY == double.MinValue || m_WavePlotMaxY < this.PointCollections[i][j].Y) m_WavePlotMaxY = this.PointCollections[i][j].Y;
  609. }
  610. if (points.Count > 1 && points.Where(_ => float.IsNaN(_.X) || float.IsNaN(_.Y)).ToList<PointF>().Count == 0)
  611. {
  612. graphics.DrawLines(m_PenCollencteions[i % 10], points.ToArray());
  613. }
  614. }
  615. }
  616. //框选
  617. if (m_StartMouseMove && m_MouseMove)
  618. {
  619. if (m_StartPoint != null && m_EndPoint != null)
  620. {
  621. int x = m_StartPoint.X > m_EndPoint.X ? (int)m_EndPoint.X : (int)m_StartPoint.X;
  622. int y = m_StartPoint.Y > m_EndPoint.Y ? (int)m_EndPoint.Y : (int)m_StartPoint.Y;
  623. int width = (int)Math.Abs(m_EndPoint.X - m_StartPoint.X);
  624. int height = (int)Math.Abs(m_EndPoint.Y - m_StartPoint.Y);
  625. System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(x, y, width, height);
  626. graphics.FillRectangle(System.Drawing.Brushes.LightGray, rectangle);
  627. graphics.DrawRectangle(new System.Drawing.Pen (System.Drawing.Color.Transparent), rectangle);
  628. }
  629. }
  630. else
  631. {
  632. //拾取坐标
  633. if (m_MouseEnter && m_CurrentPoint != null)
  634. {
  635. double X = m_WavePlotX + (m_CurrentPoint.X - m_StartDrawLineX) / ((this.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth);
  636. double Y = m_WavePlotY + (this.ActualHeight - 50 - m_CurrentPoint.Y) / ((this.ActualHeight - 50) / m_WavePlotHeight);
  637. if (this.PointCollections == null || this.PointCollections.Count <= 10)
  638. {
  639. 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));
  640. graphics.DrawLine(m_PenBlue, new System.Drawing.PointF((float)m_CurrentPoint.X, 0), new PointF((float)m_CurrentPoint.X, (float)Canvas_Main.ActualHeight - 50));
  641. //if (IsHorizontalDateTimeAxis)
  642. //{
  643. // 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));
  644. //}
  645. //else
  646. //{
  647. // 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));
  648. //}
  649. }
  650. //
  651. if (this.PlotDataPoints != null && this.PlotDataPoints.Count > 0)
  652. {
  653. if (X >= this.PlotDataPoints[0].X && X <= this.PlotDataPoints[this.PlotDataPoints.Count - 1].X)
  654. {
  655. //int index = this.PlotDataX.IndexOf((int)Math.Round(X, 0));
  656. var minDistance = this.PlotDataPoints.Min(x => Math.Abs(x.X - X));
  657. var closest = this.PlotDataPoints.First(x => Math.Abs(Math.Abs(x.X - X) - minDistance) < 0.0001f);
  658. int index = this.PlotDataPoints.IndexOf(closest);
  659. if (index >= 0)
  660. {
  661. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (PlotDataPoints[index].X - m_WavePlotX);
  662. if (StartX < m_StartDrawLineX || PlotDataPoints[index].X < m_WavePlotX) StartX = m_StartDrawLineX;
  663. if (StartX > Canvas_Main.ActualWidth || PlotDataPoints[index].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  664. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (PlotDataPoints[index].Y - m_WavePlotY);
  665. if (StartY < 0 || PlotDataPoints[index].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  666. if (StartY > Canvas_Main.ActualHeight - 50 || PlotDataPoints[index].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  667. if (!double.IsNaN(StartX) && !double.IsNaN(StartY))
  668. {
  669. System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle((int)StartX, (int)StartY, 5, 5);
  670. graphics.FillRectangle(System.Drawing.Brushes.Blue, rectangle);
  671. graphics.DrawEllipse(m_PenBlue, rectangle);
  672. if (IsHorizontalDateTimeAxis)
  673. {
  674. 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));
  675. }
  676. else
  677. {
  678. 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));
  679. }
  680. }
  681. }
  682. }
  683. }
  684. //
  685. if (this.PlotSDataPoints != null && this.PlotSDataPoints.Count > 0)
  686. {
  687. if (X >= this.PlotSDataPoints[0].X && X <= this.PlotSDataPoints[this.PlotSDataPoints.Count - 1].X)
  688. {
  689. //int index = this.PlotSDataX.IndexOf((int)Math.Round(X, 0));
  690. var minDistance = this.PlotSDataPoints.Min(x => Math.Abs(x.X - X));
  691. var closest = this.PlotSDataPoints.First(x => Math.Abs(Math.Abs(x.X - X) - minDistance) < 0.0001f);
  692. int index = this.PlotSDataPoints.IndexOf(closest);
  693. if (index >= 0)
  694. {
  695. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (PlotSDataPoints[index].X - m_WavePlotX);
  696. if (StartX < m_StartDrawLineX || PlotSDataPoints[index].X < m_WavePlotX) StartX = m_StartDrawLineX;
  697. if (StartX > Canvas_Main.ActualWidth || PlotSDataPoints[index].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  698. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (PlotSDataPoints[index].Y - m_WavePlotY);
  699. if (StartY < 0 || PlotSDataPoints[index].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  700. if (StartY > Canvas_Main.ActualHeight - 50 || PlotSDataPoints[index].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  701. if (!double.IsNaN(StartX) && !double.IsNaN(StartY))
  702. {
  703. System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle((int)StartX, (int)StartY, 5, 5);
  704. graphics.FillRectangle(System.Drawing.Brushes.Blue, rectangle);
  705. graphics.DrawEllipse(m_PenBlue, rectangle);
  706. 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));
  707. }
  708. }
  709. }
  710. }
  711. //
  712. if (this.PlotASDataPoints != null && this.PlotASDataPoints.Count > 0)
  713. {
  714. if (X >= this.PlotASDataPoints[0].X && X <= this.PlotASDataPoints[this.PlotASDataPoints.Count - 1].X)
  715. {
  716. //int index = this.PlotASDataX.IndexOf((int)Math.Round(X, 0));
  717. var minDistance = this.PlotASDataPoints.Min(x => Math.Abs(x.X - X));
  718. var closest = this.PlotASDataPoints.First(x => Math.Abs(x.X - X) == minDistance);
  719. int index = this.PlotASDataPoints.IndexOf(closest);
  720. if (index >= 0)
  721. {
  722. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (PlotASDataPoints[index].X - m_WavePlotX);
  723. if (StartX < m_StartDrawLineX || PlotASDataPoints[index].X < m_WavePlotX) StartX = m_StartDrawLineX;
  724. if (StartX > Canvas_Main.ActualWidth || PlotASDataPoints[index].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  725. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (PlotASDataPoints[index].Y - m_WavePlotY);
  726. if (StartY < 0 || PlotASDataPoints[index].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  727. if (StartY > Canvas_Main.ActualHeight - 50 || PlotASDataPoints[index].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  728. if (!double.IsNaN(StartX) && !double.IsNaN(StartY))
  729. {
  730. System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle((int)StartX, (int)StartY, 5, 5);
  731. graphics.FillRectangle(System.Drawing.Brushes.Blue, rectangle);
  732. graphics.DrawEllipse(m_PenBlue, rectangle);
  733. 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));
  734. }
  735. }
  736. }
  737. }
  738. //
  739. if (this.PointCollections != null && this.PointCollections.Count <= 10)
  740. {
  741. foreach (PointCollection point in this.PointCollections)
  742. {
  743. //if (X >= point[0].X && X <= point[point.Count - 1].X)
  744. {
  745. //int index = this.PlotASDataX.IndexOf((int)Math.Round(X, 0));
  746. var minDistance = point.Min(x => Math.Abs(x.X - X));
  747. var closest = point.First(x => Math.Abs(x.X - X) == minDistance);
  748. int index = point.IndexOf(closest);
  749. if (index >= 0)
  750. {
  751. double StartX = m_StartDrawLineX + (Canvas_Main.ActualWidth - m_StartDrawLineX) / m_WavePlotWidth * (point[index].X - m_WavePlotX);
  752. if (StartX < m_StartDrawLineX || point[index].X < m_WavePlotX) StartX = m_StartDrawLineX;
  753. if (StartX > Canvas_Main.ActualWidth || point[index].X > m_WavePlotX + m_WavePlotWidth) StartX = Canvas_Main.ActualWidth;
  754. double StartY = Canvas_Main.ActualHeight - 50 - (Canvas_Main.ActualHeight - 50.0) / m_WavePlotHeight * (point[index].Y - m_WavePlotY);
  755. if (StartY < 0 || point[index].Y > m_WavePlotY + m_WavePlotHeight) StartY = 0;
  756. if (StartY > Canvas_Main.ActualHeight - 50 || point[index].Y < m_WavePlotY) StartY = Canvas_Main.ActualHeight - 50;
  757. if (!double.IsNaN(StartX) && !double.IsNaN(StartY))
  758. {
  759. System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle((int)StartX, (int)StartY, 5, 5);
  760. graphics.FillRectangle(System.Drawing.Brushes.Blue, rectangle);
  761. graphics.DrawEllipse(m_PenBlue, rectangle);
  762. if (IsHorizontalDateTimeAxis)
  763. {
  764. 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));
  765. }
  766. else
  767. {
  768. 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));
  769. }
  770. }
  771. }
  772. }
  773. }
  774. }
  775. }
  776. }
  777. ImageBrush_BK.ImageSource = BitmapToBitmapImage(bitmap);
  778. }
  779. this.InvalidateVisual();
  780. }
  781. #endregion Private Draw
  782. #region Private
  783. private void Canvas_Main_SizeChanged(object sender, SizeChangedEventArgs e)
  784. {
  785. GraphicDraw();
  786. }
  787. private void Canvas_Main_MouseWheel(object sender, MouseWheelEventArgs e)
  788. {
  789. double factor = e.Delta < 0 ? 1.2 : 1 / 1.2;
  790. if (IsVerticalNavigationEnabled)
  791. {
  792. double Min = m_WavePlotY;
  793. double Max = m_WavePlotY + m_WavePlotHeight;
  794. double delta = (Max - Min) / 2;
  795. double center = (Max + Min) / 2;
  796. Min = center - delta * factor;
  797. Max = center + delta * factor;
  798. m_WavePlotY = Min;
  799. m_WavePlotHeight = Max - Min;
  800. }
  801. if (IsHorizontalNavigationEnabled)
  802. {
  803. double Min = m_WavePlotX;
  804. double Max = m_WavePlotX + m_WavePlotWidth;
  805. double delta = (Max - Min) / 2;
  806. double center = (Max + Min) / 2;
  807. Min = center - delta * factor;
  808. Max = center + delta * factor;
  809. m_WavePlotX = Min;
  810. m_WavePlotWidth = Max - Min;
  811. }
  812. GraphicDraw();
  813. }
  814. private void Canvas_Main_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  815. {
  816. this.Cursor = Cursors.Hand;
  817. if (e.ClickCount == 2)
  818. {
  819. FitControl();
  820. }
  821. else
  822. {
  823. m_StartPoint = Mouse.GetPosition(e.Source as FrameworkElement);
  824. if (e.LeftButton == MouseButtonState.Pressed)
  825. {
  826. m_StartMouseMove = true;
  827. }//开始绘制矩形
  828. }
  829. }
  830. private void Canvas_Main_MouseMove(object sender, MouseEventArgs e)
  831. {
  832. m_EndPoint = Mouse.GetPosition(e.Source as FrameworkElement);
  833. m_CurrentPoint = Mouse.GetPosition(e.Source as FrameworkElement);
  834. if ((Keyboard.Modifiers == ModifierKeys.Control || m_MouseMove) && m_StartMouseMove)
  835. {
  836. m_MouseMove = true;
  837. GraphicDraw();
  838. }//移动矩形位置
  839. else if (m_StartMouseMove)
  840. {
  841. m_WavePlotX = m_WavePlotX - (m_EndPoint.X - m_StartPoint.X) / (this.ActualWidth - 50) * m_WavePlotWidth;
  842. m_WavePlotY = m_WavePlotY + (m_EndPoint.Y - m_StartPoint.Y) / (this.ActualHeight - 50) * m_WavePlotHeight;
  843. m_StartPoint = m_EndPoint;
  844. GraphicDraw();
  845. }
  846. else if (this.PointCollections == null || this.PointCollections.Count <= 10)
  847. {
  848. GraphicDraw();
  849. }
  850. }
  851. private void Canvas_Main_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  852. {
  853. this.Cursor = Cursors.Arrow;
  854. if (m_StartMouseMove && m_MouseMove && e.LeftButton == MouseButtonState.Released)
  855. {
  856. m_StartMouseMove = false;
  857. m_MouseMove = false;
  858. if (m_StartPoint.X == m_EndPoint.X || m_StartPoint.Y == m_EndPoint.Y) return;
  859. double x = m_StartPoint.X > m_EndPoint.X ? m_EndPoint.X : m_StartPoint.X;
  860. double y = m_StartPoint.Y < m_EndPoint.Y ? m_EndPoint.Y : m_StartPoint.Y;
  861. double width = Math.Abs(m_EndPoint.X - m_StartPoint.X);
  862. double height = Math.Abs(m_EndPoint.Y - m_StartPoint.Y);
  863. //设置坐标轴起始位置
  864. this.m_WavePlotX = this.m_WavePlotX + (x - m_StartDrawLineX) / ((this.ActualWidth - m_StartDrawLineX) / this.m_WavePlotWidth);
  865. this.m_WavePlotY = this.m_WavePlotY + (this.ActualHeight - 50 - y) / ((this.ActualHeight - 50) / this.m_WavePlotHeight);
  866. this.m_WavePlotWidth = this.m_WavePlotWidth * (width / (this.ActualWidth - m_StartDrawLineX));
  867. this.m_WavePlotHeight = this.m_WavePlotHeight * (height / (this.ActualHeight - 50));
  868. GraphicDraw();
  869. }//结束绘制矩形
  870. m_StartMouseMove = false;
  871. m_MouseMove = false;
  872. }
  873. private void Canvas_Main_MouseEnter(object sender, MouseEventArgs e)
  874. {
  875. if (this.PointCollections == null || this.PointCollections.Count <= 10)
  876. {
  877. m_MouseEnter = true;
  878. }
  879. }
  880. private void Canvas_Main_MouseLeave(object sender, MouseEventArgs e)
  881. {
  882. if (this.PointCollections == null || this.PointCollections.Count <= 10) m_MouseEnter = false;
  883. GraphicDraw();
  884. }
  885. private void Canvas_Main_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
  886. {
  887. m_WavePlotX = WavePlotOriginX;
  888. m_WavePlotY = WavePlotOriginY;
  889. m_WavePlotWidth = WavePlotOriginWidth;
  890. m_WavePlotHeight = WavePlotOriginHeight;
  891. ResetControl();
  892. }
  893. /// <summary>
  894. /// 获取坐标间隔
  895. /// </summary>
  896. /// <returns></returns>
  897. private void GetIntervalValue(double min, double max, out int IntervalValue, out int expCount)
  898. {
  899. IntervalValue = 0;
  900. expCount = 0;
  901. double ActualValue = Math.Abs(max - min);
  902. if (ActualValue < 1)
  903. {
  904. while (ActualValue < 1)
  905. {
  906. ActualValue *= 10;
  907. expCount -= 1;
  908. }
  909. }
  910. else
  911. {
  912. while (ActualValue >= 10)
  913. {
  914. ActualValue /= 10;
  915. expCount += 1;
  916. }
  917. }
  918. if (ActualValue <= 1.2)
  919. {
  920. IntervalValue = 1;
  921. expCount -= 1;
  922. }
  923. else if (ActualValue <= 3)
  924. {
  925. IntervalValue = 2;
  926. expCount -= 1;
  927. }
  928. else if (ActualValue <= 5)
  929. {
  930. IntervalValue = 5;
  931. expCount -= 1;
  932. }
  933. else
  934. {
  935. IntervalValue = 1;
  936. }
  937. }
  938. /// <summary>
  939. /// 字符串值中包含E等科学计数法
  940. /// </summary>
  941. /// <param name="strData"></param>
  942. /// <returns></returns>
  943. private Decimal ChangeDataToD(string strData)
  944. {
  945. Decimal dData = 0.0M;
  946. if (strData.ToString().Contains("E"))
  947. {
  948. dData = Decimal.Parse(strData, System.Globalization.NumberStyles.Float);
  949. }
  950. else
  951. {
  952. dData = Decimal.Parse(strData);
  953. }
  954. return dData;
  955. }
  956. /// <summary>
  957. /// Bitmap --> BitmapImage
  958. /// </summary>
  959. /// <param name="bitmap"></param>
  960. /// <returns></returns>
  961. private BitmapImage BitmapToBitmapImage(Bitmap bitmap)
  962. {
  963. using (MemoryStream stream = new MemoryStream())
  964. {
  965. bitmap.Save(stream, ImageFormat.Png); // 坑点:格式选Bmp时,不带透明度
  966. stream.Position = 0;
  967. BitmapImage result = new BitmapImage();
  968. result.BeginInit();
  969. // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."
  970. // Force the bitmap to load right now so we can dispose the stream.
  971. result.CacheOption = BitmapCacheOption.OnLoad;
  972. result.StreamSource = stream;
  973. result.EndInit();
  974. result.Freeze();
  975. return result;
  976. }
  977. }
  978. #endregion Private
  979. }
  980. }