DrawGraphicsControl.xaml.cs 56 KB

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