柜位预测(一)――显示柜位曲线图(一)

2014-11-23 17:39:53 · 作者: · 浏览: 34

最近自己接触到的知识有些杂乱。最近涉及到了PLC的自动化控制、煤气柜位预测等方面的内容。PLC的学习目前中断了一阵子,最近接触了柜位预测的相关算法――神经网络、最小二乘法等相关的算法。今天先简单介绍一下,自己做的一个煤气柜位曲线显示的小demo,相关的算法的实现将在以后学习研究介绍吧。

1、界面曲线显示

我选择采用C#来进行实时曲线的显示,C#报表控件有很多,经过比较,在实时显示曲线上C#的开源库zedgraph控件是性能最好的,下面是我的实时曲线显示的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using IGAI.Util.Excel;

namespace ZedGraphExcelTest
{
    public partial class Form1 : Form
    {
        ExcelRead read;
        int count = 0;
        public Form1()
        {
            InitializeComponent();
            this.timeDraw.Tick += new EventHandler(timeDraw_Tick);
            read = new ExcelRead();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //获取引用
            GraphPane myPane = zgc.GraphPane;
            //设置标题
            myPane.Title.Text = "实时曲线";
            //设置X轴说明文字
            myPane.XAxis.Title.Text = "时间";
            //设置Y轴说明文字
            myPane.YAxis.Title.Text = "柜位";

            RollingPointPairList list = new RollingPointPairList(86400);

            //开始,增加的线是没有数据点的(也就是list为空)
            //增加一条名称:Voltage,颜色Color.Red,无符号,无数据的空线条
            LineItem curve = myPane.AddCurve("柜位", list, Color.Red, SymbolType.None);

            timeDraw.Interval = 50;     //设置timer控件的间隔为50毫秒
            timeDraw.Enabled = true;    //timer可用
            timeDraw.Start();           

            DateTime t1 = new DateTime(2014, 06, 01);
            DateTime t2 = new DateTime(2014, 06, 02);
            double min = new XDate(t1);
            double max = new XDate(t2);
            myPane.XAxis.Scale.Min = min;
            myPane.XAxis.Scale.Max = max;
            myPane.XAxis.Scale.MinorStep = (max - min) / 24;  //小刻度是1小时
            myPane.XAxis.Scale.MajorStep = (max - min) / 6;   //大刻度是4小时
            myPane.XAxis.Type = AxisType.Date;
            myPane.XAxis.Scale.Format = "yyyy-MM-dd\nHH:mm:ss";

            //改变轴的刻度
            zgc.AxisChange();
            read.initExcelDrive("test.xls", "30万气柜");

        }

        private void timeDraw_Tick(object sender, EventArgs e)
        {
            //确保CurveList不为空
            if (zgc.GraphPane.CurveList.Count <= 0)
            {
                return;
            }

            //取Graph第一个曲线,也就是第一步:在GraphPane.CurveList集合中查找CurveItem
            LineItem curve = zgc.GraphPane.CurveList[0] as LineItem;
            if (curve == null)
            {
                return;
            }

            //第二步:在CurveItem中访问PointPairList(或者其它的IPointList),根据自己的需要增加新数据或修改已存在的数据
            IPointListEdit list = curve.Points as IPointListEdit;

            if (list == null)
            {
                return;
            }

            DateTime time = new DateTime();
            Double value = -1;
           
            if (read.isHasData())
            {
                time = read.getExcelDataDateTime(0);
                value = read.getExcelData(1);
            }

            XDate max= new XDate(time);
            XDate min = new XDate(time.AddDays(-1));
            Scale xScale = zgc.GraphPane.XAxis.Scale;
            if (max>= xScale.Max)
            {
                xScale.Max = xScale.Max + xScale.MajorStep;
                xScale.Min = xScale.Min + xScale.MajorStep;
            }
            list.Add(max, value);

            if (value != -1)
            {
                //第三步:调用ZedGraphControl.AxisChange()方法更新X和Y轴的范围
                zgc.AxisChange();

                //第四步:调用Form.Invalidate()方法更新图表
                zgc.Invalidate();
            }
            
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            SetSize();
        }

        private void SetSize()
        {
            // 控制始终是以10像素插入矩形从客户端的形
            Rectangle formRect = this.ClientRectangle;
            formRect.Inflate(-10, -10);

            if (zgc.Size != formRect.Size)
            {
                zgc.Location = formRect.Location;
                zgc.Size = formRect.Size;
            }
        }
    }
}

2.读取Excel数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace IGAI.Util.Excel
{
    public class ExcelRead
    {
        private OleDbDataReader dataReader;
        private OleDbConnection oleDbConnection;
        public ExcelRead()
        {

        }
        ~ExcelRead()
        {
            clearExcelDrive();
        }

        public bool initExcelDrive(string excelFilePath,string sheetName)
        {
            string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + excelFilePath + ";Extended Properties=Excel 8.0;"; 
            dataReader = null;
            oleDbConnection = new OleDbConnection(strConn);
            OleDbCommand myOleDbCommand = new OleDbCommand("SELECT * FROM ["+sheetName+"$]", oleDbConnection);
            try
            {
                oleDbConnection.Open();
                dataReader = myOleDbCommand.ExecuteReader();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }

        p