
// version date 5/3/02

divgraphimagedir="." // "../divgraph"

function quickplot(stype){
 var sdata=getdata()
 if(sdata.length==0)return
 var Info=new Array()
 Info.imagedir=divgraphimagedir
 Info.xvar=xyz[0]
 Info.yvar=xyz[1]
// Info.doxtics=Info.doytics=false
 Info.dotrendline=true
 Info.trendlabely=30
 Info.trendlabelx=50
 Info.ptsize=6
 if(stype==null)stype="y vs. x"
 if(stype=="?"){
	var y=prompt("What would you like to use for the vertical axis?","y")
	if(y==null || y.length==0)return
	var x=prompt("What would you like to use for the horizontal axis?","x")
	if(x==null || x.length==0)return
	var rx=new RegExp("/"+xyz[0]+"/g")
	var ry=new RegExp("/"+xyz[1]+"/g")
	x=x.replace(rx,"x").replace(ry,"y")
	y=y.replace(rx,"x").replace(ry,"y")
	stype=y + " vs. " + x
 }
 stype=stype.replace(/ /g,"")
 if(stype=="yvs.x")selectfunc("LINE",0)
 if(stype=="ln(y)vs.x")selectfunc("1L-",0)
 if(stype=="1/yvs.x")selectfunc("2L-",0)
 if(stype=="ln(y)vs.1/x")selectfunc("ln(y)=ln(a)-b/x",0)
 if(stype=="xvs.y")selectfunc("LINE",1)
 if(stype=="ln(x)vs.y")selectfunc("1L-",1)
 if(stype=="1/xvs.y")selectfunc("2L-",1)
 if(stype=="ln(x)vs.1/y")selectfunc("ln(y)=ln(a)-b/x",1)
 Info.datatransform=stype
 GRclosegraphwindow()
 GRcleargraph(true)
 Info.debug=false//(document.info.o_debug.checked)
 GRopengraphwindow()
 GRdrawgraph(sdata,"","",Info)
 GRclosegraphdocument()  
}

function selectfunc(stype,swap){
 usr.r_apdata[swap].checked=true
 finditem(usr.ssolve,stype,-1,1,1)
 doBtn("load")
}

function getdata(){
 var n__=getxyzdata(true)
 var s=""
 for (i=0;i<n__;i++)s+=xyv[i][0]+","+xyv[i][1]+"\n"
 return s
}

function dograph(gtype)
{ //-1:trace 0: full data
 var istrace=(gtype==-1)
 var is_display=true
 var is_local=true
 var sres=""
 var withdata=((istrace || getinfo(true)) && is_data)

 if(!istrace && stheory.length && is_theory){
  sres=stheory
  if(sresults.length && sr_==stheory){
   sres=sresults+"//:"
   sres=sres.substring(0,sres.indexOf("//:"))
   sres=smodel+"<br>"+strsub(strsub(strsub(sres,"\r","<br>"),"\n","</br>"),"//","")
  }
 }
  
 jsgraph(withdata,istrace,sflags,sres)
 
}

