Problem in using List as EnumerableDataSource

Mar 10, 2011 at 8:07 AM

Hi,

    I want to plot a simple chart graph. Here I have to draw the graph which is growing dynamically ( Please note - I have to draw all the available Points; not just limited number of points like the most of the examples here in sample application). X axis => Time, Y axis a random value (between -250 to 250)

    So I created a class inherited from List (Look like this - public class PerformanceData : List<PerformanceInfo>) where I add values dynamically. But doesnot seems to be working. But I wrote a similar class ( public class PerformanceData1 : RingArray<PerformanceInfo>) and it works fine. Can any one please tell me what went wrong here? Or shouldn't I use List for this purpose?

1. Code which is working

public class PerformanceData1 : RingArray<PerformanceInfo>
    {
        private readonly PerformanceCounter counter;
        public PerformanceData1(string categoryName, string counterName) : this(new PerformanceCounter(categoryName, counterName)) { }

        public PerformanceData1(PerformanceCounter counter)
            : base(200)
        {
            if (counter == null)
                throw new ArgumentNullException("counter");

            this.counter = counter;
            timer.Tick += OnTimerTick;
            timer.Start();
        }

        private void OnTimerTick(object sender, EventArgs e)
        {
            Random rnd = new Random();
            this.Add(new PerformanceInfo(DateTime.Now, rnd.Next(-250, 250)));
        }

        private readonly DispatcherTimer timer = new DispatcherTimer();

}

 

2. Code I am trying which is not working

   public class PerformanceData : List<PerformanceInfo>
    {
        private readonly DispatcherTimer timer;
        List<PerformanceInfo> Data;
        public PerformanceData()
        {
            Data = new List<PerformanceInfo>();
            timer = new DispatcherTimer();
            timer.Tick +=new EventHandler(timer_Tick);
            timer.Start();
        }

        public List<PerformanceInfo> GetData()
        {
            return this;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            Random rnd = new Random();
            this.Add(new PerformanceInfo(DateTime.Now, rnd.Next(-250, 250)));
        }
    }

 

The main Code

 

            //PerformanceData data = new PerformanceData();           // THE NON WORKING ONE

            PerformanceData1 data = new PerformanceData1((new PerformanceCounter()));  //THE WORKING CODE from the codeplex sample codes....


            var ds = new EnumerableDataSource<PerformanceInfo>(data);
            ds.SetXMapping(pi => pi.Time.TimeOfDay.TotalSeconds);
            ds.SetYMapping(pi => pi.Value);

            LineGraph chart = plotter.AddLineGraph(ds, 2.0, String.Format("{0} - {1}", categoryName, counterName));
            return chart;

 

Question:

      Please help to make the non working code working Section (1). Kindly advise where it went wrong. Can't we use List for 'EnumerableDataSource'?