示例#1
0
 public void DrawSummaryLabel(SeriesGrid grid, string strSummary)
 {
     float summaryX = _ChartAreaStartX + _ChartWitdh + _SeriesLegendGap + 30;
     float summaryY = _BarStartY + grid.SeriesIndex * _SeriesGap;
     _ChartGraphics.DrawString(strSummary, grid.LabelFont, new SolidBrush(grid.LabelColor), summaryX, summaryY);
 }
示例#2
0
 public void DrawSeriersGrid(SeriesGrid grid)
 {
     float LabelX = _ChartAreaStartX - grid.LabelWidth;
     float LabelY = _BarStartY + grid.SeriesIndex * _SeriesGap;
     _ChartGraphics.DrawString(grid.LabelText, grid.LabelFont, new SolidBrush(grid.LabelColor), LabelX, LabelY);
 }
示例#3
0
        //Generate Line Downtime  Time Span Chart
        public void BtnDrawLineTimeSpanChart_Click(object sender, EventArgs e)
        {
            #region  validate table
            LCADownTimeDataDriver dataDriver = LCADownTimeDataDriver.Instance;
            if (!dataDriver.LCADataSet.Tables.Contains("LCALineDownTime") || dataDriver.LCADataSet.Tables["LCALineDownTime"].Rows.Count < 1)
            {
                MessageBox.Show("Please load Souce Data Firstly");
                return;
            }
            dataDriver.WriteTableToList("LCALineDownTime", typeof(LCALineState).FullName);
            List<object> dataList = dataDriver.LCADataList;
            if (dataList == null || dataList.Count == 0)
            {
                MessageBox.Show("Please Load Data and LCA line State Report First");
                return;
            }
            #endregion
            #region  initial Draw Chart setting
            TimeSpanBarChartDraw ChartDraw = TimeSpanBarChartDraw.Instance;
            ChartDraw.IniGraphics();
            ChartDraw.ChartAreaStartX = 80;
            ChartDraw.ChartAreaStartY = 80;
            ChartDraw.BarStartX = 100;
            ChartDraw.BarStartX = 100;
            #endregion
            #region Draw Series Legends
            SeriesLegend[] SeriesLegends = new SeriesLegend[LCADrawInfo.LineState_Colors().Count];
            int SereiesIndex = 0;
            foreach (KeyValuePair<String, Color> lineStateColor in LCADrawInfo.LineState_Colors())
            {
                SeriesLegends[SereiesIndex] = new SeriesLegend(SereiesIndex, lineStateColor.Key.ToString(), lineStateColor.Value);
                SereiesIndex++;
            }
            ChartDraw.DrawSeriesLengends(SeriesLegends);
            #endregion
            DataTable dtNew = new DataTable("LCALineHourlyDownTime");
            LCADownTimeRule rule = LCADownTimeRule.Instance;
            #region station column&hour row add
            dtNew.Columns.Add(LCAMachineStateToPeriod.HEADERCHAR.Period.ToString());
            foreach (LCABaseInfo.LINE_STATE lineState in (LCABaseInfo.LINE_STATE[])Enum.GetValues(typeof(LCABaseInfo.LINE_STATE)))
            {
                dtNew.Columns.Add(lineState.ToString());
            }
            for (int i = 0; i < totalHourSpan + 1; i++)
            {
                DataRow newRow = dtNew.NewRow();
                newRow[LCAMachineStateToPeriod.HEADERCHAR.Period.ToString()] = minDateTime.AddHours(i).ToString("yyyy-MM-dd-HH");
                foreach (LCABaseInfo.LINE_STATE lineState in (LCABaseInfo.LINE_STATE[])Enum.GetValues(typeof(LCABaseInfo.LINE_STATE)))
                {
                    newRow[lineState.ToString()] = 0;
                }
                dtNew.Rows.Add(newRow);
            }
            #endregion
            for (int i = 0; i < dataDriver.LCADataList.Count; i++)
            {
                LCALineState lineDowntime = (LCALineState)dataList[i];
                #region  Draw Time Span Bar
                float barStartX = (float)(lineDowntime.START_TIME.Ticks - minDateTime.Ticks) / TimeSpan.TicksPerSecond / (totalHourSpan + 1) / 3600 * ChartDraw.ChartWitdh;
                float barWidth = (float)lineDowntime.TIME_OF_STATE / (totalHourSpan + 1) / 3600 * ChartDraw.ChartWitdh;
                TimeSpanBar bar = new TimeSpanBar(0, barStartX, barWidth);
                bar.BarColor = LCADrawInfo.LineState_Colors()["Pause"];
                if (rule.IsLineDownTime(lineDowntime))
                {
                    bar.BarColor = LCADrawInfo.LineState_Colors()["Stop"];
                }
                if (rule.IsLineUpTime(lineDowntime))
                {
                    bar.BarColor = LCADrawInfo.LineState_Colors()["Run"];
                }
                ChartDraw.DrawBar(bar);
                #endregion

                LCAMachineStateToPeriod stateToHour = new LCAMachineStateToPeriod();
                lineDowntime.LCA_LINE = "LCA_CELL_2";
                #region  hourly downtime calculation
                int HourIndex = Convert.ToInt32((lineDowntime.START_TIME.Ticks - minDateTime.Ticks) / TimeSpan.TicksPerHour);
                String lineState = LCABaseInfo.LINE_STATE.Pause.ToString();
                if (rule.IsLineUpTime(lineDowntime))
                {
                    lineState = LCABaseInfo.LINE_STATE.Run.ToString();
                }
                if (rule.IsLineDownTime(lineDowntime))
                {
                    lineState = LCABaseInfo.LINE_STATE.Stop.ToString();
                }
                int hourlydowntime = System.Convert.ToInt32(dtNew.Rows[HourIndex][lineState].ToString());
                int stateTime = (int)lineDowntime.TIME_OF_STATE;
                if (lineDowntime.START_TIME.Second + lineDowntime.START_TIME.Minute * 60 + stateTime > 3600)
                {
                    dtNew.Rows[HourIndex][lineState] = hourlydowntime + 3600 - lineDowntime.START_TIME.Second - lineDowntime.START_TIME.Minute * 60;
                    int j = 1;
                    for (j = 0; j < (stateTime + lineDowntime.START_TIME.Second + lineDowntime.START_TIME.Minute * 60) / 3600 - 1; j++)
                    {
                        if (HourIndex + j + 1 > totalHourSpan - 1)
                        {
                            break;
                        }
                        dtNew.Rows[HourIndex + j + 1][lineState] = System.Convert.ToInt32(dtNew.Rows[HourIndex + j + 1][lineState].ToString()) + 3600;
                    }
                    if (HourIndex + j + 1 > totalHourSpan - 1)
                    {
                        continue;
                    }
                    dtNew.Rows[HourIndex + j + 1][lineState] = System.Convert.ToInt32(dtNew.Rows[HourIndex + j + 1][lineState].ToString()) + (lineDowntime.START_TIME.Second + lineDowntime.START_TIME.Minute * 60 + stateTime) % 3600;
                }
                else
                {
                    dtNew.Rows[HourIndex][lineState] = hourlydowntime + stateTime;
                }
                #endregion
            }
            #region Get Hourly Output Data

            string strSQL = String.Format(@"select  to_char(lca.birth_date,'yyyy-MM-dd-hh24') as hourly,count(*) as output 
from lca.lca_data2 lca
                    where  lca.birth_date between to_date('{0}','dd-MM-yyyy HH24:mi:ss') and
                    to_date('{1}','dd-MM-yyyy HH24:mi:ss')   and lca_line='{2}' 
                   group by to_char(lca.birth_date,'yyyy-MM-dd-hh24') order by 1", startDateTime.ToString("dd-MM-yyyy HH:mm:ss"), endDateTime.ToString("dd-MM-yyyy HH:mm:ss"),strCellNo);
            dataDriver.CrunchTable(strSQL, "OutputSourceData");
            DataTable dtOutput = dataDriver.LCADataSet.Tables["OutputSourceData"];

            if (dtOutput.Rows.Count == 0)
            {
                MessageBox.Show("No Output!");
                return;
            }

            dtNew.Columns.Add("Output");
            for (int i = 0, j = 0; i < dtNew.Rows.Count; i++)
            {
                if (i >= dtNew.Rows.Count || j >= dtOutput.Rows.Count)
                {
                    break;
                }
                else
                {
                    if (dtNew.Rows[i][0].ToString() == dtOutput.Rows[j][0].ToString())
                    {
                        dtNew.Rows[i]["Output"] = dtOutput.Rows[j][1];
                        j++;
                    }
                    else
                    {
                        dtNew.Rows[i]["Output"] = 0;
                    }
                }
            }
            //DataTable dtCombine = dataDriver.CombineDataTable(dtNew, dataDriver.LCADataSet.Tables["OutputSourceData"], "HourlyOutput");
            #endregion

            #region Draw TimeSpan  Grid
            SeriesGrid lineGrid = new SeriesGrid(0, "LCA_CELL_2");
            ChartDraw.DrawSeriersGrid(lineGrid);
            for (int i = 0; i < totalHourSpan + 2; i++)
            {
                String LabelText = (minDateTime.AddHours(i).Hour).ToString();
                TimeSpanGrid gird = new TimeSpanGrid(LabelText, (float)ChartDraw.ChartWitdh / (totalHourSpan + 1) * i, 0, 500);
                ChartDraw.DrawTimeSpanGrid(gird);
            }
            #endregion

            #region Draw Interval Chart

            //Get Interval Data
            TimeIntervalChartDraw graph = TimeIntervalChartDraw.Instance;
            #region Draw Chart Area
            IntervalChartArea chartArea = new IntervalChartArea(100, 150, 800, 430);
            graph.colorSplit = 8;
            graph.IniGraphics(ChartDraw.ChartImg);
            graph.AddChartArea(chartArea);
            #endregion
            #region Get Time Span
            DataTable dtSQL = new DataTable("SQLTabel");
            DataTable dtInterval = new DataTable("IntervalTabel");
            dtInterval.Columns.Add("Time", typeof(int));
            dtInterval.Columns.Add("Interval", typeof(int));

            DataTable dtIntervalGrid = new DataTable("GridData");
            dtIntervalGrid.Columns.Add("Time", typeof(DateTime));
            dtIntervalGrid.Columns.Add("Interval", typeof(int));

            #endregion
            #region Draw Grid
            Int32 yMid = Convert.ToInt32(textMid.Text);
            Int32 yMax = Convert.ToInt32(textMax.Text);
            //span = Convert.ToDateTime(strEnd) - Convert.ToDateTime(strFrom);
            LegendGrid grid = new LegendGrid((int)totalHourSpan + 1, yMid);
            graph.AddAxis(grid);
            #endregion
            strSQL = String.Format(@"select birth_date from lca.lca_data2 
                    where  birth_date between to_date('{0}','dd-MM-yyyy HH24:mi:ss') and    to_date('{1}','dd-MM-yyyy HH24:mi:ss')  and lca_line='{2}' 
                  order by 1", startDateTime.ToString("dd-MM-yyyy HH:mm:ss"), endDateTime.ToString("dd-MM-yyyy HH:mm:ss"),strCellNo);
            dataDriver.CrunchTable(strSQL, "BirthDataSort");
            dtSQL = dataDriver.LCADataSet.Tables["BirthDataSort"];
            if (dtSQL.Rows.Count == 0)
            {
                return;
            }
            int intRed = 0;
            int intGreen = 0;
            int intYellow = 0;
            #region Draw First Point
            IntervalBar barPoint = new IntervalBar();
            DataRow newIntervalRow = dtInterval.NewRow();
            TimeSpan span;
            span = (DateTime)dtSQL.Rows[0][0] - startDateTime;
            newIntervalRow[0] = span.TotalSeconds;
            newIntervalRow[1] = span.TotalSeconds;

            if (Convert.ToInt32(newIntervalRow[1]) >= yMid && Convert.ToInt32(newIntervalRow[1]) < yMax)
            {
                barPoint.BarHeight = yMid;
                barPoint.BarColor = Color.Yellow;
                intYellow++;
            }
            else if (Convert.ToInt32(newIntervalRow[1]) >= yMax)
            {
                barPoint.BarHeight = yMid;
                barPoint.BarColor = Color.Orchid;
                intRed++;
            }
            else
            {
                barPoint.BarHeight = Convert.ToInt32(newIntervalRow[1]);
                barPoint.BarColor = Color.LightSkyBlue;
                intGreen++;
            }
            barPoint.XStart = 0;
            barPoint.YStart = barPoint.BarHeight;
            barPoint.BarWidth = Convert.ToInt32(newIntervalRow[1]);

            graph.DrawBarPoint(barPoint);
            dtInterval.Rows.Add(newIntervalRow);

            DataRow dtGrid = dtIntervalGrid.NewRow();
            if (Convert.ToInt32(newIntervalRow[1]) >= yMax)
            {
                dtGrid[0] = startDateTime.AddSeconds(Convert.ToInt32(newIntervalRow[0]));
                dtGrid[1] = newIntervalRow[1];
                dtIntervalGrid.Rows.Add(dtGrid);
            }
            #endregion
            #region Draw Other Points
            for (int i = 1; i < dtSQL.Rows.Count; i++)
            {

                newIntervalRow = dtInterval.NewRow();
                span = ((DateTime)dtSQL.Rows[i][0]) - startDateTime;
                newIntervalRow[0] = span.TotalSeconds;
                newIntervalRow[1] = (((DateTime)dtSQL.Rows[i][0]).Ticks - ((DateTime)dtSQL.Rows[i - 1][0]).Ticks) / TimeSpan.TicksPerSecond;
                if (Convert.ToInt32(newIntervalRow[1]) >= yMid && Convert.ToInt32(newIntervalRow[1]) < yMax)
                {
                    barPoint.BarHeight = yMid;
                    barPoint.BarColor = Color.Yellow;
                    intYellow++;
                }
                else if (Convert.ToInt32(newIntervalRow[1]) >= yMax)
                {
                    barPoint.BarHeight = yMid;
                    barPoint.BarColor = Color.Orchid;
                    intRed++;

                    dtGrid = dtIntervalGrid.NewRow();
                    dtGrid[0] = Convert.ToDateTime(startDateTime).AddSeconds(Convert.ToInt32(newIntervalRow[0]));
                    dtGrid[1] = newIntervalRow[1];
                    dtIntervalGrid.Rows.Add(dtGrid);
                }
                else
                {
                    barPoint.BarHeight = Convert.ToInt32(newIntervalRow[1]);
                    barPoint.BarColor = Color.LightSkyBlue;
                    intGreen++;
                }

                barPoint.YStart = barPoint.BarHeight;
                barPoint.BarWidth = Convert.ToInt32(newIntervalRow[1]);
                barPoint.XStart = Convert.ToInt32(newIntervalRow[0]) - Convert.ToInt32(newIntervalRow[1]);

                graph.DrawBarPoint(barPoint);
                dtInterval.Rows.Add(newIntervalRow);


            }
            #endregion

            //graph.AddSeries(dtInterval);
            dtInvervalAbnormal.DataSource = dtIntervalGrid;
            graph.AddCountLabel(intRed, intGreen, intYellow);
            graph.DrawStartLabel(dtSQL.Rows[0][0].ToString());
            graph.DrawEndLabel(dtSQL.Rows[dtSQL.Rows.Count - 1][0].ToString());
            #endregion

            String fileName = "LineDT" + startDateTime.ToString("yyyyMMddHH") + " - " + endDateTime.ToString("yyyyMMddHH");
            DGVlineHourly.DataSource = dtNew;
            buManualImport.Enabled = true;
            dataDriver.WriteDataToFile(dtNew, fileName);
            tabControl1.SelectedTab = TPLineHourly;
            //ChartDraw.saveImg(fileName);
            //ChartForm newForm = new ChartForm();
            //newForm.Text = ChartDraw.ImgFileName;
            //newForm.Height = ChartDraw.ChartHeight;
            //newForm.Width = ChartDraw.ChartWitdh;
            //newForm.BackgroundImage = ChartDraw.ChartImg;
            //newForm.BackgroundImageLayout = ImageLayout.Stretch;

            graph.saveImg(fileName);
            ChartForm newForm = new ChartForm();
            newForm.Text = graph.ImgFileName;
            newForm.Height = ChartDraw.ChartHeight;
            newForm.Width = ChartDraw.ChartWitdh;
            newForm.BackgroundImage = graph.ChartImg;
            newForm.BackgroundImageLayout = ImageLayout.Stretch;
            newForm.Show();
            graph.ChartImg = newForm.BackgroundImage;

        }
示例#4
0
        //Make Time Span Chart
        private void BtnDrawStationTimeSpanChart_Click(object sender, EventArgs e)
        {
            #region datasource verify
            LCADownTimeDataDriver dataDriver = LCADownTimeDataDriver.Instance;
            DataTable dt = (DataTable)DGVstationState.DataSource;
            DataTable dtSummary = (DataTable)DGVstationHourly.DataSource;
            if (dt == null || dt.Rows.Count == 0)
            {
                MessageBox.Show("Please Load Data First");
                return;
            }
            #endregion
            #region  start draw setting
            TimeSpanBarChartDraw ChartDraw = TimeSpanBarChartDraw.Instance;
            ChartDraw.ChartAreaStartX = 80;
            ChartDraw.ChartAreaStartY = 80;
            ChartDraw.BarStartX = 100;
            ChartDraw.BarStartX = 100;
            ChartDraw.IniGraphics();
            #endregion
            #region draw Serier Grid&Legend
            int StationIndex = 0;
            double summaryText = 0.00;
            foreach (LCABaseInfo.STATION_NAME station in (LCABaseInfo.STATION_NAME[])Enum.GetValues(typeof(LCABaseInfo.STATION_NAME)))
            {
                SeriesGrid grid = new SeriesGrid(StationIndex, station.ToString());
                ChartDraw.DrawSeriersGrid(grid);
                for (int i = 0; i < dtSummary.Rows.Count; i++)
                {
                    summaryText += Convert.ToDouble(dtSummary.Rows[i][StationIndex + 1].ToString());
                }

                summaryText = Math.Round(summaryText / (double)60, 2);
                ChartDraw.DrawSummaryLabel(grid, summaryText.ToString() + "m");
                StationIndex++;
                summaryText = 0.00;
            }
            SeriesLegend[] SeriesLegends = new SeriesLegend[LCADrawInfo.MachineState_Colors().Count];
            int SereiesIndex = 0;
            foreach (KeyValuePair<int, Color> machineStateColor in LCADrawInfo.MachineState_Colors())
            {
                SeriesLegends[SereiesIndex] = new SeriesLegend(SereiesIndex, machineStateColor.Key.ToString(), machineStateColor.Value);
                SereiesIndex++;
            }
            ChartDraw.DrawSeriesLengends(SeriesLegends);
            #endregion
            #region draw TimeSpan bar
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                LCAMachineState2 lcaDownTime = new LCAMachineState2();
                lcaDownTime.START_TIME = (DateTime)dr[LCAMachineState2.HEADERCHAR.START_TIME.ToString()];
                lcaDownTime.STOP_TIME = (DateTime)dr[LCAMachineState2.HEADERCHAR.STOP_TIME.ToString()];
                lcaDownTime.TIME_OF_STATE = System.Convert.ToInt32(dr[LCAMachineState2.HEADERCHAR.TIME_OF_STATE.ToString()]);
                lcaDownTime.CURRENTSTATE = System.Convert.ToInt32(dr[LCAMachineState2.HEADERCHAR.CURRENTSTATE.ToString()]);
                lcaDownTime.STATION_NAME = dr[LCAMachineState2.HEADERCHAR.STATION_NAME.ToString()].ToString();
                Boolean IsStateSelect = false;
                Boolean IsStationSelect = false;
                foreach (String state in LbxState.SelectedItems)
                {
                    if (lcaDownTime.CURRENTSTATE == (int)Enum.Parse(typeof(LCABaseInfo.OEE_STATE), state))
                    {
                        IsStateSelect = true;
                        break;
                    }
                }
                if (!IsStateSelect) continue;
                foreach (String station in LbxStation.SelectedItems)
                {
                    if (lcaDownTime.STATION_NAME == station)
                    {
                        IsStationSelect = true;
                        break;
                    }
                }
                if (!IsStationSelect) continue;
                StationIndex = 0;
                float barStartX = (float)(lcaDownTime.START_TIME.Ticks - minDateTime.Ticks) / TimeSpan.TicksPerSecond / (totalHourSpan + 1) / 3600 * ChartDraw.ChartWitdh;
                float barWidth = (float)lcaDownTime.TIME_OF_STATE / (totalHourSpan + 1) / 3600 * ChartDraw.ChartWitdh;
                foreach (LCABaseInfo.STATION_NAME station in (LCABaseInfo.STATION_NAME[])Enum.GetValues(typeof(LCABaseInfo.STATION_NAME)))
                {
                    if (lcaDownTime.STATION_NAME == station.ToString()) { break; }
                    StationIndex++;
                }
                TimeSpanBar bar = new TimeSpanBar(StationIndex, barStartX, barWidth);
                bar.BarColor = LCADrawInfo.MachineState_Colors()[(int)lcaDownTime.CURRENTSTATE];
                ChartDraw.DrawBar(bar);
            }
            #endregion
            #region draw TimeSpan Grid
            for (int i = 0; i < totalHourSpan + 2; i++)
            {
                String LabelText = (minDateTime.AddHours(i).Hour).ToString();
                TimeSpanGrid gird = new TimeSpanGrid(LabelText, (float)ChartDraw.ChartWitdh / (totalHourSpan + 1) * i, 0, 500);
                ChartDraw.DrawTimeSpanGrid(gird);
            }
            #endregion
            //ChartDraw.ImgFilePath = @"E:\LCA Data\test.jpg";
            String ImgFileName = "MachineDT" + startDateTime.ToString("yyyMMddHH") + " - " + endDateTime.ToString("yyyyMMddHH");
            ChartDraw.saveImg(ImgFileName);
            ChartForm newForm = new ChartForm();
            newForm.Text = ChartDraw.ImgFileName;
            newForm.Height = ChartDraw.ChartHeight;
            newForm.Width = ChartDraw.ChartWitdh;
            newForm.BackgroundImage = ChartDraw.ChartImg;
            newForm.BackgroundImageLayout = ImageLayout.Stretch;
            newForm.Show();
        }
示例#5
0
        public void CreateCustomHourlyChart(DateTime dtFrom, DateTime dtEnd)
        {
            startDateTime = dtFrom;
            endDateTime = dtEnd;
            #region  load datasource
            LCADownTimeDataDriver dataDriver = LCADownTimeDataDriver.Instance;
            if (dataDriver.LCADataSet.Tables.Count > 0)
            {
                dataDriver.LCADataSet.Tables.Clear();
            }
            LCAMachineState2 lcaDownTime = new LCAMachineState2();
            String sql = LCAMachineState2.CreateSql(startDateTime.ToString("dd-MM-yyyy HH:mm:ss"), endDateTime.ToString("dd-MM-yyyy HH:mm:ss"),strCellNo);
            dataDriver.CrunchTable(sql, "LCA_DOWNTIME");
            for (int i = 0; i < dataDriver.LCADataSet.Tables["LCA_DOWNTIME"].Rows.Count; i++)
            {
                if ((DateTime)dataDriver.LCADataSet.Tables["LCA_DOWNTIME"].Rows[i][LCAMachineState2.HEADERCHAR.STOP_TIME.ToString()] > dtEnd)
                {
                    dataDriver.LCADataSet.Tables["LCA_DOWNTIME"].Rows[i][LCAMachineState2.HEADERCHAR.STOP_TIME.ToString()] = dtEnd;
                    TimeSpan tempSpan = dtEnd - Convert.ToDateTime(dataDriver.LCADataSet.Tables["LCA_DOWNTIME"].Rows[i][LCAMachineState2.HEADERCHAR.START_TIME.ToString()]);
                    dataDriver.LCADataSet.Tables["LCA_DOWNTIME"].Rows[i][LCAMachineState2.HEADERCHAR.TIME_OF_STATE.ToString()] = tempSpan.TotalSeconds;
                }
            }
            DGVstationState.DataSource = dataDriver.LCADataSet.Tables["LCA_DOWNTIME"];

            #endregion

            #region UI
            if (dataDriver.LCADataSet.Tables["LCA_DOWNTIME"].Rows.Count == 0)
            {
                MessageBox.Show("No Record Found");
                BtnDrawStationTimeSpanChart.Enabled = false;
                BtnStationHourlyReport.Enabled = false;
                BtnReportLine.Enabled = false;
                BtnDrawLineTimeSpanChart.Enabled = false;
                buManualImport.Enabled = false;
                LbxState.Enabled = false;
                LbxStation.Enabled = false;
                return;
            }
            LbxState.Enabled = true;
            LbxStation.Enabled = true;
            tabControl1.SelectedTab = TPStationState;
            #endregion

            #region Loda maxdate&mindate from Data Resouce
            DateTime thisStartTime, thisEndTime;
            minDateTime = startDateTime;
            maxDateTime = startDateTime;
            for (int i = 0; i < dataDriver.LCADataSet.Tables["LCA_DOWNTIME"].Rows.Count; i++)
            {
                thisStartTime = (DateTime)dataDriver.LCADataSet.Tables["LCA_DOWNTIME"].Rows[i][LCAMachineState2.HEADERCHAR.START_TIME.ToString()];
                thisEndTime = (DateTime)dataDriver.LCADataSet.Tables["LCA_DOWNTIME"].Rows[i][LCAMachineState2.HEADERCHAR.STOP_TIME.ToString()];
                if (thisStartTime < minDateTime)
                {
                    minDateTime = thisStartTime;
                }
                if (thisEndTime > maxDateTime)
                {
                    maxDateTime = thisEndTime;
                }
            }
            totalSecondSpan = (maxDateTime.Ticks - minDateTime.Ticks) / TimeSpan.TicksPerSecond;
            totalHourSpan = (maxDateTime.Ticks - minDateTime.Ticks) / TimeSpan.TicksPerHour;
            #endregion

            #region Load Station Data
            #region  validate Table
            DataTable dt = (DataTable)DGVstationState.DataSource;
            DataTable dtNew = new DataTable("LCAHourlyDownTime");
            if (dt == null || dt.Rows.Count == 0)
            {
                MessageBox.Show("Please Load Data First");
                return;
            }
            #endregion
            #region station column&hour row add
            dtNew.Columns.Add(LCAMachineStateToPeriod.HEADERCHAR.Period.ToString());
            foreach (LCABaseInfo.STATION_NAME station in (LCABaseInfo.STATION_NAME[])Enum.GetValues(typeof(LCABaseInfo.STATION_NAME)))
            {
                dtNew.Columns.Add(station.ToString());
            }
            for (int i = 0; i < totalHourSpan + 1; i++)
            {
                DataRow newRow = dtNew.NewRow();
                newRow[LCAMachineStateToPeriod.HEADERCHAR.Period.ToString()] = minDateTime.AddHours(i).ToString("yyyy-MM-dd-HH");
                foreach (LCABaseInfo.STATION_NAME station in (LCABaseInfo.STATION_NAME[])Enum.GetValues(typeof(LCABaseInfo.STATION_NAME)))
                {
                    newRow[station.ToString()] = 0;
                }
                dtNew.Rows.Add(newRow);
            }
            #endregion
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                //LCAMachineState2 lcaDownTime = new LCAMachineState2();
                lcaDownTime.START_TIME = (DateTime)dr[LCAMachineState2.HEADERCHAR.START_TIME.ToString()];
                lcaDownTime.STOP_TIME = (DateTime)dr[LCAMachineState2.HEADERCHAR.STOP_TIME.ToString()];
                lcaDownTime.TIME_OF_STATE = System.Convert.ToInt32(dr[LCAMachineState2.HEADERCHAR.TIME_OF_STATE.ToString()]);
                lcaDownTime.CURRENTSTATE = System.Convert.ToInt32(dr[LCAMachineState2.HEADERCHAR.CURRENTSTATE.ToString()]);
                lcaDownTime.STATION_NAME = dr[LCAMachineState2.HEADERCHAR.STATION_NAME.ToString()].ToString();
                #region  check select option
                Boolean IsState = false;
                Boolean IsStation = false;



                foreach (String state in LbxState.SelectedItems)
                {
                    if (lcaDownTime.CURRENTSTATE == (int)Enum.Parse(typeof(LCABaseInfo.OEE_STATE), state))
                    {
                        IsState = true;
                        break;
                    }
                }
                if (!IsState) continue;
                foreach (String station in LbxStation.SelectedItems)
                {
                    if (lcaDownTime.STATION_NAME == station)
                    {
                        IsStation = true;
                        break;
                    }
                }
                if (!IsStation) continue;
                #endregion
                #region  hourly downtime calculation
                int HourIndex = Convert.ToInt32((lcaDownTime.START_TIME.Ticks - minDateTime.Ticks) / TimeSpan.TicksPerHour);
                int hourlydowntime = System.Convert.ToInt32(dtNew.Rows[HourIndex][lcaDownTime.STATION_NAME].ToString());
                int stateTime = (int)lcaDownTime.TIME_OF_STATE;
                if (lcaDownTime.START_TIME.Second + lcaDownTime.START_TIME.Minute * 60 + stateTime > 3600)
                {
                    dtNew.Rows[HourIndex][lcaDownTime.STATION_NAME] = hourlydowntime + 3600 - lcaDownTime.START_TIME.Second - lcaDownTime.START_TIME.Minute * 60;
                    int j = 1;
                    for (j = 0; j < (stateTime + lcaDownTime.START_TIME.Second + lcaDownTime.START_TIME.Minute * 60) / 3600 - 1; j++)
                    {
                        if (HourIndex + j + 1 > totalHourSpan - 1)
                        {
                            break;
                        }
                        dtNew.Rows[HourIndex + j + 1][lcaDownTime.STATION_NAME] = System.Convert.ToInt32(dtNew.Rows[HourIndex + j + 1][lcaDownTime.STATION_NAME].ToString()) + 3600;
                    }
                    if (HourIndex + j + 1 > totalHourSpan - 1)
                    {
                        continue;
                    }
                    dtNew.Rows[HourIndex + j + 1][lcaDownTime.STATION_NAME] = System.Convert.ToInt32(dtNew.Rows[HourIndex + j + 1][lcaDownTime.STATION_NAME].ToString()) + (lcaDownTime.START_TIME.Second + lcaDownTime.START_TIME.Minute * 60 + stateTime) % 3600;
                }
                else
                {
                    dtNew.Rows[HourIndex][lcaDownTime.STATION_NAME] = hourlydowntime + stateTime;
                }
                #endregion
            }
            DGVstationHourly.DataSource = dtNew;
            tabControl1.SelectedTab = TPStationHourly;
            #endregion

            #region Draw Station Span Chart
            GetStationSpanChart();
            #endregion

            #region Load Line Data
            #region  validate table

            if (!dataDriver.LCADataSet.Tables.Contains("LCA_DOWNTIME") || dataDriver.LCADataSet.Tables["LCA_DOWNTIME"].Rows.Count < 1)
            {
                MessageBox.Show("Please load Souce Data Firstly");
                return;
            }
            dataDriver.WriteTableToList("LCA_DOWNTIME", typeof(LCAMachineState2).FullName);
            List<object> dataList = dataDriver.LCADataList;
            DataTable dtLineNew = new DataTable("LCALineDownTime");
            if (dataList == null || dataList.Count == 0)
            {
                MessageBox.Show("Please Load Data First");
                return;
            }
            #endregion
            #region initial first state row
            DataRow iniRow;
            iniRow = dtLineNew.NewRow();
            foreach (LCALineState.HEADERCHAR field in Enum.GetValues(typeof(LCALineState.HEADERCHAR)))
            {

                dtLineNew.Columns.Add(field.ToString(), typeof(LCALineState).GetProperty(field.ToString()).PropertyType);
                //dtNew.Columns.Add(field.ToString());
            }
            iniRow[LCALineState.HEADERCHAR.START_TIME.ToString()] = startDateTime;
            iniRow[LCALineState.HEADERCHAR.STOP_TIME.ToString()] = startDateTime;
            //iniRow[LCALineState.HEADERCHAR.START_TIME.ToString()] = startDateTime.ToString("yyyy-MM-dd HH:mm:ss");
            //iniRow[LCALineState.HEADERCHAR.STOP_TIME.ToString()] = startDateTime.ToString("yyyy-MM-dd HH:mm:ss");
            iniRow[LCALineState.HEADERCHAR.TIME_OF_STATE.ToString()] = 0;
            iniRow[LCALineState.HEADERCHAR.HLD1_STATE.ToString()] = 1000;
            iniRow[LCALineState.HEADERCHAR.HLD2_STATE.ToString()] = 1000;
            iniRow[LCALineState.HEADERCHAR.BSE_STATE.ToString()] = 1000;
            iniRow[LCALineState.HEADERCHAR.SWG1_STATE.ToString()] = 1000;
            iniRow[LCALineState.HEADERCHAR.SWG2_STATE.ToString()] = 1000;
            iniRow[LCALineState.HEADERCHAR.ULRT_STATE.ToString()] = 1000;
            iniRow[LCALineState.HEADERCHAR.STATION.ToString()] = "START";
            iniRow[LCALineState.HEADERCHAR.ERROR_DESCRIPTION.ToString()] = "";
            iniRow[LCALineState.HEADERCHAR.LCA_LINE.ToString()] = "";
            dtLineNew.Rows.Add(iniRow);
            #endregion
            #region add new state row
            DataRow drLine;
            LCALineState[] LineStateArray = new LCALineState[dataList.Count];
            LCAMachineState2 thisDownTime, nextDownTime;
            for (int i = 0; i < dataList.Count; i++)
            {
                thisDownTime = (LCAMachineState2)dataList[i];
                if (i == dataList.Count - 1)
                {
                    nextDownTime = new LCAMachineState2();
                    nextDownTime.START_TIME = maxDateTime;
                }
                else
                {
                    nextDownTime = (LCAMachineState2)dataList[i + 1];
                }
                DataRow lastRow = dtLineNew.Rows[i];
                drLine = dtLineNew.NewRow();
                foreach (LCALineState.HEADERCHAR field in Enum.GetValues(typeof(LCALineState.HEADERCHAR)))
                {
                    drLine[field.ToString()] = lastRow[field.ToString()];
                }
                drLine[LCALineState.HEADERCHAR.START_TIME.ToString()] = thisDownTime.START_TIME.ToString("yyyy-MM-dd HH:mm:ss");
                drLine[LCALineState.HEADERCHAR.STOP_TIME.ToString()] = nextDownTime.START_TIME.ToString("yyyy-MM-dd HH:mm:ss");
                drLine[LCALineState.HEADERCHAR.TIME_OF_STATE.ToString()] = (nextDownTime.START_TIME.Ticks - thisDownTime.START_TIME.Ticks) / TimeSpan.TicksPerSecond;
                drLine[LCALineState.HEADERCHAR.LCA_LINE.ToString()] = thisDownTime.LINE;
                drLine[LCALineState.HEADERCHAR.STATION.ToString()] = thisDownTime.STATION_NAME;
                drLine[LCALineState.HEADERCHAR.ERROR_DESCRIPTION.ToString()] = thisDownTime.ERROR_DESCRIPTION;
                drLine[LCALineState.HEADERCHAR.LCA_LINE.ToString()] = thisDownTime.LINE;
                int MachineState = (int)thisDownTime.CURRENTSTATE;
                switch (thisDownTime.STATION_NAME)
                {
                    case "HgaLoad1":
                        drLine[LCALineState.HEADERCHAR.HLD1_STATE.ToString()] = MachineState;
                        break;
                    case "HgaLoad2":
                        drLine[LCALineState.HEADERCHAR.HLD2_STATE.ToString()] = MachineState;
                        break;
                    case "BossEngage":
                        drLine[LCALineState.HEADERCHAR.BSE_STATE.ToString()] = MachineState;
                        break;
                    case "SWAGE1":
                        drLine[LCALineState.HEADERCHAR.SWG1_STATE.ToString()] = MachineState;
                        break;
                    case "SWAGE2":
                        drLine[LCALineState.HEADERCHAR.SWG2_STATE.ToString()] = MachineState;
                        break;
                    case "ULRT":
                        drLine[LCALineState.HEADERCHAR.ULRT_STATE.ToString()] = MachineState;
                        break;
                    default:
                        break;
                }
                dtLineNew.Rows.Add(drLine);
            }
            #endregion
            dataDriver.addTable(dtLineNew);
            DGVlineState.DataSource = dtLineNew;
            tabControl1.SelectedTab = TPLineState;
            #endregion

            #region Draw Line Data Chart

            #region Draw Line D/T Chart
            #region  validate table
            dataDriver = LCADownTimeDataDriver.Instance;
            if (!dataDriver.LCADataSet.Tables.Contains("LCALineDownTime") || dataDriver.LCADataSet.Tables["LCALineDownTime"].Rows.Count < 1)
            {
                MessageBox.Show("Please load Souce Data Firstly");
                return;
            }
            dataDriver.WriteTableToList("LCALineDownTime", typeof(LCALineState).FullName);
            List<object> dataLineList = dataDriver.LCADataList;
            if (dataList == null || dataList.Count == 0)
            {
                MessageBox.Show("Please Load Data and LCA line State Report First");
                return;
            }
            #endregion

            #region  initial Draw Chart setting
            TimeSpanBarChartDraw ChartDraw = TimeSpanBarChartDraw.Instance;
            ChartDraw.IniGraphics();
            ChartDraw.ChartAreaStartX = 80;
            ChartDraw.ChartAreaStartY = 80;
            ChartDraw.BarStartX = 100;
            ChartDraw.BarStartX = 100;
            #endregion

            #region Draw Series Legends
            SeriesLegend[] SeriesLegends = new SeriesLegend[LCADrawInfo.LineState_Colors().Count];
            int SereiesIndex = 0;
            foreach (KeyValuePair<String, Color> lineStateColor in LCADrawInfo.LineState_Colors())
            {
                SeriesLegends[SereiesIndex] = new SeriesLegend(SereiesIndex, lineStateColor.Key.ToString(), lineStateColor.Value);
                SereiesIndex++;
            }
            ChartDraw.DrawSeriesLengends(SeriesLegends);
            #endregion
            DataTable dtLineChartNew = new DataTable("LCALineHourlyDownTime");
            LCADownTimeRule rule = LCADownTimeRule.Instance;

            #region station column&hour row add
            dtLineChartNew.Columns.Add(LCAMachineStateToPeriod.HEADERCHAR.Period.ToString());
            foreach (LCABaseInfo.LINE_STATE lineState in (LCABaseInfo.LINE_STATE[])Enum.GetValues(typeof(LCABaseInfo.LINE_STATE)))
            {
                dtLineChartNew.Columns.Add(lineState.ToString());
            }
            for (int i = 0; i < totalHourSpan + 1; i++)
            {
                DataRow newRow = dtLineChartNew.NewRow();
                newRow[LCAMachineStateToPeriod.HEADERCHAR.Period.ToString()] = minDateTime.AddHours(i).ToString("yyyy-MM-dd-HH");
                foreach (LCABaseInfo.LINE_STATE lineState in (LCABaseInfo.LINE_STATE[])Enum.GetValues(typeof(LCABaseInfo.LINE_STATE)))
                {
                    newRow[lineState.ToString()] = 0;
                }
                dtLineChartNew.Rows.Add(newRow);
            }
            #endregion

            for (int i = 0; i < dataDriver.LCADataList.Count; i++)
            {
                LCALineState lineDowntime = (LCALineState)dataList[i];
                #region  Draw Time Span Bar
                float barStartX = (float)(lineDowntime.START_TIME.Ticks - minDateTime.Ticks) / TimeSpan.TicksPerSecond / totalSecondSpan * ChartDraw.ChartWitdh;
                float barWidth = (float)lineDowntime.TIME_OF_STATE / totalSecondSpan * ChartDraw.ChartWitdh;
                TimeSpanBar bar = new TimeSpanBar(0, barStartX, barWidth);
                bar.BarColor = LCADrawInfo.LineState_Colors()["Pause"];
                if (rule.IsLineDownTime(lineDowntime))
                {
                    bar.BarColor = LCADrawInfo.LineState_Colors()["Stop"];
                }
                if (rule.IsLineUpTime(lineDowntime))
                {
                    bar.BarColor = LCADrawInfo.LineState_Colors()["Run"];
                }
                ChartDraw.DrawBar(bar);
                #endregion

                LCAMachineStateToPeriod stateToHour = new LCAMachineStateToPeriod();
                lineDowntime.LCA_LINE = "LCA_CELL_2";
                #region  hourly downtime calculation
                int HourIndex = Convert.ToInt32((lineDowntime.START_TIME.Ticks - minDateTime.Ticks) / TimeSpan.TicksPerHour);
                String lineState = LCABaseInfo.LINE_STATE.Pause.ToString();
                if (rule.IsLineUpTime(lineDowntime))
                {
                    lineState = LCABaseInfo.LINE_STATE.Run.ToString();
                }
                if (rule.IsLineDownTime(lineDowntime))
                {
                    lineState = LCABaseInfo.LINE_STATE.Stop.ToString();
                }
                int hourlydowntime = System.Convert.ToInt32(dtLineChartNew.Rows[HourIndex][lineState].ToString());
                int stateTime = (int)lineDowntime.TIME_OF_STATE;
                if (lineDowntime.START_TIME.Second + lineDowntime.START_TIME.Minute * 60 + stateTime > 3600)
                {
                    dtLineChartNew.Rows[HourIndex][lineState] = hourlydowntime + 3600 - lineDowntime.START_TIME.Second - lineDowntime.START_TIME.Minute * 60;
                    int j = 1;
                    for (j = 0; j < (stateTime + lineDowntime.START_TIME.Second + lineDowntime.START_TIME.Minute * 60) / 3600 - 1; j++)
                    {
                        if (HourIndex + j + 1 > totalHourSpan - 1)
                        {
                            break;
                        }
                        dtLineChartNew.Rows[HourIndex + j + 1][lineState] = System.Convert.ToInt32(dtLineChartNew.Rows[HourIndex + j + 1][lineState].ToString()) + 3600;
                    }
                    if (HourIndex + j + 1 > totalHourSpan - 1)
                    {
                        continue;
                    }
                    dtLineChartNew.Rows[HourIndex + j + 1][lineState] = System.Convert.ToInt32(dtLineChartNew.Rows[HourIndex + j + 1][lineState].ToString()) + (lineDowntime.START_TIME.Second + lineDowntime.START_TIME.Minute * 60 + stateTime) % 3600;
                }
                else
                {
                    dtLineChartNew.Rows[HourIndex][lineState] = hourlydowntime + stateTime;
                }
                #endregion
            }

            #endregion

            #region Draw Output Interval Chart
            #region Get Hourly Output Data
            string strFrom, strEnd;
            OleDbUtility oleUtility = new OleDbUtility("ods", "users", "users");
            oleUtility.getConnection();
            oleUtility.OpenConnection();

            strFrom = dtFrom.ToString("yyyy-MM-dd HH:mm");
            strEnd = dtEnd.ToString("yyyy-MM-dd HH:mm");
            string strSQL = "select to_char(lca.birth_date,'yyyy-MM-dd-hh24') as hourly,count(*) as output from lca.lca_data2 lca where to_char(lca.birth_date,'yyyy-MM-dd hh24:mi') between '" + strFrom + "' and '" + strEnd + "'   and lca_line='"+strCellNo+"'  group by to_char(lca.birth_date,'yyyy-MM-dd-hh24') order by to_char(lca.birth_date,'yyyy-MM-dd-hh24')";

            DataTable dtOutput = new DataTable("OutputSourceData");
            dtOutput = oleUtility.SQLQuery_DataTable(strSQL);

            dtLineChartNew.Columns.Add("Output");
            for (int i = 0, j = 0; i < dtLineChartNew.Rows.Count; i++)
            {
                if (i >= dtLineChartNew.Rows.Count || j >= dtOutput.Rows.Count)
                {
                    break;
                }
                else
                {
                    if (dtLineChartNew.Rows[i][0].ToString() == dtOutput.Rows[j][0].ToString())
                    {
                        dtLineChartNew.Rows[i]["Output"] = dtOutput.Rows[j][1];
                        j++;
                    }
                    else
                    {
                        dtLineChartNew.Rows[i]["Output"] = 0;
                    }
                }
            }
            //DataTable dtCombine = dataDriver.CombineDataTable(dtNew, dataDriver.LCADataSet.Tables["OutputSourceData"], "HourlyOutput");
            #endregion

            #region Draw TimeSpan  Grid
            SeriesGrid lineGrid = new SeriesGrid(0, "LCA_CELL_2");
            ChartDraw.DrawSeriersGrid(lineGrid);
            for (int i = 0; i < totalHourSpan + 2; i++)
            {
                String LabelText = (minDateTime.AddHours(i).Hour).ToString();
                TimeSpanGrid gird = new TimeSpanGrid(LabelText, (float)3600 / totalSecondSpan * ChartDraw.ChartWitdh * i, 0, 500);
                ChartDraw.DrawTimeSpanGrid(gird);
            }
            #endregion

            #region Draw Chart Area
            TimeIntervalChartDraw graph = TimeIntervalChartDraw.Instance;
            IntervalChartArea chartArea = new IntervalChartArea(100, 150, 800, 430);
            graph.colorSplit = 8;
            graph.IniGraphics(ChartDraw.ChartImg);
            graph.AddChartArea(chartArea);
            #endregion

            #region Get Time Span
            DataTable dtSQL = new DataTable("SQLTabel");
            DataTable dtInterval = new DataTable("IntervalTabel");
            dtInterval.Columns.Add("Time", typeof(int));
            dtInterval.Columns.Add("Interval", typeof(int));

            DataTable dtIntervalGrid = new DataTable("GridData");
            dtIntervalGrid.Columns.Add("Time", typeof(DateTime));
            dtIntervalGrid.Columns.Add("Interval", typeof(int));

            TimeSpan span = dtEnd - dtFrom;

            strFrom = dtFrom.ToString("yyyy-MM-dd HH:mm:ss");
            strEnd = dtEnd.ToString("yyyy-MM-dd HH:mm:ss");

            #endregion

            #region Draw Grid
            Int32 yMid = Convert.ToInt32(textMid.Text);
            Int32 yMax = Convert.ToInt32(textMax.Text);
            span = dtEnd - dtFrom;
            LegendGrid grid = new LegendGrid(Convert.ToInt32(span.TotalHours), yMid);
            graph.AddAxis(grid);
            #endregion

            #region Output Data
            strSQL = "select to_char(birth_date,'yyyy-MM-dd hh24:mi:ss') as dateChar from lca.lca_data2 where to_char(birth_date,'yyyy-MM-dd hh24:mi:ss') between '" + strFrom + "' and '" + strEnd + "'  and lca_line='2'  order by birth_date";
            dtSQL = oleUtility.SQLQuery_DataTable(strSQL);
            if (dtSQL.Rows.Count == 0)
            {
                return;
            }
            int intRed = 0;
            int intGreen = 0;
            int intYellow = 0;
            #endregion

            #region Draw First Point
            IntervalBar barPoint = new IntervalBar();
            DataRow newIntervalRow = dtInterval.NewRow();
            span = Convert.ToDateTime(dtSQL.Rows[0][0].ToString()) - Convert.ToDateTime(strFrom);
            newIntervalRow[0] = span.TotalSeconds;
            newIntervalRow[1] = span.TotalSeconds;

            if (Convert.ToInt32(newIntervalRow[1]) >= yMid && Convert.ToInt32(newIntervalRow[1]) < yMax)
            {
                barPoint.BarHeight = yMid;
                barPoint.BarColor = Color.Yellow;
                intYellow++;
            }
            else if (Convert.ToInt32(newIntervalRow[1]) >= yMax)
            {
                barPoint.BarHeight = yMid;
                barPoint.BarColor = Color.Orchid;
                intRed++;
            }
            else
            {
                barPoint.BarHeight = Convert.ToInt32(newIntervalRow[1]);
                barPoint.BarColor = Color.LightSkyBlue;
                intGreen++;
            }
            barPoint.XStart = 0;
            barPoint.YStart = barPoint.BarHeight;
            barPoint.BarWidth = Convert.ToInt32(newIntervalRow[1]);

            graph.DrawBarPoint(barPoint);
            dtInterval.Rows.Add(newIntervalRow);

            DataRow dtGrid = dtIntervalGrid.NewRow();
            if (Convert.ToInt32(newIntervalRow[1]) >= yMax)
            {
                dtGrid[0] = Convert.ToDateTime(strFrom).AddSeconds(Convert.ToInt32(newIntervalRow[0]));
                dtGrid[1] = newIntervalRow[1];
                dtIntervalGrid.Rows.Add(dtGrid);
            }
            #endregion

            #region Draw Other Points
            for (int i = 1; i < dtSQL.Rows.Count; i++)
            {

                newIntervalRow = dtInterval.NewRow();
                span = Convert.ToDateTime(dtSQL.Rows[i][0].ToString()) - Convert.ToDateTime(strFrom);
                newIntervalRow[0] = span.TotalSeconds;
                newIntervalRow[1] = span.TotalSeconds - Convert.ToInt32(dtInterval.Rows[i - 1][0].ToString());

                if (Convert.ToInt32(newIntervalRow[1]) >= yMid && Convert.ToInt32(newIntervalRow[1]) < yMax)
                {
                    barPoint.BarHeight = yMid;
                    barPoint.BarColor = Color.Yellow;
                    intYellow++;
                }
                else if (Convert.ToInt32(newIntervalRow[1]) >= yMax)
                {
                    barPoint.BarHeight = yMid;
                    barPoint.BarColor = Color.Orchid;
                    intRed++;

                    dtGrid = dtIntervalGrid.NewRow();
                    dtGrid[0] = Convert.ToDateTime(strFrom).AddSeconds(Convert.ToInt32(newIntervalRow[0]));
                    dtGrid[1] = newIntervalRow[1];
                    dtIntervalGrid.Rows.Add(dtGrid);
                }
                else
                {
                    barPoint.BarHeight = Convert.ToInt32(newIntervalRow[1]);
                    barPoint.BarColor = Color.LightSkyBlue;
                    intGreen++;
                }

                barPoint.YStart = barPoint.BarHeight;
                barPoint.BarWidth = Convert.ToInt32(newIntervalRow[1]);
                barPoint.XStart = Convert.ToInt32(newIntervalRow[0]) - Convert.ToInt32(newIntervalRow[1]);

                graph.DrawBarPoint(barPoint);
                dtInterval.Rows.Add(newIntervalRow);


            }
            #endregion
            #endregion

            #region Show Chart ON Form
            dtInvervalAbnormal.DataSource = dtIntervalGrid;
            graph.AddCountLabel(intRed, intGreen, intYellow);

            graph.DrawStartLabel(dtSQL.Rows[0][0].ToString());
            graph.DrawEndLabel(dtSQL.Rows[dtSQL.Rows.Count - 1][0].ToString());

            String fileName = "LineDT" + startDateTime.ToString("yyyy-MM-dd-HH");
            DGVlineHourly.DataSource = dtLineChartNew;
            tabControl1.SelectedTab = TPLineHourly;

            graph.saveImg(fileName);
            graph.saveWebImg(fileName);
            //ChartForm newForm = new ChartForm();
            //newForm.Text = graph.ImgFileName;
            //newForm.Height = ChartDraw.ChartHeight;
            //newForm.Width = ChartDraw.ChartWitdh;
            //newForm.BackgroundImage = graph.ChartImg;
            //newForm.BackgroundImageLayout = ImageLayout.Stretch;
            //newForm.Show();
            #endregion

            #endregion
        }
