Skip to content

E3. What foresight adds ​

Goal: foresight's extensions in a script that still runs in gnuplot.

gnuplot
# foresight's extensions, kept out of gnuplot's way: gnuplot reads the else branches
$P << EOD
1 3.00
2 2.61
3 2.43
4 2.37
5 2.35
EOD
set title 'pressure'
set xlabel 'iteration'
if (strstrt(GPVAL_TERMINALS, 'html') > 0) {
   set terminal svg size 600,320 theme vfd       # a theme: the colors of a 1980s car dashboard
   set output 'pe03.svg'
   set caption 'The pressure of the run.'        # for a report (save's info), not drawn
   set readout vertical top right
   unset key
   plot $P using 1:2 with lines title 'p', '' using 2 with readout format '%5.2f' title 'P'
} else {
   set terminal pngcairo size 600,320
   set output 'pe03.png'
   plot $P using 1:2 with lines title 'p'
}
pe03
open SVG ↗

Why it works ​

  • foresight's own words are few and named: theme on the terminals (classic, washi, night, vfd, lcd, paper), the styles readout and gauge, set readout, set caption, set table ... markdown, the html terminal and its viewer. gnuplot stops on each of them, so they go in the branch only foresight reads.
  • if (...) { ... } else { ... } is gnuplot's block if: both programs parse the whole block and run one branch.
  • Everything else in the script is gnuplot's: a script that keeps its extensions in such branches runs in both.

Try this ​

  • theme washi and theme night: the light and dark colors of these pages.
  • with gauge instead of with readout: a dial.
  • set table 'p.md' markdown and plot $P with table: the data as a Markdown table (F2).

Pitfalls ​

  • Inline data ('-') cannot be read inside a { } block: use a datablock ($P << EOD) as above.
  • GPVAL_VERSION is 6.0 in both programs: test the terminals, not the version.

Reference: themes, readouts. Next: E4. What foresight does not do.

foresight is available under your choice of license: GPL-3.0-or-later, BSD-2-Clause, BSD-3-Clause or MIT (License).