Пример #1
0
        /// <summary>
        /// Adds a single data point to the chart
        /// </summary>
        /// <param name="year">X Value - Year being recorded</param>
        /// <param name="numAlive">Y Value - Number of people alive</param>
        public void AddDataPointToChart(YearStatistic stat)
        {
            DataPoint dataPoint = new DataPoint();

            dataPoint.AxisLabel = stat.Year.ToString();
            dataPoint.Label     = stat.NumPeopleAlive.ToString();
            dataPoint.XValue    = stat.Year;
            dataPoint.YValues   = new double[] { stat.NumPeopleAlive };
            chart.Series[0].Points.Add(dataPoint);
        }
Пример #2
0
        /// <summary>
        /// Finds the year(s) with the most people alive and adds a data point to the chart for each year evaluated
        /// </summary>
        /// <param name="startYear"></param>
        /// <param name="endYear"></param>
        /// <param name="people"></param>
        /// <returns>The year(s) with the most people alive</returns>
        public List <YearStatistic> FindYearsWithMostPeopleAlive(int startYear, int endYear, XmlNodeList people)
        {
            List <YearStatistic> bestYears = new List <YearStatistic>();
            int numAliveDuringBestYears    = 0;

            for (int year = startYear; year < endYear; year++)
            {
                int numAlive = 0;
                foreach (XmlNode person in people)
                {
                    int    birthYear = Int32.Parse(person.SelectSingleNode("BirthYear").InnerText);
                    int    deathYear = Int32.Parse(person.SelectSingleNode("DeathYear").InnerText);
                    Person p         = new Person(birthYear, deathYear);
                    if (p.WasAliveDuringYear(year))
                    {
                        numAlive++;
                    }
                }

                // If the year being evaluated has the same number of people alive as other best year(s),
                // add it to the list to keep track of all years. If it has more people alive than other best
                // years, clear the list and start it over with the new best year.
                YearStatistic stat = new YearStatistic(year, numAlive);
                if (numAlive == numAliveDuringBestYears)
                {
                    bestYears.Add(stat);
                }
                else if (numAlive > numAliveDuringBestYears)
                {
                    bestYears.Clear();
                    bestYears.Add(stat);
                    numAliveDuringBestYears = numAlive;
                }
                AddDataPointToChart(stat);
            }

            return(bestYears);
        }