using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using System.Diagnostics;

namespace GcDevicePc.CK_UI
{
    public partial class UserCtl : UserControl
    {
        private MasterPane mster;
        ColorSymbolRotator rotator;
        PointPairList listNmoc = new PointPairList();
        PointPairList listTHC = new PointPairList();
        PointPairList listCH4 = new PointPairList();
        GraphPane myPaneNmoc ;
        GraphPane myPaneThc ;
        GraphPane myPaneCh4 ;
        public UserCtl()
        {
            InitializeComponent();
        }

        private void UserCtl_Load(object sender, EventArgs e)
        {
            InitZedgrap(zedGraphControl1);
            data();
            datadisp();
           
        }
        private void InitZedgrap(ZedGraphControl zedgraph)
        {
            try
            {
                mster = zedgraph.MasterPane;
                mster.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45.0f);
                mster.PaneList.Clear();
                mster.Title.IsVisible = true;
                mster.Title.Text = "12小时非甲烷总烃趋势图";
                mster.Margin.All = 10;
                mster.InnerPaneGap = 0;
                //zedgraph.GraphPane.XAxis.CrossAuto = true;
                //zedgraph.GraphPane.YAxis.CrossAuto = true;
                 rotator = new ColorSymbolRotator();
                 myPaneNmoc = new GraphPane(new Rectangle(40, 40, 600, 400),
                   "",
                   "",
                   "");
                 myPaneThc = new GraphPane(new Rectangle(40, 40, 600, 400),
                   "",
                   "",
                   "");
                 myPaneCh4 = new GraphPane(new Rectangle(40, 40, 600, 400),
                   "",
                   "",
                   "");
                
                 setpane(myPaneNmoc, "NMOC");
                 setpane(myPaneThc, "THC");
                 setpane(myPaneCh4, "CH4");

                 using (Graphics g = zedgraph.CreateGraphics())
                 {
                     ZedGraphControl z1 = zedgraph;

                     mster.SetLayout(g, PaneLayout.SingleColumn);
                     z1.AxisChange();

                     //z1.IsAutoScrollRange = true;
                     //z1.IsShowHScrollBar = true;
                     //z1.IsShowVScrollBar = true;
                     //z1.IsSynchronizeXAxes = true;

                 }

               
                 zedgraph.AxisChange();

            }
            catch(Exception ex)
            {
                Trace.Write(ex);
            }
        }
        private void setpane(GraphPane myPaneT, string text)
        {
            try
            {
              
                
                myPaneT.Fill.IsVisible = false;
                myPaneT.YAxis.Title.Text = text;
                myPaneT.Chart.Fill = new Fill(Color.White, Color.LightYellow, 45.0F);
                myPaneT.BaseDimension = 3.0F;
                myPaneT.XAxis.Title.IsVisible = false;
                myPaneT.XAxis.Scale.IsVisible = false;
                myPaneT.Legend.IsVisible = false;
                myPaneT.Border.IsVisible = false;
                myPaneT.Title.IsVisible = false;
                myPaneT.XAxis.MajorTic.IsOutside = false;
                myPaneT.XAxis.MinorTic.IsOutside = false;
                myPaneT.XAxis.MajorGrid.IsVisible = true;
                myPaneT.XAxis.MinorGrid.IsVisible = true;
                myPaneT.XAxis.Type = AxisType.Date;
               // myPaneT.Margin.All = 0;
               
                   // myPaneT.Margin.Top = 20;
              
                    myPaneT.YAxis.Scale.IsSkipLastLabel = true;
                    LineItem myCurve = myPaneT.AddCurve("",
                      null, rotator.NextColor, SymbolType.None);
                    myCurve.Symbol.Fill = new Fill(Color.White);
             
                //myPaneT.YAxis.MinSpace = 60;
                //myPaneT.Y2Axis.MinSpace = 20;
                mster.Add(myPaneT);
              
               

            }
            catch(Exception ex)
            {
                Trace.Write(ex);
            }
        }
        private void datadisp()
        {
            try
            {
                if (myPaneNmoc == null || myPaneCh4 == null || myPaneThc == null|| listNmoc.Count<1||listCH4.Count<1||listTHC.Count<1)
                {
                    return;
                }
                textBox1.Text =Math.Round(listNmoc[listNmoc.Count - 1].Y,2).ToString() + "ppm";
                textBox2.Text =Math.Round(listTHC[listTHC.Count - 1].Y,2).ToString() + "ppm";
                textBox3.Text =Math.Round(listCH4[listCH4.Count - 1].Y,2).ToString() + "ppm";
                mster.PaneList.Clear();
                myPaneCh4.CurveList.Clear();
                myPaneNmoc.CurveList.Clear();
                myPaneThc.CurveList.Clear();
                
                //DateTime dt = DateTime.Now;
                //double minx = (double)new XDate(dt.AddHours(-5));
                //double maxx = (double)new XDate(dt);
                //myPaneNmoc.XAxis.Scale.Min = listNmoc[0].X-100;
                //myPaneNmoc.XAxis.Scale.Max = listNmoc[listNmoc.Count - 1].X+1000;
                //myPaneNmoc.XAxis.Scale.Min = minx;
                //myPaneNmoc.XAxis.Scale.Max = maxx;
                myPaneNmoc.XAxis.CrossAuto = true;
                myPaneNmoc.YAxis.CrossAuto = true;
                LineItem myCurve = myPaneNmoc.AddCurve("",
                        listNmoc, rotator.NextColor, SymbolType.None);
                myCurve.Symbol.Fill = new Fill(Color.White);
                mster.Add(myPaneNmoc);
               // myPaneThc.XAxis.Scale.Min = minx;
               // myPaneThc.XAxis.Scale.Max = maxx;
                myPaneThc.XAxis.CrossAuto = true;
                myPaneThc.YAxis.CrossAuto = true;
                LineItem myCurve1 = myPaneThc.AddCurve("",
                       listTHC, rotator.NextColor, SymbolType.None);
                myCurve1.Symbol.Fill = new Fill(Color.White);
                mster.Add(myPaneThc);
               // myPaneCh4.XAxis.Scale.Min = minx;
               // myPaneCh4.XAxis.Scale.Max = maxx;
                myPaneCh4.XAxis.CrossAuto = true;
                myPaneCh4.YAxis.CrossAuto = true;
                LineItem myCurve2 = myPaneCh4.AddCurve("",
                       listCH4, rotator.NextColor, SymbolType.None);
                myCurve2.Symbol.Fill = new Fill(Color.White);
                mster.Add(myPaneCh4);
                zedGraphControl1.AxisChange();
                zedGraphControl1.Refresh();
               
            }
            catch(Exception ex)
            {
                Trace.Write(ex);
            }
          //  zedgraph.AxisChange();
        }
        private void data()
        {
            try
            {
                //double x, y;
                ////int j = 0;
                ////if (listNmoc.Count > 0)
                ////{
                ////    j = (int)listNmoc[listNmoc.Count - 1].X;
                ////}
                //for (int i = 0; i < 36 ; i++)
                //{
                //    x = (double)new XDate(DateTime.Now.AddSeconds(2));
                //    y = 3.0 * (1.5 + Math.Sin((double)i * 0.2));
                //    listNmoc.Add(x, y);
                //    listCH4.Add(x, y);
                //    listTHC.Add(x, y);
                //}

                SQLiteOperate.VOCSqlite sql = new SQLiteOperate.VOCSqlite(CKVocAnalyzer.GlobalCKV.DataBasePath);
                sql.open();
                DateTime dt = DateTime.Now;
                string start=dt.AddHours(-12).ToString("yyyy-MM-dd HH:mm:ss");
                string end=dt.ToString("yyyy-MM-dd HH:mm:ss");
                sql.selectTimes(start,end );
                sql.close();
                if(sql.ReturnData==null)
                {
                    return;
                }
                listNmoc.Clear();
                listCH4.Clear();
                listTHC.Clear();
                for (int i = 0; i < sql.ReturnData.Count; ++i)
                {
                    if(sql.ReturnData[i].name=="NMOC")
                    {
                        double times = (double)new XDate(Convert.ToDateTime(sql.ReturnData[i].Time));
                        double v = sql.ReturnData[i].value;
                        listNmoc.Add(times,v);
                    }
                    if (sql.ReturnData[i].name == "THC")
                    {
                        double times1 = (double)new XDate(Convert.ToDateTime(sql.ReturnData[i].Time));
                        double v1 = sql.ReturnData[i].value;
                        listTHC.Add(times1, v1);
                    }
                    if (sql.ReturnData[i].name == "CH4")
                    {
                        double times2 = (double)new XDate(Convert.ToDateTime(sql.ReturnData[i].Time));
                        double v2 = sql.ReturnData[i].value;
                        listCH4.Add(times2, v2);
                    }

                }
               
               
            }catch(Exception ex)
            {
                Trace.Write(ex);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
           
            data();
            datadisp();
        }
    }
}
