PlotPainter.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Media;
  5. using System.Windows.Shapes;
  6. namespace MuchInfo.Chart.WPF.Helpers
  7. {
  8. internal class PlotPainter
  9. {
  10. #region Fields
  11. private Dictionary<Color, PlotPainter.PaintObject> _lineDictionary;
  12. private Polyline _polyline;
  13. private Rect _rect;
  14. private Dictionary<Color, PlotPainter.PaintObject> _shapeDictionary;
  15. private double _thickness;
  16. #endregion Fields
  17. #region Constructors
  18. public PlotPainter(Rect rect, DoubleCollection dashStyle, double thickness)
  19. {
  20. this.DashStyle = dashStyle;
  21. this._thickness = thickness.Equals(default(double)) ? 1.2 : thickness;
  22. this._rect = rect;
  23. this._lineDictionary = new Dictionary<Color, PlotPainter.PaintObject>();
  24. this._shapeDictionary = new Dictionary<Color, PlotPainter.PaintObject>();
  25. }
  26. #endregion Constructors
  27. #region Properties
  28. #region Internal Properties
  29. /// <summary>
  30. /// Gets or sets the dash style.
  31. /// </summary>
  32. /// <value>The dash style.</value>
  33. internal DoubleCollection DashStyle
  34. {
  35. get;
  36. set;
  37. }
  38. #endregion Internal Properties
  39. #endregion Properties
  40. #region Methods
  41. #region Internal Methods
  42. /// <summary>
  43. /// Adds the line to line group.
  44. /// </summary>
  45. /// <param name="line">The line.</param>
  46. /// <param name="color">The color.</param>
  47. internal void AddLine(Geometry line, Color color)
  48. {
  49. if (this._lineDictionary == null) return;
  50. if (!this._lineDictionary.ContainsKey(color))
  51. {
  52. var path = new Path();
  53. if (this.DashStyle != null)
  54. {
  55. foreach (var current in DashStyle)
  56. {
  57. path.StrokeDashArray.Add(current);
  58. }
  59. }
  60. var rectangleGeometry = new RectangleGeometry { Rect = (this._rect) };
  61. path.Clip = rectangleGeometry;
  62. path.UseLayoutRounding = (true);
  63. path.Stroke = new SolidColorBrush(color);
  64. path.StrokeThickness = this._thickness;
  65. var geometryGroup = new GeometryGroup();
  66. path.Data = (geometryGroup);
  67. this._lineDictionary.Add(color, new PlotPainter.PaintObject(path, geometryGroup));
  68. }
  69. this._lineDictionary[color].GeometryGroup.Children.Add(line);
  70. }
  71. /// <summary>
  72. /// Adds the point to polyline.
  73. /// </summary>
  74. /// <param name="point">The point.</param>
  75. /// <param name="baseColor">Color of the base.</param>
  76. internal void AddPoint(Point point, Color baseColor)
  77. {
  78. if (this._polyline == null)
  79. {
  80. this._polyline = new Polyline { Stroke = new SolidColorBrush(baseColor) };
  81. if (this.DashStyle != null)
  82. {
  83. foreach (var current in DashStyle)
  84. {
  85. this._polyline.StrokeDashArray.Add(current);
  86. }
  87. }
  88. var rectangleGeometry = new RectangleGeometry { Rect = (this._rect) };
  89. this._polyline.Clip = rectangleGeometry;
  90. this._polyline.UseLayoutRounding = true;
  91. this._polyline.StrokeThickness = this._thickness;
  92. this._polyline.Points = new PointCollection();
  93. }
  94. this._polyline.Points.Add(point);
  95. }
  96. /// <summary>
  97. /// Adds the shape to group.
  98. /// </summary>
  99. /// <param name="shape">The shape.</param>
  100. /// <param name="color">The color.</param>
  101. internal void AddShape(Geometry shape, Color color )
  102. {
  103. if (this._shapeDictionary == null) return;
  104. if (!this._shapeDictionary.ContainsKey(color))
  105. {
  106. var rectangleGeometry = new RectangleGeometry { Rect = (this._rect) };
  107. var path = new Path
  108. {
  109. Clip = (rectangleGeometry),
  110. UseLayoutRounding = (true),
  111. Stroke = (new SolidColorBrush(color)),
  112. Fill = (new SolidColorBrush(color)),
  113. StrokeThickness = (1.0)
  114. };
  115. var geometryGroup = new GeometryGroup();
  116. path.Data = geometryGroup;
  117. this._shapeDictionary.Add(color, new PlotPainter.PaintObject(path, geometryGroup));
  118. }
  119. this._shapeDictionary[color].GeometryGroup.Children.Add(shape);
  120. }
  121. /// <summary>
  122. /// Adds the shape to group.
  123. /// </summary>
  124. /// <param name="shape">The shape.</param>
  125. /// <param name="color">The color.</param>
  126. internal void AddShape(Geometry shape, Color color,bool Isfill)
  127. {
  128. if (this._shapeDictionary == null) return;
  129. if (!this._shapeDictionary.ContainsKey(color))
  130. {
  131. var rectangleGeometry = new RectangleGeometry { Rect = (this._rect) };
  132. var path = new Path
  133. {
  134. Clip = (rectangleGeometry),
  135. UseLayoutRounding = (true),
  136. Stroke = (new SolidColorBrush(color)),
  137. StrokeThickness = (1.0)
  138. };
  139. if (Isfill)
  140. {
  141. path.Fill = (new SolidColorBrush(color));
  142. }
  143. var geometryGroup = new GeometryGroup();
  144. path.Data = geometryGroup;
  145. this._shapeDictionary.Add(color, new PlotPainter.PaintObject(path, geometryGroup));
  146. }
  147. this._shapeDictionary[color].GeometryGroup.Children.Add(shape);
  148. }
  149. /// <summary>
  150. /// 获取绘制图形
  151. /// </summary>
  152. internal List<FrameworkElement> GetGraph()
  153. {
  154. var list = new List<FrameworkElement>();
  155. //添加shape paths
  156. if (_shapeDictionary != null)
  157. {
  158. list.AddRange(_shapeDictionary.Select(shape => shape.Value.Path).Cast<FrameworkElement>());
  159. }
  160. //添加line paths
  161. if (this._lineDictionary != null)
  162. {
  163. list.AddRange(_lineDictionary.Select(shape => shape.Value.Path).Cast<FrameworkElement>());
  164. }
  165. //添加poly line
  166. if (this._polyline != null)
  167. {
  168. list.Add(this._polyline);
  169. }
  170. return list;
  171. }
  172. #endregion Internal Methods
  173. #endregion Methods
  174. #region Nested Types
  175. /// <summary>
  176. /// 绘图对象
  177. /// </summary>
  178. private class PaintObject
  179. {
  180. #region Constructors
  181. /// <summary>
  182. /// Initializes a new instance of the <see cref="PaintObject"/> class.
  183. /// </summary>
  184. /// <param name="path">The path.</param>
  185. /// <param name="geometryGroup">The geometry group.</param>
  186. public PaintObject(Path path, GeometryGroup geometryGroup)
  187. {
  188. this.Path = path;
  189. this.GeometryGroup = geometryGroup;
  190. }
  191. #endregion Constructors
  192. #region Properties
  193. #region Public Properties
  194. /// <summary>
  195. /// Gets or sets the geometry group.
  196. /// </summary>
  197. public GeometryGroup GeometryGroup
  198. {
  199. get;
  200. private set;
  201. }
  202. /// <summary>
  203. /// Gets or sets the path.
  204. /// </summary>
  205. public Path Path
  206. {
  207. get;
  208. private set;
  209. }
  210. #endregion Public Properties
  211. #endregion Properties
  212. }
  213. #endregion Nested Types
  214. }
  215. }