using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Media; using System.Windows.Shapes; namespace MuchInfo.Chart.WPF.Helpers { internal class PlotPainter { #region Fields private Dictionary _lineDictionary; private Polyline _polyline; private Rect _rect; private Dictionary _shapeDictionary; private double _thickness; #endregion Fields #region Constructors public PlotPainter(Rect rect, DoubleCollection dashStyle, double thickness) { this.DashStyle = dashStyle; this._thickness = thickness.Equals(default(double)) ? 1.2 : thickness; this._rect = rect; this._lineDictionary = new Dictionary(); this._shapeDictionary = new Dictionary(); } #endregion Constructors #region Properties #region Internal Properties /// /// Gets or sets the dash style. /// /// The dash style. internal DoubleCollection DashStyle { get; set; } #endregion Internal Properties #endregion Properties #region Methods #region Internal Methods /// /// Adds the line to line group. /// /// The line. /// The color. internal void AddLine(Geometry line, Color color) { if (this._lineDictionary == null) return; if (!this._lineDictionary.ContainsKey(color)) { var path = new Path(); if (this.DashStyle != null) { foreach (var current in DashStyle) { path.StrokeDashArray.Add(current); } } var rectangleGeometry = new RectangleGeometry { Rect = (this._rect) }; path.Clip = rectangleGeometry; path.UseLayoutRounding = (true); path.Stroke = new SolidColorBrush(color); path.StrokeThickness = this._thickness; var geometryGroup = new GeometryGroup(); path.Data = (geometryGroup); this._lineDictionary.Add(color, new PlotPainter.PaintObject(path, geometryGroup)); } this._lineDictionary[color].GeometryGroup.Children.Add(line); } /// /// Adds the point to polyline. /// /// The point. /// Color of the base. internal void AddPoint(Point point, Color baseColor) { if (this._polyline == null) { this._polyline = new Polyline { Stroke = new SolidColorBrush(baseColor) }; if (this.DashStyle != null) { foreach (var current in DashStyle) { this._polyline.StrokeDashArray.Add(current); } } var rectangleGeometry = new RectangleGeometry { Rect = (this._rect) }; this._polyline.Clip = rectangleGeometry; this._polyline.UseLayoutRounding = true; this._polyline.StrokeThickness = this._thickness; this._polyline.Points = new PointCollection(); } this._polyline.Points.Add(point); } /// /// Adds the shape to group. /// /// The shape. /// The color. internal void AddShape(Geometry shape, Color color ) { if (this._shapeDictionary == null) return; if (!this._shapeDictionary.ContainsKey(color)) { var rectangleGeometry = new RectangleGeometry { Rect = (this._rect) }; var path = new Path { Clip = (rectangleGeometry), UseLayoutRounding = (true), Stroke = (new SolidColorBrush(color)), Fill = (new SolidColorBrush(color)), StrokeThickness = (1.0) }; var geometryGroup = new GeometryGroup(); path.Data = geometryGroup; this._shapeDictionary.Add(color, new PlotPainter.PaintObject(path, geometryGroup)); } this._shapeDictionary[color].GeometryGroup.Children.Add(shape); } /// /// Adds the shape to group. /// /// The shape. /// The color. internal void AddShape(Geometry shape, Color color,bool Isfill) { if (this._shapeDictionary == null) return; if (!this._shapeDictionary.ContainsKey(color)) { var rectangleGeometry = new RectangleGeometry { Rect = (this._rect) }; var path = new Path { Clip = (rectangleGeometry), UseLayoutRounding = (true), Stroke = (new SolidColorBrush(color)), StrokeThickness = (1.0) }; if (Isfill) { path.Fill = (new SolidColorBrush(color)); } var geometryGroup = new GeometryGroup(); path.Data = geometryGroup; this._shapeDictionary.Add(color, new PlotPainter.PaintObject(path, geometryGroup)); } this._shapeDictionary[color].GeometryGroup.Children.Add(shape); } /// /// 获取绘制图形 /// internal List GetGraph() { var list = new List(); //添加shape paths if (_shapeDictionary != null) { list.AddRange(_shapeDictionary.Select(shape => shape.Value.Path).Cast()); } //添加line paths if (this._lineDictionary != null) { list.AddRange(_lineDictionary.Select(shape => shape.Value.Path).Cast()); } //添加poly line if (this._polyline != null) { list.Add(this._polyline); } return list; } #endregion Internal Methods #endregion Methods #region Nested Types /// /// 绘图对象 /// private class PaintObject { #region Constructors /// /// Initializes a new instance of the class. /// /// The path. /// The geometry group. public PaintObject(Path path, GeometryGroup geometryGroup) { this.Path = path; this.GeometryGroup = geometryGroup; } #endregion Constructors #region Properties #region Public Properties /// /// Gets or sets the geometry group. /// public GeometryGroup GeometryGroup { get; private set; } /// /// Gets or sets the path. /// public Path Path { get; private set; } #endregion Public Properties #endregion Properties } #endregion Nested Types } }