r/Batch • • May 05 '21

Show 'n Tell Graph.bat : a utility script to generate Bar Graphs

I don't think anyone's ever going to use this, but this seemed like a fun challenge in Batch. Given a file, it will generate a bar graph and put it into the variable "final". For example:

CALL GRAPH "input.txt" /X 1 /Y 2 /C █ /A 3 /YS 5 /W 1 /D 2 /NS 3

Will take the data from "input.txt", make a graph at position 1;2 using █ characters, divide the data by 3 with a Y scale of 5, with bars of width 1 and distance 2, and a Y offset by 3. Here are all the options. Each has a default, so you don't have to specify each one.

The first argument is always the file name, the rest can be put in any order.

  • X is X coordinate (default 1)

  • Y is Y coordinate (default 1)

  • D is distance between bars (starts from 0) (default 1)

  • C is display char (default #)

  • A is data adjust, ie if it's 3 it's divide each point by 3 (default 1)

  • W is bar length (starts from 0) (default 1)

  • XR is start of X scale (default 1)

  • XS is X scale factor (default 1)

  • YR is start of Y scale (default 1)

  • YS is Y scale factor (default 1)

  • NS is the offset for the Y scale (default 2)

First, save this script into graph.bat.

SETLOCAL ENABLEDELAYEDEXPANSION
SET "param=%*"
SET "disp="&SET "disp[x]="&SET "disp[y]="&SET "final="
SET "param[C]=#"
SET /A "max=dif=param[num]=cur=0", "param[X]=param[NS]=param[Y]=param[D]=param[A]=param[W]=param[XR]=param[XS]=param[YR]=param[YS]=1", "param[NS]=2"
(ECHO %param:/=&SET /A "param[num]+=1"&SET param[!param[num]!]=%)>NUL
FOR /L %%P in (1, 1, %param[num]%) DO (
    FOR /F "tokens=1-2 delims= " %%G in ("!param[%%P]!") DO (
        IF defined param[%%G] (
            SET "param[%%G]=%%H"
        ) else (
            ECHO Invalid Switch /%%G
            GOTO :EOF
        )
    )
)
IF %param[D]% NEQ 2 (
    SET /A "param[D]-=1"
)
FOR /F %%Q in (%~1) DO (
    SET /A "num=%%Q / param[A]", "cur+=1", "scale[x]=((cur - 1) * param[XS]) + param[XR]"
    IF !num! GTR !max! (
        SET /A "dif=num - max"
        FOR /L %%G in (1, 1, !dif!) DO (
            SET /A "scale[ycur]+=1", "scale[y]=((scale[ycur] - 1) * param[YS]) + param[YR]"
            SET "disp=!disp!%ESC%[D%ESC%[A%param[C]%"
            SET "disp[y]=!disp[y]!%ESC%[%param[X]%G%ESC%[A!scale[y]!"
        )
        SET "add=!disp!"
        SET "max=!num!"
    ) else IF !num! EQU 0 (
        SET "add=%ESC%[A"
    ) else (
        SET /A "dif=(max - num) * 7"
        IF !dif! EQU 0 (
            SET "add=!disp!"
        ) else (
            FOR %%L in (!dif!) DO (
                SET "add=!disp:~0,-%%L!"
            )
        )
    )
    FOR /L %%G in (1, 1, %param[W]%) DO (
        SET "final=!final!!add!%ESC%[C%ESC%[!num!B"
    )
    SET "final=!final!!width[disp]!%ESC%[%param[D]%C"
    IF %param[D]% LSS 2 (
        SET /A "scale[num]=((cur - 1) * (param[W] + param[D] + 1)) + param[X] + param[NS]"
    ) else (
        SET /A "scale[num]=((cur - 1) * (param[W] + param[D])) + param[X] + param[NS]"
    )
    SET "disp[x]=!disp[x]!%ESC%[!scale[num]!G!scale[x]!"
)

ENDLOCAL&SET "final=%ESC%[%param[Y]%;%param[X]%H%ESC%[%max%B%disp[y]%%ESC%[%max%B%ESC%[%param[NS]%G%ESC%[2C%final%%disp[x]%"

This script is decently fast and only needs one pass through the data. To generate the bars, it takes the maximum bar length and subtracts/adds to it, so it doesn't iterate again for each bar. And all you have to do is CALL GRAPH with your parameters. You must also define %ESC% in your code as the escape character. For example, here's a sample script:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
MODE 140, 30
FOR /F %%A in ('ECHO PROMPT $E^| CMD') DO SET "ESC=%%A"

(CHCP 65001)>NUL

CALL GRAPH "input.txt" /X 1 /Y 2 /C █ /A 3 /YS 5 /W 1 /D 2 /NS 3

ECHO %ESC%[2J%ESC%[4mTemperature vs. Day%ESC%[24m%final%
PAUSE>NUL

EXIT /B

This generates https://imgur.com/0JCkfSj

Have fun,

Lowsun

17 Upvotes

0 comments sorted by