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