Navigation

Own programs

Snippets

Libraries

Deutsch

BarChart

This class is for displaying a bar chart in the console. It has many constructors which set the descriptions (horizontal, strings), the values (vertical) and the jumps of the values. You can only use integers for the values. The example below should show how it works. In that example, only one of the constructors is shown, but in the test class, every ctor is used

Note: It also works in the Windows-console, but the width of it is not really useful for a bar chart

You can download the test class together with the bar chart class here: barchart.zip
(I diddn't use the optional argument bool beginAtZero in the test class. If you use it, the vertical axe always begins at zero)

Example:

I choosed an example that also displays nice in the Windows console - a frequency analysis of the letters in the german language.

001: #include <iostream>
002: using namespace std;
003: 
004: #include "barchart.h"
005: 
006: BarChart testCtor6();
007: 
008: int main(int argc, char *argv[]) {
009:     testCtor6().display();
010:     cout << endl << "Press Enter to quit";
011:     getchar(); //wait
012: }
013: 
014: BarChart testCtor6() {
015:     // unsigned long = values between 0 and ULONG_MAX inclusive
016:     // ULONG_MAX is at least FFFFFFFF (4'294'967'295), but can differ between platforms
017:     // Usually, it's FFFFFFFF (4'294'967'295) on 32-Bit-platforms
018:     // source: http://upload.wikimedia.org/wikipedia/de/4/48/Alphabet_haufigkeit.png
019:     //         http://home.att.net/~jackklein/c/inttypes.html
020:     map<string, unsigned long> values;
021:     values["A"] = 11945590l;
022:     values["B"] = 4561458l;
023:     values["C"] = 6044137l;
024:     values["D"] = 9270289l;
025:     values["E"] = 29924830l;
026:     values["F"] = 3535341l;
027:     values["G"] = 5900424l;
028:     values["H"] = 8391552l;
029:     values["I"] = 15885699l;
030:     values["J"] = 626522l;
031:     values["K"] = 3314564l;
032:     values["L"] = 8306966l;
033:     values["M"] = 5367829l;
034:     values["N"] = 17890909l;
035:     values["O"] = 6381588l;
036:     values["P"] = 2722782l;
037:     values["Q"] = 114811l;
038:     return BarChart(values, 1000000l);
039: }
040: 

Output:

30000000 |                 #                                                 
29000000 |                 #                                                 
28000000 |                 #                                                 
27000000 |                 #                                                 
26000000 |                 #                                                 
25000000 |                 #                                                 
24000000 |                 #                                                 
23000000 |                 #                                                 
22000000 |                 #                                                 
21000000 |                 #                                                 
20000000 |                 #                                                 
19000000 |                 #                                                 
18000000 |                 #                                   #             
17000000 |                 #                                   #             
16000000 |                 #               #                   #             
15000000 |                 #               #                   #             
14000000 |                 #               #                   #             
13000000 |                 #               #                   #             
12000000 | #               #               #                   #             
11000000 | #               #               #                   #             
10000000 | #               #               #                   #             
 9000000 | #           #   #               #                   #             
 8000000 | #           #   #           #   #           #       #             
 7000000 | #           #   #           #   #           #       #             
 6000000 | #       #   #   #       #   #   #           #       #   #         
 5000000 | #   #   #   #   #       #   #   #           #   #   #   #         
 4000000 | #   #   #   #   #   #   #   #   #           #   #   #   #         
 3000000 | #   #   #   #   #   #   #   #   #       #   #   #   #   #   #     
 2000000 | #   #   #   #   #   #   #   #   #       #   #   #   #   #   #     
 1000000 | #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   #     
         |___________________________________________________________________
           A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q  

Press Enter to quit