Exemple #1
0
        // TODO review code in this method and try to simplify
        internal static string FormatTimeSeries <TIndex, TData>(TimeSeries <TIndex, TData> timeSeries,
                                                                int maxNumRowsToPrint, Func <TIndex, string> indexFormatter, Func <TData, string> dataFormatter)
            where TIndex : ITimePeriod <TIndex>
        {
            if (maxNumRowsToPrint != -1 && maxNumRowsToPrint < 1)
            {
                throw new ArgumentException("maxNumRowsToPrint must be either equal to -1, or greater than 0", nameof(maxNumRowsToPrint));
            }

            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine($"Count = {timeSeries.Count}");

            if (maxNumRowsToPrint == -1 || maxNumRowsToPrint > 1)
            {
                int rowToPrintBreak = -1;

                if (maxNumRowsToPrint != -1 && timeSeries.Count + 1 > maxNumRowsToPrint)
                {
                    rowToPrintBreak = (maxNumRowsToPrint + 1) / 2 - 1;
                }

                var rowsFormatted = new List <(string IndexFormatted, string DataFormatted)>(timeSeries.Count);

                int rowToPrintBreakOriginal = rowToPrintBreak;
                for (var i = 0; i < timeSeries.Count; i++)
                {
                    if (i == rowToPrintBreak)
                    {
                        i = timeSeries.Count - i - 1 + (maxNumRowsToPrint & 1);
                        rowToPrintBreak = -1;
                    }
                    else
                    {
                        string indexFormatted = indexFormatter(timeSeries.Indices[i]);
                        string dataFormatted  = dataFormatter(timeSeries[i]);
                        rowsFormatted.Add((indexFormatted, dataFormatted));
                    }
                }

                int maxTotalWidth;
                int maxIndexWidth = 0;
                if (rowsFormatted.Count == 0)
                {
                    maxTotalWidth = $"Count = {timeSeries.Count}".Length;
                }
                else
                {
                    maxIndexWidth = rowsFormatted.Max(tuple => tuple.IndexFormatted.Length);
                    maxTotalWidth = maxIndexWidth + rowsFormatted.Max(tuple => tuple.DataFormatted.Length) + 2;
                }

                for (var i = 0; i < rowsFormatted.Count; i++)
                {
                    if (i == rowToPrintBreakOriginal)
                    {
                        stringBuilder.AppendLine(new string('.', maxTotalWidth));
                    }
                    var(indexFormatted, dataFormatted) = rowsFormatted[i];
                    string row = indexFormatted.PadRight(maxIndexWidth + 2, ' ') +
                                 dataFormatted;
                    stringBuilder.AppendLine(row);
                }

                if (rowToPrintBreakOriginal >= rowsFormatted.Count)
                {
                    stringBuilder.AppendLine(new string('.', maxTotalWidth));
                }
            }
            return(stringBuilder.ToString().TrimEnd());
        }
Exemple #2
0
 public static string FormatIndex <TIndex, TData>(this TimeSeries <TIndex, TData> timeSeries,
                                                  string indexFormat, int maxNumRowsToPrint)
     where TIndex : ITimePeriod <TIndex>, IFormattable
 {
     return(timeSeries.FormatIndex(indexFormat, Thread.CurrentThread.CurrentCulture, maxNumRowsToPrint));
 }
Exemple #3
0
 public static string FormatData <TIndex, TData>(this TimeSeries <TIndex, TData> timeSeries, string dataFormat)
     where TIndex : ITimePeriod <TIndex>
     where TData : IFormattable
 {
     return(timeSeries.FormatData(dataFormat, Thread.CurrentThread.CurrentCulture, DefaultNumRowsToPrint));
 }
Exemple #4
0
 public static string FormatIndex <TIndex, TData>(this TimeSeries <TIndex, TData> timeSeries,
                                                  string indexFormat, IFormatProvider indexFormatProvider)
     where TIndex : ITimePeriod <TIndex>, IFormattable
 {
     return(timeSeries.FormatIndex(indexFormat, indexFormatProvider, DefaultNumRowsToPrint));
 }