| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- 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<Color, PlotPainter.PaintObject> _lineDictionary;
- private Polyline _polyline;
- private Rect _rect;
- private Dictionary<Color, PlotPainter.PaintObject> _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<Color, PlotPainter.PaintObject>();
- this._shapeDictionary = new Dictionary<Color, PlotPainter.PaintObject>();
- }
- #endregion Constructors
- #region Properties
- #region Internal Properties
- /// <summary>
- /// Gets or sets the dash style.
- /// </summary>
- /// <value>The dash style.</value>
- internal DoubleCollection DashStyle
- {
- get;
- set;
- }
- #endregion Internal Properties
- #endregion Properties
- #region Methods
- #region Internal Methods
- /// <summary>
- /// Adds the line to line group.
- /// </summary>
- /// <param name="line">The line.</param>
- /// <param name="color">The color.</param>
- 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);
- }
- /// <summary>
- /// Adds the point to polyline.
- /// </summary>
- /// <param name="point">The point.</param>
- /// <param name="baseColor">Color of the base.</param>
- 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);
- }
- /// <summary>
- /// Adds the shape to group.
- /// </summary>
- /// <param name="shape">The shape.</param>
- /// <param name="color">The color.</param>
- 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);
- }
- /// <summary>
- /// Adds the shape to group.
- /// </summary>
- /// <param name="shape">The shape.</param>
- /// <param name="color">The color.</param>
- 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);
- }
-
- /// <summary>
- /// 获取绘制图形
- /// </summary>
- internal List<FrameworkElement> GetGraph()
- {
- var list = new List<FrameworkElement>();
- //添加shape paths
- if (_shapeDictionary != null)
- {
- list.AddRange(_shapeDictionary.Select(shape => shape.Value.Path).Cast<FrameworkElement>());
- }
- //添加line paths
- if (this._lineDictionary != null)
- {
- list.AddRange(_lineDictionary.Select(shape => shape.Value.Path).Cast<FrameworkElement>());
- }
- //添加poly line
- if (this._polyline != null)
- {
- list.Add(this._polyline);
- }
- return list;
- }
- #endregion Internal Methods
- #endregion Methods
- #region Nested Types
- /// <summary>
- /// 绘图对象
- /// </summary>
- private class PaintObject
- {
- #region Constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="PaintObject"/> class.
- /// </summary>
- /// <param name="path">The path.</param>
- /// <param name="geometryGroup">The geometry group.</param>
- public PaintObject(Path path, GeometryGroup geometryGroup)
- {
- this.Path = path;
- this.GeometryGroup = geometryGroup;
- }
- #endregion Constructors
- #region Properties
- #region Public Properties
- /// <summary>
- /// Gets or sets the geometry group.
- /// </summary>
- public GeometryGroup GeometryGroup
- {
- get;
- private set;
- }
- /// <summary>
- /// Gets or sets the path.
- /// </summary>
- public Path Path
- {
- get;
- private set;
- }
- #endregion Public Properties
- #endregion Properties
- }
- #endregion Nested Types
- }
- }
|