!if $wims_read_parm!=slib_header
!goto proc
!endif
slib_title=Histogram
slib_parms=3\
,[data]\
,[list of numbers] which determine the intervals, or [first end, width]\
,option : noxtics, xtics  [optional liste] ; noytics, ytics number of graduations ;baton, color [red by default]

slib_author=Adeline Grelot and Bernadette PERRIN-RIOU
slib_out=data for plot
slib_synonyme=plot frequency, frequency plot
slib_comment=The first data must be the effectifs. So stat/effectif may be used before. The output must be used with the command \draw (in first position). If xtics is followed by a number n, there are n graduations. Il it is followed by a vector, the graduations are on the intervalles and the text is those given in the vector.In the case of baton diagramm, the subdivisions should be regular. 
slib_example= [1,5,2,3,10,13],[3,4,6,7,12,14],xtics 4,ytics 10, color salmon
[1,5,2,3,10,13],[3,4,6,7,12,14],xtics [a,b,c,d,e,f,g],ytics 10, color salmon
!exit

:proc

slib_parm=
slib_out=
!reset slib_optx, slib_opty, slib_couleur,slib_baton,slib_pdata, slib_grad,slib_listx,slib_listxx, slib_param, slib_parm

slib_data=!item 1 to 2  of $wims_read_parm
slib_parm= !item 3 to -1 of $wims_read_parm

!readproc slib/stat/dataproc $slib_data

slib_u= $slib_x
slib_w=$slib_y

slib_u=!declosing $slib_u
slib_cntu=!itemcnt $slib_u
slib_w=!declosing $slib_w

!if $slib_w=$empty 
	slib_w=!values x for x=1 to $slib_cntu
!endif

slib_cnt=!itemcnt $slib_w 
!if $slib_cnt =2
	slib_begin=!item 1 of $slib_w
	slib_pas=!item 2 of $slib_w
	slib_w=!values $slib_begin+x*$slib_pas for x = 0 to $[$slib_cntu+1]
	slib_cnt=!itemcnt $slib_w 
!endif 
slib_w=!sort numeric item $slib_w
slib_x1=!item 1 of $slib_w 
slib_x2=!item -1 of $slib_w

slib_ocnt=!itemcnt of $slib_parm
!for slib_i=1 to $slib_ocnt
	slib_param=!item $slib_i of $slib_parm
	slib_param=!words2items  $slib_param
	!if xtics isitemof $slib_param
		slib_optx=1
		slib_param=!replace xtics by $  in $slib_param
		slib_param=!nonempty item $slib_param
		slib_param=!nospace $slib_param
		slib_param=!declosing $slib_param  
		slib_param=!nonempty item $slib_param
		slib_c=!itemcnt $slib_param
	
		!if $slib_c=1
			slib_arrondi=$[ceil(($slib_x2-$slib_x1)/$slib_param)]
			slib_cnt_arr=0
				!if $slib_arrondi<1
					!for  slib_u=1 to 10
						!if $[floor($slib_arrondi)] <>0
						!break
						!else
							!advance slib_cnt_arr
							slib_arrondi=$[$slib_arrondi*10]	
						!endif
					!next slib_u
					slib_arrondi=$[$slib_arrondi/10^($slib_cn_arr+1)]
				!else
					!for  slib_u=1 to 10
						!if $[floor($slib_arrondi)] =0
							!break
						!else
							!advance slib_cnt_arr
							slib_arrondi=$[$slib_arrondi/10]	
						!endif
					!next slib_u	
					slib_arrondi=$[$slib_arrondi*10^(-2+$slib_cn_arr)]		
				!endif
			
			slib_listx=!values $[$slib_arrondi*floor($slib_x1/$slib_arrondi)]+x*$slib_arrondi for x=0 to $[$slib_param+1]
			slib_listxx=$slib_listx
		!else
			slib_listx= $slib_param
		!endif	
	!endif xtics
	!if ytics isitemof $slib_param
		slib_opty=1
		slib_param=!replace ytics by $  in $slib_param
		slib_param=!nonempty item $slib_param
		slib_param=!words2items  $slib_param
		slib_param=!declosing $slib_param 
		slib_param=!nonempty item $slib_param 
		slib_pcnt=!itemcnt $slib_param
		!if $slib_pcnt=1
			slib_ra=$slib_param
		!endif
	!endif ytics

	!if color isitemof $slib_param
		slib_param=!replace color by $  in $slib_param
		slib_param=!nonempty item $slib_param
		couleur=$slib_param
	!else 
		couleur=$empty
	!endif

	!if baton isitemof  $slib_parm
		slib_baton=baton
	!else
		slib_baton=$empty
	!endif
