Saturday, May 18, 2024
HomeJavaFormatting ASCII Charts With jOOQ – Java, SQL and jOOQ.

Formatting ASCII Charts With jOOQ – Java, SQL and jOOQ.


A little or no identified characteristic in jOOQ is the Formattable.formatChart() functionality, which permits for formatting any jOOQ consequence as an ASCII chart. This may be helpful for fast plotting of ends in your console utility.

Assuming you’ve gotten a consequence set of this way (which is what you’re getting if you name consequence.format() or simply consequence.toString())

+---+---+---+---+
|c  | v1| v2| v3|
+---+---+---+---+
|a  |  1|  2|  3|
|b  |  2|  1|  1|
|c  |  4|  0|  1|
+---+---+---+---+

This consequence might be produced by any question, or you possibly can assemble it with out executing a question like this:

Area<String>  c  = subject("c", VARCHAR);
Area<Integer> v1 = subject("v1", INTEGER);
Area<Integer> v2 = subject("v2", INTEGER);
Area<Integer> v3 = subject("v3", INTEGER);

End result<Record4<String, Integer, Integer, Integer>> consequence = 
    create.newResult(c, v1, v2, v3); 
                      
consequence.add(create.newRecord(c, v1, v2, v3).values("a", 1, 2, 3));
consequence.add(create.newRecord(c, v1, v2, v3).values("b", 2, 1, 1));
consequence.add(create.newRecord(c, v1, v2, v3).values("c", 4, 0, 1));

Then, calling consequence.formatChart() will produce a pleasant ASCII chart like this, by default:

4.00|                                                  █████████████████████████
3.86|                                                  █████████████████████████
3.73|                                                  █████████████████████████
3.59|                                                  █████████████████████████
3.45|                                                  █████████████████████████
3.32|                                                  █████████████████████████
3.18|                                                  █████████████████████████
3.05|                                                  █████████████████████████
2.91|                                                  █████████████████████████
2.77|                                                  █████████████████████████
2.64|                                                  █████████████████████████
2.50|                                                  █████████████████████████
2.36|                                                  █████████████████████████
2.23|                                                  █████████████████████████
2.09|                                                  █████████████████████████
1.95|                         ██████████████████████████████████████████████████
1.82|                         ██████████████████████████████████████████████████
1.68|                         ██████████████████████████████████████████████████
1.55|                         ██████████████████████████████████████████████████
1.41|                         ██████████████████████████████████████████████████
1.27|                         ██████████████████████████████████████████████████
1.14|                         ██████████████████████████████████████████████████
1.00|███████████████████████████████████████████████████████████████████████████
----+---------------------------------------------------------------------------
    |            a                        b                        c            

It consists of the primary column because the class label on the x-axis, and the second column as the worth to be plotted on the y-axis. You possibly can tweak all types of configuration, together with peak and width:

consequence.formatChart(new ChartFormat().dimensions(6, 20));

To get this a lot smaller chart:

4.00|          █████
3.00|          █████
2.00|     ██████████
1.00|███████████████
----+---------------
    |  a    b    c  

To incorporate the values of the opposite worth columns in a stacked chart (which is the default) write:

consequence.formatChart(new ChartFormat()
    .dimensions(40, 8)
    .values(1, 2, 3)
);

Producing:

6.00|▒▒▒▒▒▒▒▒▒▒▒▒                       
5.00|▒▒▒▒▒▒▒▒▒▒▒▒            ▒▒▒▒▒▒▒▒▒▒▒
4.00|▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒███████████
3.00|▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓███████████
2.00|▓▓▓▓▓▓▓▓▓▓▓▓███████████████████████
1.00|███████████████████████████████████
----+-----------------------------------
    |     a           b          c      

If these default ASCII characters from the candy outdated 90s MS-DOS and BBS occasions don’t align effectively along with your font (e.g. as on this weblog), you possibly can swap them like this:

consequence.formatChart(new ChartFormat()
    .dimensions(40, 8)
    .values(1, 2, 3)
    .shades('@', 'o', '.')
);

And now you’re getting:

6.00|............                       
5.00|............            ...........
4.00|........................@@@@@@@@@@@
3.00|oooooooooooooooooooooooo@@@@@@@@@@@
2.00|oooooooooooo@@@@@@@@@@@@@@@@@@@@@@@
1.00|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
----+-----------------------------------
    |     a           b          c      

Want displaying 100% charts? No drawback

consequence.formatChart(new ChartFormat()
    .dimensions(40, 8)
    .values(1, 2, 3)
    .shades('@', 'o', '.')
    .show(Show.HUNDRED_PERCENT_STACKED)
);

And now, the result’s:

100.00%|................................
 80.00%|......................@@@@@@@@@@
 60.00%|...........ooooooooooo@@@@@@@@@@
 40.00%|ooooooooooo@@@@@@@@@@@@@@@@@@@@@
 20.00%|ooooooooooo@@@@@@@@@@@@@@@@@@@@@
  0.00%|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-------+--------------------------------
       |     a          b         c     

Who wants MS Excel? Nobody, when you’ve got jOOQ!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments