Ejemplo n.º 1
0
        private void InitializeSeries(Chart chart)
        {
#if DEBUG
            Trace.WriteLine("Chart was initialized (" + DateTime.Now.ToLongTimeString() + ")");
#endif
            chart.SeriesInitialized = true;
            foreach (var series in chart.Series)
            {
                var index = _colorIndexer++;
                series.Chart = chart;
                series.Stroke = series.Stroke ??
                                new SolidColorBrush(
                                    Colors[(int) (index - Colors.Count*Math.Truncate(index/(decimal) Colors.Count))]);
                series.Fill = series.Fill ??
                              new SolidColorBrush(
                                  Colors[(int) (index - Colors.Count*Math.Truncate(index/(decimal) Colors.Count))])
                              {
                                  Opacity = DefaultFillOpacity
                              };
                series.RequiresPlot = true;
                series.RequiresAnimation = true;
                var observable = series.Values as INotifyCollectionChanged;
                if (observable != null)
                    observable.CollectionChanged += chart.OnDataSeriesChanged;
            }

            chart.ClearAndPlot();
            var anim = new DoubleAnimation
            {
                From = 0,
                To = 1,
                Duration = TimeSpan.FromMilliseconds(1000)
            };
            if (!chart.DisableAnimation) chart.Canvas.BeginAnimation(OpacityProperty, anim);

            chart.Series.CollectionChanged += (sender, args) =>
            {
                chart._seriesChanged.Stop();
                chart._seriesChanged.Start();

                if (args.OldItems != null)
                    foreach (var serie in args.OldItems.Cast<Series>())
                    {
                        chart.EraseSerieBuffer.Add(serie);
                    }

                var newElements = args.NewItems != null ? args.NewItems.Cast<Series>() : new List<Series>();



                chart.RequiresScale = true;
                foreach (var serie in chart.Series.Where(x => !newElements.Contains(x)))
                {
                    chart.EraseSerieBuffer.Add(serie);
                    serie.RequiresPlot = true;
                }


                if (args.NewItems != null)
                    foreach (var series in newElements)
                    {
                        var index = _colorIndexer++;
                        series.Chart = chart;
                        series.Stroke = series.Stroke ??
                                new SolidColorBrush(
                                    Colors[(int)(index - Colors.Count * Math.Truncate(index / (decimal)Colors.Count))]);
                        series.Fill = series.Fill ??
                                      new SolidColorBrush(
                                          Colors[(int)(index - Colors.Count * Math.Truncate(index / (decimal)Colors.Count))])
                                      {
                                          Opacity = DefaultFillOpacity
                                      };
                        series.RequiresPlot = true;
                        series.RequiresAnimation = true;
                        var observable = series.Values as INotifyCollectionChanged;
                        if (observable != null)
                            observable.CollectionChanged += chart.OnDataSeriesChanged;
#if DEBUG
                        if (observable == null) Trace.WriteLine("series do not implements INotifyCollectionChanged");
#endif
                    }
            };
        }