简体   繁体   中英

How can I make a bar Histogram in c#?

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);
} 

Please help and thank you <3

Horizontal Bar Chart

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.

I would do something like this:

  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

For example:

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

Sample Output

在此处输入图像描述


Vertical Bar Chart

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

Sample Output

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM