# Essays/Unicode Histogram

## Unicode Histogram

Illustrates these techniques:

- Use of special unicode code points to make a simple histogram

- A cheap 'n' cheerful character-only animation using the IJX window

- Implementing a moderately accurate delay for J602 JWD (Macintosh).
On the Macintosh (

j602a_mac_intel.dmg)6!:3delays only in multiples of one second.

Unicode has a set of code points (aka "characters") for building a crude histogram:

u: 16b2588 NB. U+2588 (9608) █ 1.0 -start of horiz bar u: 16b2589 NB. U+2589 (9609) ▉ .875 u: 16b258a NB. U+258a (9610) ▊ .75 u: 16b258b NB. U+258b (9611) ▋ .625 u: 16b258c NB. U+258c (9612) ▌ .5 u: 16b258d NB. U+258d (9613) ▍ .375 u: 16b258e NB. U+258e (9614) ▎ .25 u: 16b258f NB. U+258f (9615) ▏ .125

These block characters appear best when using the font "APL385 Unicode" (provided here by courtesy of the British APL Association):

*Click to download font:* APL385 Unicode

Some common non-proportional fonts like "Courier" and "Courier New" haven't designed them correctly.

Notice that these code points divide the basic block (U+2588) into **eights**, not tenths!

Unicodecode points(see:[1])

are conventionally represented asU+xxxxwherexxxxis a hex numeral

eg.U+2588.

This hex numeral can be expressed in J602 as:16b2588

but in practice we will employ the corresponding decimal numeral, viz. 9608.

We can display the histogram for the number 3.4 (say) by means of 3 whole blocks (U+2588) followed by one of the partial blocks, U+258d being the closest fractional part (0.375) to 0.4:

16b2588 16b2588 16b2588 16b258d 9608 9608 9608 9613 u: 9608 9608 9608 ███ u: 9608 9608 9608 9608 ████ u: 9608 9608 9608 9613 ███▍

But how do we compute the fractional part for any given real number?

The following demo shows how:

NB. Demo using unicode to display a histogram / progress bar fh=: 9608 + [: >. 7.5 - 8 * ] hst=: (9608 #~ <.) , 9616 -.~ [: fh ] - <. delay=: 3 : 0 NB. dynamic wait for y seconds if. y-: 0 do. return. end. if. IFWIN do. 6!:3 y else. t1=. (|y) + t=. 6!:1'' while. t<t1 do. t=. 6!:1'' end. i.0 0 end. ) smclear=: 3 : 0 NB. clear (/refill) the IJX session NB. y must be '' or LF-separated try. if. IFIJX_0_ do. NB. provided locale 0 is ijx... wd 'psel ',SMHWNDP_0_ if. 0<#y do. wd 'set e *',LF ,~ y else. wd 'set e ""' end. else. @@ NB. force an error end. catch. smoutput 'smclear: >>>cannot identify J session window' end. ) th=: 3 : 0 NB. run the animated histogram for (y) frames for_i. 10 %~ i.y do. smclear'' smoutput (5j1 ": i) , TAB , u: hst i delay 0.1 end. ) th 101

See the verb `fh` (=fractional-histogram),
which is used in the verb `hst`,
which outputs the whole histogram as a string of integers specifying unicode characters.

Thus, taking our earlier example, displaying the histogram for 3.4:

hst 3.4 9608 9608 9608 9613 u: hst 3.4 ███▍

Possible uses:

- Embedding mini-histograms in a grid or table of figures

- Implementing a progress bar

Contributed by Ian Clark