水平条形图
水平图表最容易做到这一点,我们水平显示值,垂直显示键(单词),因为我们有“无限”垂直空间,可能会有很多单词显示数据。
创建这样的图表也更容易,因为我们将单个单词的所有数据写入一行。我们可以做一个垂直图表,但这会有点棘手。
我会这样做:
- 在一个循环中,为每个项目写下单词并在其左侧用空格填充,这样我们就可以为图表留出均匀的左边距
- 写一个由
█字符组成的字符串,表示单词的实例数
例如:
// Populate some random word counts for an example
var RepeatedWordCount = new Dictionary<string, int>
{
{"the", 20}, {"you", 13}, {"was", 9}, {"as", 5},
{"a", 17}, {"in", 15}, {"I", 1}, {"he", 10},
{"they", 2}, {"of", 19}, {"on", 7}, {"that", 12},
{"his", 3}, {"it", 11}, {"to", 16}, {"for", 8},
{"are", 6}, {"with", 4}, {"is", 14}, {"and", 18},
};
// Get the length of the longest word so we can pad all
// words to the same length and have an even margin
var maxWordLength = RepeatedWordCount.Keys.Max(k => k.Length);
// Create some colors to differentiate the bars
var colors = new[]
{
ConsoleColor.Red, ConsoleColor.DarkBlue, ConsoleColor.Green,
ConsoleColor.DarkYellow, ConsoleColor.Cyan, ConsoleColor.DarkMagenta,
ConsoleColor.DarkRed, ConsoleColor.Blue, ConsoleColor.DarkGreen,
ConsoleColor.Yellow, ConsoleColor.DarkCyan, ConsoleColor.Magenta,
};
var colorIndex = 0;
foreach (KeyValuePair<string, int> kvp in RepeatedWordCount)
{
// Write the word and vertical line
Console.Write(kvp.Key.PadLeft(maxWordLength + 2, ' ') + " | ");
// Write the chart value using the next color in the array
Console.ForegroundColor = colors[colorIndex++ % colors.Length];
Console.WriteLine(new string('█', kvp.Value));
Console.ResetColor();
}
Console.Write("\nDone! Press any key to exit...");
Console.ReadKey();
样本输出
垂直条形图
现在我们已经了解了如何制作水平图表,我们可以考虑如何制作垂直图表。在这种情况下,我们需要知道每个字符串的值,然后,从图表顶部开始,其中“行”是最大值,为每个值等于或的字符串写一个实心块大于当前行的值。
在图表的底部,我们需要垂直写下每个单词,所以我们必须为每个单词每行写一个字符。
可能有些代码更具描述性:
// Populate some random word counts for an example
var RepeatedWordCount = new Dictionary<string, int>
{
{"the", 20}, {"you", 13}, {"was", 9}, {"as", 5},
{"a", 17}, {"in", 15}, {"I", 1}, { "he", 10},
{"they", 2}, {"of", 19}, {"on", 7}, {"that", 12},
{"his", 3}, {"it", 11}, {"to", 16}, {"for", 8},
{"are", 6}, {"with", 4}, {"is", 14}, {"and", 18},
};
// Get the count value for the word with the highest count
var maxWordCount = RepeatedWordCount.Values.Max();
// Create some colors to differentiate the bars
var colors = new[]
{
ConsoleColor.Red, ConsoleColor.DarkBlue, ConsoleColor.Green,
ConsoleColor.DarkYellow, ConsoleColor.Cyan, ConsoleColor.DarkMagenta,
ConsoleColor.DarkRed, ConsoleColor.Blue, ConsoleColor.DarkGreen,
ConsoleColor.Yellow, ConsoleColor.DarkCyan, ConsoleColor.Magenta,
};
Console.WriteLine();
// Get all the counts
var values = RepeatedWordCount.Values.ToList();
// Start at the top of the chart, where the value is maxWordCount
// and work our way down, writing a block for each string that
// has a value greater than or equal to the count for that row
for (int i = maxWordCount; i > 0; i--)
{
for (int j = 0; j < RepeatedWordCount.Count; j++)
{
Console.ForegroundColor = colors[j % colors.Length];
Console.Write(values[j] >= i ? "███" : " ");
Console.ResetColor();
}
Console.WriteLine();
}
// Write each word vertically below it's line
var words = RepeatedWordCount.Keys.ToList();
var maxWordLength = words.Max(w => w.Length);
for (var i = 0; i < maxWordLength; i++)
{
foreach (var word in words)
{
Console.Write(word.Length > i ? $" {word[i]} " : " ");
}
Console.WriteLine();
}
Console.Write("\nDone! Press any key to exit...");
Console.ReadKey();
样本输出