WPF 曲线图表控件(自制)(二)

    如果还有不懂的请去资源区下载控件包含所有源码

http://download.csdn.net/download/koloumi/9947692
接下来将代码上的。
界面调整部分的代码函数
WPF 曲线图表控件(自制)(二)_第1张图片

    还有一个重要的函数就是控件的MyChart_SizeChanged事件。
/// 
        /// 尺寸改变
        /// 
        /// 
        /// 
        private void MyChart_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Size newsize = e.NewSize;
            if(!e.WidthChanged)
            {
                this.Width = newsize.Width;
            }
            if(!e.HeightChanged)
            {
                this.Height = newsize.Height;
            }
            AdjustSize();
            AdjustScale();
            AdjustLines();
            AdjustLinesAndPoints();
        }
    接下来是先看下定义的属性:
/// 
        /// 数据集
        /// 
        private Dictionary<string, DrawData> _data = new Dictionary<string, DrawData>();
        /// 
        /// 坐标原点
        /// 
        private Point _origPoint = new Point();
        /// 
        /// X轴上的文字
        /// 
        private List _xTip = new List();
        /// 
        /// Y轴上的文字
        /// 
        private List _yTip = new List();
        /// 
        /// X轴的区间
        /// 
        private VectorChart2 _xLimt = new VectorChart2(double.MaxValue, double.MinValue);
        /// 
        /// Y轴的区间
        /// 
        private VectorChart2 _yLimt = new VectorChart2(double.MaxValue, double.MinValue);
        /// 
        /// 每个像素映射为多少距离
        /// 
        private VectorChart2 _everyDisForPiexl = new VectorChart2();
        /// 
        /// 缩放中心点的偏移
        /// 
        private VectorChart2 _centerOffect = new VectorChart2(0, 0);
        /// 
        /// 当前线的缩放比例
        /// 
        private Vector4 _currentLinesScale = new Vector4(1, 1, 1, 1);
        /// 
        /// 每个刻度之间的间隔
        /// 
        private static double _everyDis = 30;
    在这个控件上我们将每个刻度的间隔直接定死,这样有需要的时候直接修改刻度上的数值,而不用去移动刻度。如果尺寸变大只要增加刻度线,减少反之。
    在控件中用到的数据结构
/// 
    /// 向量
    /// 
    public struct VectorChart2
    {
        public double Vec1;

        public double Vec2;

        public VectorChart2(double v1, double v2)
        {
            this.Vec1 = v1;
            this.Vec2 = v2;
        }
        /// 
        /// Point 转换VectorChart2
        /// 
        /// 
        public static implicit operator VectorChart2(Point p)
        {
            return new VectorChart2(p.X, p.Y);
        }
        /// 
        /// 乘法
        /// 
        /// 
        /// 
        /// 
        public static VectorChart2 operator * (VectorChart2 v1, double ratio)
        {
            return new VectorChart2(v1.Vec1 * ratio, v1.Vec2 * ratio);
        }
    }
    /// 
    /// 4维向量
    /// 
    public struct Vector4
    {
        /// 
        ///
        /// 
        public static Vector4 Empty = new Vector4() { _isEmpty = true };

        public double Vec1;

        public double Vec2;

        public double Vec3;

        public double Vec4;
        /// 
        /// 是否为空数据
        /// 
        private bool _isEmpty;

        public Vector4(double Vec1, double Vec2, double Vec3, double Vec4)
        {
            this.Vec1 = Vec1;
            this.Vec2 = Vec2;
            this.Vec3 = Vec3;
            this.Vec4 = Vec4;
            _isEmpty = false;
        }

        /// 
        /// 重载等号运算符
        /// 
        /// 
        /// 
        /// 
        public static bool operator ==(Vector4 v1, Vector4 v2)
        {
            if (v1.Vec1 == v2.Vec1 && v1.Vec2 == v2.Vec2 && v1.Vec3 == v2.Vec3 && v1.Vec4 == v2.Vec4 && v1._isEmpty == v2._isEmpty)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// 
        /// 重载
        /// 
        /// 
        /// 
        /// 
        public static bool operator !=(Vector4 v1, Vector4 v2)
        {
            return !(v1 == v2);
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
    /// 
    /// 绘图数据
    /// 
    public class DrawData : IDisposable
    {
        /// 
        /// 线
        /// 
        public Polyline Line;
        /// 
        /// 线的标记点
        /// 
        public List Points = new List();
        /// 
        /// 源数据集合
        /// 
        public ObservableCollection Ps = null;
        /// 
        /// 极值
        /// 
        public Vector4 Vec4 = Vector4.Empty;
        /// 
        /// 提示
        /// 
        public LineTitle LineTitle = new LineTitle();


        public DrawData()
        {

        }

        ~DrawData()
        {
            this.Dispose();
        }
        #region IDisposable Support
        private bool disposedValue = false; // 要检测冗余调用

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)。
                    Line = null;
                    Ps = null;
                    Points = null;
                    LineTitle = null;
                }

                // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
                // TODO: 将大型字段设置为 null。

                disposedValue = true;
            }
        }

        // TODO: 仅当以上 Dispose(bool disposing) 拥有用于释放未托管资源的代码时才替代终结器。
        // ~DrawData() {
        //   // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
        //   Dispose(false);
        // }

        // 添加此代码以正确实现可处置模式。
        public void Dispose()
        {
            // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
            Dispose(true);
            // TODO: 如果在以上内容中替代了终结器,则取消注释以下行。
            GC.SuppressFinalize(this);
        }
        #endregion
    }