function jsgraph(withdata,istrace,sflags,sres){
	var x=0
	var y=0
	var n=0
	swidth=parseInt(swidth)
	sheight=parseInt(sheight)
	GRinit()
	GRopengraphwindow(swidth+200,sheight+150)
	Data=new Array() 
	Labels=new Array()
	Info=new Array()
	Info.doptlabels=Info.dopoints=true
	Info.dogridx=Info.dogridy=true
	Info.title="<center><h3>"+stitle+"</h3>"
	if(stitle1.length)Info.title+="<b>"+stitle1+"</b>"
	if(sres.length)Info.title+="<br><b>"+sres+"</b>"
	Info.title+="<br><a href=javascript:print()>Print</a>&nbsp;&nbsp;<a href=javascript:close()>Close</a></center>"
	Info.xaxislabel=xlabel
	Info.yaxislabel=ylabel
	Info.defaultcolor="blue"
	Info.graphwidth=swidth
	Info.graphheight=sheight
	Info.graphtop=100
	Info.graphleft=100
	Info.ticwidth=1
	Info.maxaddpoints=0
	Info.ptsize=4
	Info.linewidth=2
	Info.docurve=(sflags.indexOf("ALLLINES")>=0)
	Info.curvecolor=""
	Info.dopoints=!Info.docurve
	Info.dotrendline=(sflags.indexOf("DOTREND")>=0)
	Info.imagedir=divgraphimagedir
	Info.doxticnums=(sflags.indexOf("NOXNUMS")<0)
	Info.doyticnums=(sflags.indexOf("NOYNUMS")<0)

//limits

	Info.xmin=NaN
	Info.xmax=NaN
	Info.ymin=NaN
	Info.ymax=NaN

  var s=xlimits
  if (s!="from auto"){
	var v=s.split(" ")
	Info.xmin=eval(v[1])
	Info.xmax=eval(v[3])
	Info.xticmajor=eval(v[5])
  }

  var s=ylimits
  if (s!="from auto"){
	var v=s.split(" ")
	Info.ymin=eval(v[1])
	Info.ymax=eval(v[3])
	Info.yticmajor=eval(v[5])
  }

//data

	var colors=new Array("red","blue","green","purple","teal","brown","skyblue","yellow","black")

	Info.keyx=10
	Info.keyy=100
	Info.key=""
	Info.trendlabelx=400
	Info.trendlabely=-50
	Info.xvar=Info.xlabel
	Info.yvar=Info.ylabel

        plotfunc(Data,Labels,withdata,istrace,false)

	var i=1
//	var sc=colors[i%colors.length]
//	Info.key+=sc+"="+Vlist[i]+";"
	Info.xnumdigits=0
	Info.xticmajor=Info.xticminor=Info.yticmajor=Info.yticminor="auto"
	Info.xticfirst=Info.yticfirst=NaN

//Info.debug=true

//data

	Info.keepopen=true
	GRdrawgraph("",Data,Labels,Info)

//curve

	Info.docurve=true
	Info.dopoints=false
	Info.dotrendline=false

	Data=new Array() 
	Labels=new Array()

        plotfunc(Data,Labels,false,istrace,true)

	if(Data.length){
		Info.append=true
		Info.linewidth=1
		Info.curvecolor="red"
		GRdrawgraph("",Data,Labels,Info)
	}

	GRclosegraphdocument()
	return 1
}



function plotfunc(Data,Labels,withdata,istrace,docurve)
{
 xfunc=mathof(sxtext,false)
 yfunc=mathof(sytext,false)
 var j_=0
 var d1_=1E199
 var d2_=-1E199
 var x_=0
 var y_=0
 if(!istrace && n__>0)
 {
  for(var i__=0;i__<n__;i__++)
  {
   for(iq_=0;iq_<nq_;iq_++)eval(xyz[iq_]+"="+xyv[i__][iq_])
   x_=eval(xfunc)
   y_=eval(yfunc)
   if(withdata)plotxy(Data,x_,y_,0,Labels,"("+x_+","+y_+")")
   x_=xyv[i__][0]
   d1_=Math.min(d1_,x_)
   d2_=Math.max(d2_,x_)
   j_++
  }
 }
 if (!is_theory || stheory.length==0)return
 if (withdata || !docurve)return
 if(istrace || n__==0)
 {
  var s=(istrace?xlimits:usr.txlimits.value)
  var v=s.split(" ")
  d1_=eval(v[1])
  d2_=eval(v[3])
 }
 var ds_=(d2_-d1_)/50 
 var sm=mathof(stheory,false)
 if(ds_==0 || d2_<d1_) return
 var sx_=xyz[0]
 for(var x__=d1_;x__<=d2_;x__+=ds_)
 {
  eval(sx_+"="+x__)
  var ex_=eval(xfunc)
  //ex_=roundoff(ex_,-5)
  var ey_=eval(sm)
  //ey_=roundoff(ey_,-5)
  plotxy(Data,ex_,ey_,1,Labels,"")
  j_++
 }
 return
}

function dospecial(iwhich)
{
 if(iwhich & 1)
 {
  var s=sxtext
  if(s=="" || s=="special")s="x"
  var sx=prompt("Special function for horizontal axis?",s)
  if (sx!=null)
  {
   if(sx=="" ||sx=="special")sx="x"
   usr.sxfunc.options[usr.sxfunc.length-1].text=sx
   usr.sxfunc.options[usr.sxfunc.length-1].value=sx
   usr.sxfunc.selectedIndex=usr.sxfunc.length-1 
  }
 }
 if(iwhich & 2)
 {
  var s=sytext
  if(s=="" || s=="special")s="y"
  var sy=prompt("Special function for vertical axis?",s)
  if (sy!=null)
  {
   if(sy=="" ||sy=="special")sy="y"
   usr.syfunc.options[usr.syfunc.length-1].text=sy
   usr.syfunc.options[usr.syfunc.length-1].value=sy
   usr.syfunc.selectedIndex=usr.syfunc.length-1
  }
 }
}

function plotxy(Data,x,y,ch,Labels,lbl)
{
 if(lbl.length)Labels[Data.length]=lbl
 Data[Data.length]=new Array(x,y,ch)
}



