!goto $wims_read_parm

:def
title=Histogram
synonyme=plot frequency, frequency plot
input=data1d
!exit

:proc

cnt=!itemcnt $formula
range=!trim $range
!default range=$range1
range=!trim $range
!default range=1
range=$[$range]
data1=!item 1 of $formula
data1=$[$data1]
!if NaN isin $range or Inf isin $range or \
	NaN isin $data1 or Inf isin $data1 or \
	$range*10000<abs($data1)
 error=bad_formula
 !exit
!endif

list=!values (floor(x/$range)+0.5)*($range) for x in $formula
!if NaN isin $list or Inf isin $list
 error=bad_formula
 !exit
!endif
list=!sort numeric item $list

x1=!item 1 of $list
x2=!item -1 of $list
y1=0
y2=1

old=$x1
ocnt=1
!reset pdata
!for i=2 to $cnt
 curr=!item $i of $list
 !if $curr=$old
  !advance ocnt
 !else
  pdata=!append line $old $ocnt to $pdata
  y2=$[max($y2,$ocnt)]
  old=$curr
  ocnt=1
 !endif
!next i
pdata=!append line $old $ocnt to $pdata
insplot_data=$pdata

y2=$[ceil($y2*1.2)]
xdiff=$[max($range,0.1*($x2-($x1)))]
x1=$[$x1-$xdiff]
x2=$[$x2+$xdiff]

insplot_set=size $[$picx/500], $[$picy/400]; boxwidth $range
!insplot [$x1:$x2] [$y1:$y2] 'insplot_data' notitle with boxes
result=$ins_out
!if getins notin $result
 result=
!endif

!exit

:output

<p class="wimscenter">
$result
</p>
<p>
Interval = $range. Data: $cnt numbers { $formula }
</p>
!exit