!next slib_i

!default slib_optx=0
!default slib_opty=0
!default couleur=red
!default slib_listx=$slib_w

!default slib_ra=5
slib_listx=!declosing $slib_listx
slib_y1=0
slib_y2=0
slib_uu=

!if $slib_baton notsametext $empty
	slib_ww=
	!for slib_i= 1 to  $slib_cntu
		slib_r=$[($slib_x2-$slib_x1)/(($slib_cntu)*4)]
		slib_j=!item $slib_i of $slib_w
		slib_ww=  !append item $[$slib_j+1-$slib_r],$[$slib_j+1+$slib_r] to $slib_ww
		slib_j=!item $slib_i of $slib_u
		slib_uu=!append item $slib_j,0 to $slib_uu
	!next slib_i
	slib_uu=!append item 0 to $slib_uu
	slib_w1=$slib_ww
	slib_ww=$slib_w
	slib_w=$slib_w1
	slib_x1=!item 1 of $slib_w 
	slib_x2=!item -1 of $slib_w
	slib_y2=!sort numeric item $slib_u
	slib_y2=!item -1  of $slib_y2
	slib_cnt=!itemcnt $slib_w
	slib_cnt=$[$slib_cnt+1]
!else
	slib_uu=
	!for slib_i=1 to $[$slib_cntu]
		slib_it=!item $slib_i of $slib_u
		slib_inf=!item $slib_i of $slib_w
		slib_sup=!item $[$slib_i+1] of $slib_w
		slib_it=$[$slib_it/($slib_sup-$slib_inf)]
		slib_y2=$[max($slib_y2,$slib_it)]
		slib_uu=!append item $slib_it to $slib_uu
	!next slib_i
	slib_ww=$slib_w
!endif
slib_pas=$[ceil($slib_y2/$slib_ra)]
slib_ord=0
slib_d=$[($slib_x2-$slib_x1)/5]
slib_minx=$[$slib_x1*1.01]

!if $slib_opty !=0
	!for slib_i=1 to $slib_ra
		slib_ord=$[$slib_pas+$slib_ord]
		!if $slib_ord <$[$slib_y2+$slib_pas]
			slib_grad=!append line text black,$[$slib_minx-$slib_d],$slib_ord,small,$slib_ord to $slib_grad
			slib_grad=!append line segment $[$slib_minx-$slib_d/5],$slib_ord,$[$slib_minx+$slib_d/5],$slib_ord,black\
vline $slib_minx,0,black to $slib_grad
		!endif
	!next slib_i
!endif

!if $slib_optx!=0 
	!for slib_i=1 to $[$slib_cntu+1]
		slib_wi=!item $slib_i of $slib_listx
		!if $slib_listxx=$empty
			slib_listxx=$slib_ww
		!endif	
		slib_wj=!item $slib_i of $slib_listxx
		!if $slib_baton notsametext  $empty
			slib_wj=$[$slib_wj+1]
		!endif
		slib_grad=!append line text black,$slib_wj,$[$slib_y1-($slib_y2-$slib_y1)/10],small,$slib_wi to $slib_grad
		slib_grad=!append line line $slib_wj,$[$slib_y1-($slib_y2-$slib_y1)/30],$slib_wj,$[$slib_y1+($slib_y2-$slib_y1)/30], black to $slib_grad
	!next slib_i
!endif


slib_pdata=xrange $[$slib_minx-2*$slib_d],$[$slib_x2+$slib_d]\
yrange $[min(1,$slib_y1-($slib_y2-$slib_y1)/5)],$[$slib_y2]\
$slib_grad\
lines $couleur

!for slib_i=1 to $[$slib_cnt-1]
	slib_inf=!item $slib_i of $slib_w
	slib_sup=!item $[$slib_i+1] of $slib_w
	slib_f=!item $slib_i of $slib_uu
	slib_pdata=!append  item $slib_inf,0,$slib_inf,$slib_f,$slib_sup,$slib_f,$slib_sup,0 to $slib_pdata
!next slib_i

slib_out=$slib_pdata\
hline 0,0,black