Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
20
XamDataChart AutoRefresh
posted

Hi,

I'm Building a demo App with the XamDataChart, I have 32 LineSeries linked to observablecollections and refreshing data each 100 ms with 15 new DataPoints per Serie, with the databiding, the updates are so fast and the chart do not render properly, I read an XamChart post with a RefreshEnabled property, the XamDataChart have a property similar/equivalent to perform refresh of the chart manually.

Thank you in advance.

Parents Reply
  • 30692
    Offline posted in reply to Graham Murray

    Here's an alternate version that uses the Reactive Extensions for .NET to generate/buffer the data and reduce the number of refreshes:

    public partial class MainPage 
            : UserControl, INotifyPropertyChanged
        {
            private ObservableCollection<TestDataItem> _liveData =
                new ObservableCollection<TestDataItem>();
    
            public MainPage()
            {
                InitializeComponent();
    
                DataContext = this;
                Data = new ManualResetProxy<TestDataItem>(_liveData);
                
                Random rand = new Random();
                var liveValues = Observable.GenerateWithTime(
                    5.0, (v) => true,
                    (v) => new TestDataItem 
                    { 
                        Label = DateTime.Now, 
                        Value = v 
                    },
                    (v) => TimeSpan.FromMilliseconds(20),
                    (v) =>
                    {
                        if (rand.NextDouble() > .5)
                        {
                            return v + rand.NextDouble();
                        }
                        
                        return v - rand.NextDouble();
                    });
    
                var chunks = liveValues.BufferWithTime(
                    TimeSpan.FromSeconds(.5));
    
                chunks.ObserveOnDispatcher()
                    .Subscribe(
                    (items) =>
                    {
                        foreach (var item in items)
                        {
                            _liveData.Add(item);
    
                            if (_liveData[_liveData.Count - 1].Label -
                            _liveData[0].Label >
                            TimeSpan.FromSeconds(30))
                            {
                                _liveData.RemoveAt(0);
                            }
                        }
                        Data.Reset();
                    });
            }
    
            private ManualResetProxy<TestDataItem> _data;
            public ManualResetProxy<TestDataItem> Data
            {
                get { return _data; }
                set 
                { 
                    _data = value; 
                    RaisePropertyChanged("Data");
                }
            }
    
            private void RaisePropertyChanged(string p)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(
                        this,
                        new PropertyChangedEventArgs(p));
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    
        public class ManualResetProxy<T>
            : IEnumerable<T>, INotifyCollectionChanged
        {
            private IEnumerable<T> _inner;
            private DateTime _lastEvent;
    
            public ManualResetProxy(
                IEnumerable<T> inner)
            {
                _inner = inner;
                INotifyCollectionChanged _coll =
                    _inner as INotifyCollectionChanged;
            }
    
            public void Reset()
            {
                if (CollectionChanged != null)
                {
                    CollectionChanged(
                        this,
                        new NotifyCollectionChangedEventArgs(
                            NotifyCollectionChangedAction.Reset));
                }
            }
    
            public IEnumerator<T> GetEnumerator()
            {
                return _inner.GetEnumerator();
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return _inner.GetEnumerator();
            }
    
            public event NotifyCollectionChangedEventHandler CollectionChanged;
        }
    
        public class TestDataItem
        {
            public DateTime Label { get; set; }
            public double Value { get; set; }
        }
    

    Cool, huh?

    -Graham

Children