public void draw(ArcSWAT.SWATUnitColumnYearResult result, ArcSWAT.SeasonType season)
        {
            setup();
            clear();

            if (result.ColumnDisplay.Equals("Flow")) return; //don't display for flow

            //scatter
            DataTable dt = result.CompareWithObserved.SeasonTableForStatistics(season); //already removed missing value

            string col_observed = result.CompareWithObserved.ChartColumns[1];
            string col_simulated = result.CompareWithObserved.ChartColumns[0];
            _dt = dt.Copy();
            foreach (DataRow r in _dt.Rows)
            {
                double observed = double.Parse(r[col_observed].ToString());
                double simulated = double.Parse(r[col_simulated].ToString());

                if (observed > 0) r[col_observed] = Math.Log(observed);
                if (simulated > 0) r[col_simulated] = Math.Log(simulated);
            }
            _scatter.XValueMember = col_observed;
            _scatter.YValueMembers = col_simulated;
            this.DataSource = _dt.Rows;

            _xColName = col_observed;
            _yColName = col_simulated;

            //1:1 line
            double max = Compute(_dt, "Max(" + col_observed + ")", "");
            max = Math.Max(max,Compute(_dt, "Max(" + col_simulated + ")", ""));
            double round = Math.Round(max);
            if (round < max) round += 1;
            max = round;

            _line.Points.Add(new DataPoint(0, 0));
            _line.Points.Add(new DataPoint(max, max));

            //set x,y max
            _chartArea.AxisX.Maximum = max;
            _chartArea.AxisY.Maximum = max;
            _chartArea.AxisX.Minimum = 0;
            _chartArea.AxisY.Minimum = 0;

            //
            if (result.IsFlow)
            {
                _chartArea.AxisX.Title = string.Format("Ln({0} observed ({1}))", result.ColumnDisplay, result.Unit);
                _chartArea.AxisY.Title = string.Format("Ln({0} simulated ({1}))", result.ColumnDisplay, result.Unit);
            }
            else //loading
            {
                string interval = "day";
                if (result.UnitResult.Interval == SWATResultIntervalType.MONTHLY) interval = "month";
                else if (result.UnitResult.Interval == SWATResultIntervalType.YEARLY) interval = "year";
                _chartArea.AxisX.Title = string.Format("Ln({0} observed ({1}/{2}))", result.ColumnDisplay, result.Unit, interval);
                _chartArea.AxisY.Title = string.Format("Ln({0} simulated ({1}/{2}))", result.ColumnDisplay, result.Unit, interval);
            }
        }
        private ArcSWAT.SWATUnitColumnYearResult getResult(ArcSWAT.ScenarioResult result)
        {
            if (_unitType == ArcSWAT.SWATUnitType.UNKNOWN) return null;

            //get the unit
            ArcSWAT.SWATUnit unit = result.getSWATUnit(_unitType, _id);
            if (unit == null) return null;

            //get unit results
            foreach (ArcSWAT.SWATUnitResult unitResult in unit.Results.Values)
            {
                ArcSWAT.SWATUnitColumnYearResult r = unitResult.getResult(_col, -1);
                if (r != null) return r;
            }
            return null;
        }
        private void setChartArea(DataTable dt, ArcSWAT.SWATResultIntervalType interval)
        {
            if (interval == ArcSWAT.SWATResultIntervalType.MONTHLY) //monthly
            {
                _chartArea.AxisX.Title = "Time (monthly)";
                if (dt.Rows.Count == 12) //for one year
                {
                    _chartArea.AxisX.LabelStyle.Format = "yyyy/MM";
                    _chartArea.AxisX.LabelStyle.Angle = 0;

                    _chartArea.AxisX.MajorTickMark.Interval = 1;
                    _chartArea.AxisX.MajorTickMark.IntervalType = DateTimeIntervalType.Months;
                }
                else
                {
                    _chartArea.AxisX.LabelStyle.Format = "yyyy";
                    _chartArea.AxisX.LabelStyle.Angle = 0;

                    _chartArea.AxisX.MajorTickMark.Interval = 1; //half a year
                    _chartArea.AxisX.MajorTickMark.IntervalType = DateTimeIntervalType.Years;
                }
            }
            else if (interval == ArcSWAT.SWATResultIntervalType.DAILY) //daily
            {
                _chartArea.AxisX.Title = "Time (daily)";
                _chartArea.AxisX.LabelStyle.Format = "yyyy-MM-dd";
                //_chartArea.AxisX.LabelStyle.Angle = -45;

                if (dt.Rows.Count == 365 || dt.Rows.Count == 366) //for one year
                {
                    _chartArea.AxisX.MajorTickMark.Interval = 1;
                    _chartArea.AxisX.MajorTickMark.IntervalType = DateTimeIntervalType.Months;
                }
                else//all year
                {
                    _chartArea.AxisX.MajorTickMark.Interval = 1; //half a year
                    _chartArea.AxisX.MajorTickMark.IntervalType = DateTimeIntervalType.Years;
                }
            }
            else //yearly
            {
                _chartArea.AxisX.Title = "Year";
                _chartArea.AxisX.LabelStyle.Format = "yyyy";
                _chartArea.AxisX.LabelStyle.Angle = 0;

                _chartArea.AxisX.MajorTickMark.Interval = 1; //half a year
                _chartArea.AxisX.MajorTickMark.IntervalType = DateTimeIntervalType.Years;
            }
        }
        private void DrawGraph(DataTable dt, string xColName, StringCollection yColNames, ArcSWAT.SWATResultIntervalType interval)
        {
            if (_chartArea == null)
            {
                this.ChartAreas.Clear();
                this.Series.Clear();
                this.Titles.Clear();

                _chartArea = this.ChartAreas.Add("chart_area");
                _chartArea.AxisY.Title = "y";
                _chartArea.AxisX.MajorGrid.Enabled = false;
                _chartArea.AxisY.MajorGrid.Enabled = false;
                _chartArea.AxisX.MajorTickMark.TickMarkStyle = TickMarkStyle.AcrossAxis;

                //context menu
                System.Windows.Forms.ToolStripMenuItem exportMenu =
                    new System.Windows.Forms.ToolStripMenuItem("Export current results to CSV");
                exportMenu.Click += (ss, _e) => { export(); };

                this.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip();
                this.ContextMenuStrip.Items.Add(exportMenu);
            }

            clear();

            if (dt.Rows == null || dt.Rows.Count == 0) return;
            if(yColNames == null || yColNames.Count == 0) return;
            if (interval == ArcSWAT.SWATResultIntervalType.UNKNOWN) return;

            this.DataSource = dt.Rows;
            if(yColNames.Count == 1)
                _chartArea.AxisY.Title = yColNames[0];

            int index = 0;
            foreach (string yColName in yColNames)
            {
                Series line = getLine(index);
                line.XValueMember = xColName;
                line.YValueMembers = yColName;
                line.LegendText = yColName;

                if (interval == ArcSWAT.SWATResultIntervalType.MONTHLY) //monthly
                {
                    line.ToolTip = "#VALY{F4}(#VALX{yyyy/MM})";
                    //if (rows.Length == 12) //for one year
                    //    line.IsValueShownAsLabel = true;
                    //else
                    //     line.IsValueShownAsLabel = false;
                }
                else if (interval == ArcSWAT.SWATResultIntervalType.DAILY) //daily
                {
                    line.ToolTip = "#VALY{F4}(#VALX{yyyy-MM-dd})";
                    //line.IsValueShownAsLabel = false;
                }
                else //yearly
                {
                    line.ToolTip = "#VALY{F4}(#VALX{yyyy})";
                }
                if (yColNames.Count > 1)
                    line.ToolTip = yColName + ":" + line.ToolTip;

                if (index == 0)
                    line.Color = System.Drawing.Color.Red;
                else if (yColName.ToLower().Contains("observed")) //the observed data is always green
                    line.Color = System.Drawing.Color.Green;
                else
                    line.Color = System.Drawing.Color.Blue;

                index++;
            }
            setChartArea(dt, interval);

            _dt = dt;
            _xColName = xColName;
            _yColNames = yColNames;

            this.DataBind();
        }
        /// <summary>
        /// Retrieve difference data table between two scenarios
        /// </summary>
        /// <param name="type"></param>
        /// <param name="resultType"></param>
        /// <param name="col"></param>
        /// <param name="compareScenario"></param>
        /// <returns></returns>
        public DataTable getDifference(ArcSWAT.SWATUnitType type, string resultType, string col,
            ArcSWAT.ScenarioResult compareScenario, System.ComponentModel.BackgroundWorker worker = null)
        {
            string tableId = string.Format("{0}_{1}_{2}_{3}_{4}", type, resultType, col,
                compareScenario.ModelType, compareScenario.Scenario.Name);

            if (!_differenceDataset.Tables.Contains(tableId))
            {
                List<int> ids = getSWATUnitIDs(type);

                DataTable dt = new System.Data.DataTable(tableId);
                dt.Columns.Add("ID", typeof(int));
                dt.Columns.Add("R2", typeof(double));
                foreach (int id in ids)
                {
                    if (worker != null)
                        worker.ReportProgress(0, string.Format("{0}:{1}",type,id));

                    ArcSWAT.SWATUnit unit = getSWATUnit(type, id);
                    if (unit == null) continue;

                    ArcSWAT.SWATUnitResult unitResult = unit.getResult(resultType);
                    if (unitResult == null) continue;

                    ArcSWAT.SWATUnitColumnYearResult oneUnitResult = unitResult.getResult(col, -1);
                    if (oneUnitResult == null) continue;

                    try
                    {
                        DataRow r = dt.NewRow();
                        r[0] = id;
                        r[1] = oneUnitResult.Compare(compareScenario).Statistics.Statistic("", StatisticCompareType.R2);
                        dt.Rows.Add(r);
                    }
                    catch (System.Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine(e.Message);
                    }
                }
                _differenceDataset.Tables.Add(dt);
            }
            return _differenceDataset.Tables[tableId];
        }