GraphScan3.cs 10.4 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms.DataVisualization.Charting;

namespace FLY.ControlLibrary
{
    /// <summary>
    /// X轴显示为 mm 
    /// </summary>
    public class GraphScan3:GraphScan,IGraphBase3 
    {
        #region IGraphBase3
        /// <summary>
        /// mm/分区
        /// </summary>
        public static readonly DependencyProperty MmOfBoltProperty =
            DependencyProperty.Register("MmOfBolt", typeof(double), typeof(GraphScan3), new PropertyMetadata(
                10.0,
                new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
                {
                    (d as GraphScan3).NotifyPropertyChanged("MmOfBolt");
                })));
        /// <summary>
        /// mm/分区
        /// </summary>
        public double MmOfBolt
        {
            get
            {
                return (double)GetValue(MmOfBoltProperty);
            }
            set
            {
                if (value != MmOfBolt)
                {
                    SetValue(MmOfBoltProperty, value);
                    NotifyPropertyChanged("MmOfBolt");
                }
            }
        }


        /// <summary>
        /// X轴间隔 单位分区
        /// </summary>
        public static readonly DependencyProperty XIntervalProperty =
            DependencyProperty.Register("XInterval", typeof(int), typeof(GraphScan3), new PropertyMetadata(
                5,
                new PropertyChangedCallback(delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
                {
                    (d as GraphScan3).NotifyPropertyChanged("XInterval");
                })));
        /// <summary>
        /// X轴间隔 单位分区
        /// </summary>
        public int XInterval
        {
            get
            {
                return (int)GetValue(XIntervalProperty);
            }
            set
            {
                
                if (value != XInterval)
                {
                    SetValue(XIntervalProperty, value);
                    NotifyPropertyChanged("XInterval");
                }
            }
        }
        #endregion


        public GraphScan3():base()
        {
            PropertyChanged += GraphScan3_PropertyChanged;
        }

        private void GraphScan3_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "MmOfBolt")
            {
                mRefresh.UpdateData = true;
            }
            else if (e.PropertyName == "XInterval")
            {
                mRefresh.UpdateX = true;
            }
        }

        protected override void InitializeComponent2()
        {
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
            System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();

            // 
            // chart1
            // 
            chartArea1.Name = "Default";
            chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisX.IsMarksNextToAxis = false;
            chartArea1.AxisX.Interval = XInterval;
            chartArea1.AxisX.IsLabelAutoFit = true;
            chartArea1.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont;

            chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));

            
            //this.chart1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(78)))), ((int)(((byte)(160)))), ((int)(((byte)(34)))));
            //chartArea1.BackColor = this.chart1.BackColor;
            //chartArea1.BackColor = System.Drawing.Color.Transparent;
            //chartArea1.BackColor = System.Drawing.Color.OldLace;
            //chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            //chartArea1.BackSecondaryColor = System.Drawing.Color.White;
            chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            
            //chartArea1.Position.Height = 100;// 42F;
            //chartArea1.Position.Width = 100;// 88F;
            //chartArea1.Position.X = 0;// 3F;
            //chartArea1.Position.Y = 0;// 10F;

            //chartArea1.ShadowColor = System.Drawing.Color.Transparent;
            chart1.ChartAreas.Add(chartArea1);
            

            series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
            series1.BorderWidth = 2;

            series1.ChartArea = chartArea1.Name;
            series1.ChartType = ChartType;//System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;

            series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));

            series1.MarkerSize = 6;
            //series1.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
            series1.Name = "Series 1";
            //series1.ShadowColor = System.Drawing.Color.Black;
            //series1.ShadowOffset = 1;
            series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
            series1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;


            this.chart1.Series.Add(series1);

            chart1.Name = "chart1";


            this.chart1.BackColor = System.Drawing.Color.Transparent;

            this.chart1.PrePaint += new System.EventHandler<System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs>(Chart1_PrePaint);

        }



        protected override void UpdateOnePoint(int index)
        {
            double multi = Multi;
            int emptyvalue = EmptyValue;
            DataPoint point;
            double v;
            if (DataSource[index] == emptyvalue)
                v = double.NaN;
            else
                v = DataSource[index] / multi;

            if (index >= chart1.Series[0].Points.Count)
            {
                chart1.Series[0].Points.AddXY(
                FirstBoltNo + index, v);
                point = chart1.Series["Series 1"].Points[index];
            }
            else
            {
                point = chart1.Series["Series 1"].Points[index];

                point.YValues[0] = v;
                point.XValue = FirstBoltNo + index;
            }

            point.AxisLabel = (point.XValue * MmOfBolt).ToString("F0") + "mm";

            //设定空数据
            if (double.IsNaN(point.YValues[0]))
            {
                point.IsEmpty = true;

                System.Drawing.Color c = System.Drawing.Color.Transparent;
                point.MarkerColor = c;
                point.BorderColor = c;
                point.Color = c;
            }
            else
            {
                point.IsEmpty = false;

                System.Drawing.Color c;

                double alarm;
                double yrange;
                double target;
                bool ispercent;
                GetActualTargetAlarm(out target, out alarm, out yrange, out ispercent);

                double threshold1 = alarm * multi;
                double threshold2 = threshold1 * 2;
                target *= multi;


                if ((DataSource[index] > (target + threshold2)) || (DataSource[index] < (target - threshold2)))
                {
                    c = System.Drawing.Color.Red;
                }
                else if ((DataSource[index] > (target + threshold1)) || (DataSource[index] < (target - threshold1)))
                {
                    c = System.Drawing.Color.Orange;
                }
                else
                {
                    c = System.Drawing.Color.Green;
                }

                if (ChartType == SeriesChartType.Line)
                {
                    point.MarkerColor = c;
                    point.BorderColor = chart1.Series["Series 1"].BorderColor;
                    point.Color = chart1.Series["Series 1"].Color;
                }
                else
                {
                    point.MarkerColor = c;
                    point.BorderColor = c;
                    point.Color = c;
                }
            }
        }

        /// <summary>
        /// 与 FirstBoltNo,FirstIndex,LastIndex 相关
        /// </summary>
        public override void UpdateXRange()
        {
            int firstBoltNo = FirstBoltNo;
            int first = FirstIndex;
            int last = LastIndex;
            if (first > last)
            {
                //交换
                int swap = last;
                last = first;
                first = swap;
            }
            else if (first == last)
            {
                last = first + 10;
            }
            // Set manual minimum and maximum values.
            Axis axis = chart1.ChartAreas["Default"].AxisX;

            axis.Minimum = firstBoltNo + first;
            axis.Maximum = firstBoltNo + last;
            axis.Interval = XInterval;
            //0必须在 写着0mm

            axis.IntervalOffset = (0 - axis.Minimum) % XInterval;
        }
    }
}