svgNS="http://www.w3.org/2000/svg";


function CreateButton(text,x,y,str) {
 var txtel=SVGDoc.createElementNS(svgNS,'text')
 txtel.appendChild(SVGDoc.createTextNode(text))
 var ellheight=txtel.getBBox().height;
 var ellwidth=txtel.getBBox().width
 var ell=SVGDoc.createElementNS(svgNS,'rect')
 ell.setAttribute("x",x-ellwidth/2*1.5)
 ell.setAttribute("y",y-ellheight/2*1.5)
 ell.setAttribute("width",ellwidth*1.5)
 ell.setAttribute("height",ellheight*1.5)
 ell.addEventListener("click",new Function(str),false)
 txtel.setAttribute("x",x-ellwidth/2)
 txtel.setAttribute("y",(+y)+5)
 txtel.addEventListener("click",new Function(str),false)
 SVGDoc.documentElement.appendChild(ell)
 SVGDoc.documentElement.appendChild(txtel)
 return ell
}

_PIE_Fills=['#eeeeff','#eeffee','#ffeeee','#ccffff']
function DrawPie(x,y,data,tStr) {
 pie=SVGDoc.getElementById('piegraph')
 if (pie) pie.parentNode.removeChild(pie)
 var pie=SVGDoc.createElementNS(svgNS,'g')
 total=0
	for (var i=0;i<data.length;i++) {
  total+=data[i].value
	}
 var startAngle=0
	for (var i=0;i<data.length;i++) {
  var slice=SVGDoc.createElementNS(svgNS,'path')
  slice.setAttribute('style','fill:'+_PIE_Fills[i%_PIE_Fills.length])
  endAngle = startAngle - data[i].value * Math.PI * 2 / total;
  if (Math.abs(endAngle-startAngle)>Math.PI) ang=1
   else ang=0
   var d="M0,0 L"+(100*Math.cos(startAngle))+','+(100*Math.sin(startAngle))+' A100,100 0,'+ang+' 0 '+(100*(Math.cos(endAngle)))+','+(100*(Math.sin(endAngle)))+'Z';
  slice.setAttribute("d", d);
  pie.appendChild(slice)
  midAngle=(startAngle+endAngle)/2; 
  var label=SVGDoc.createElementNS(svgNS,'text')
  label.appendChild(SVGDoc.createTextNode(data[i].label+' '+String.fromCharCode(0xA3)+data[i].value))
  label.setAttribute("x", Math.round(140*Math.cos(midAngle))-label.getBBox().width/2);
  label.setAttribute("y", Math.round(120*Math.sin(midAngle)-label.getBBox().height/2));
  pie.appendChild(label)
  startAngle=endAngle        
	}
 var title=SVGDoc.createElementNS(svgNS,'text')
 title.appendChild(SVGDoc.createTextNode(tStr))
 title.setAttribute("x", 0-title.getBBox().width/2);
 title.setAttribute("y", "-140");
 title.setAttribute("stroke","red")
 pie.appendChild(title)
 pie.setAttribute('style','stroke:black;stroke-width:1px;fill-rule:evenodd')
 pie.setAttribute('transform','translate('+x+' '+y+')')
 pie.setAttribute('id','piegraph')
 SVGDoc.documentElement.appendChild(pie)
}

