如何在C#中制作直方图直方图? [英] How can I make a bar Histogram in c#?

查看:287
本文介绍了如何在C#中制作直方图直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的老师希望我们做一个图表,计算一个字符串上重复多少个单词,并显示类似这样的内容我(在这里图表)12我(在这里图表)2你们明白了,但他希望我们用Console.Bacground/Forground Color做到这一点.但是他从来没有解释过如何做,也没有给出任何指示,所以我毫无头绪,他说我们需要一个循环,但是我做了一个循环,只对单词

So my teacher want us to do a chart that counts how many words repeat on a string and that displays sort of like this I (Chart here) 12 me (Chart here) 2 you guys get the point but he want us to do it with Console.Bacground/Forground Color but he never explained how to or gaved any instructions on how so i have no clue, he said we need a loop but i made this one and did not work only colored the words

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

请帮助并谢谢< 3

Please help and thank you <3

推荐答案

水平条形图

为此,最容易使用水平图,因为我们有无限"的垂直空间,所以水平显示值,垂直显示键(单词),因此很可能会显示很多单词.

A Horizonal chart would be easiest for this, where we display the values horizontally and the keys (words) vertically because we have "unlimited" vertical space and there will likely be many words to display data for.

创建这样的图形也更容易,因为我们在一行中写了一个单词的所有数据.我们可以制作一个垂直图表,但这会有点棘手.

It's also easier to create a graph like this since we're writing all the data for a single word in one line. We could do a vertical chart, but that would be a little trickier.

我会做这样的事情:

  1. 在循环中,为每个项目写一个单词,并在单词的左侧填充空格,这样我们就可以使图形的左边距均匀
  2. 写一串个字符,代表该单词的实例数量
  1. In a loop, for each item, write the word and pad the left of it with spaces so we have an even left margin for the graph
  2. Write a string of characters representing the number of instances of the word

例如:

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

示例输出

垂直条形图

现在我们看到了如何制作水平图,现在我们可以考虑如何制作垂直图了.在这种情况下,我们需要知道每个字符串的值,然后从图表的顶部(行"为最大值)开始,为每个字符串等于或等于的实心块编写一个实心块大于当前行的值.

Now that we see how to do a horizontal chart, we can think about how to do a vertical one. In this case, we need to know the values for each string, and then, starting at the top of the chart, where the "row" is the max value, write a solid block for each string that has a value that's equal to or greater than the current row's value.

然后在图表的底部,我们需要垂直地写每个单词,因此我们必须为每个单词每行写一个字符.

At the bottom of the chart, we then need to write each word vertically, so we have to write a single character per row for each one.

也许一些代码更具描述性:

Probably some code is more descriptive:

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

示例输出

这篇关于如何在C#中制作直方图直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