Newer
Older
Correlator / PipeGallery / Model / ColumnInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace PipeGallery.Model
{
    /// <summary>
    /// 立柱图信息类
    /// </summary>
    public class ColumnInfo : ChartBase
    {
        private double _xValue;
        //时间
        public double XValue
        {
            get { return _xValue; }
            set { _xValue = value; }
        }

        private double _startValue;
        //立柱的开始值
        public double StartValue
        {
            get { return _startValue; }
            set { _startValue = value; }
        }

        private double _endValue;
        //立柱的结束值
        public double EndValue
        {
            get { return _endValue; }
            set { _endValue = value; }
        }

        private Path _columnPath =null;
        private PathFigure _columnPathFigure = new PathFigure();
        private ColumnGroupInfo _columnGroupInfo;
        private double _xStart, _xMark;
        private double _yh; //Y轴在图表中的实际高度
        private double _maxy, _miny; //y的最大,最小值


        /// <summary>
        /// 画一个柱状
        /// </summary>
        /// <param name="canvas"></param>
        internal void DrawColumn(ColumnGroupInfo columnGroupInfo, Canvas canvas, double xStart, double xMark, double yh, double maxy, double miny)
        {

            _columnGroupInfo = columnGroupInfo;
            _xStart = xStart;
            _xMark = xMark;
            _yh = yh;
            _maxy = maxy;
            _miny = miny;

            PathGeometry columnPathPathGeometry = new PathGeometry();
            columnPathPathGeometry.Figures.Add(_columnPathFigure);
            if (_columnPath == null)
            {
                _columnPath = new Path();
                _columnPath.StrokeThickness = 1;
            }
            if (_columnPath.Parent!=null)
            {
                Canvas canvasParent = _columnPath.Parent as Canvas;
                if (canvasParent!=null)
                {
                    canvasParent.Children.Remove(_columnPath);
                }
            }
            canvas.Children.Add(_columnPath);

            _columnPath.Data = columnPathPathGeometry;
            RefreshColor();
            RefreshPointCollection();

        }

        public void RefreshColor()
        {
            if (_columnGroupInfo != null)
            {
                string columnColor = _columnGroupInfo.Color;

                SolidColorBrush solidColorBrush = new SolidColorBrush();
                solidColorBrush.Color = (Color)ColorConverter.ConvertFromString(columnColor);

                _columnPath.Stroke = solidColorBrush;
                _columnPath.Fill = solidColorBrush;
            }
        }

        public void RefreshPointCollection()
        {
            double startValueHeight = _yh - (_startValue - _miny) * _yh / (_maxy - _miny);
            double endValueHeight = _yh - (_endValue - _miny) * _yh / (_maxy - _miny);

            double v1, v2;

            if (startValueHeight > endValueHeight)
            {
                v1 = startValueHeight;
                v2 = endValueHeight;
            }
            else
            {
                v1 = endValueHeight;
                v2 = startValueHeight;
            }

            PathFigure pathFigure = _columnPathFigure;
            pathFigure.Segments.Clear();

            Point p1 = new Point(_xStart - _xMark / 2 + 1, v1);
            pathFigure.StartPoint = p1;
            Point p2 = new Point(_xStart - _xMark / 2 + 1, v2);
            AddLineSegment(p2, pathFigure);
            Point p3 = new Point(_xStart + _xMark / 2 - 1, v2);
            AddLineSegment(p3, pathFigure);
            Point p4 = new Point(_xStart + _xMark / 2 - 1, v1);
            AddLineSegment(p4, pathFigure);
            Point p5 = new Point(_xStart - _xMark / 2 + 1, v1);
            AddLineSegment(p5, pathFigure);
        }

        /// <summary>
        /// 往PathFigure添加一条直线
        /// </summary>
        /// <param name="p"></param>
        public void AddLineSegment(Point p, PathFigure pathFigure)
        {
            LineSegment lineSegment = new LineSegment();
            lineSegment.Point = p;
            pathFigure.Segments.Add(lineSegment);
        }
    }
}