DrawGraphicsControl.xaml.cs 61 KB

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