MS Chart not showing all the series values at same time ( for the same X value)

StackOverflow https://stackoverflow.com/questions/21669046

  •  09-10-2022
  •  | 
  •  

سؤال

Apologies for not able to find a more suitable title for my issue here. I am using MS Chart control in my project to plot real-time values from a source that generates 4 values per second. X axis is time at which the value is generated and the Y axis the value itself. I am dynamically adding a new series for each variable. All the variables are manually added by the user one-by-one and then a series will be created for it.

Basic part seems to be working except the chart is plotting values from different series at different time even though the values are generated at same time. Following image shows the exact issue:

ms-chart issue

As you can see, the list in bottom is a list of values I am plotting. It shows values and the time they are generated at (hh:mm:ss.ms)

When I plot these values, which is shown in the chart area, this is what I get. See the location of the latest values (marked by red arrows) for all the series. The latest value is at different time even though they all are generated at the same time. Every series seem to have a separate X axis. I expected the latest Y values should be plotted at the same X value and that should be shown at the extreme right of the chart. More like the bottom most series is being shown. That is the very first series created. Every time only the series that is created first works fine and the following series lags for some reason.

Chart, share X axis between different series looks like similar issue to mine but is not answered.

Chart Multiple series different length generate duplicate x axis seems to be the exact issue I am having but the difference is - I am plotting a real-time graph. I can't loop through every series after adding a new data point to mark whether a point is empty or not. That will be a lot of overhead. Some Googling suggested use of InsertEmptyPoints() to every series to match all the X axis. MS Chart sample code also explains using this method but I am not sure where/when to use this method. If I use it after creating a series but before adding a data point - the code crashes. I have tried something like this:

_chart.Series.Add(series);
_chart.DataManipulator.InsertEmptyPoints(250, IntervalType.Milliseconds, name);

Am I doing something wrong here? Thanks for any help offered.

هل كانت مفيدة؟

المحلول

I got this working by adding empty points. I filled every series with max possible data points to begin with. Say I want to show 100 data points in every series before I start deleting the oldest (Points[0]) data point so that my chart will look like it's moving from right to left. In that case I will add 100 empty points while creating every series using:

            for (int i = 0; i < 100; i++)
            {
                series.Points.AddXY(i.ToString(), double.NaN);
                series.Points[i].AxisLabel = " ";
            }

Then, while adding real data just do it normal way:

            _chart.Series[index].Points.AddXY(time, YValue);                        

           // delete more than max. points. 
           if (_chart.Series[index].Points.Count > MaxTrendPoints - 10) // -10 is adjustment. Got it by trial and error.
                 _chart.Series[index].Points.RemoveAt(0);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top