-
Notifications
You must be signed in to change notification settings - Fork 2
/
AsciiBarGraph.cs
117 lines (101 loc) · 4.29 KB
/
AsciiBarGraph.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace atlas
{
public class AsciiBarChart
{
public Dictionary<string, long> Data;
public AsciiBarChart(Dictionary<string, long> data) => Data = data;
public AsciiBarChart(List<long> data) => Data = data.ToDictionary(x => x.ToString(), x => x);
public string[] DrawVertical(int height)
{
var sb = new StringBuilder();
var values = Data.Values.ToArray();
var min = Data.Values.Min();
var max = Data.Values.Max();
var lables = Data.Keys.ToArray();
var maxLabelLength = lables.Max(x => x.Length);
var maxBarheight = height - maxLabelLength - 1;
for (int i = 0; i < values.Length; i++)
values[i] = (long)Math.Round((double)values[i] / max * maxBarheight);
for (int i = maxBarheight; i > 0; i--)
{
var legend = (long)(i != maxBarheight ? (float)max / maxBarheight * i : max);
var strLegend = FormatNumber(legend) + "┫";
if (i == 1)
sb.Append(strLegend.PadLeft(5));
else if (i % 4 == 0 && i != maxBarheight)
sb.Append(strLegend.PadLeft(5));
else if (i == maxBarheight)
sb.Append(strLegend.PadLeft(5));
else
sb.Append("┫".PadLeft(5));
for (long j = 0; j < Data.Keys.Count; j++)
sb.Append(values[j] >= i ? " ███ " : " ");
sb.AppendLine();
}
sb.Append("┗".PadLeft(5));
foreach(var label in lables)
sb.Append("━━╋━━");
sb.AppendLine();
for (int i = 0; i < maxLabelLength; i++)
{
sb.Append(" ");
foreach (var label in lables)
sb.Append(label.Length > i ? $" {label[i]} " : " ");
sb.AppendLine();
}
return sb.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
}
public string[] DrawHorizontal(int width, bool compact = true)
{
var sb = new StringBuilder();
var rawvalues = Data.Values.ToArray();
var values = Data.Values.ToArray();
var max = Data.Values.Max();
var labels = Data.Keys.ToArray();
var maxLabelLength = labels.Max(x => x.Length);
var maxBarWidth = width - maxLabelLength - 1;
for (long i = 0; i < values.Length; i++)
values[i] = (long)Math.Round((double)values[i] / max * maxBarWidth);
for (int i = 0; i < Data.Keys.Count; i++)
{
var legend = rawvalues[i];
string strLegend = FormatNumber(legend);
if (compact)
sb.Append($"{$"{(labels[i] + ':').PadRight(maxLabelLength + 1)} {strLegend,4}".PadRight(1)}");
else
sb.Append($"{i.ToString().PadRight(Data.Keys.Count.ToString().Length)}");
sb.Append(' ');
sb.Append("┣".PadRight((int)values[i], '━'));
sb.AppendLine();
}
if (!compact)
{
sb.AppendLine();
for (int i = 0; i < labels.Length; i++)
{
var legend = rawvalues[i];
var strLegend = FormatNumber(legend);
sb.AppendLine($"{i.ToString().PadRight(2)} {(labels[i]+':').PadRight(maxLabelLength+1)} {rawvalues[i]}");
}
}
return sb.ToString().Split(Environment.NewLine);
}
private static string FormatNumber(long legend)
{
var strLegend = legend.ToString();
if (legend > 1000000000000)
strLegend = $"{legend / 1000000000000}T";
else if (legend > 1000000000)
strLegend = $"{legend / 1000000000}G";
else if (legend > 1000000)
strLegend = $"{legend / 1000000}M";
else if (legend > 1000)
strLegend = $"{legend / 1000}K";
return strLegend;
}
}
}