调整曲线 刻度(直接修改)部分:

WPF 曲线图表控件(自制)(二)_第2张图片

然后是调整所有绘图区的尺寸:

WPF 曲线图表控件(自制)(二)_第3张图片

极值的调整 这里乘以0.15 是为了扩大上限 让初始曲线能够包括在视图中。

WPF 曲线图表控件(自制)(二)_第4张图片

还有曲线刻度的添加式修改:(因为增加删除速度比较慢,所以只在有必要的时候调用这个,比如尺寸改变的时候,平时直接修改刻度即可)
/// 
        /// 调整刻度\网格线(删除重新添加)
        /// 
        protected void AdjustScale()
        {
            //if (!IsHaveLine) return;
            if (X_Axis.Width == 0 || X_Axis.Height == 0 || X_Axis.Width == double.NaN || X_Axis.Height == double.NaN) return;
            //if (GridLines.Width == 0 || GridLines.Height == 0 || GridLines.Width == double.NaN || double.IsNaN(GridLines.Height)) return;
            ///清除X轴的刻度
            if (X_Axis.Children.Count > 1)
            {
                X_Axis.Children.RemoveRange(1, X_Axis.Children.Count - 1);
            }
            ///清除Y轴的刻度
            if (Y_Axis.Children.Count > 1)
            {
                Y_Axis.Children.RemoveRange(1, Y_Axis.Children.Count - 1);
            }
            ///清除网格线
            GridLines.Children.Clear();
            ///清除刻度线的提示文字
            _xTip.Clear();
            _yTip.Clear();

            ///添加X轴的
            for (double i = _origPoint.X; i < X_Axis.Width; i += _everyDis)
            {
                Line x_scale = new Line();
                x_scale.StrokeThickness = 2;
                x_scale.Stroke = new SolidColorBrush(Colors.Black);
                x_scale.StrokeStartLineCap = PenLineCap.Round;
                x_scale.StrokeEndLineCap = PenLineCap.Round;
                x_scale.Width = 4;
                x_scale.Height = 20;
                x_scale.X1 = 2;
                x_scale.Y1 = 4;
                x_scale.X2 = 2;
                x_scale.Y2 = x_scale.Height;
                Canvas.SetLeft(x_scale, i);
                Canvas.SetTop(x_scale, 0);
                X_Axis.Children.Add(x_scale);
                ///网格线
                GridLines.Children.Add(GetGridLine(new Point(i - 2, 0), false, GridLinesArea.Vec2));
                //double.IsNaN(GridLines.Height) ? GridLines.ActualHeight : GridLines.Height
                ///添加提示文字
                TextBlock block = new TextBlock();
                block.FontSize = 10;
                block.Text = i.ToString();
                Canvas.SetLeft(block, i);
                Canvas.SetTop(block, 25);
                _xTip.Add(block);
                X_Axis.Children.Add(block);
            }
            ///添加Y轴的
            for (double i = _origPoint.Y; i < Y_Axis.Height; i += _everyDis)
            {
                Line y_scale = new Line();
                y_scale.StrokeThickness = 2;
                y_scale.Stroke = new SolidColorBrush(Colors.Black);
                y_scale.StrokeStartLineCap = PenLineCap.Round;
                y_scale.StrokeEndLineCap = PenLineCap.Round;
                y_scale.Width = 20;
                y_scale.Height = 4;
                y_scale.X1 = 4;
                y_scale.Y1 = 2;
                y_scale.X2 = y_scale.Width;
                y_scale.Y2 = 2;
                Canvas.SetBottom(y_scale, i + 1);
                Canvas.SetRight(y_scale, 2);
                Y_Axis.Children.Add(y_scale);
                ///网格线
                GridLines.Children.Add(GetGridLine(new Point(i - 2, 0), true, GridLinesArea.Vec1));
                //double.IsNaN(GridLines.Width) ? GridLines.ActualWidth : GridLines.Width)
                ///添加提示文字
                TextBlock block = new TextBlock();
                block.FontSize = 10;
                block.Text = i.ToString();
                Canvas.SetBottom(block, i);
                Canvas.SetRight(block, 10);
                _yTip.Add(block);
                Y_Axis.Children.Add(block);
            }

        }
