C# 学习笔记(五)----- 绘制曲线图坐标系

 //Initialization 初始化
        #region

        private void InitData()//初始化所有数据
        {
            count = 0;
            countCH1 = countCH2 = countCH3 = countCH4 = 0;
            label_Time_Count.Text = option.config.AppSettings.Settings["isTrigger"].Value.Equals("1") ? "触发次数" : "总时间";
            panel_mV.Visible = option.config.AppSettings.Settings["SelectY"].Value.Equals("1") ? true : false;
            panel_mA.Visible = option.config.AppSettings.Settings["SelectY"].Value.Equals("1") ? false : true;
            endTime = 3600 * Convert.ToDouble(option.config.AppSettings.Settings["Hour"].Value);
            endCount = Convert.ToDouble(option.config.AppSettings.Settings["Count"].Value);
            stopValue = limitCurrent = int.Parse(option.config.AppSettings.Settings["StopValue"].Value);
            minValue = int.Parse(option.config.AppSettings.Settings["minY"].Value);
            maxCH1Current = maxCH2Current = maxCH3Current = maxCH4Current= int.Parse(option.config.AppSettings.Settings["minY"].Value);
            minCH1Current = minCH2Current = minCH3Current = minCH4Current = int.Parse(option.config.AppSettings.Settings["maxY"].Value);
            samplingTime = int.Parse(option.config.AppSettings.Settings["Second"].Value);
            testType = GetTestType(option.config.AppSettings.Settings["SelectY"].Value, option.config.AppSettings.Settings["isTrigger"].Value, option.config.AppSettings.Settings["TriggerMode"].Value);
            onTime = Convert.ToByte(string.Format("{0:X}", int.Parse(option.config.AppSettings.Settings["ON_Time"].Value)), 16);
            offTime = Convert.ToByte(string.Format("{0:X}", int.Parse(option.config.AppSettings.Settings["OFF_Time"].Value)), 16);
            dataValue[0] = dataValue[1] = dataValue[2] = dataValue[3] = dataValue[4] = 0;
            runTimeTemp = dataValueTemp1 = dataValueTemp2 = dataValueTemp3 = dataValueTemp4 = 0;
            columnCh1 = columnCh2 = columnCh3 = columnCh4 = "null";
            //清空所有事件
            SetValue = null;
            PointArrived = null;
            StopPoint = null;
            StartValue = null;
            EndValue = null;
            CaptureEvent = null;
            ReachStopValue = null;
            MaxValue = null;
            MinValue = null;

            if (graphics != null)
                graphics.Dispose();
            if (gCH1 != null)
                gCH1.Dispose();
            if (gCH2 != null)
                gCH2.Dispose();
            if (gCH3 != null)
                gCH3.Dispose();
            if (gCH4 != null)
                gCH4.Dispose();
            if (image != null)
                image.Dispose();
            if (imgCH1 != null)
                imgCH1.Dispose();
            if (imgCH2 != null)
                imgCH2.Dispose();
            if (imgCH3 != null)
                imgCH3.Dispose();
            if (imgCH4 != null)
                imgCH4.Dispose();
    
            fileName = "";
            productStr = "";
            tbxStartTime.Text = tbxEndTime.Text = tbxTotalTime.Text = tbxMouseX.Text = tbxMouseY.Text = "";
            tbxCH1Model.Text = tbxStartValue1.Text = tbxEndValue1.Text = tbxCurrentValue1.Text = tbxMAXValue1.Text = tbxMINValue1.Text = tbxCH1OutLimit. Text ="";
            tbxCH2Model.Text = tbxStartValue2.Text = tbxEndValue2.Text = tbxCurrentValue2.Text = tbxMAXValue2.Text = tbxMINValue2.Text = tbxCH2OutLimit.Text = "";
            tbxCH3Model.Text = tbxStartValue3.Text = tbxEndValue3.Text = tbxCurrentValue3.Text = tbxMAXValue3.Text = tbxMINValue3.Text = tbxCH3OutLimit.Text = "";
            tbxCH4Model.Text = tbxStartValue4.Text = tbxEndValue4.Text = tbxCurrentValue4.Text = tbxMAXValue4.Text = tbxMINValue4.Text = tbxCH4OutLimit.Text = "";
            tbxCH1EndTime.Text = tbxCH2EndTime.Text = tbxCH3EndTime.Text = tbxCH4EndTime.Text = "";
            labelCHEnd.Text = "达到 " + option.config.AppSettings.Settings["StopValue"].Value+" "
                + (option.config.AppSettings.Settings["SelectY"].Value == "1" ? "mV" : (option.config.AppSettings.Settings["SelectY"].Value == "2" ? "mA" : "uA"))
                + " 用时";
            labelLimitCurrent.Text = "超过 " + option.config.AppSettings.Settings["StopValue"].Value + " "
                + (option.config.AppSettings.Settings["SelectY"].Value == "1" ? "mV" : (option.config.AppSettings.Settings["SelectY"].Value == "2" ? "mA" : "uA"))
                + " 次数";
            CaptureEvent += SnipAll; //订阅截图

            if (option.config.AppSettings.Settings["CH1"].Value == "1")
            {
                tbxCH1Model.Text = option.config.AppSettings.Settings["CH1Model"].Value;
                columnCh1 = option.config.AppSettings.Settings["CH1Model"].Value;
                productStr += "  CH1:" + option.config.AppSettings.Settings["CH1Model"].Value;
                fileName += option.config.AppSettings.Settings["CH1Model"].Value + "_";
                StartValue += CH1_StartValue;
                SetValue += CH1_SetValue;
                PointArrived += CH1_PointArrived;
                CaptureEvent += SnipCH1;
                if (testType == TestType.mV)
                {
                    StopPoint += CH1_StopPoint;
                    ReachStopValue += CH1_ReachStopValue;

                }
                else
                {
                    
                    EndValue += CH1_EndValue;
                    ReachStopValue += CH1_OverLimitValue;
                    MaxValue += CH1_maxValue;
                    MinValue += CH1_minValue;
                }
            }
            if (option.config.AppSettings.Settings["CH2"].Value == "1")
            {
                tbxCH2Model.Text = option.config.AppSettings.Settings["CH2Model"].Value;
                columnCh2 = option.config.AppSettings.Settings["CH2Model"].Value;
                productStr += "  CH2:" + option.config.AppSettings.Settings["CH2Model"].Value;
                fileName += option.config.AppSettings.Settings["CH2Model"].Value + "_";
                StartValue += CH2_StartValue;
                SetValue += CH2_SetValue;
                PointArrived += CH2_PointArrived;
                CaptureEvent += SnipCH2;
                if (testType == TestType.mV)
                {
                    StopPoint += CH2_StopPoint;
                    ReachStopValue += CH2_ReachStopValue;
                }
                else
                {
                    
                    EndValue += CH2_EndValue;
                    ReachStopValue += CH2_OverLimitValue;
                    MaxValue += CH2_maxValue;
                    MinValue += CH2_minValue;
                }
            }
            if (option.config.AppSettings.Settings["CH3"].Value == "1")
            {
                tbxCH3Model.Text = option.config.AppSettings.Settings["CH3Model"].Value;
                columnCh3 = option.config.AppSettings.Settings["CH3Model"].Value;
                productStr += "  CH3:" + option.config.AppSettings.Settings["CH3Model"].Value;
                fileName += option.config.AppSettings.Settings["CH3Model"].Value + "_";
                StartValue += CH3_StartValue;
                SetValue += CH3_SetValue;
                PointArrived += CH3_PointArrived;
                CaptureEvent += SnipCH3;
                if (testType == TestType.mV)
                {
                    StopPoint += CH3_StopPoint;
                    ReachStopValue += CH3_ReachStopValue;
                }
                else
                {
                   
                    EndValue += CH3_EndValue;
                    ReachStopValue += CH3_OverLimitValue;
                    MaxValue += CH3_maxValue;
                    MinValue += CH3_minValue;
                }
            }
            if (option.config.AppSettings.Settings["CH4"].Value == "1")
            {
                tbxCH4Model.Text = option.config.AppSettings.Settings["CH4Model"].Value;
                columnCh4 = option.config.AppSettings.Settings["CH4Model"].Value;
                productStr += "  CH4:" + option.config.AppSettings.Settings["CH4Model"].Value;
                fileName += option.config.AppSettings.Settings["CH4Model"].Value + "_";
                StartValue += CH4_StartValue;
                SetValue += CH4_SetValue;
                PointArrived += CH4_PointArrived;
                CaptureEvent += SnipCH4;
                if (testType == TestType.mV)
                {
                    StopPoint += CH4_StopPoint;
                    ReachStopValue += CH4_ReachStopValue;
                }
                else
                {
                    
                    EndValue += CH4_EndValue;
                    ReachStopValue += CH4_OverLimitValue;
                    MaxValue += CH4_maxValue;
                    MinValue += CH4_minValue;
                }
            }
           // InitDataTable();
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;//最大化窗体以作图表
            //InitChart(pictureBox.Width, pictureBox.Height);
            imgWidth = pictureBox.Width;
            imgHeight = pictureBox.Height;
            panelWidth = panelBottom.Width;
            panelHeight = panelBottom.Height;
            image = new Bitmap(imgWidth, imgHeight);
            imgCH1 = new Bitmap(imgWidth, imgHeight);
            imgCH2 = new Bitmap(imgWidth, imgHeight);
            imgCH3 = new Bitmap(imgWidth, imgHeight);
            imgCH4 = new Bitmap(imgWidth, imgHeight);
            graphics = Graphics.FromImage(image);
            gCH1 = Graphics.FromImage(imgCH1);
            gCH2 = Graphics.FromImage(imgCH2);
            gCH3 = Graphics.FromImage(imgCH3);
            gCH4 = Graphics.FromImage(imgCH4);
            InitChart(graphics);
            InitChart(gCH1);
            InitChart(gCH2);
            InitChart(gCH3);
            InitChart(gCH4);
            DrawLegends();
            pictureBox.BackgroundImage = image;
            pictureBox.Refresh();

            isInit = true;
        }
        private void InitChart(Graphics g)//初始化表格
        {

            float yLength;
            float xLength;
            
            //int pointXOffset = 1;
            //int pointYOffset = 1;
            //

           // imgWidth = width;
          //  imgHeight = height;

            // Graphics g = e.Graphics;//--------------------------------------------------------------
           // Bitmap img = new Bitmap(imgWidth, imgHeight);


           // Graphics g = Graphics.FromImage(img);
            g.SmoothingMode = SmoothingMode.HighQuality;//抗锯齿
            g.FillRectangle(Brushes.LightBlue, 0, 0, imgWidth, imgHeight);
            

            //Border
            // g.DrawLine(borderLineTopLeft, 0, 0, imgWidth, 0);
            //  g.DrawLine(borderLineTopLeft, 0, 0, 0, imgHeight);
            // g.DrawLine(borderLineBottomRight, 0, imgHeight, imgWidth, imgHeight);
            // g.DrawLine(borderLineBottomRight, imgWidth, 0, imgWidth, imgHeight);
            //轴
            yLength = imgHeight - blankWidthBottom - blankWidthTop - Top;
            xLength = imgWidth - blankWidthLeft - blankWidthRight;

            //Y轴
            Point yPoint1 = new Point(blankWidthLeft + pointXYOffset, Top + pointXYOffset);
            Point yPoint2 = new Point(blankWidthLeft + pointXYOffset, imgHeight - blankWidthBottom + pointXYOffset);
            g.DrawLine(yAxiesLine, yPoint1, yPoint2);//Y-Axies
            g.DrawLine(yAxiesLine, blankWidthLeft + pointXYOffset, Top + pointXYOffset, blankWidthLeft - 5 + pointXYOffset, 10 + Top + pointXYOffset);//Y轴箭头
            g.DrawLine(yAxiesLine, blankWidthLeft + pointXYOffset, Top + pointXYOffset, blankWidthLeft + 5 + pointXYOffset, 10 + Top + pointXYOffset);//Y轴箭头
            //yLength = imgHeight - blankWidth - 50;
            yMajorGrid.DashStyle = DashStyle.Dot;
          //  string[] yLabel = new string[10];
            string[] yLabel = GetYlabel(Convert.ToDouble(option.config.AppSettings.Settings["maxY"].Value), Convert.ToDouble(option.config.AppSettings.Settings["minY"].Value), 10);
          //  if(option.config.AppSettings.Settings["SelectY"].Value == "1")
         //       yLabel = GetYlabelForV(Convert.ToDouble(option.config.AppSettings.Settings["maxY"].Value), Convert.ToDouble(option.config.AppSettings.Settings["minY"].Value), 10);
         //   else
          //      yLabel = GetYlabel(Convert.ToDouble(option.config.AppSettings.Settings["maxY"].Value), Convert.ToDouble(option.config.AppSettings.Settings["minY"].Value), 10);

            for (int i = 0; i <= 10; i++)
            {
                //Y轴 MajorTickMark
                g.DrawLine(yMajorTickMark, blankWidthLeft - 10 + pointXYOffset, imgHeight - blankWidthBottom - (float)yLength / 10 * i + pointXYOffset, blankWidthLeft + pointXYOffset, imgHeight - blankWidthBottom - (float)yLength / 10 * i + pointXYOffset);
                if (i % 2 == 0)
                    //Y轴 MajorGrid
                    g.DrawLine(yMajorGrid, blankWidthLeft + pointXYOffset, imgHeight - blankWidthBottom - (float)yLength / 10 * i + pointXYOffset, blankWidthLeft + xLength + pointXYOffset, imgHeight - blankWidthBottom - (float)yLength / 10 * i + pointXYOffset);
                g.DrawString(string.Format("{0,5}", yLabel[i]), labelFont, fontBrush, blankWidthLeft - 55 + pointXYOffset, imgHeight - blankWidthBottom - (float)yLength / 10 * i - 8 + pointXYOffset);
            }
            for (int i = 0; i <= 100; i++)
            {
                //Y轴 MinorTickMark
                g.DrawLine(yMinorTickMark, blankWidthLeft - 6 + pointXYOffset, imgHeight - blankWidthBottom - (float)yLength / 100 * i + pointXYOffset, blankWidthLeft + pointXYOffset, imgHeight - blankWidthBottom - (float)yLength / 100 * i + pointXYOffset);
            }   
            yInterval = ((float)yLength) / (int.Parse(option.config.AppSettings.Settings["maxY"].Value) - int.Parse(option.config.AppSettings.Settings["minY"].Value));
            string yName = option.config.AppSettings.Settings["SelectY"].Value == "1" ? "电压:mV" : (option.config.AppSettings.Settings["SelectY"].Value == "2" ? "电流:mA" : "电流:uA");
            g.DrawString(yName, labelNameFont, fontBrush, blankWidthLeft + 20 + pointXYOffset, 5 + Top + pointXYOffset);

            //截止线(电压), 限定线(电流)
            yEndGrid.DashStyle = DashStyle.Dot;
            g.DrawLine(yEndGrid, blankWidthLeft + pointXYOffset, imgHeight - blankWidthBottom - (stopValue - minValue) * yInterval + pointXYOffset, blankWidthLeft + xLength + pointXYOffset, imgHeight - blankWidthBottom - (stopValue - minValue) * yInterval + pointXYOffset);
            g.DrawString(option.config.AppSettings.Settings["StopValue"].Value + (option.config.AppSettings.Settings["SelectY"].Value == "1" ? " mV" : (option.config.AppSettings.Settings["SelectY"].Value == "2" ? " mA" : " uA"))
                , labelendValueFont, endValueBrush, imgWidth - blankWidthRight + 5 + pointXYOffset, imgHeight - blankWidthBottom - (stopValue - minValue) * yInterval - 6 + pointXYOffset);
            
            //X轴
            Point xPoint1 = new Point(blankWidthLeft + pointXYOffset, imgHeight - blankWidthBottom + pointXYOffset);
            Point xPoint2 = new Point(imgWidth -blankWidthRight + 10, imgHeight - blankWidthBottom + pointXYOffset);
            g.DrawLine(xAxiesLine, xPoint1, xPoint2);
            xMajorGrid.DashStyle = DashStyle.Dot;
            // xLength = imgWidth - 2 * blankWidth - 50;
            if (testType == TestType.mA_Trigger || testType == TestType.uA_Trigger
                || testType == TestType.mA_Trigger_Software || testType == TestType.uA_Trigger_Software)//触发模式
            {
                string[] xLabel = GetXLabel(Convert.ToDouble(option.config.AppSettings.Settings["Count"].Value), 10);
                for (int j = 0; j <= 10; j++)
                {
                    g.DrawLine(xMajorTickMark, blankWidthLeft + (float)xLength / 10 * j + pointXYOffset, imgHeight - blankWidthBottom + pointXYOffset, blankWidthLeft + (float)xLength / 10 * j + pointXYOffset, imgHeight - blankWidthBottom + 10 + pointXYOffset);
                    g.DrawLine(xMajorGrid, blankWidthLeft + (float)xLength / 10 * j + pointXYOffset, imgHeight - blankWidthBottom + pointXYOffset, blankWidthLeft + (float)xLength / 10 * j + pointXYOffset, imgHeight - blankWidthBottom - yLength + pointXYOffset);
                    g.DrawString(xLabel[j], labelFont, fontBrush, blankWidthLeft + (float)xLength / 10 * j - 12 + pointXYOffset, imgHeight - blankWidthBottom + 15 + pointXYOffset);
                }
                g.DrawString("触发次数", labelNameFont, fontBrush, blankWidthLeft + xLength / 2 + pointXYOffset - 28, imgHeight - blankWidthBottom + 40 + pointXYOffset);
                xInterval = ((float)xLength) / ((float)Convert.ToDouble(option.config.AppSettings.Settings["Count"].Value));
            }
            else
            {
                string[] xLabel = GetXLabel_Time(Convert.ToDouble(option.config.AppSettings.Settings["Hour"].Value), 12);
                for (int j = 0; j <= 12; j++)
                {
                    g.DrawLine(xMajorTickMark, blankWidthLeft + (float)xLength / 12 * j + pointXYOffset, imgHeight - blankWidthBottom + pointXYOffset, blankWidthLeft + (float)xLength / 12 * j + pointXYOffset, imgHeight - blankWidthBottom + 10 + pointXYOffset);
                    g.DrawLine(xMajorGrid, blankWidthLeft + (float)xLength / 12 * j + pointXYOffset, imgHeight - blankWidthBottom + pointXYOffset, blankWidthLeft + (float)xLength / 12 * j + pointXYOffset, imgHeight - blankWidthBottom - yLength + pointXYOffset);
                    g.DrawString(xLabel[j], labelFont, fontBrush, blankWidthLeft + (float)xLength / 12 * j - 30 + pointXYOffset, imgHeight - blankWidthBottom + 15 + pointXYOffset);
                }
                g.DrawString("时间", labelNameFont, fontBrush, blankWidthLeft + xLength / 2 + pointXYOffset - 15, imgHeight - blankWidthBottom + 40 + pointXYOffset);
                xInterval = ((float)xLength) / ((float)Convert.ToDouble(option.config.AppSettings.Settings["Hour"].Value) * 3600);
            }
           // g.FillEllipse(Brushes.Red, 100 + (float)600 * xInterval, imgHeight - 100 - (float)6000 * yInterval, 20, 20);//for check 点偏移量
          // DrawLegends();
          // pictureBox.BackgroundImage = img;
           //return img;
           //g.Dispose();

        }
        private void DrawLegends()//自定义表格Legends
        {
            int middle = (imgWidth - blankWidthLeft - 50) / 2;
            if (option.config.AppSettings.Settings["CH1"].Value == "1")
            {
                ch1Brush = new SolidBrush(GetColor(option.config.AppSettings.Settings["CH1Color"].Value));
                graphics.FillEllipse(ch1Brush, blankWidthLeft + middle - 150 - 15, Top + 2, 8, 8);
                graphics.DrawString(option.config.AppSettings.Settings["CH1Model"].Value, labelNameFont, Brushes.Black, blankWidthLeft + middle - 150, Top);
                gCH1.FillEllipse(ch1Brush, blankWidthLeft + middle - 15, Top + 2, 8, 8);
                gCH1.DrawString(option.config.AppSettings.Settings["CH1Model"].Value, labelNameFont, Brushes.Black, blankWidthLeft + middle, Top);
            }
            if (option.config.AppSettings.Settings["CH2"].Value == "1")
            {
                ch2Brush = new SolidBrush(GetColor(option.config.AppSettings.Settings["CH2Color"].Value));
                graphics.FillEllipse(ch2Brush, blankWidthLeft + middle - 50 - 15, Top + 2, 8, 8);
                graphics.DrawString(option.config.AppSettings.Settings["CH2Model"].Value, labelNameFont, Brushes.Black, blankWidthLeft + middle - 50, Top);
                gCH2.FillEllipse(ch2Brush, blankWidthLeft + middle - 15, Top + 2, 8, 8);
                gCH2.DrawString(option.config.AppSettings.Settings["CH2Model"].Value, labelNameFont, Brushes.Black, blankWidthLeft + middle, Top);
                
            }
            if (option.config.AppSettings.Settings["CH3"].Value == "1")
            {
                ch3Brush = new SolidBrush(GetColor(option.config.AppSettings.Settings["CH3Color"].Value));
                graphics.FillEllipse(ch3Brush, blankWidthLeft + middle + 50 - 15, Top + 2, 8, 8);
                graphics.DrawString(option.config.AppSettings.Settings["CH3Model"].Value, labelNameFont, Brushes.Black, blankWidthLeft + middle + 50, Top);
                gCH3.FillEllipse(ch3Brush, blankWidthLeft + middle - 15, Top + 2, 8, 8);
                gCH3.DrawString(option.config.AppSettings.Settings["CH3Model"].Value, labelNameFont, Brushes.Black, blankWidthLeft + middle, Top);
           
            }
            if (option.config.AppSettings.Settings["CH4"].Value == "1")
            {
                ch4Brush = new SolidBrush(GetColor(option.config.AppSettings.Settings["CH4Color"].Value));
                graphics.FillEllipse(ch4Brush, blankWidthLeft + middle + 150 - 15, Top + 2, 8, 8);
                graphics.DrawString(option.config.AppSettings.Settings["CH4Model"].Value, labelNameFont, Brushes.Black, blankWidthLeft + middle + 150, Top);
                gCH4.FillEllipse(ch4Brush, blankWidthLeft + middle - 15, Top + 2, 8, 8);
                gCH4.DrawString(option.config.AppSettings.Settings["CH4Model"].Value, labelNameFont, Brushes.Black, blankWidthLeft + middle, Top);
            }
        }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值