clear
set verbose off
open denmark -q

include multiplot.gfn

# Select an example case
CASE = 1

# Fine tune the graph if wished
bundle options = _(FONT_SIZE=10, PLOT_HEIGHT=700)


if CASE == 1
    setopt gnuplot persist --time-series --with-lines
    gnuplot LRM --output=tf1.gp { set title "foo" ; set yrange[10:12]; \
      set ylabel "ylabel"; set linetype 1 lw 2 pt 6 lc rgb "blue" ; }
    gnuplot LRY --output=tf2.gp { set title "foo" ; set xlabel "xlabel"; }
    gnuplot IBO --output=tf3.gp { set title "foo" ; set grid ; }
    gnuplot IDE --output=tf4.gp
elif CASE == 2
    qqplot LRM --output=tf1.gp
    qqplot LRY --output=tf2.gp
    qqplot IBO --output=tf3.gp
    qqplot IDE --output=tf4.gp
elif CASE == 3
    qqplot LRM --output=tf1.gp
    qqplot LRY --output=tf2.gp
    gnuplot IBO --output=tf3.gp --time-series --with-lines \
      { set title "foo" ; set grid ; }
    gnuplot IDE --output=tf4.gp --time-series --with-lines
elif CASE == 4						# qqplot FAILS
    gnuplot IBO --output=tf1.gp --time-series --with-lines \
      { set title "foo" ; set yrange[-10:10];  set ylabel "ylabel"; }
    gnuplot IDE --output=tf2.gp --time-series --with-lines \
      { set title "foo" ; set xlabel "xlabel"; }
    qqplot IBO --output=tf3.gp
    freq IDE --plot=tf4.gp
elif CASE == 5
    var 2 LRM LRY --silent
    matrix irf_11 = irf(1, 1, 0.1)
    matrix irf_12 = irf(1, 2, 0.1)
    gnuplot 1 2 3 --matrix=irf_11 --time-series --with-lines --output=irf_11.gp
    gnuplot 1 2 3 --matrix=irf_12 --time-series --with-lines --single-yaxis --output=irf_12.gp
    strings filenames = defarray("irf_11.gp","irf_12.gp")
    multiplot(filenames, "display")
endif


# Collect all filenames into an array
strings filenames = defarray("tf1.gp","tf2.gp","tf3.gp","tf4.gp")

# Automatic grid determination + instant output on screen
string Gin = multiplot(filenames, "display", , , options)

# Automatic grid determination + pdf output
Gin = multiplot(filenames, "my.pdf", , , options)
# print Gin               # uncomment for printing gnuplot command

# Multiplot with 1 row + svg output
Gin = multiplot(filenames, "my.svg", 1, , options)

# Multiplot with 4 rows + png output
Gin = multiplot(filenames, "foo.png", 4, , options)

# Automatic grid determination + eps output
Gin = multiplot(filenames, "my.eps", , , options)


# Show the new feature from version 0.3
# which requires gretl functionality from 2023
if $version > 20222
    string a,b,c,d
    qqplot LRM --buffer=a
    qqplot LRY --buffer=b
    qqplot IBO --buffer=c
    qqplot IDE --buffer=d
    multiplot(defarray(a,b,c,d),,,, options)
endif