调整线和点:(主要是,缩放和拖动)

WPF 曲线图表控件(自制)(二)_第5张图片

调整线和点的大小比例AdjustLinesAndPointsSize 这个函数可以删掉,后来发现没有用。

鼠标滚轮事件:
/// 
        /// 鼠标滚轮事件
        /// 
        /// 
        protected override void OnMouseWheel(MouseWheelEventArgs e)
        {
            base.OnMouseWheel(e);
            VectorChart2 curPDraw = (VectorChart2)e.GetPosition(Draw);
            VectorChart2 curPLinesAndPoint = (VectorChart2)e.GetPosition(DrawLineAndPoint);

            double delta = 1;
            if(e.Delta >= 120)
            {
                delta = 1.2;
            }
            else if(e.Delta <= -120)
            {
                delta = (double)5 / (double)6;
            }
            if (IsXZoom)
            {
                DrawLineAndPoint.Width *= delta;
                curPDraw.Vec1 *= delta;
                curPLinesAndPoint.Vec1 *= delta;
            }
            if (IsYZoom)
            {
                DrawLineAndPoint.Height *= delta;
                curPDraw.Vec2 *= delta;
                curPLinesAndPoint.Vec2 *= delta;
            }

            _currentLinesScale.Vec4 *= delta;
            //Canvas.SetLeft(DrawLineAndPoint, curPDraw.Vec1 - curPLinesAndPoint.Vec1);
            //Canvas.SetBottom(DrawLineAndPoint, -(DrawArea.Vec2 - (curPLinesAndPoint.Vec2 - curPDraw.Vec2)));

            ///调整刻度
            AdjustLines();

            AdjustLinesAndPoints();
        }
重置按钮:

WPF 曲线图表控件(自制)(二)_第6张图片
鼠标移动的时候计算移动的差值然后调用该函数:
WPF 曲线图表控件(自制)(二)_第7张图片
获取偏移量函数:

/// 
        /// 获取偏移量
        /// 
        /// 
        protected VectorChart2 GetOffectValue(VectorChart2 offectValue)
        {
            return new VectorChart2( -offectValue.Vec1 * _everyDisForPiexl.Vec1, offectValue.Vec2 * _everyDisForPiexl.Vec2);
        }
至此全部结束 如果还有不懂的请去资源区下载控件包含所有源码

http://download.csdn.net/download/koloumi/9947692

你可能感兴趣的:(WPF控件,控件,wpf,界面,曲线控件)