/*
            Member:     ChartPopulation
            Purpose:    This function charts a population graph given a start date and an end
            data along with a list of species to chart side by side.  This graph is useful for
            comparing success between different species.
        */
        public static string ChartPopulation(DateTime beginTime, DateTime endTime, DataSet included)
        {
            DataSet includedData = included;
            DateTime begin = beginTime;
            DateTime end = endTime;
            ChartControl chartControl = new ChartControl ();
            ChartControl.PhysicalPath = ServerSettings.ChartPath + "\\";
            ChartControl.VirtualPath = "/Terrarium/ChartData";

            if ( includedData != null )
            {
                SqlConnection myConnection = new SqlConnection(ServerSettings.SpeciesDsn);

                myConnection.Open();

                int index=0;
                ChartPointCollection[] data=new ChartPointCollection[12];
                Chart[] charts=new Chart[12];
                chartControl.Charts.Clear();

                long beginTicks = beginTime.Ticks;
                long totalTicks = endTime.Ticks - beginTicks;
                float[] buckets = new float[24];

                foreach(DataRow item in includedData.Tables[0].Rows)
                {
                    SqlCommand command = new SqlCommand("TerrariumGrabSpeciesDataInDateRange", myConnection);
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add("@SpeciesName", SqlDbType.VarChar, 50).Value = item["SpeciesName"];
                    command.Parameters.Add("@BeginDate", SqlDbType.DateTime, 8).Value = begin;
                    command.Parameters.Add("@EndDate", SqlDbType.DateTime, 8).Value = end;

                    SqlDataReader dr = command.ExecuteReader();
                    data[index] = new ChartPointCollection();

                    while(dr.Read())
                    {
                        TimeSpan offset = endTime - ((DateTime) dr["SampleDateTime"]);
                        int bucket = 24 - (int) Math.Round(offset.TotalHours);

                        if ( bucket > -1 && bucket < buckets.Length ) {
                            buckets[bucket] = (float) (int) dr["Population"];
                        }
                    }

                    dr.Close();

                    for(int i = 0; i < buckets.Length; i++)
                    {
                        data[index].Add(new ChartPoint(i.ToString(), buckets[i]));
                        buckets[i] = 0;
                    }

                    charts[index] = new SmoothLineChart(data[index], Color.Black );
                    charts[index].LineMarker = new CircleLineMarker(0,charts[index].Fill.Color ,charts[index].Fill.Color );
                    charts[index].ShowLineMarkers = false;
                    charts[index].DataLabels.ShowValue = false;
                    charts[index].Legend = item["SpeciesName"].ToString();
                    charts[index].Fill.Color = colors[index];
                    charts[index].Line.Color = colors[index];
                    charts[index].Line.Width = 2;

                    chartControl.Charts.Add(charts[index]);
                    index++;
                }
            }

            chartControl.Width = 640;
            chartControl.Height = 480;
            chartControl.ChartPadding = 40;
            chartControl.TopPadding = 32;
            chartControl.Border = new ChartLine( Color.Black );

            chartControl.Background = new ChartInterior( Color.FromArgb( 216, 216, 216 ) );

            chartControl.GridLines = GridLines.Horizontal;
            chartControl.Legend.Position = LegendPosition.Right;

            chartControl.ChartTitle = new ChartText();
            chartControl.ChartTitle.Text = "Population (24 hours starting " + begin.ToString() + ")";
            chartControl.ChartTitle.Font = new Font( "Verdana", 10.0f, FontStyle.Bold );
            chartControl.ShowXValues = false;
            chartControl.XTicksInterval = 4;
            chartControl.YCustomStart = 1;
            chartControl.RedrawChart();

            return "~/ChartData/" + chartControl.ImageID + ".png";
        }
