Foresight, FORtran Easy Svg Interactive Gnuplot-like Html Tool, v0.20.0 is out

FORtran Easy Svg Interactive Gnuplot-like Html Tool

foresight is a pure Fortran library and a command-line tool. It draws what gnuplot draws, from your solver’s arrays or from a gnuplot script: interactive HTML pages that need nothing but a browser, SVG, PDF with embedded fonts, LaTeX pictures, and text for a terminal. It needs no Python, no gnuplot, and no graphics library: only a Fortran compiler. v0.20.0 is the first release I announce here (written with the help of an AI assistant).

Disclaimer: foresight and fplotlib. fplotlib (fortran-lang) overlaps with foresight in part: both are pure Fortran, both write figures for papers, both set TeX math in labels. They are siblings, not one built on the other, and they differ:

  • Interface. fplotlib offers matplotlib’s interface from Fortran. foresight’s interface is gnuplot’s: its command language, interpreted in Fortran, and a Fortran API named after the commands (fig%set_logscale('y') is set logscale y).
  • Outputs. fplotlib writes images, PNG, GIF, and EPS among them. foresight writes interactive pages and vector formats (SVG, PDF, LaTeX pictures), and no raster images at all.
  • Choose fplotlib for matplotlib’s style, raster output, or LFortran and flang.
  • Choose foresight when your figures are, or should be, gnuplot scripts; when you watch a running job; or when your LaTeX paper should set its own labels.

foresight takes no code from fplotlib, but it borrows fplotlib’s method of checking a layout against a reference typesetter glyph by glyph. The comparison page has the details. foresight is also young: twenty minor releases in two months, used so far mostly by its author, and its API may still change before v1.0.

anim_watch

foresight --watch: a run’s log grows, and the page redraws, keeping your zoom; the readout shows the latest iteration.

Three ways to use it

1. gnuplot, from Fortran. foresight interprets a large subset of gnuplot 6.0’s language: plot, splot, fit, stats, set table, functions, loops, datablocks, enhanced text and the 2D styles. Each feature is probed on gnuplot before it is written, and a harness compares gnuplot’s own demo pages, drawn by both programs, page by page. Run your scripts with foresight script.gp, or call the same commands from your program. A script can keep running in gnuplot too. A command outside the subset is an error naming its line, never silently skipped.

2. Watch a running job. foresight --watch run.gp re-renders whenever the solver appends to its log:

  • In the browser, the page reloads itself and keeps your zoom, or follows the newest data.
  • Over plain ssh, the plot is redrawn in place in the terminal, with characters, Unicode blocks or Braille dots.
  • On a dashboard, seven-segment readouts and gauges in the colours of an old car’s display show the numbers you watch for.

3. Figures for papers and reports.

  • PDF written by foresight itself, at the size it is printed (gnuplot’s pdfcairo syntax, size single for a journal column), with two font families compiled in and embedded.
  • Exact text metrics on every device, so the SVG, the PDF and the page agree.
  • TeX math in labels ($\frac{t}{\tau}$), laid out as LaTeX lays it out.
  • LaTeX labels: gnuplot’s cairolatex, so your paper’s LaTeX sets the labels in its own fonts.
  • Reports: save returns an alternative text and a caption for each figure, and set table ... markdown writes the numbers as Markdown tables.

splot as gnuplot draws it: a pm3d surface, depth sorted and lit (set pm3d lighting).

anim_zoom

The interactive page: each zoom regenerates the tick labels by the same rules as the Fortran side, exact decimals at every scale.

anim_term

The same run over ssh: foresight --watch -e "set terminal block braille" redraws the terminal in place.

A taste

From a solver’s arrays:

program taste
use foresight, only : figure_object, I4P, R8P
implicit none
type(figure_object)    :: fig
real(R8P), allocatable :: t(:)
integer(I4P)           :: i

t = [(0.05_R8P * i, i = 0, 200)]
call fig%set_title('A damped oscillation')
call fig%set_xlabel('time [s]')
call fig%plot(t, exp(-0.3_R8P * t) * cos(2.0_R8P * t), title='u(t)', lw=2.0_R8P)
call fig%plot(t, exp(-0.3_R8P * t), title='envelope', dt=2_I4P)
call fig%save('taste.html')   ! .svg, .pdf, .tex, .txt: the extension picks the format
endprogram taste

or from the log it writes, with a gnuplot script:

set logscale y
set format y '10^{%L}'
plot 'run.dat' using 1:2 with lines title 'continuity', '' using 1:3 with lines title 'momentum'
curl -fsSL https://github.com/szaghi/foresight/releases/latest/download/install.sh | bash
foresight --watch residuals.gp

The one-line installer builds the latest release with FoBiS or fpm and installs it into ~/.local, usable through pkg-config. foresight is also an fpm and a FoBiS dependency.

The installation page’s library example: two panels, three line styles, a log axis with enhanced-text exponents.

A few more from the gallery

A run monitor in the vacuum-fluorescent theme: readouts, segmented bars, residuals.

TeX math in the axis labels and the key, in Latin Modern Math.

An isosurface of a voxel grid (set vgrid, vfill, with isosurface).

A complex function over the plane, coloured by its phase.

A wind rose of sectors.

Two panels across a page, in Latin Modern: a figure a LaTeX paper takes as it is.

The gallery has all 226 cookbook plots, by category, each with its script in the cookbook.

How it is checked

  • Tests:
    • 18 test programs and 190 golden files compared byte for byte, on every push;
    • gfortran 16.2, nvfortran 26.9 and Intel ifx 2026.2, debug and optimised.
  • Against gnuplot:
    • a harness draws gnuplot 6.0’s own demo scripts with both programs, rasterizes every page and scores the difference;
    • a change may not make a page worse.
  • Against LaTeX:
    • the math layout is checked glyph by glyph against LuaLaTeX;
    • the PDFs are checked with qpdf, pdffonts and pdftotext;
    • the example paper is built by the CI with Tectonic.
  • Docs: every snippet, output, and plot in the documentation comes from a program the CI compiles and runs.
  • Honest limits:

A family of tools

foresight is one of three projects with one goal: a Fortran program, from the running solver to its paper, with no runtime but its own.

  • FIGURE, Dear ImGui and ImPlot for Fortran, is the interactive side: windows, widgets, live plots.
    • It already builds on foresight: its tick labels come from foresight’s number formatter and tick module, so they are the same digits on gfortran, nvfortran, and ifx.
    • Its plots can take foresight’s gnuplot tick rule later.
  • BARUCH, a pure Fortran CommonMark reader and writer, is the reports side.
    • foresight is one of its first clients: once BARUCH’s streaming writer is released, the reports of foresight’s path F will be written through it instead of plain write, and its pages will read Markdown.
    • foresight does not depend on BARUCH: what a report needs of a figure is in figure_info already.

foresight builds with FoBiS or fpm, and its only dependencies are my own Fortran libraries (PENF, BeFoR64, FACE, StringiFor), fetched by either. It runs on Linux and other POSIX systems; on Windows, run it in WSL 2. It is available under your choice of license: GPL-3.0-or-later, BSD-2-Clause, BSD-3-Clause or MIT. The compiled-in fonts keep their own licenses (SIL OFL 1.1, GUST Font License).

Coming next: text placed exactly where gnuplot places it, with equal plot areas, on every device (#6), and whatever the first users find.

Feedback, issues, and contributions are welcome, and in this phase bug reports most of all.

Stefano

3 Likes