DrawingGannBox.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using Microsoft.VisualBasic.CompilerServices;
  2. using MuchInfo.Chart.Data;
  3. using MuchInfo.Chart.Data.EnumTypes;
  4. using MuchInfo.Chart.Infrastructure.Helpers;
  5. using MuchInfo.Chart.Infrastructure.Utilities;
  6. using MuchInfo.Chart.WPF.Controls.Drawing;
  7. using MuchInfo.Chart.WPF.Controls.Editors;
  8. using MuchInfo.Chart.WPF.Helpers;
  9. using MuchInfo.Chart.WPF.Primitives.Interfaces;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Media;
  16. using System.Windows.Shapes;
  17. using System.Xml.Linq;
  18. using NPOI.SS.Formula.Functions;
  19. namespace MuchInfo.Chart.WPF.Primitives.Drawing
  20. {
  21. public class DrawingGannBox : Drawing2PointBase
  22. {
  23. private Color CurColor;
  24. private List<Line> CurLines;
  25. private ExtendableLine CurExtendableLine;
  26. public Color CenterColor
  27. {
  28. get
  29. {
  30. return this.CurColor;
  31. }
  32. set
  33. {
  34. bool flag = !value.Equals(this.CurColor);
  35. if (flag)
  36. {
  37. this.CurColor = value;
  38. this.ColorChanged();
  39. this.PropertyChanged();
  40. }
  41. }
  42. }
  43. public DrawingGannBox()
  44. {
  45. this.CurColor = Colors.Yellow;
  46. this.CurLines = new List<Line>();
  47. }
  48. public override DrawingToolType ToolType()
  49. {
  50. return DrawingToolType.GannBox;
  51. }
  52. public override IDrawingTool Clone()
  53. {
  54. return new DrawingGannBox
  55. {
  56. mColor = Colors.Yellow
  57. };
  58. }
  59. protected override List<FrameworkElement> GetDrawingPlots(Chart owner, ScaleLayer aScale, Rect aRect)
  60. {
  61. //初始化 ExtendableLine
  62. if (this.CurExtendableLine == null)
  63. {
  64. this.CurExtendableLine = new ExtendableLine(this, owner);
  65. this.CurExtendableLine.Stroke = new SolidColorBrush(this.CurColor);
  66. this.CurExtendableLine.StrokeThickness = 1.4;
  67. DoubleCollection doubleCollection = new DoubleCollection();
  68. doubleCollection.Add(10.0);
  69. doubleCollection.Add(10.0);
  70. this.CurExtendableLine.SetMainStrokeDash(doubleCollection);
  71. this.CurExtendableLine.ExtensionStroke = this.CurExtendableLine.Stroke;
  72. }
  73. //初始化颜色
  74. if (!ColorHelper.ColorIsContrasting(this.CurColor, owner.ChartBackgroundColor))
  75. {
  76. this.CurColor = ColorHelper.InverseColor(this.CurColor);
  77. }
  78. this.CurLines.Clear();
  79. List<FrameworkElement> RltLineList = new List<FrameworkElement>();
  80. for (int i = 0; i < 17; i++)
  81. {
  82. var LineTmp = new Line()
  83. {
  84. Stroke = new SolidColorBrush(this.mColor),
  85. StrokeThickness = 1.4,
  86. };
  87. this.CurLines.Add(LineTmp);
  88. RltLineList.Add(LineTmp);
  89. }
  90. this.MoveDrawingToNewLocations(aRect);
  91. RltLineList.Add(this.CurExtendableLine);
  92. return RltLineList;
  93. }
  94. public override string RootName
  95. {
  96. get { return "DrawGannBox"; }
  97. }
  98. public override string MenuDescription()
  99. {
  100. return LanguageManager.FindResource(LanguageConst.DrawingTool_Abbreviation_GannBox);
  101. }
  102. public override string Abbreviation()
  103. {
  104. return "Gann";
  105. }
  106. public override string Description()
  107. {
  108. return this.MenuDescription();
  109. }
  110. public override List<ControlPair> GetPropertyEditors()
  111. {
  112. List<ControlPair> list = new List<ControlPair>();
  113. ControlPair controlPair = new ControlPair();
  114. TextBlock textBlock = new TextBlock();
  115. textBlock.Text = ("Fann Color");
  116. controlPair.ControlLeft = textBlock;
  117. ColorOpacityEditor controlRight = new ColorOpacityEditor(this, "Color", "", "");
  118. controlPair.ControlRight = controlRight;
  119. list.Add(controlPair);
  120. controlPair = new ControlPair();
  121. textBlock = new TextBlock();
  122. textBlock.Text = ("Center Line Color");
  123. controlPair.ControlLeft = textBlock;
  124. controlRight = new ColorOpacityEditor(this, "CenterColor", "", "");
  125. controlPair.ControlRight = controlRight;
  126. list.Add(controlPair);
  127. return list;
  128. }
  129. protected override void ColorChanged()
  130. {
  131. this.CurExtendableLine.Stroke = new SolidColorBrush(this.CurColor);
  132. try
  133. {
  134. List<Line>.Enumerator enumerator = this.CurLines.GetEnumerator();
  135. while (enumerator.MoveNext())
  136. {
  137. Line current = enumerator.Current;
  138. current.Stroke = (new SolidColorBrush(this.mColor));
  139. this.CurExtendableLine.ExtensionStroke = this.CurExtendableLine.Stroke;
  140. }
  141. }
  142. finally
  143. {
  144. }
  145. }
  146. protected override void MoveDrawingToNewLocations(Rect aRect)
  147. {
  148. //TODO:调整ExtendableLine主线位置
  149. this.CurExtendableLine.SetPosition(this.mDrawP1, this.mDrawP2, this.mDrawP1.X > this.mDrawP2.X, this.mDrawP1.X < this.mDrawP2.X, aRect);
  150. if (this.mDrawP1.X != this.mDrawP2.X && this.mDrawP1.Y != this.mDrawP2.Y)
  151. {
  152. Point P1 = new Point(this.mDrawP1.X, this.mDrawP1.Y);
  153. Point P2 = new Point(this.mDrawP2.X, this.mDrawP2.Y);
  154. var absX = P1.X > P2.X ? P1.X - P2.X : P2.X - P1.X;
  155. var absY = P1.Y > P2.Y ? P1.Y - P2.Y : P2.Y - P1.Y;
  156. var minX = P1.X > P2.X ? P2.X : P1.X;
  157. var minY = P1.Y > P2.Y ? P2.Y : P1.Y;
  158. var maxX = P1.X > P2.X ? P1.X : P2.X;
  159. var maxY = P1.Y > P2.Y ? P1.Y : P2.Y;
  160. //TODO:添加 横线、纵线
  161. var multiple = 0.25;
  162. for (int i = 0; i < 5; i++)
  163. {
  164. //横线
  165. var TmpHY = i * multiple * absY + minY;
  166. this.CurLines[i].X1 = minX;
  167. this.CurLines[i].Y1 = TmpHY;
  168. this.CurLines[i].X2 = maxX;
  169. this.CurLines[i].Y2 = TmpHY;
  170. //纵线
  171. var TmpVX = i * multiple * absX + minX;
  172. this.CurLines[i + 5].X1 = TmpVX;
  173. this.CurLines[i + 5].Y1 = minY;
  174. this.CurLines[i + 5].X2 = TmpVX;
  175. this.CurLines[i + 5].Y2 = maxY;
  176. //网格交叉线
  177. if (i > 0 && i < 4)
  178. {
  179. Point A = new Point();
  180. Point B = new Point();
  181. Point TmpPointH = new Point();
  182. Point TmpPointV = new Point();
  183. if (P1.X < P2.X && P1.Y < P2.Y)
  184. {
  185. var TmpPointHTmp = new Point(maxX, TmpHY);
  186. double slope = GeometryHelper.LineSlope(ref P1, ref TmpPointHTmp);
  187. TmpPointH = new Point(GeometryHelper.XFromLine(TmpPointHTmp, slope, aRect.Height),
  188. aRect.Height);
  189. var TmpPointVTmp = new Point(TmpVX, maxY);
  190. slope = GeometryHelper.LineSlope(ref P1, ref TmpPointVTmp);
  191. TmpPointV = new Point(aRect.Width,
  192. GeometryHelper.YFromLine(TmpPointVTmp, slope, aRect.Width));
  193. }
  194. else if (P1.X < P2.X && P1.Y > P2.Y)
  195. {
  196. var TmpPointHTmp = new Point(maxX, TmpHY);
  197. double slope = GeometryHelper.LineSlope(ref P1, ref TmpPointHTmp);
  198. TmpPointH = new Point(aRect.Width,
  199. GeometryHelper.YFromLine(TmpPointHTmp, slope, aRect.Width));
  200. var TmpPointVTmp = new Point(TmpVX, minY);
  201. slope = GeometryHelper.LineSlope(ref P1, ref TmpPointVTmp);
  202. TmpPointV = new Point(GeometryHelper.XFromLine(TmpPointVTmp, slope, aRect.Top), aRect.Top);
  203. }
  204. else if (P1.X > P2.X && P1.Y < P2.Y)
  205. {
  206. var TmpPointHTmp = new Point(minX, TmpHY);
  207. double slope = GeometryHelper.LineSlope(ref P1, ref TmpPointHTmp);
  208. TmpPointH = new Point(aRect.Left, GeometryHelper.YFromLine(TmpPointHTmp, slope, aRect.Left));
  209. var TmpPointVTmp = new Point(TmpVX, maxY);
  210. slope = GeometryHelper.LineSlope(ref P1, ref TmpPointVTmp);
  211. TmpPointV = new Point(GeometryHelper.XFromLine(TmpPointVTmp, slope, aRect.Bottom),
  212. aRect.Bottom);
  213. }
  214. else if (P1.X > P2.X && P1.Y > P2.Y)
  215. {
  216. var TmpPointHTmp = new Point(minX, TmpHY);
  217. double slope = GeometryHelper.LineSlope(ref P1, ref TmpPointHTmp);
  218. TmpPointH = new Point(aRect.Left, GeometryHelper.YFromLine(TmpPointHTmp, slope, aRect.Left));
  219. var TmpPointVTmp = new Point(TmpVX, minY);
  220. slope = GeometryHelper.LineSlope(ref P1, ref TmpPointVTmp);
  221. TmpPointV = new Point(GeometryHelper.XFromLine(TmpPointVTmp, slope, aRect.Top), aRect.Top);
  222. }
  223. A = P1;
  224. B = TmpPointH;
  225. Point startPoint = new Point();
  226. Point endPoint = new Point();
  227. PointOfLineAtRectangleCalculator(ref startPoint, ref endPoint, A, B, aRect);
  228. this.CurLines[i + 10].X1 = startPoint.X;
  229. this.CurLines[i + 10].Y1 = startPoint.Y;
  230. this.CurLines[i + 10].X2 = endPoint.X;
  231. this.CurLines[i + 10].Y2 = endPoint.Y;
  232. B = TmpPointV;
  233. PointOfLineAtRectangleCalculator(ref startPoint, ref endPoint, A, B, aRect);
  234. this.CurLines[i + 13].X1 = startPoint.X;
  235. this.CurLines[i + 13].Y1 = startPoint.Y;
  236. this.CurLines[i + 13].X2 = endPoint.X;
  237. this.CurLines[i + 13].Y2 = endPoint.Y;
  238. }
  239. }
  240. }
  241. }
  242. protected override void AddFromXml(XElement xml)
  243. {
  244. bool flag = xml.Attribute("CClr") != null;
  245. if (flag)
  246. {
  247. this.CurColor = ColorHelper.ColorFromString(xml.Attribute("CClr").Value);
  248. }
  249. }
  250. protected override void AddToXml(XElement xml, List<object> args)
  251. {
  252. xml.Add(new XAttribute("CClr", this.CurColor.ToString()));
  253. }
  254. protected override void AddFromInfo(DrawingToolInfo info)
  255. {
  256. base.AddFromInfo(info);
  257. try
  258. {
  259. XElement xElement = XElement.Parse(info.XML);
  260. bool flag = xElement.Name == "Root";
  261. if (flag)
  262. {
  263. bool flag2 = xElement.Attribute("Color1") != null;
  264. if (flag2)
  265. {
  266. this.CurColor = ColorHelper.ColorFromInt(int.Parse(xElement.Attribute("Color1").Value));
  267. }
  268. }
  269. }
  270. catch (Exception expr_A1)
  271. {
  272. ProjectData.SetProjectError(expr_A1);
  273. ProjectData.ClearProjectError();
  274. }
  275. }
  276. protected override void AddToInfo(DrawingToolInfo info)
  277. {
  278. base.AddToInfo(info);
  279. XElement xElement = new XElement("Root");
  280. xElement.Add(new XAttribute("Color1", ColorHelper.ColorToInt(this.CurColor)));
  281. info.XML = xElement.ToString();
  282. }
  283. public override ImageSource Icon()
  284. {
  285. ImageSource result;
  286. try
  287. {
  288. result = ImageHelper.GetImage("FibFan.png");
  289. }
  290. catch (Exception expr_3A)
  291. {
  292. ProjectData.SetProjectError(expr_3A);
  293. result = null;
  294. ProjectData.ClearProjectError();
  295. }
  296. return result;
  297. }
  298. protected override bool CanExtendLeft()
  299. {
  300. return false;
  301. }
  302. protected override bool CanExtendRight()
  303. {
  304. return false;
  305. }
  306. }
  307. }