Example #2
0
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SmoothLineChart lcTBudget;
            DataRow         drownew;
            DataTable       tblOSIReport = new DataTable();
            tblOSIReport.Columns.Add("month");
            tblOSIReport.Columns.Add("tamount");
            chaBudCon.ChartTitle.Text = Request.QueryString["division"] + " Office Supplies Budget Consumption FY: 2007-2008 [As of " + DateTime.Now.ToString("MMMM dd, yyyy") + "]";

            double     dblHighest    = 0;
            int        intCtr        = 0;
            int        intDiviCtr    = 0;
            double[]   dblTotal      = new double[12];
            double[]   dblMonthTotal = new double[12];
            string[]   strMonth      = new string[] { "April", "May", "June", "July", "August", "September", "October", "November", "December", "January", "February", "March" };
            DateTime[] dteFrom       = new DateTime[] { Convert.ToDateTime("4/1/2007"), Convert.ToDateTime("5/1/2007"), Convert.ToDateTime("6/1/2007"), Convert.ToDateTime("7/1/2007"), Convert.ToDateTime("8/1/2007"), Convert.ToDateTime("9/1/2007"), Convert.ToDateTime("10/1/2007"), Convert.ToDateTime("11/1/2007"), Convert.ToDateTime("12/1/2007"), Convert.ToDateTime("1/1/2008"), Convert.ToDateTime("2/1/2008"), Convert.ToDateTime("3/1/2008") };
            DateTime[] dteTo         = new DateTime[] { Convert.ToDateTime("4/30/2007"), Convert.ToDateTime("5/31/2007"), Convert.ToDateTime("6/30/2007"), Convert.ToDateTime("7/31/2007"), Convert.ToDateTime("8/31/2007"), Convert.ToDateTime("9/30/2007"), Convert.ToDateTime("10/31/2007"), Convert.ToDateTime("11/30/2007"), Convert.ToDateTime("12/31/2007"), Convert.ToDateTime("1/31/2008"), Convert.ToDateTime("2/29/2008"), Convert.ToDateTime("3/31/2008") };
            Color[]    clrDivision   = new Color[] { Color.Blue, Color.Yellow, Color.Purple, Color.DarkGreen, Color.Orange, Color.DarkBlue, Color.Brown, Color.Pink };

            DataTable tblRC = new DataTable();
            using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["GreatPlains"].ToString()))
            {
                SqlCommand cmd = cn.CreateCommand();
                cn.Open();
                SqlDataReader dr;

                cmd.CommandText = "SELECT custnmbr,custname FROM rm00101 WHERE rm00101.custclas='HQ DEPARTMENT' AND rm00101.custnmbr IN ('" + clsRC.GetSQLInClauseGP(Request.QueryString["divicode"]) + "') ORDER BY custname";
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(tblRC);

                foreach (DataRow drow in tblRC.Rows)
                {
                    for (intCtr = 0; intCtr <= 11; intCtr++)
                    {
                        dblMonthTotal[intCtr] = 0;
                    }

                    for (intCtr = 0; intCtr <= 11; intCtr++)
                    {
                        cmd.CommandText = "SELECT SUM(sop30200.docamnt) AS pTotal FROM sop30200 INNER JOIN rm00101 ON rm00101.custnmbr = sop30200.custnmbr WHERE rm00101.custclas='HQ DEPARTMENT' AND (left(sop30200.sopnumbe,3)='OSD') AND rm00101.custnmbr='" + drow["custnmbr"] + "' AND sop30200.docdate BETWEEN '" + dteFrom[intCtr] + "' AND '" + dteTo[intCtr] + "'";
                        dr = cmd.ExecuteReader();
                        if (dr.Read())
                        {
                            dblMonthTotal[intCtr] = dblMonthTotal[intCtr] + (Convert.IsDBNull(dr["pTotal"].ToString()) || dr["pTotal"].ToString() == "" ? 0 : Convert.ToDouble(dr["pTotal"].ToString()));
                            dblTotal[intCtr]      = dblTotal[intCtr] + dblMonthTotal[intCtr];
                        }
                        dr.Close();
                    }

                    for (intCtr = 0; intCtr <= 11; intCtr++)
                    {
                        drownew            = tblOSIReport.NewRow();
                        drownew["month"]   = strMonth[intCtr];
                        drownew["tamount"] = dblMonthTotal[intCtr];
                        tblOSIReport.Rows.Add(drownew);
                    }

                    lcTBudget                 = new SmoothLineChart();
                    lcTBudget.Line.Width      = 2;
                    lcTBudget.Line.Color      = clrDivision[intDiviCtr];
                    lcTBudget.Legend          = drow["custname"].ToString();
                    lcTBudget.DataSource      = tblOSIReport.DefaultView;
                    lcTBudget.DataXValueField = "month";
                    lcTBudget.DataYValueField = "tamount";
                    lcTBudget.DataBind();
                    chaBudCon.Charts.Add(lcTBudget);

                    tblOSIReport.Rows.Clear();
                    intDiviCtr++;
                }

                for (intCtr = 0; intCtr <= 11; intCtr++)
                {
                    drownew            = tblOSIReport.NewRow();
                    drownew["month"]   = strMonth[intCtr];
                    drownew["tamount"] = dblTotal[intCtr];
                    tblOSIReport.Rows.Add(drownew);
                    if (dblHighest == 0)
                    {
                        dblHighest = dblTotal[intCtr];
                    }
                    else
                    if (dblTotal[intCtr] > dblHighest)
                    {
                        dblHighest = dblTotal[intCtr];
                    }
                }

                lcTBudget                             = new SmoothLineChart();
                lcTBudget.Line.Width                  = 2;
                lcTBudget.Line.Color                  = Color.Red;
                lcTBudget.DataLabels.Visible          = true;
                lcTBudget.DataLabels.Background.Color = System.Drawing.Color.White;
                lcTBudget.Legend                      = Request.QueryString["division"];
                lcTBudget.DataSource                  = tblOSIReport.DefaultView;
                lcTBudget.DataXValueField             = "month";
                lcTBudget.DataYValueField             = "tamount";
                lcTBudget.DataBind();
                chaBudCon.Charts.Add(lcTBudget);
            }

            chaBudCon.YCustomEnd      = Convert.ToInt32(dblHighest) + 1000;
            chaBudCon.YValuesInterval = Convert.ToInt32((dblHighest + 1000) / 10);
            chaBudCon.RedrawChart();
        }
    }
        /*
            Member:     ChartVitals
            Purpose:    This function charts all of the vital statistics between a given
            start and end data for a single species.  This is useful for determining the
            effectiveness of your creatures code.
        */
        public static string ChartVitals(DateTime beginTime, DateTime endTime, string species)
        {
            ChartControl chartControl = new ChartControl ();
            ChartControl.PhysicalPath = ServerSettings.ChartPath + "\\";
            ChartControl.VirtualPath = "/Terrarium/ChartData";

            ChartPointCollection[] data=new ChartPointCollection[8];
            Chart[] charts=new Chart[8];

            DateTime begin = beginTime;
            DateTime end = endTime;

            SqlConnection myConnection = new SqlConnection(ServerSettings.SpeciesDsn);

            myConnection.Open();

            long bottomTicks = begin.Ticks;
            long totalTicks = end.Ticks - begin.Ticks;

            SqlCommand command = new SqlCommand("TerrariumGrabSpeciesDataInDateRange", myConnection);
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add("@SpeciesName", SqlDbType.VarChar, 50).Value = species;
            command.Parameters.Add("@BeginDate", SqlDbType.DateTime, 8).Value = begin;
            command.Parameters.Add("@EndDate", SqlDbType.DateTime, 8).Value = end;

            for(int i = 0; i < data.Length; i++)
            {
                data[i] = new ChartPointCollection();
            }

            float[,] buckets = new float[8,24];
            for(int i = 0; i < 8; i++) {
                for(int j = 0; j < 24; j++) {
                    buckets[i, j] = -1;
                }
            }

            using(SqlDataReader dr = command.ExecuteReader())
            {
                while(dr.Read())
                {
                    TimeSpan offset = endTime - ((DateTime) dr["SampleDateTime"]);
                    int bucket = 24 - (int) Math.Round(offset.TotalHours);

                    if ( bucket > -1 && bucket <= buckets.GetUpperBound(1) ) {
                        buckets[0, bucket] = (float) (int) dr["BirthCount"];
                        buckets[1, bucket] = (float) (int) dr["StarvedCount"];
                        buckets[2, bucket] = (float) (int) dr["KilledCount"];
                        buckets[3, bucket] = (float) (int) dr["ErrorCount"];
                        buckets[4, bucket] = (float) (int) dr["TimeoutCount"];
                        buckets[5, bucket] = (float) (int) dr["SickCount"];
                        buckets[6, bucket] = (float) (int) dr["OldAgeCount"];
                        buckets[7, bucket] = (float) (int) dr["SecurityViolationCount"];
                    }

                    /*
                    long recordTicks = ((DateTime) dr["SampleDateTime"]).Ticks;
                    float normalizedSampleTime = (float) ((double) (recordTicks - bottomTicks) / (double) (totalTicks / 24));
                    int ibirth = (int) dr["BirthCount"];
                    int istarve = (int) dr["StarvedCount"];
                    int ikilled = (int) dr["KilledCount"];
                    int ierror = (int) dr["ErrorCount"];
                    int itimeout = (int) dr["TimeoutCount"];
                    int isick = (int) dr["SickCount"];
                    int iold = (int) dr["OldAgeCount"];
                    int isecurity = (int) dr["SecurityViolationCount"];

                    data[0].Add(new ChartPoint(normalizedSampleTime.ToString(), (float) ibirth));
                    data[1].Add(new ChartPoint(normalizedSampleTime.ToString(), (float) istarve));
                    data[2].Add(new ChartPoint(normalizedSampleTime.ToString(), (float) ikilled));
                    data[3].Add(new ChartPoint(normalizedSampleTime.ToString(), (float) ierror));
                    data[4].Add(new ChartPoint(normalizedSampleTime.ToString(), (float) itimeout));
                    data[5].Add(new ChartPoint(normalizedSampleTime.ToString(), (float) isick));
                    data[6].Add(new ChartPoint(normalizedSampleTime.ToString(), (float) iold));
                    data[7].Add(new ChartPoint(normalizedSampleTime.ToString(), (float) isecurity));
                    */
                }
                dr.Close();
            }

            string[] names = new string[] { "Birth", "Starved", "Killed", "Errors", "Timed Out", "Sick", "Old Age", "Security Exception" };

            for(int i = 0; i < 24; i++)
            {
                data[0].Add(new ChartPoint(i.ToString(), buckets[0, i]));
                data[1].Add(new ChartPoint(i.ToString(), buckets[1, i]));
                data[2].Add(new ChartPoint(i.ToString(), buckets[2, i]));
                data[3].Add(new ChartPoint(i.ToString(), buckets[3, i]));
                data[4].Add(new ChartPoint(i.ToString(), buckets[4, i]));
                data[5].Add(new ChartPoint(i.ToString(), buckets[5, i]));
                data[6].Add(new ChartPoint(i.ToString(), buckets[6, i]));
                data[7].Add(new ChartPoint(i.ToString(), buckets[7, i]));
            }

            for(int i = 0; i < charts.Length; i++) {
                charts[i] = new SmoothLineChart(data[i], colors[i]);
                charts[i].Legend = names[i];
                charts[i].Fill.Color = colors[i];
                charts[i].Line.Color = colors[i];
                charts[i].ShowLineMarkers = false;
                charts[i].DataLabels.ShowValue = false;
                charts[i].Line.Width = 2;
                chartControl.Charts.Add(charts[i]);
            }

            chartControl.Width = 640;
            chartControl.Height = 480;
            chartControl.ChartPadding = 40;
            chartControl.TopPadding = 32;
            chartControl.Border = new ChartLine( Color.Black );
            chartControl.Background = new ChartInterior( Color.FromArgb( 216, 216, 216 ) );
            chartControl.GridLines = GridLines.Horizontal;
            chartControl.Legend.Position = LegendPosition.Right;

            chartControl.ChartTitle = new ChartText();
            chartControl.ChartTitle.Text = "Vitals (24 hours starting " + begin.ToString() + ")";
            chartControl.ChartTitle.Font = new Font( "Verdana", 10.0f, FontStyle.Bold );
            chartControl.ShowXValues = false;
            chartControl.XTicksInterval = 4;
            chartControl.RedrawChart();

            return "~/ChartData/" + chartControl.ImageID + ".png";
        }