Exemple #1
0
        public static Heatmap ToHeatmap(this TimeSeries ts, ByPeriod xPeriod = ByPeriod.Day)
        {
            var heatmap = new Heatmap();

            if (ts.Times.Count < 2)
            {
                Console.WriteLine("Кол-во элементов для heatmap меньше 2");
                return(new Heatmap());
            }

            var tshm = from t in ts.TimeValues
                       group t by new DateTime(
                t.Key.Year,
                xPeriod >= ByPeriod.Month ? t.Key.Month : 1,
                xPeriod >= ByPeriod.Day ? t.Key.Day : 1,
                xPeriod >= ByPeriod.Hour ? t.Key.Hour : 0,
                0,
                0)
                       into g
                       select g;

            foreach (var t in tshm)
            {
                heatmap.AddYZ(t.Key, t.Select(n => n.Value).ToList());
            }

            // формируем ось х
            var begin  = ts.TimeBegin;
            var end    = begin;
            var format = "";

            switch (xPeriod)
            {
            case ByPeriod.Year:
                break;

            case ByPeriod.Month:
                end    = begin.AddMonths(1);
                format = "dd";
                break;

            case ByPeriod.Day:
                end    = begin.AddDays(1);
                format = "HH:mm";
                break;

            case ByPeriod.Hour:
                end    = begin.AddHours(1);
                format = "mm:ss";
                break;

            case ByPeriod.Minute:
                end    = begin.AddMinutes(1);
                format = "ss";
                break;

            case ByPeriod.Second:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            var x = new List <DateTime>();

            if (ts.PeriodInSeconds == null)
            {
                throw new Exception("Значение PeriodInSeconds класса TimeSeries не определено");
            }
            var seconds = (int)ts.PeriodInSeconds;

            while (begin < end)
            {
                x.Add(begin);
                begin = begin.AddSeconds(seconds);
            }

            foreach (var time in x)
            {
                heatmap.AddX(time.ToString(format));
            }

            return(heatmap);
        }