示例#6
0
        //Button Line Hourly Report Chart
        public DataTable GetHourData()
        {
            #region 注释
            /*
            #region  validate table
            LCADownTimeDataDriver dataDriver = LCADownTimeDataDriver.Instance;
            if (!dataDriver.LCADataSet.Tables.Contains("LCALineDownTime") || dataDriver.LCADataSet.Tables["LCALineDownTime"].Rows.Count < 1)
            {
                MessageBox.Show("Please load Souce Data Firstly");
                return;
            }
            dataDriver.WriteTableToList("LCALineDownTime", typeof(LCALineState).FullName);
            List<object> dataList = dataDriver.LCADataList;
            if (dataList == null || dataList.Count == 0)
            {
                MessageBox.Show("Please Load Data and LCA line State Report First");
                return;
            }
            #endregion
            DataTable dtNew = new DataTable("LCALineHourlyDownTime");
            LCADownTimeRule rule = LCADownTimeRule.Instance;
            #region station column&hour row add
            dtNew.Columns.Add(LCAMachineStateToPeriod.HEADERCHAR.Period.ToString());
            foreach (LCABaseInfo.LINE_STATE lineState in (LCABaseInfo.LINE_STATE[])Enum.GetValues(typeof(LCABaseInfo.LINE_STATE)))
            {
                dtNew.Columns.Add(lineState.ToString());
            }

            for (int i = 0; i < totalHourSpan; i++)
            {
                DataRow newRow = dtNew.NewRow();
                newRow[LCAMachineStateToPeriod.HEADERCHAR.Period.ToString()] = minDateTime.AddHours(i).ToString("yyyy-MM-dd-HH");
                foreach (LCABaseInfo.LINE_STATE lineState in (LCABaseInfo.LINE_STATE[])Enum.GetValues(typeof(LCABaseInfo.LINE_STATE)))
                {
                    newRow[lineState.ToString()] = 0;
                }
                dtNew.Rows.Add(newRow);
            }
            #endregion
            for (int i = 0; i < dataDriver.LCADataList.Count; i++)
            {
                LCALineState lineDowntime = (LCALineState)dataList[i];

                LCAMachineStateToPeriod stateToHour = new LCAMachineStateToPeriod();
                lineDowntime.LCA_LINE = "LCA_CELL_2";
                #region  hourly downtime calculation
                int HourIndex = Convert.ToInt32((lineDowntime.START_TIME.Ticks - minDateTime.Ticks) / TimeSpan.TicksPerHour);
                String lineState = LCABaseInfo.LINE_STATE.Pause.ToString();
                if (rule.IsLineUpTime(lineDowntime))
                {
                    lineState = LCABaseInfo.LINE_STATE.Run.ToString();
                }
                if (rule.IsLineDownTime(lineDowntime))
                {
                    lineState = LCABaseInfo.LINE_STATE.Stop.ToString();
                }
                int hourlydowntime = System.Convert.ToInt32(dtNew.Rows[HourIndex][lineState].ToString());
                int stateTime = (int)lineDowntime.TIME_OF_STATE;
                if (lineDowntime.START_TIME.Second + lineDowntime.START_TIME.Minute * 60 + stateTime > 3600)
                {
                    dtNew.Rows[HourIndex][lineState] = hourlydowntime + 3600 - lineDowntime.START_TIME.Second - lineDowntime.START_TIME.Minute * 60;
                    int j = 1;
                    for (j = 0; j < (stateTime + lineDowntime.START_TIME.Second + lineDowntime.START_TIME.Minute * 60) / 3600 - 1; j++)
                    {
                        if (HourIndex + j + 1 > totalHourSpan - 1)
                        {
                            break;
                        }
                        dtNew.Rows[HourIndex + j + 1][lineState] = System.Convert.ToInt32(dtNew.Rows[HourIndex + j + 1][lineState].ToString()) + 3600;
                    }
                    if (HourIndex + j + 1 > totalHourSpan - 1)
                    {
                        continue;
                    }
                    dtNew.Rows[HourIndex + j + 1][lineState] = System.Convert.ToInt32(dtNew.Rows[HourIndex + j + 1][lineState].ToString()) + (lineDowntime.START_TIME.Second + lineDowntime.START_TIME.Minute * 60 + stateTime) % 3600;
                }
                else
                {
                    dtNew.Rows[HourIndex][lineState] = hourlydowntime + stateTime;
                }
                #endregion
            }
            #region Get Hourly Output Data
            string strFrom, strEnd;
            OleDbUtility oleUtility = new OleDbUtility("ods", "users", "users");
            oleUtility.getConnection();
            oleUtility.OpenConnection();

            strFrom = dateTimePicker1.Value.AddDays(-1).ToString("yyyy-MM-dd") + " 07:00";
            strEnd = dateTimePicker1.Value.ToString("yyyy-MM-dd") + " 07:00";
            string strSQL = "select to_char(lca.birth_date,'yyyy-MM-dd-hh24') as hourly,count(*) as output from lca.lca_data2 lca where to_char(lca.birth_date,'yyyy-MM-dd hh24:mi') between '" + strFrom +"' and '"+ strEnd + "'  group by to_char(lca.birth_date,'yyyy-MM-dd-hh24') order by to_char(lca.birth_date,'yyyy-MM-dd-hh24')" ;

            DataTable dtOutput = new DataTable("OutputSourceData");
            dtOutput = oleUtility.SQLQuery_DataTable(strSQL);

            dtNew.Columns.Add("Output");
            for (int i = 0,j=0; i < dtNew.Rows.Count; i++)
            {
                if (i >= dtNew.Rows.Count || j >= dtOutput.Rows.Count)
                {
                    break;
                }
                else
                {
                    if (dtNew.Rows[i][0].ToString() == dtOutput.Rows[j][0].ToString())
                    {
                        dtNew.Rows[i]["Output"] = dtOutput.Rows[j][1];
                        j++;
                    }
                    else
                    {
                        dtNew.Rows[i]["Output"] = 0;
                    }
                }
            }
            //DataTable dtCombine = dataDriver.CombineDataTable(dtNew, dataDriver.LCADataSet.Tables["OutputSourceData"], "HourlyOutput");
            
            DGVlineHourly.DataSource = dtNew;
            */
            #endregion
            #region  validate table
            LCADownTimeDataDriver dataDriver = LCADownTimeDataDriver.Instance;
            if (!dataDriver.LCADataSet.Tables.Contains("LCALineDownTime") || dataDriver.LCADataSet.Tables["LCALineDownTime"].Rows.Count < 1)
            {
                MessageBox.Show("Please load Souce Data Firstly");
                return null;
            }
            dataDriver.WriteTableToList("LCALineDownTime", typeof(LCALineState).FullName);
            List<object> dataList = dataDriver.LCADataList;
            if (dataList == null || dataList.Count == 0)
            {
                MessageBox.Show("Please Load Data and LCA line State Report First");
                return null;
            }
            #endregion
            #region  initial Draw Chart setting
            TimeSpanBarChartDraw ChartDraw = TimeSpanBarChartDraw.Instance;
            ChartDraw.IniGraphics();
            ChartDraw.ChartAreaStartX = 80;
            ChartDraw.ChartAreaStartY = 80;
            ChartDraw.BarStartX = 100;
            ChartDraw.BarStartX = 100;
            #endregion
            #region Draw Series Legends
            SeriesLegend[] SeriesLegends = new SeriesLegend[LCADrawInfo.LineState_Colors().Count];
            int SereiesIndex = 0;
            foreach (KeyValuePair<String, Color> lineStateColor in LCADrawInfo.LineState_Colors())
            {
                SeriesLegends[SereiesIndex] = new SeriesLegend(SereiesIndex, lineStateColor.Key.ToString(), lineStateColor.Value);
                SereiesIndex++;
            }
            ChartDraw.DrawSeriesLengends(SeriesLegends);
            #endregion
            DataTable dtNew = new DataTable("LCALineHourlyDownTime");
            LCADownTimeRule rule = LCADownTimeRule.Instance;
            #region station column&hour row add
            dtNew.Columns.Add(LCAMachineStateToPeriod.HEADERCHAR.Period.ToString());
            foreach (LCABaseInfo.LINE_STATE lineState in (LCABaseInfo.LINE_STATE[])Enum.GetValues(typeof(LCABaseInfo.LINE_STATE)))
            {
                dtNew.Columns.Add(lineState.ToString());
            }
            for (int i = 0; i < totalHourSpan + 1; i++)
            {
                DataRow newRow = dtNew.NewRow();
                newRow[LCAMachineStateToPeriod.HEADERCHAR.Period.ToString()] = minDateTime.AddHours(i).ToString("yyyy-MM-dd-HH");
                foreach (LCABaseInfo.LINE_STATE lineState in (LCABaseInfo.LINE_STATE[])Enum.GetValues(typeof(LCABaseInfo.LINE_STATE)))
                {
                    newRow[lineState.ToString()] = 0;
                }
                dtNew.Rows.Add(newRow);
            }
            #endregion
            for (int i = 0; i < dataDriver.LCADataList.Count; i++)
            {
                LCALineState lineDowntime = (LCALineState)dataList[i];
                #region  Draw Time Span Bar
                float barStartX = (float)(lineDowntime.START_TIME.Ticks - minDateTime.Ticks) / TimeSpan.TicksPerSecond / totalSecondSpan * ChartDraw.ChartWitdh;
                float barWidth = (float)lineDowntime.TIME_OF_STATE / totalSecondSpan * ChartDraw.ChartWitdh;
                TimeSpanBar bar = new TimeSpanBar(0, barStartX, barWidth);
                bar.BarColor = LCADrawInfo.LineState_Colors()["Pause"];
                if (rule.IsLineDownTime(lineDowntime))
                {
                    bar.BarColor = LCADrawInfo.LineState_Colors()["Stop"];
                }
                if (rule.IsLineUpTime(lineDowntime))
                {
                    bar.BarColor = LCADrawInfo.LineState_Colors()["Run"];
                }
                ChartDraw.DrawBar(bar);
                #endregion

                LCAMachineStateToPeriod stateToHour = new LCAMachineStateToPeriod();
                lineDowntime.LCA_LINE = "LCA_CELL_2";
                #region  hourly downtime calculation
                int HourIndex = Convert.ToInt32((lineDowntime.START_TIME.Ticks - minDateTime.Ticks) / TimeSpan.TicksPerHour);
                String lineState = LCABaseInfo.LINE_STATE.Pause.ToString();
                if (rule.IsLineUpTime(lineDowntime))
                {
                    lineState = LCABaseInfo.LINE_STATE.Run.ToString();
                }
                if (rule.IsLineDownTime(lineDowntime))
                {
                    lineState = LCABaseInfo.LINE_STATE.Stop.ToString();
                }
                int hourlydowntime = System.Convert.ToInt32(dtNew.Rows[HourIndex][lineState].ToString());
                int stateTime = (int)lineDowntime.TIME_OF_STATE;
                if (lineDowntime.START_TIME.Second + lineDowntime.START_TIME.Minute * 60 + stateTime > 3600)
                {
                    dtNew.Rows[HourIndex][lineState] = hourlydowntime + 3600 - lineDowntime.START_TIME.Second - lineDowntime.START_TIME.Minute * 60;
                    int j = 1;
                    for (j = 0; j < (stateTime + lineDowntime.START_TIME.Second + lineDowntime.START_TIME.Minute * 60) / 3600 - 1; j++)
                    {
                        if (HourIndex + j + 1 > totalHourSpan - 1)
                        {
                            break;
                        }
                        dtNew.Rows[HourIndex + j + 1][lineState] = System.Convert.ToInt32(dtNew.Rows[HourIndex + j + 1][lineState].ToString()) + 3600;
                    }
                    if (HourIndex + j + 1 > totalHourSpan - 1)
                    {
                        continue;
                    }
                    dtNew.Rows[HourIndex + j + 1][lineState] = System.Convert.ToInt32(dtNew.Rows[HourIndex + j + 1][lineState].ToString()) + (lineDowntime.START_TIME.Second + lineDowntime.START_TIME.Minute * 60 + stateTime) % 3600;
                }
                else
                {
                    dtNew.Rows[HourIndex][lineState] = hourlydowntime + stateTime;
                }
                #endregion
            }
            #region Get Hourly Output Data
            string strFrom, strEnd;
            OleDbUtility oleUtility = new OleDbUtility("ods", "users", "users");
            oleUtility.getConnection();
            oleUtility.OpenConnection();


            strFrom = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 07:00";
            strEnd = DateTime.Now.ToString("yyyy-MM-dd") + " 07:00";

            switch (LbxShift.SelectedItem.ToString())
            {
                case "Day":
                    strFrom = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 07:00";
                    strEnd = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 19:00"; break;
                case "Night":
                    strFrom = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 19:00";
                    strEnd = DateTime.Now.ToString("yyyy-MM-dd") + " 07:00"; break;
                default:
                    strFrom = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 07:00";
                    strEnd = DateTime.Now.ToString("yyyy-MM-dd") + " 07:00"; break;
            }

            string strSQL = "select to_char(lca.birth_date,'yyyy-MM-dd-hh24') as hourly,count(*) as output from lca.lca_data2 lca where to_char(lca.birth_date,'yyyy-MM-dd hh24:mi') between '" + strFrom + "' and '" + strEnd + "'  and lca_line='"+ strCellNo + "'  group by to_char(lca.birth_date,'yyyy-MM-dd-hh24') order by to_char(lca.birth_date,'yyyy-MM-dd-hh24')";

            DataTable dtOutput = new DataTable("OutputSourceData");
            dtOutput = oleUtility.SQLQuery_DataTable(strSQL);

            dtNew.Columns.Add("Output");
            for (int i = 0, j = 0; i < dtNew.Rows.Count; i++)
            {
                if (i >= dtNew.Rows.Count || j >= dtOutput.Rows.Count)
                {
                    break;
                }
                else
                {
                    if (dtNew.Rows[i][0].ToString() == dtOutput.Rows[j][0].ToString())
                    {
                        dtNew.Rows[i]["Output"] = dtOutput.Rows[j][1];
                        j++;
                    }
                    else
                    {
                        dtNew.Rows[i]["Output"] = 0;
                    }
                }
            }

            dtOutput.Clear();
            for (int i = 0; i < dtNew.Rows.Count; i++)
            {
                DataRow dtRowOutput = dtOutput.NewRow();
                dtRowOutput[0] = dtNew.Rows[i]["Output"];
                dtOutput.Rows.Add(dtRowOutput);
            }

            //DataTable dtCombine = dataDriver.CombineDataTable(dtNew, dataDriver.LCADataSet.Tables["OutputSourceData"], "HourlyOutput");
            #endregion
            SeriesGrid lineGrid = new SeriesGrid(0, "LCA_CELL_2");
            ChartDraw.DrawSeriersGrid(lineGrid);
            #region Draw TimeSpan  Grid
            for (int i = 0; i < totalHourSpan + 2; i++)
            {
                String LabelText = (minDateTime.AddHours(i).Hour).ToString();
                TimeSpanGrid gird = new TimeSpanGrid(LabelText, (float)3600 / totalSecondSpan * ChartDraw.ChartWitdh * i, 0, 500);
                ChartDraw.DrawTimeSpanGrid(gird);
            }
            #endregion
            #region Draw Interval Chart

            switch (LbxShift.SelectedItem.ToString())
            {
                case "All Shift": strFrom = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 07:00:00"; strEnd = DateTime.Now.ToString("yyyy-MM-dd") + " 07:00:00"; break;
                case "Day": strFrom = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 07:00:00"; strEnd = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 19:00:00"; break;
                case "Night": strFrom = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 19:00:00"; strEnd = DateTime.Now.ToString("yyyy-MM-dd") + " 07:00:00"; break;
            }
            TimeSpan span = Convert.ToDateTime(strEnd) - Convert.ToDateTime(strFrom);
            //Get Interval Data
            TimeIntervalChartDraw graph = TimeIntervalChartDraw.Instance;
            #region Draw Chart Area
            IntervalChartArea chartArea = new IntervalChartArea(100, 150, 800, 430);
            graph.colorSplit = 8;
            graph.IniGraphics(ChartDraw.ChartImg);
            graph.AddChartArea(chartArea);
            #endregion
            DataTable dtSQL = new DataTable("SQLTabel");
            DataTable dtInterval = new DataTable("IntervalTabel");
            dtInterval.Columns.Add("x", typeof(int));
            dtInterval.Columns.Add("y", typeof(int));
            #region Draw Grid
            Int32 yMid = Convert.ToInt32(textMid.Text);
            Int32 yMax = Convert.ToInt32(textMax.Text);
            span = Convert.ToDateTime(strEnd) - Convert.ToDateTime(strFrom);
            LegendGrid grid = new LegendGrid(Convert.ToInt32(span.TotalHours), yMid);
            graph.AddAxis(grid);
            #endregion
            #region Get Source Data
            strSQL = "select to_char(birth_date,'yyyy-MM-dd hh24:mi:ss') as dateChar from lca.lca_data2 where to_char(birth_date,'yyyy-MM-dd hh24:mi:ss') between '" + strFrom + "' and '" + strEnd + "'  and lca_line='"+strCellNo+"'  order by birth_date";
            dtSQL = oleUtility.SQLQuery_DataTable(strSQL);
            if (dtSQL.Rows.Count == 0)
            {
                return null;
            }
            #endregion
            int intRed = 0;
            int intGreen = 0;
            int intYellow = 0;
            IntervalBar barPoint = new IntervalBar();
            DataRow newIntervalRow = dtInterval.NewRow();
            span = Convert.ToDateTime(dtSQL.Rows[0][0].ToString()) - Convert.ToDateTime(strFrom);
            newIntervalRow[0] = span.TotalSeconds;
            newIntervalRow[1] = span.TotalSeconds;
            #region Draw Point
            if (Convert.ToInt32(newIntervalRow[1]) >= yMid && Convert.ToInt32(newIntervalRow[1]) < yMax)
            {
                barPoint.BarHeight = yMid;
                barPoint.BarColor = Color.Yellow;
                intYellow++;
            }
            else if (Convert.ToInt32(newIntervalRow[1]) >= yMax)
            {
                barPoint.BarHeight = yMid;
                barPoint.BarColor = Color.Orchid;
                intRed++;
            }
            else
            {
                barPoint.BarHeight = Convert.ToInt32(newIntervalRow[1]);
                barPoint.BarColor = Color.LightSkyBlue;
                intGreen++;
            }
            barPoint.XStart = 0;
            barPoint.YStart = barPoint.BarHeight;
            barPoint.BarWidth = Convert.ToInt32(newIntervalRow[1]);

            graph.DrawBarPoint(barPoint);
            dtInterval.Rows.Add(newIntervalRow);

            for (int i = 1; i < dtSQL.Rows.Count; i++)
            {

                newIntervalRow = dtInterval.NewRow();
                span = Convert.ToDateTime(dtSQL.Rows[i][0].ToString()) - Convert.ToDateTime(strFrom);
                newIntervalRow[0] = span.TotalSeconds;
                newIntervalRow[1] = span.TotalSeconds - Convert.ToInt32(dtInterval.Rows[i - 1][0].ToString());


                if (Convert.ToInt32(newIntervalRow[1]) >= yMid && Convert.ToInt32(newIntervalRow[1]) < yMax)
                {
                    barPoint.BarHeight = yMid;
                    barPoint.BarColor = Color.Yellow;
                    intYellow++;
                }
                else if (Convert.ToInt32(newIntervalRow[1]) >= yMax)
                {
                    barPoint.BarHeight = yMid;
                    barPoint.BarColor = Color.Orchid;
                    intRed++;
                }
                else
                {
                    barPoint.BarHeight = Convert.ToInt32(newIntervalRow[1]);
                    barPoint.BarColor = Color.LightSkyBlue;
                    intGreen++;
                }

                barPoint.YStart = barPoint.BarHeight;
                barPoint.BarWidth = Convert.ToInt32(newIntervalRow[1]);
                barPoint.XStart = Convert.ToInt32(newIntervalRow[0]) - Convert.ToInt32(newIntervalRow[1]);

                graph.DrawBarPoint(barPoint);
                dtInterval.Rows.Add(newIntervalRow);

            }
            #endregion
            //graph.AddSeries(dtInterval);

            graph.AddCountLabel(intRed, intGreen, intYellow);
            graph.DrawStartLabel(dtSQL.Rows[0][0].ToString());
            graph.DrawEndLabel(dtSQL.Rows[dtSQL.Rows.Count - 1][0].ToString());
            #endregion
            String fileName = "";
            switch (LbxShift.SelectedItem.ToString())
            {
                case "All Shift":
                    fileName = "LineDT" + startDateTime.ToString("yyyy-MM-dd"); break;
                case "Day":
                    fileName = "LineDT" + startDateTime.ToString("yyyy-MM-dd") + "M"; break;
                case "Night":
                    fileName = "LineDT" + startDateTime.ToString("yyyy-MM-dd") + "N"; break;
            }
            DGVlineHourly.DataSource = dtNew;
            buManualImport.Enabled = true;
            //dataDriver.WriteDataToFile(dtNew, fileName);
            tabControl1.SelectedTab = TPLineHourly;
            //ChartDraw.saveImg(fileName);
            //ChartForm newForm = new ChartForm();
            //newForm.Text = ChartDraw.ImgFileName;
            //newForm.Height = ChartDraw.ChartHeight;
            //newForm.Width = ChartDraw.ChartWitdh;
            //newForm.BackgroundImage = ChartDraw.ChartImg;
            //newForm.BackgroundImageLayout = ImageLayout.Stretch;

            graph.saveImg(fileName);
            graph.saveWebImg(fileName);
            ChartForm newForm = new ChartForm();
            newForm.Text = graph.ImgFileName;
            newForm.Height = ChartDraw.ChartHeight;
            newForm.Width = ChartDraw.ChartWitdh;
            newForm.BackgroundImage = graph.ChartImg;
            newForm.BackgroundImageLayout = ImageLayout.Stretch;

            return dtOutput;
            //newForm.Show();
        }