Почему анимации Live Chart прекращаются, когда я использую коллекцию stati c SeriesCollection? - PullRequest
0 голосов
/ 14 марта 2020

Я создаю приложение C# WinForm. Я использую один элемент управления LiveChart Chart для отображения нескольких наборов данных в al oop. Сначала на графике отображаются данные за этот месяц, затем через минуту на графике отображаются данные за этот год. Это зациклено. Когда я создаю новый SeriesCollection() с каждым Timer тиком, график воспроизводит анимацию правильно.

Примерно так:

rising animation works correctly with ever new series collection

С теми же данными я хотел использовать stati c SeriesCollection, и когда я использую версию этих коллекций stati c, анимация останавливается после первой l oop.

Как это:

rising animation stops after first loop

Мой код с Dynami c SeriesCollections:

using LiveCharts;
using LiveCharts.Wpf;
using System;
using System.Linq;

namespace ExApp.Forms.MainMenu.FillData
{
    internal class MonthlyIncomeCartesianChart
    {
        private static System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer { Interval = 1500 };
        private static bool Month;

        internal static void Fill(LiveCharts.WinForms.CartesianChart chart)
        {
            timer.Tick += TickTock;
            timer.Start();

            chart.Series = new SeriesCollection
            {
                new LineSeries
                    {
                        Title = "Income",
                        Values = new ChartValues<double> { 1000,2000,300,40,500,6000,700,800,90,100,110,120 }
                    },
                new LineSeries
                    {
                        Title = "Expense",
                        Values = new ChartValues<double> {110,22,32,430,51,62,73,820,91,180,175,1210},
                        PointGeometry = null
                    }
            };
            Month = false;
            chart.AxisX.Add(new Axis
            {
                Title = "This Month",
                Labels = (Enumerable.Range(1, 14/*DateTime.Today.Day*/).Select(q => q.ToString()).ToList())
            });

            chart.AxisY.Add(new Axis
            {
                Title = "Currency",
                LabelFormatter = value => value.ToString("C")
            });
            chart.LegendLocation = LegendLocation.Right;
        }
        private static void TickTock(object sender, EventArgs e)
        {
            LiveCharts.WinForms.CartesianChart chart = FormReferrer.MainMenu.cartesianChart_MainMenuSubMenuSummaryMonthlyIncome;
            chart.AxisY.Clear();
            chart.AxisX.Clear();
            if (Month)
            {
                Month = false;
                chart.Series = new SeriesCollection
                {
                    new LineSeries
                    {
                        Title = "Income",
                        Values = new ChartValues<double> { 1000,2000,300,40,500,6000,700,800,90,100,110,120 }
                    },
                    new LineSeries
                    {
                        Title = "Expence",
                        Values = new ChartValues<double> {110,22,32,430,51,62,73,820,91,180,175,1210},
                        PointGeometry = null
                    }
                };
                chart.AxisX.Add(new Axis
                {
                    Title = "This Year",
                    Labels = new[] { "Jan", "Fab", "Mar", "Apr", "May", "Jun", "Jul", "Agu", "Sep", "Oct", "Nov", "Dec" }
                });
                chart.AxisY.Add(new Axis
                {
                    Title = "Currency",
                    LabelFormatter = value => value.ToString("C")
                });
            }
            else
            {
                Month = true;
                chart.Series = new SeriesCollection
                {
                    new LineSeries
                    {
                        Title = "Income",
                        Values = new ChartValues<double> { 10,20,30,40,50,60,70,80,90,100,110,120,130,140 }
                    },
                    new LineSeries
                    {
                        Title = "Expence",
                        Values = new ChartValues<double> {11,22,32,43,51,62,73,82,91,180,175,121,136,250},
                        PointGeometry = null
                    }
                };
                chart.AxisX.Add(new Axis
                {
                    Title = "This Month",
                    Labels = (Enumerable.Range(1, 14).Select(q => q.ToString()).ToList())
                });

                chart.AxisY.Add(new Axis
                {
                    Title = "Currency",
                    LabelFormatter = value => value.ToString("C")
                });
            }
        }
    }
}

Мой код с stati c SeriesCollections:

using LiveCharts;
using LiveCharts.Wpf;
using System;
using System.Linq;

namespace ExApp.Forms.MainMenu.FillData
{
    internal class MonthlyIncomeCartesianChart2
    {
        private static System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer { Interval = 1500 };
        private static bool Month;
        static SeriesCollection thisMonth;
        static SeriesCollection thisYear;
        internal static void Fill(LiveCharts.WinForms.CartesianChart chart)
        {
            timer.Tick += TickTock;
            timer.Start();


            thisYear = new SeriesCollection
                {
                    new LineSeries
                    {
                        Title = "Income",
                        Values = new ChartValues<double> { 1000,2000,300,40,500,6000,700,800,90,100,110,120 }
                    },
                    new LineSeries
                    {
                        Title = "Expence",
                        Values = new ChartValues<double> {110,22,32,430,51,62,73,820,91,180,175,1210},
                        PointGeometry = null
                    }
                };



            thisMonth = new SeriesCollection
            {
                new LineSeries
                    {
                        Title = "Income",
                        Values = new ChartValues<double> { 10,20,30,40,50,60,70,80,90,100,110,120,130,140 }
                    },
                new LineSeries
                    {
                        Title = "Expense",
                        Values = new ChartValues<double> {11,22,32,43,51,62,73,82,91,180,175,121,136,250},
                        PointGeometry = null
                    }
            };



            chart.Series = thisMonth;
            Month = true;
            chart.AxisX.Add(new Axis
            {
                Title = "This Month",
                Labels = (Enumerable.Range(1, 14).Select(q => q.ToString()).ToList())
            });

            chart.AxisY.Add(new Axis
            {
                Title = "Currency",
                LabelFormatter = value => value.ToString("C")
            });
            chart.LegendLocation = LegendLocation.Right;
        }
        private static void TickTock(object sender, EventArgs e)
        {
            LiveCharts.WinForms.CartesianChart chart = FormReferrer.MainMenu.cartesianChart_MainMenuSubMenuSummaryMonthlyIncome;
            chart.AxisY.Clear();
            chart.AxisX.Clear();
            if (Month)
            {
                Month = false;
                chart.Series = thisYear;
                chart.AxisX.Add(new Axis
                {
                    Title = "This Year",
                    Labels = new[] { "Jan", "Fab", "Mar", "Apr", "May", "Jun", "Jul", "Agu", "Sep", "Oct", "Nov", "Dec" }
                });
                chart.AxisY.Add(new Axis
                {
                    Title = "Currency",
                    LabelFormatter = value => value.ToString("C")
                });
            }
            else
            {
                Month = true;
                chart.Series = thisMonth;
                chart.AxisX.Add(new Axis
                {
                    Title = "This Month",
                    Labels = (Enumerable.Range(1, 14).Select(q => q.ToString()).ToList())
                });

                chart.AxisY.Add(new Axis
                {
                    Title = "Currency",
                    LabelFormatter = value => value.ToString("C")
                });
            }
        }
    }
}

Я хочу, чтобы "анимация нарастания" была на циклах, работающих с stati c SeriesCollections. Как я могу это сделать?

...