Sunday, June 5, 2022
HomeProgrammingFormatting 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 end result as an ASCII chart. This may be helpful for fast plotting of leads to your console software.

Assuming you’ve gotten a end result set of this type (which is what you’re getting whenever you name end result.format() or simply end result.toString())

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

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

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

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

Then, calling end result.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 contains 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’ll be able to tweak all kinds of configuration, together with peak and width:

end result.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:

end result.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 nicely along with your font (e.g. as on this weblog), you possibly can change them like this:

end result.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      

Choose displaying 100% charts? No drawback

end result.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, if in case you have jOOQ!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments