set terminal svg size 1920,1080 font "Linux Biolinum,20"
set output "<%= svg.path %>"

set multiplot layout 2,1

set border 11

set xdata time
set timefmt "%Y-%m-%d"
set format x "%Y-%m"
set grid
set lmargin 6

set xrange [ "<%= @weights.first %>" : "<%= plot_end %>" ]
set yrange [ <%= [goal-1, @weights.min].min.floor %> : <%= [goal+1, @weights.max].max.ceil %> ]
set ytics 1 nomirror
set mxtics 1
set xtics 2592000 nomirror

set obj 1 rectangle behind from screen 0,0 to screen 1,1
set obj 1 fillstyle solid 1.0 fillcolor rgb "white" linewidth 0

set obj 2 rectangle behind from "<%= @weights.first %>",<%= goal-1 %> to "<%= plot_end %>",<%= goal+1 %>
set obj 2 fillstyle solid 0.2 fillcolor rgb "green" linewidth 0

plot <%= goal %> t "Goal" lc rgb "forest-green" lw 2, \
"<%= weight_dat.path %>" using 1:2 w points t "Weight" pt 13 ps 0.3 lc rgb "navy", \
"<%= weight_dat.path %>" using 1:3 w l t "" lt 1 lw 2 lc rgb "navy", \
"<%= weight_dat.path %>" using 1:4 w points t "" lc rgb "navy" pt 7 ps 0.2

unset obj 1

set yrange [ 0 : <%= quantize((@weights.first..@weights.last).map{|d| [consumed_at(d), allowed_kcal(d, 0), allowed_kcal(d, rate)]}.flatten.max) %> ]
set ytics <%= quantize(200) %> nomirror
set key right bottom

plot "<%= input_dat.path %>" using 1:2:(0):($4-$2) w vectors nohead lc rgb "navy" notitle, \
"<%= input_dat.path %>" using 1:2 w points pt 13 ps 0.3 lc rgb "navy" t "Consumed energy", \
"<%= input_dat.path %>" using 1:3 w lines lc rgb "black" t "0 kg/week", \
"<%= input_dat.path %>" using 1:4 w lines lc rgb "forest-green" lw 2 t "<%= rate %> kg/week"

unset multiplot
