DrawGraphicsControl.xaml.cs 60 KB

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