function LineGraph(x,y,xt1,yt1,xt2,yt2,height,width,tStr) {
 var xtics=30 
 var ytics=10
 var path=''
 var g=SVGDoc.createElementNS(svgNS,'g') 
 this.Graph=g
 var key=SVGDoc.createElementNS(svgNS,'g')
 var lineCount=0
	this.AddLine=function(c,x,y,keyTitle) {
 var Line={}
  var Data=SVGDoc.createElementNS(svgNS,'g')
  var Path=SVGDoc.createElementNS(svgNS,'path')
  Line.Path=Path
  Line.Path.setAttribute('stroke',c)
  Line.Path.setAttribute('fill','none')
  Line.Path.setAttribute('d','M '+(x-xt1)*(width/(xt2-xt1))+' '+(height-(y-yt1)*(height/(yt2-yt1))))
  Data.appendChild(Line.Path)
  g.appendChild(Data)
		Line.AddData=function(x,y) {
   Line.Path.setAttribute('d',Line.Path.getAttribute('d')+' L '+(x-xt1)*(width/(xt2-xt1))+' '+(height-(y-yt1)*(height/(yt2-yt1))))
		}
  ktext=SVGDoc.createElementNS(svgNS,'text')
  ktext.appendChild(SVGDoc.createTextNode(keyTitle))
  ktext.setAttribute("stroke",c)
  ktext.setAttribute("stroke-width","0.5")
  ktext.setAttribute("fill",c)
  ktext.setAttribute("y",(lineCount++)*20)
  key.appendChild(ktext)
  return Line
	}

 var title=SVGDoc.createElementNS(svgNS,'text')
 title.appendChild(SVGDoc.createTextNode(tStr))
	if (typeof title.getBBox!='undefined') try { twidth=title.getBBox().width } catch (e) {twidth=200 }
  else twidth=200
 title.setAttribute("x", width/2-twidth/2);
 title.setAttribute("y", "-20");
 title.setAttribute("stroke","red")

 g.appendChild(title)
 g.appendChild(DrawAxis())
 g.setAttribute("transform","translate("+x+" "+y+")")
 key.setAttribute("transform","translate("+(+x+width+10)+" "+y+")")
 SVGDoc.documentElement.appendChild(g)
 SVGDoc.documentElement.appendChild(key)
 return this

	function DrawAxis() {
  var axis=SVGDoc.createElementNS(svgNS,'g')
  line=SVGDoc.createElementNS(svgNS,'path')
  line.setAttribute('stroke','black')
  line2=SVGDoc.createElementNS(svgNS,'path')
  line2.setAttribute('stroke','black')
  path="M 0 "+height
 	for (var i=0;i<xtics;i++) {
   path+='l 0 5 l 0 -5 l '+width/xtics+' 0'
   t=SVGDoc.createElementNS(svgNS,'text')
   t.appendChild(SVGDoc.createTextNode((xt1+i*((xt2-xt1)/xtics)).toPrecision(2)))
 if (typeof t.getBBox!='undefined') {
  try {
   twidth=t.getBBox().width
   theight=t.getBBox().height
  } catch (e) {
   twidth=20
   theight=20
		}
 } else {
  twidth=20
  theight=20
 }
   t.setAttribute("x",i*width/xtics-twidth/2)
   t.setAttribute("y",height+10+theight)
   axis.appendChild(t)
 	}
  path+='l 0 5 l 0 -5'
  line.setAttribute('d',path)
  t=SVGDoc.createElementNS(svgNS,'text')
  t.appendChild(SVGDoc.createTextNode((xt1+i*((xt2-xt1)/xtics)).toPrecision(2)))
 
 if (typeof t.getBBox!='undefined') {
  try {
   twidth=t.getBBox().width
   theight=t.getBBox().height
  } catch (e) {
   twidth=20
   theight=20
		}
 } else {
  twidth=20
  theight=20
 }

  t.setAttribute("x",i*width/xtics-twidth/2)
  t.setAttribute("y",height+10+theight)
  axis.appendChild(t)
  line2=SVGDoc.createElementNS(svgNS,'path')
  line2.setAttribute('stroke','black')
  path="M 0 "+height
 	for (var i=0;i<ytics;i++) {
   path+='l -5 0 l 5 0 l 0 -'+height/ytics
   t=SVGDoc.createElementNS(svgNS,'text')
   t.appendChild(SVGDoc.createTextNode((yt1+i*((yt2-yt1)/ytics)).toPrecision(4)))
 if (typeof t.getBBox!='undefined') {
  try {
   twidth=t.getBBox().width
   theight=t.getBBox().height
  } catch (e) {
   twidth=20
   theight=20
		}
 } else {
  twidth=20
  theight=20
 }
   t.setAttribute("y",height-i*height/ytics+theight/2)
   t.setAttribute("x",-10-twidth)
   axis.appendChild(t)
 	}
  path+='l -5 0 l 5 0'
   t=SVGDoc.createElementNS(svgNS,'text')
   t.appendChild(SVGDoc.createTextNode((yt1+i*((yt2-yt1)/ytics)).toPrecision(4)))
 if (typeof t.getBBox!='undefined') {
  try {
   twidth=t.getBBox().width
   theight=t.getBBox().height
  } catch (e) {
   twidth=20
   theight=20
		}
 } else {
  twidth=20
  theight=20
 }
   t.setAttribute("y",height-i*height/ytics+theight/2)
   t.setAttribute("x",-10-twidth)
   axis.appendChild(t)
  line2.setAttribute('d',path)
  axis.appendChild(line)
  axis.appendChild(line2)
  return axis
 }

} 



