【问题标题】:How can I make a bar Histogram in c#?如何在 C# 中制作条形直方图?
【发布时间】:2020-04-10 18:54:44
【问题描述】:

所以我的老师想让我们做一个图表,计算一个字符串上重复的单词数量,并显示如下 我(这里的图表)12 我(这里的图表)2 你们明白了,但他希望我们用 Console.Bacground/Forground Color 来做 但他从来没有解释过如何做,也没有给出任何指示,所以我不知道,他说我们需要一个循环,但我做了这个,没有工作,只是给文字上色

foreach (KeyValuePair<string, int> kvp in RepeatedWordCount)
{
    Console.BackgroundColor = ConsoleColor.White;
    Console.WriteLine("\n");
    Console.WriteLine(kvp.Key + kvp.Value);
} 

请帮忙,谢谢

【问题讨论】:

  • 尝试找出如何编写一个看起来像你想要的样子的字符(例如,对于一个酒吧,你可以只使用一堆 X 或者如果你比你更有创意检查 ASCII 表并在那里的扩展字符集中找到合适的字符)。之后尝试计算条与其他条相比应该有多“高”。最后,如果您将最高栏设置为例如10个字符的高度,那么你应该能够计算出其余的。如果您知道每个条必须写多少个字符,只需在每个条中使用 for 循环即可;-)

标签: c# charts histogram


【解决方案1】:

水平条形图

水平图表最容易做到这一点,我们水平显示值,垂直显示键(单词),因为我们有“无限”垂直空间,可能会有很多单词显示数据。

创建这样的图表也更容易,因为我们将单个单词的所有数据写入一行。我们可以做一个垂直图表,但这会有点棘手。

我会这样做:

  1. 在一个循环中,为每个项目写下单词并在其左侧用空格填充,这样我们就可以为图表留出均匀的左边距
  2. 写一个由字符组成的字符串,表示单词的实例数

例如:

// 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();

样本输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 2018-07-20
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    相关资源
    最近更新 更多