/*
ASCII2MathML-SVGPlotAxis.js v3.41, May 13th 2009
=====================================================================
Copyright Dr.ir. S.A. Miedema, www.Fullxml.org

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
(at http://www.gnu.org/licences/lgpl.html) for more details.
*/

function setBorder(x) { border = x } 
function setMargin(x) { border = x } 

function setGraphType(setTypeOfGraph) {
  TypeOfGraph = setTypeOfGraph;
}

function setViewport(setWidth,setHeight,setMargin,andGraphArea) {
  width=setWidth;
  height=setHeight;
  border=setMargin;
  legendheight = 0;
  piechartitems = [];
  dotitems = [];
  if (andGraphArea!=null && andGraphArea) setGraphArea(0,width,0,height);
}

function viewportBorder(borderwidth,bordercolor) {
  stroke=bordercolor;
  strokewidth=borderwidth;
  mrect([strokewidth-1,strokewidth-1],[width-strokewidth+1,height-strokewidth+1]);
}

function setGraphArea(setXmin,setXmax,setYmin,setYmax) {
  xmin=setXmin;
  xmax=setXmax;
  ymin=setYmin;
  ymax=setYmax;
}
function drawGraphArea() {
  axes();
}

function axesBorder(borderwidth,bordercolor) {
  stroke=bordercolor;
  strokewidth=borderwidth;
  rect([xmin,ymin],[xmax,ymax]);
}

function setGridandTicks(setXGrid,setYGrid,setXTick,setYTick,setTickLength) {
  xgrid=setXGrid;
  ygrid=setYGrid;
  xtick=setXTick;
  ytick=setYTick;
  if (setTickLength!=null) ticklength=setTickLength;
}

function setLabelsandTitle(setAxis,setXLabel,setYLabel,setTitle) {
  graphAxis = setAxis;
  graphXLabel = setXLabel;
  graphYLabel = setYLabel;
  graphTitle = setTitle;
}

function setLegend(setHeight, setDirection, setMargin, setItems, setDirectionItems) {
  legendheight = setHeight;
  legenddirection = setDirection;
  legendmargin = setMargin;
  legenditems = setItems;
  legenddirectionitems = setDirectionItems;
}

function legendBorder(borderwidth,bordercolor) {
  stroke=bordercolor;
  strokewidth=borderwidth;
  mrect([axisborder,0],[width-axisborder,legendheight]);
}

function drawLegendItem(setItem,itemText,itemType) {
  var lx, ly, rows, cols, row, col, p, rowheight, colwidth;
  if (legenddirection=="horizontal") {
    cols = legenddirectionitems;
    rows = ceil(legenditems/legenddirectionitems-0.01);
    row = ceil(setItem/legenddirectionitems -0.01);
    col = (setItem-(row-1)*legenddirectionitems);
  } else {
    rows = legenddirectionitems;
    cols = ceil(legenditems/legenddirectionitems-0.01);
    col = ceil(setItem/legenddirectionitems -0.01);
    row = (setItem-(col-1)*legenddirectionitems);
  }
  rowheight = (legendheight-2*legendmargin)/rows;
  colwidth = (width-2*axisborder-2*legendmargin)/cols;
  lx = axisborder+legendmargin+(col-1)*colwidth;
  ly = height-(legendheight-legendmargin-(row-0.5)*rowheight);
  lx = (lx-origin[0])/xunitlength;
  ly = (height-ly-origin[1])/yunitlength;
  if (itemType=="line") {
    p = [lx,ly];
    q = [lx+0.18,ly];
    line(p, q, "legend_line"+setItem);
  }
  if (itemType=="rect") {
    p = [lx,ly-0.05];
    q = [lx+0.18,ly+0.05];
    setFill(stroke,1);
    rect(p, q, "legend_rect"+setItem);
    setFill("none",1);
  }
  p = [lx+0.2,ly];
  text(p,itemText,"rightmiddle","legend_text"+setItem,null,null);
}

function axes(dx,dy,labels,gdx,gdy,xlabel,ylabel,title) {
  if (graphAxis!=null && labels==null) labels=graphAxis;
  if (graphXLabel!=null && xlabel==null) xlabel=graphXLabel;
  if (graphYLabel!=null && ylabel==null) ylabel=graphYLabel;
  if (graphTitle!=null && title==null) title=graphTitle;
  if (labels==null) labels=Automatic;
  if (xlabel==null) xlabel="";
  if (ylabel==null) ylabel="";
  if (title==null) title="";
  if (TypeOfGraph==LinearLinear) linlinaxes(dx,dy,labels,gdx,gdy,xlabel,ylabel,title);
  if (TypeOfGraph==LinearLogaritmic) linlogaxes(dx,dy,labels,gdx,gdy,xlabel,ylabel,title);
  if (TypeOfGraph==LogaritmicLinear) loglinaxes(dx,dy,labels,gdx,gdy,xlabel,ylabel,title);
  if (TypeOfGraph==LogaritmicLogaritmic) loglogaxes(dx,dy,labels,gdx,gdy,xlabel,ylabel,title);
  if (TypeOfGraph==Polar) polaraxes(dx,dy,labels,gdx,gdy,xlabel,ylabel,title);
}

function linlinaxes(dx,dy,labels,gdx,gdy,xlabel,ylabel,title) {
  var x, y, ldx, ldy, lx, ly, lxp, lyp, pnode, st, xlblx, xlbly, xlblhor, xlblver, ylblx, ylbly, ylblhor, ylblver, oldfontsize, logx, logy, xoffset, yoffset, titlex, titley;
  if (!initialized) initPicture();
  if (typeof dx=="string") { dx = null; }
  if (typeof dy=="string") { gdx = dy; dy = null; }
  if (xscl!=null) {dx = xscl; gdx = xscl;}
  if (yscl!=null) {dy = yscl; gdy = yscl}
  if (xtick!=null) {dx = xtick}
  if (ytick!=null) {dy = ytick}
  dx = (dx==null?xunitlength:dx*xunitlength);
  dy = (dy==null?dx:dy*yunitlength);
  if (xgrid!=null) gdx = xgrid;
  if (ygrid!=null) gdy = ygrid;
  if (xlabel==null) xlabel="";
  if (ylabel==null) ylabel="";
  if (title==null) title="";
  strokewidth=1;
  stroke="black";
  rect([xmin,ymin],[xmax,ymax]);
  // Axes
  if (gdx!=null) {
    gdx = (typeof gdx=="string"?dx:gdx*xunitlength);
    gdy = (gdy==null?dy:gdy*yunitlength);
    pnode = myCreateElementSVG("path");
    st="";
    for (x = origin[0]; x<=width-axisborder+0.001; x = x+gdx)
      if (x>=axisborder) st += " M"+x+","+axisborder+" "+x+","+(height-axisborder-legendheight);
    for (x = origin[0]-gdx; x>=0+axisborder; x = x-gdx)
      st += " M"+x+","+axisborder+" "+x+","+(height-axisborder-legendheight);
    for (y = height-origin[1]; y<=height-axisborder-legendheight; y = y+gdy)
      if (y>=axisborder) st += " M"+axisborder+","+y+" "+(width-axisborder)+","+y;
    for (y = height-origin[1]-gdy; y>=0+axisborder-0.001; y = y-gdy)
      st += " M"+axisborder+","+y+" "+(width-axisborder)+","+y;
    pnode.setAttribute("d",st);
    setSVGGridStroke(pnode);
    setSVGFill(pnode);
    svgpicture.appendChild(pnode);
  }
  // Thicks
  xoffset=origin[0];
  yoffset=origin[1];
  if (labels=="topleft") {xoffset=origin[0]+xmin*xunitlength;yoffset=origin[1]+ymax*yunitlength;}
  if (labels=="topright") {xoffset=origin[0]+xmax*xunitlength;yoffset=origin[1]+ymax*yunitlength;}
  if (labels=="bottomleft") {xoffset=origin[0]+xmin*xunitlength;yoffset=origin[1]+ymin*yunitlength;}
  if (labels=="bottomright") {xoffset=origin[0]+xmax*xunitlength;yoffset=origin[1]+ymin*yunitlength;}
  pnode = myCreateElementSVG("path");
  st="M"+axisborder+","+(height-yoffset)+" "+(width-axisborder)+","+ (height-yoffset)
   +" M"+xoffset+","+axisborder+" "+xoffset+","+(height-axisborder-legendheight);
  for (x = xoffset; x<=width-axisborder+0.001; x = x+dx)
    st += " M"+x+","+(height-yoffset+ticklength)+" "+x+","+
           (height-yoffset-ticklength);
  for (x = xoffset; x>=0+axisborder; x = x-dx)
    st += " M"+x+","+(height-yoffset+ticklength)+" "+x+","+
           (height-yoffset-ticklength);
  for (y = height-yoffset; y<=height-axisborder-legendheight; y = y+dy)
    st += " M"+(xoffset+ticklength)+","+y+" "+(xoffset-ticklength)+","+y;
  for (y = height-yoffset; y>=0+axisborder-0.001; y = y-dy)
    st += " M"+(xoffset+ticklength)+","+y+" "+(xoffset-ticklength)+","+y;
  // Labels 
  if (labels!=null) {
    ldx = dx/xunitlength;
    ldy = dy/yunitlength;
    
    if (xmin<0 && xmax<=0) {lx=xmax;lyp="right";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmax+5/6*axisborder/xunitlength;ylblhor="middle";xoffset=ticklength/xunitlength;}
    if (xmin<0 && xmax>0) {lx=0;lyp="left";xlblx=0;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;}
    if (xmin>=0 && xmax>0) {lx=xmin;lyp="left";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;}
    
    if (ymin<0 && ymax<=0) {ly=ymax;lxp="above";xlbly=ymax+0.9*axisborder/yunitlength;xlblver="below";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=ticklength/yunitlength;titley=ymin-axisborder/yunitlength;}
    if (ymin<0 && ymax>0) {ly=0;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=0;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;}
    if (ymin>=0 && ymax>0) {ly=ymin;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;}

	if (labels=="topleft")  {
		ly=ymax;lxp="above";xlbly=ymax+0.9*axisborder/yunitlength;xlblver="below";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=ticklength/yunitlength;titley=ymin-axisborder/yunitlength;
		lx=xmin;lyp="left";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;
		}
	if (labels=="topright") {
		ly=ymax;lxp="above";xlbly=ymax+0.9*axisborder/yunitlength;xlblver="below";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=ticklength/yunitlength;titley=ymin-axisborder/yunitlength;
		lx=xmax;lyp="right";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmax+5/6*axisborder/xunitlength;ylblhor="middle";xoffset=ticklength/xunitlength;
		}
	if (labels=="bottomleft") {
		ly=ymin;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;
		lx=xmin;lyp="left";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;
		}
	if (labels=="bottomright") {
		ly=ymin;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;
		lx=xmax;lyp="right";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmax+5/6*axisborder/xunitlength;ylblhor="middle";xoffset=ticklength/xunitlength;
		}
	setaxisfont();
    var ddx = floor(1.1-log(ldx)/log(10))+1;
    var ddy = floor(1.1-log(ldy)/log(10))+1;
    if (ddx<0) ddx=0;
    if (ddy<0) ddy=0;
    for (x = 0; x<=xmax; x = x+ldx)
      if (x>=xmin && horizontallabels) text([x,ly+yoffset],AxisMML+chopZ(x.toFixed(ddx))+AxisMML,lxp,svgpicture.getAttribute("name")+"_hor"+x,null,0);
    for (x = -ldx; xmin<=x; x = x-ldx)
      if (x<=xmax && horizontallabels) text([x,ly+yoffset],AxisMML+chopZ(x.toFixed(ddx))+AxisMML,lxp,svgpicture.getAttribute("name")+"_hor"+x,null,0);
    for (y = 0; y<=ymax; y = y+ldy)
      if (y>=ymin && verticallabels) text([lx+xoffset,y],AxisMML+chopZ(y.toFixed(ddy))+AxisMML,lyp,svgpicture.getAttribute("name")+"_ver"+y,null,0);
    for (y = -ldy; ymin<=y; y = y-ldy)
      if (y<=ymax && verticallabels) text([lx+xoffset,y],AxisMML+chopZ(y.toFixed(ddy))+AxisMML,lyp,svgpicture.getAttribute("name")+"_ver"+y,null,0);
    setlabelfont();
    text([xlblx,xlbly],LabelMML+xlabel+LabelMML,xlblver+xlblhor,svgpicture.getAttribute("name")+"_xlabel",null,0);
    text([ylblx,ylbly],LabelMML+ylabel+LabelMML,ylblver+ylblhor,svgpicture.getAttribute("name")+"_ylabel",null,-90);
    settitlefont();
    titlex=(xmin+xmax)/2;
    if (xlblver=="above") text([titlex,titley-ticklength/yunitlength],TitleMML+title+TitleMML,"belowmiddle",svgpicture.getAttribute("name")+"_title",null,0)
    if (xlblver=="below") text([titlex,titley+ticklength/yunitlength],TitleMML+title+TitleMML,"abovemiddle",svgpicture.getAttribute("name")+"_title",null,0)
  }
  pnode.setAttribute("d",st);
  setSVGAxesStroke(pnode);
  setSVGFill(pnode);
  svgpicture.appendChild(pnode);
  setdefaults();
}

function linlogaxes(dx,dy,labels,gdx,gdy,xlabel,ylabel,title) {
  var x, y, ldx, ldy, lx, ly, lxp, lyp, pnode, st, xlblx, xlbly, xlblhor, xlblver, ylblx, ylbly, ylblhor, ylblver, oldfontsize,logx, logy, xoffset, yoffset, titlex, titley;
  if (!initialized) initPicture();
  if (typeof dx=="string") { dx = null; }
  if (typeof dy=="string") { gdx = dy; dy = null; }
  if (xscl!=null) {dx = xscl; gdx = xscl;}
  if (yscl!=null) {dy = yscl; gdy = yscl}
  if (xtick!=null) {dx = xtick}
  if (ytick!=null) {dy = ytick}
  dx = (dx==null?xunitlength:dx*xunitlength);
  dy = (dy==null?dx:dy*yunitlength);
  if (xgrid!=null) gdx = xgrid;
  if (ygrid!=null) gdy = ygrid;
  if (xlabel==null) xlabel="";
  if (ylabel==null) ylabel="";
  if (title==null) title="";
  stroke="black";
  rect([xmin,ymin],[xmax,ymax]);
  // Axes
  if (gdx!=null) {
    gdx = (typeof gdx=="string"?dx:gdx*xunitlength);
    gdy = (gdy==null?dy:gdy*yunitlength);
    pnode = myCreateElementSVG("path");
    st="";
    for (x = origin[0]; x<=width-axisborder+0.001-gdx; x = x+gdx) {
      if (x>=axisborder) st += " M"+x+","+axisborder+" "+x+","+(height-axisborder-legendheight);
      if (x>=axisborder) {
        for (logx = 2; logx<11; logx = logx +1)
          st += " M"+(x+log(logx)*gdx)+","+axisborder+" "+(x+log(logx)*gdx)+","+(height-axisborder-legendheight);
        }
      }
    for (x = origin[0]-gdx; x>=0+axisborder; x = x-gdx) {
      st += " M"+x+","+axisborder+" "+x+","+(height-axisborder-legendheight);
      for (logx = 2; logx<11; logx = logx +1)
        st += " M"+(x+log(logx)*gdx)+","+axisborder+" "+(x+log(logx)*gdx)+","+(height-axisborder-legendheight);
      }
    for (y = height-origin[1]; y<=height-axisborder-legendheight; y = y+gdy)
      if (y>=axisborder) st += " M"+axisborder+","+y+" "+(width-axisborder)+","+y;
    for (y = height-origin[1]-gdy; y>=0+axisborder-0.001; y = y-gdy)
      st += " M"+axisborder+","+y+" "+(width-axisborder)+","+y;
    pnode.setAttribute("d",st);
    setSVGGridStroke(pnode);
    setSVGFill(pnode);
    svgpicture.appendChild(pnode);
  }
  // Thicks
  xoffset=origin[0];
  yoffset=origin[1];
  if (labels=="topleft") {xoffset=origin[0]+xmin*xunitlength;yoffset=origin[1]+ymax*yunitlength;}
  if (labels=="topright") {xoffset=origin[0]+xmax*xunitlength;yoffset=origin[1]+ymax*yunitlength;}
  if (labels=="bottomleft") {xoffset=origin[0]+xmin*xunitlength;yoffset=origin[1]+ymin*yunitlength;}
  if (labels=="bottomright") {xoffset=origin[0]+xmax*xunitlength;yoffset=origin[1]+ymin*yunitlength;}
  pnode = myCreateElementSVG("path");
  st="M"+axisborder+","+(height-yoffset)+" "+(width-axisborder)+","+ (height-yoffset)
   +" M"+xoffset+","+axisborder+" "+xoffset+","+(height-axisborder-legendheight);
  for (x = xoffset; x<=width-axisborder+0.001; x = x+dx)
    st += " M"+x+","+(height-yoffset+ticklength)+" "+x+","+
           (height-yoffset-ticklength);
  for (x = xoffset; x>=0+axisborder; x = x-dx)
    st += " M"+x+","+(height-yoffset+ticklength)+" "+x+","+
           (height-yoffset-ticklength);
  for (y = height-yoffset; y<=height-axisborder-legendheight; y = y+dy)
    st += " M"+(xoffset+ticklength)+","+y+" "+(xoffset-ticklength)+","+y;
  for (y = height-yoffset; y>=0+axisborder-0.001; y = y-dy)
    st += " M"+(xoffset+ticklength)+","+y+" "+(xoffset-ticklength)+","+y;
  // Labels 
  if (labels!=null) {
    ldx = dx/xunitlength;
    ldy = dy/yunitlength;
    
    if (xmin<0 && xmax<=0) {lx=xmax;lyp="right";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmax+5/6*axisborder/xunitlength;ylblhor="middle";xoffset=ticklength/xunitlength;}
    if (xmin<0 && xmax>0) {lx=0;lyp="left";xlblx=0;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;}
    if (xmin>=0 && xmax>0) {lx=xmin;lyp="left";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;}
    
    if (ymin<0 && ymax<=0) {ly=ymax;lxp="above";xlbly=ymax+0.9*axisborder/yunitlength;xlblver="below";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=ticklength/yunitlength;titley=ymin-axisborder/yunitlength;}
    if (ymin<0 && ymax>0) {ly=0;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=0;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;}
    if (ymin>=0 && ymax>0) {ly=ymin;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;}

	if (labels=="topleft")  {
		ly=ymax;lxp="above";xlbly=ymax+0.9*axisborder/yunitlength;xlblver="below";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=ticklength/yunitlength;titley=ymin-axisborder/yunitlength;
		lx=xmin;lyp="left";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;
		}
	if (labels=="topright") {
		ly=ymax;lxp="above";xlbly=ymax+0.9*axisborder/yunitlength;xlblver="below";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=ticklength/yunitlength;titley=ymin-axisborder/yunitlength;
		lx=xmax;lyp="right";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmax+5/6*axisborder/xunitlength;ylblhor="middle";xoffset=ticklength/xunitlength;
		}
	if (labels=="bottomleft") {
		ly=ymin;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;
		lx=xmin;lyp="left";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;
		}
	if (labels=="bottomright") {
		ly=ymin;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;
		lx=xmax;lyp="right";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmax+5/6*axisborder/xunitlength;ylblhor="middle";xoffset=ticklength/xunitlength;
		}
	setaxisfont();
    var ddx = floor(1.1-log(ldx)/log(10))+1;
    var ddy = floor(1.1-log(ldy)/log(10))+1;
    if (ddx<0) ddx=0;
    if (ddy<0) ddy=0;
    for (x = 0; x<=xmax; x = x+ldx)
      if (x>=xmin) text([x,ly+yoffset],AxisMML+"10^"+chopZ(x.toFixed(ddx))+AxisMML,lxp,svgpicture.getAttribute("name")+"_hor"+x,null,0);
    for (x = -ldx; xmin<=x; x = x-ldx)
      if (x<=xmax) text([x,ly+yoffset],AxisMML+"10^"+chopZ(x.toFixed(ddx))+AxisMML,lxp,svgpicture.getAttribute("name")+"_hor"+x,null,0);
    for (y = 0; y<=ymax; y = y+ldy)
      if (y>=ymin) text([lx+xoffset,y],AxisMML+chopZ(y.toFixed(ddy))+AxisMML,lyp,svgpicture.getAttribute("name")+"_ver"+y,null,0);
    for (y = -ldy; ymin<=y; y = y-ldy)
      if (y<=ymax) text([lx+xoffset,y],AxisMML+chopZ(y.toFixed(ddy))+AxisMML,lyp,svgpicture.getAttribute("name")+"_ver"+y,null,0);
    setlabelfont();
    text([xlblx,xlbly],LabelMML+xlabel+LabelMML,xlblver+xlblhor,svgpicture.getAttribute("name")+"_xlabel",null,0);
    text([ylblx,ylbly],LabelMML+ylabel+LabelMML,ylblver+ylblhor,svgpicture.getAttribute("name")+"_ylabel",null,-90);
    settitlefont();
    titlex=(xmin+xmax)/2;
    if (xlblver=="above") text([titlex,titley-ticklength/yunitlength],TitleMML+title+TitleMML,"belowmiddle",svgpicture.getAttribute("name")+"_title",null,0)
    if (xlblver=="below") text([titlex,titley+ticklength/yunitlength],TitleMML+title+TitleMML,"abovemiddle",svgpicture.getAttribute("name")+"_title",null,0)
  }
  pnode.setAttribute("d",st);
  setSVGAxesStroke(pnode);
  setSVGFill(pnode);
  svgpicture.appendChild(pnode);
  setdefaults();
}

function loglinaxes(dx,dy,labels,gdx,gdy,xlabel,ylabel,title) {
  var x, y, ldx, ldy, lx, ly, lxp, lyp, pnode, st, xlblx, xlbly, xlblhor, xlblver, ylblx, ylbly, ylblhor, ylblver, oldfontsize, logx, logy, xoffset, yoffset, titlex, titley;
  if (!initialized) initPicture();
  if (typeof dx=="string") { dx = null; }
  if (typeof dy=="string") { gdx = dy; dy = null; }
  if (xscl!=null) {dx = xscl; gdx = xscl;}
  if (yscl!=null) {dy = yscl; gdy = yscl}
  if (xtick!=null) {dx = xtick}
  if (ytick!=null) {dy = ytick}
  dx = (dx==null?xunitlength:dx*xunitlength);
  dy = (dy==null?dx:dy*yunitlength);
  if (xgrid!=null) gdx = xgrid;
  if (ygrid!=null) gdy = ygrid;
  if (xlabel==null) xlabel="";
  if (ylabel==null) ylabel="";
  if (title==null) title="";
  stroke="black";
  rect([xmin,ymin],[xmax,ymax]);
  // Axes
  if (gdx!=null) {
    gdx = (typeof gdx=="string"?dx:gdx*xunitlength);
    gdy = (gdy==null?dy:gdy*yunitlength);
    pnode = myCreateElementSVG("path");
    st="";
    for (x = origin[0]; x<=width-axisborder+0.001; x = x+gdx) 
      if (x>=axisborder) st += " M"+x+","+axisborder+" "+x+","+(height-axisborder-legendheight);
    for (x = origin[0]-gdx; x>=0+axisborder; x = x-gdx) 
      st += " M"+x+","+axisborder+" "+x+","+(height-axisborder-legendheight);
    for (y = height-origin[1]; y<=height-axisborder-legendheight-gdy; y = y+gdy) {
      if (y>=axisborder) st += " M"+axisborder+","+y+" "+(width-axisborder)+","+y;
      if (y>=axisborder) {
        for (logy = 1; logy<11; logy = logy +1)
		  st += " M"+axisborder+","+(y+(1-log(logy))*gdy)+" "+(width-axisborder)+","+(y+(1-log(logy))*gdy);
		}
      }
    for (y = height-origin[1]-gdy; y>=0+axisborder-0.001; y = y-gdy) {
      st += " M"+axisborder+","+y+" "+(width-axisborder)+","+y;
      for (logy = 1; logy<11; logy = logy +1)
		st += " M"+axisborder+","+(y+(1-log(logy))*gdy)+" "+(width-axisborder)+","+(y+(1-log(logy))*gdy);
      }
    pnode.setAttribute("d",st);
    setSVGGridStroke(pnode);
    setSVGFill(pnode);
    svgpicture.appendChild(pnode);
  }
  // Thicks
  xoffset=origin[0];
  yoffset=origin[1];
  if (labels=="topleft") {xoffset=origin[0]+xmin*xunitlength;yoffset=origin[1]+ymax*yunitlength;}
  if (labels=="topright") {xoffset=origin[0]+xmax*xunitlength;yoffset=origin[1]+ymax*yunitlength;}
  if (labels=="bottomleft") {xoffset=origin[0]+xmin*xunitlength;yoffset=origin[1]+ymin*yunitlength;}
  if (labels=="bottomright") {xoffset=origin[0]+xmax*xunitlength;yoffset=origin[1]+ymin*yunitlength;}
  pnode = myCreateElementSVG("path");
  st="M"+axisborder+","+(height-yoffset)+" "+(width-axisborder)+","+ (height-yoffset)
   +" M"+xoffset+","+axisborder+" "+xoffset+","+(height-axisborder-legendheight);
  for (x = xoffset; x<=width-axisborder+0.001; x = x+dx)
    st += " M"+x+","+(height-yoffset+ticklength)+" "+x+","+
           (height-yoffset-ticklength);
  for (x = xoffset; x>=0+axisborder; x = x-dx)
    st += " M"+x+","+(height-yoffset+ticklength)+" "+x+","+
           (height-yoffset-ticklength);
  for (y = height-yoffset; y<=height-axisborder-legendheight; y = y+dy)
    st += " M"+(xoffset+ticklength)+","+y+" "+(xoffset-ticklength)+","+y;
  for (y = height-yoffset; y>=0+axisborder-0.001; y = y-dy)
    st += " M"+(xoffset+ticklength)+","+y+" "+(xoffset-ticklength)+","+y;
  // Labels 
  if (labels!=null) {
    ldx = dx/xunitlength;
    ldy = dy/yunitlength;
    
    if (xmin<0 && xmax<=0) {lx=xmax;lyp="right";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmax+5/6*axisborder/xunitlength;ylblhor="middle";xoffset=ticklength/xunitlength;}
    if (xmin<0 && xmax>0) {lx=0;lyp="left";xlblx=0;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;}
    if (xmin>=0 && xmax>0) {lx=xmin;lyp="left";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;}
    
    if (ymin<0 && ymax<=0) {ly=ymax;lxp="above";xlbly=ymax+0.9*axisborder/yunitlength;xlblver="below";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=ticklength/yunitlength;titley=ymin-axisborder/yunitlength;}
    if (ymin<0 && ymax>0) {ly=0;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=0;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;}
    if (ymin>=0 && ymax>0) {ly=ymin;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;}

	if (labels=="topleft")  {
		ly=ymax;lxp="above";xlbly=ymax+0.9*axisborder/yunitlength;xlblver="below";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=ticklength/yunitlength;titley=ymin-axisborder/yunitlength;
		lx=xmin;lyp="left";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;
		}
	if (labels=="topright") {
		ly=ymax;lxp="above";xlbly=ymax+0.9*axisborder/yunitlength;xlblver="below";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=ticklength/yunitlength;titley=ymin-axisborder/yunitlength;
		lx=xmax;lyp="right";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmax+5/6*axisborder/xunitlength;ylblhor="middle";xoffset=ticklength/xunitlength;
		}
	if (labels=="bottomleft") {
		ly=ymin;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;
		lx=xmin;lyp="left";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;
		}
	if (labels=="bottomright") {
		ly=ymin;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;
		lx=xmax;lyp="right";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmax+5/6*axisborder/xunitlength;ylblhor="middle";xoffset=ticklength/xunitlength;
		}

    setaxisfont();
    var ddx = floor(1.1-log(ldx)/log(10))+1;
    var ddy = floor(1.1-log(ldy)/log(10))+1;
    if (ddx<0) ddx=0;
    if (ddy<0) ddy=0;
    for (x = 0; x<=xmax; x = x+ldx)
      if (x>=xmin) text([x,ly+yoffset],AxisMML+chopZ(x.toFixed(ddx))+AxisMML,lxp,svgpicture.getAttribute("name")+"_hor"+x,null,0);
    for (x = -ldx; xmin<=x; x = x-ldx)
      if (x<=xmax) text([x,ly+yoffset],AxisMML+chopZ(x.toFixed(ddx))+AxisMML,lxp,svgpicture.getAttribute("name")+"_hor"+x,null,0);
    for (y = 0; y<=ymax; y = y+ldy)
      if (y>=ymin) text([lx+xoffset,y],AxisMML+"10^"+chopZ(y.toFixed(ddy))+AxisMML,lyp,svgpicture.getAttribute("name")+"_ver"+y,null,0);
    for (y = -ldy; ymin<=y; y = y-ldy)
      if (y<=ymax) text([lx+xoffset,y],AxisMML+"10^"+chopZ(y.toFixed(ddy))+AxisMML,lyp,svgpicture.getAttribute("name")+"_ver"+y,null,0);
    setlabelfont();
    text([xlblx,xlbly],LabelMML+xlabel+LabelMML,xlblver+xlblhor,svgpicture.getAttribute("name")+"_xlabel",null,0);
    text([ylblx,ylbly],LabelMML+ylabel+LabelMML,ylblver+ylblhor,svgpicture.getAttribute("name")+"_ylabel",null,-90);
	settitlefont();
    titlex=(xmin+xmax)/2;
    if (xlblver=="above") text([titlex,titley-ticklength/yunitlength],TitleMML+title+TitleMML,"belowmiddle",svgpicture.getAttribute("name")+"_title",null,0)
    if (xlblver=="below") text([titlex,titley+ticklength/yunitlength],TitleMML+title+TitleMML,"abovemiddle",svgpicture.getAttribute("name")+"_title",null,0)
  }
  pnode.setAttribute("d",st);
  setSVGAxesStroke(pnode);
  setSVGFill(pnode);
  svgpicture.appendChild(pnode);
  setdefaults();
}


function loglogaxes(dx,dy,labels,gdx,gdy,xlabel,ylabel,title) {
  var x, y, ldx, ldy, lx, ly, lxp, lyp, pnode, st, xlblx, xlbly, xlblhor, xlblver, ylblx, ylbly, ylblhor, ylblver, oldfontsize, logx, logy, xoffset, yoffset, titlex, titley;
  if (!initialized) initPicture();
  if (typeof dx=="string") { dx = null; }
  if (typeof dy=="string") { gdx = dy; dy = null; }
  if (xscl!=null) {dx = xscl; gdx = xscl;}
  if (yscl!=null) {dy = yscl; gdy = yscl}
  if (xtick!=null) {dx = xtick}
  if (ytick!=null) {dy = ytick}
  if (xlabel==null) xlabel="";
  if (ylabel==null) ylabel="";
  if (title==null) title="";
  dx = (dx==null?xunitlength:dx*xunitlength);
  dy = (dy==null?dx:dy*yunitlength);
  if (xgrid!=null) gdx = xgrid;
  if (ygrid!=null) gdy = ygrid;
  stroke="black";
  rect([xmin,ymin],[xmax,ymax]);
  // Axes
  if (gdx!=null) {
    gdx = (typeof gdx=="string"?dx:gdx*xunitlength);
    gdy = (gdy==null?dy:gdy*yunitlength);
    pnode = myCreateElementSVG("path");
    st="";

    for (x = origin[0]; x<=width-axisborder+0.001-gdx; x = x+gdx) {
      if (x>=axisborder) st += " M"+x+","+axisborder+" "+x+","+(height-axisborder-legendheight);
      if (x>=axisborder) {
        for (logx = 2; logx<11; logx = logx +1)
          st += " M"+(x+log(logx)*gdx)+","+axisborder+" "+(x+log(logx)*gdx)+","+(height-axisborder-legendheight);
        }
      }
    for (x = origin[0]-gdx; x>=0+axisborder; x = x-gdx) {
      st += " M"+x+","+axisborder+" "+x+","+(height-axisborder-legendheight);
      for (logx = 2; logx<11; logx = logx +1)
        st += " M"+(x+log(logx)*gdx)+","+axisborder+" "+(x+log(logx)*gdx)+","+(height-axisborder-legendheight);
      }

    for (y = height-origin[1]; y<=height-axisborder-legendheight-gdy; y = y+gdy) {
      if (y>=axisborder) st += " M"+axisborder+","+y+" "+(width-axisborder)+","+y;
      if (y>=axisborder) {
        for (logy = 1; logy<11; logy = logy +1)
		  st += " M"+axisborder+","+(y+(1-log(logy))*gdy)+" "+(width-axisborder)+","+(y+(1-log(logy))*gdy);
		}
      }
    for (y = height-origin[1]-gdy; y>=0+axisborder-0.001; y = y-gdy) {
      st += " M"+axisborder+","+y+" "+(width-axisborder)+","+y;
      for (logy = 1; logy<11; logy = logy +1)
		st += " M"+axisborder+","+(y+(1-log(logy))*gdy)+" "+(width-axisborder)+","+(y+(1-log(logy))*gdy);
      }
    pnode.setAttribute("d",st);
    setSVGGridStroke(pnode);
    setSVGFill(pnode);
    svgpicture.appendChild(pnode);
  }
  // Thicks
  xoffset=origin[0];
  yoffset=origin[1];
  if (labels=="topleft") {xoffset=origin[0]+xmin*xunitlength;yoffset=origin[1]+ymax*yunitlength;}
  if (labels=="topright") {xoffset=origin[0]+xmax*xunitlength;yoffset=origin[1]+ymax*yunitlength;}
  if (labels=="bottomleft") {xoffset=origin[0]+xmin*xunitlength;yoffset=origin[1]+ymin*yunitlength;}
  if (labels=="bottomright") {xoffset=origin[0]+xmax*xunitlength;yoffset=origin[1]+ymin*yunitlength;}
  pnode = myCreateElementSVG("path");
  st="M"+axisborder+","+(height-yoffset)+" "+(width-axisborder)+","+ (height-yoffset)
   +" M"+xoffset+","+axisborder+" "+xoffset+","+(height-axisborder-legendheight);
  for (x = xoffset; x<=width-axisborder+0.001; x = x+dx)
    st += " M"+x+","+(height-yoffset+ticklength)+" "+x+","+
           (height-yoffset-ticklength);
  for (x = xoffset; x>=0+axisborder; x = x-dx)
    st += " M"+x+","+(height-yoffset+ticklength)+" "+x+","+
           (height-yoffset-ticklength);
  for (y = height-yoffset; y<=height-axisborder-legendheight; y = y+dy)
    st += " M"+(xoffset+ticklength)+","+y+" "+(xoffset-ticklength)+","+y;
  for (y = height-yoffset; y>=0+axisborder-0.001; y = y-dy)
    st += " M"+(xoffset+ticklength)+","+y+" "+(xoffset-ticklength)+","+y;
  // Labels 
  if (labels!=null) {
    ldx = dx/xunitlength;
    ldy = dy/yunitlength;
    
    if (xmin<0 && xmax<=0) {lx=xmax;lyp="right";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmax+5/6*axisborder/xunitlength;ylblhor="middle";xoffset=ticklength/xunitlength;}
    if (xmin<0 && xmax>0) {lx=0;lyp="left";xlblx=0;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;}
    if (xmin>=0 && xmax>0) {lx=xmin;lyp="left";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;}
    
    if (ymin<0 && ymax<=0) {ly=ymax;lxp="above";xlbly=ymax+0.9*axisborder/yunitlength;xlblver="below";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=ticklength/yunitlength;titley=ymin-axisborder/yunitlength;}
    if (ymin<0 && ymax>0) {ly=0;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=0;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;}
    if (ymin>=0 && ymax>0) {ly=ymin;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;}

	if (labels=="topleft")  {
		ly=ymax;lxp="above";xlbly=ymax+0.9*axisborder/yunitlength;xlblver="below";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=ticklength/yunitlength;titley=ymin-axisborder/yunitlength;
		lx=xmin;lyp="left";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;
		}
	if (labels=="topright") {
		ly=ymax;lxp="above";xlbly=ymax+0.9*axisborder/yunitlength;xlblver="below";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=ticklength/yunitlength;titley=ymin-axisborder/yunitlength;
		lx=xmax;lyp="right";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmax+5/6*axisborder/xunitlength;ylblhor="middle";xoffset=ticklength/xunitlength;
		}
	if (labels=="bottomleft") {
		ly=ymin;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;
		lx=xmin;lyp="left";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;
		}
	if (labels=="bottomright") {
		ly=ymin;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;
		lx=xmax;lyp="right";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmax+5/6*axisborder/xunitlength;ylblhor="middle";xoffset=ticklength/xunitlength;
		}
	
	setaxisfont();
    var ddx = floor(1.1-log(ldx)/log(10))+1;
    var ddy = floor(1.1-log(ldy)/log(10))+1;
    if (ddx<0) ddx=0;
    if (ddy<0) ddy=0;
    for (x = 0; x<=xmax; x = x+ldx)
      if (x>=xmin) text([x,ly+yoffset],AxisMML+"10^"+chopZ(x.toFixed(ddx))+AxisMML,lxp,svgpicture.getAttribute("name")+"_hor"+x,null,0);
    for (x = -ldx; xmin<=x; x = x-ldx)
      if (x<=xmax) text([x,ly+yoffset],AxisMML+"10^"+chopZ(x.toFixed(ddx))+AxisMML,lxp,svgpicture.getAttribute("name")+"_hor"+x,null,0);
    for (y = 0; y<=ymax; y = y+ldy)
      if (y>=ymin) text([lx+xoffset,y],AxisMML+"10^"+chopZ(y.toFixed(ddy))+AxisMML,lyp,svgpicture.getAttribute("name")+"_ver"+y,null,0);
    for (y = -ldy; ymin<=y; y = y-ldy)
      if (y<=ymax) text([lx+xoffset,y],AxisMML+"10^"+chopZ(y.toFixed(ddy))+AxisMML,lyp,svgpicture.getAttribute("name")+"_ver"+y,null,0);
	setlabelfont();
    text([xlblx,xlbly],LabelMML+xlabel+LabelMML,xlblver+xlblhor,svgpicture.getAttribute("name")+"_xlabel",null,0);
    text([ylblx,ylbly],LabelMML+ylabel+LabelMML,ylblver+ylblhor,svgpicture.getAttribute("name")+"_ylabel",null,-90);
	settitlefont();
    titlex=(xmin+xmax)/2;
    if (xlblver=="above") text([titlex,titley-ticklength/yunitlength],TitleMML+title+TitleMML,"belowmiddle",svgpicture.getAttribute("name")+"_title",null,0)
    if (xlblver=="below") text([titlex,titley+ticklength/yunitlength],TitleMML+title+TitleMML,"abovemiddle",svgpicture.getAttribute("name")+"_title",null,0)
  }
  pnode.setAttribute("d",st);
  setSVGAxesStroke(pnode);
  setSVGFill(pnode);
  svgpicture.appendChild(pnode);
  setdefaults();
}

function polaraxes(dx,dy,labels,gdx,gdy,xlabel,ylabel,title) {
  var x, y, ldx, ldy, lx, ly, lxp, lyp, pnode, st, xlblx, xlbly, xlblhor, xlblver, ylblx, ylbly, ylblhor, ylblver, oldfontsize, logx, logy, xoffset, yoffset, titlex, titley, r, a;
  if (!initialized) initPicture();
  if (typeof dx=="string") { dx = null; }
  if (typeof dy=="string") { gdx = dy; dy = null; }
  if (xscl!=null) {dx = xscl; gdx = xscl;}
  if (yscl!=null) {dy = yscl; gdy = yscl}
  if (xtick!=null) {dx = xtick}
  if (ytick!=null) {dy = ytick}
  dx = (dx==null?xunitlength:dx*xunitlength);
  dy = (dy==null?dx:dy*yunitlength);
  if (xgrid!=null) gdx = xgrid;
  if (ygrid!=null) gdy = ygrid;
  if (xlabel==null) xlabel="";
  if (ylabel==null) ylabel="";
  if (title==null) title="";
  rect([xmin,ymin],[xmax,ymax]);
  // Axes
  if (gdx!=null) {
    gdx = (typeof gdx=="string"?dx:gdx*xunitlength);
    gdy = (gdy==null?dy:gdy*yunitlength);
    pnode = myCreateElementSVG("path");
    st="";
    setPen(0.5,gridstroke,"5 5 5 5",0.5);
    for (r = 0; r<=xmax*1.41+0.001; r = r+gdx/xunitlength)
      polarplot(r,0,2*pi);
    for (a = 0; a<=2*pi; a = a + pi*gdy/yunitlength) 
    {
      if (a<=pi/4) r = xmax/abs(cos(a));
      if (a>=pi/4 && a<=3*pi/4) r = xmax/abs(sin(a));
      if (a>=3*pi/4 && a<=5*pi/4) r = xmax/abs(cos(a));
      if (a>=5*pi/4 && a<=7*pi/4) r = xmax/abs(sin(a));
      if (a>=7*pi/4) r = xmax/abs(cos(a));
      line([0,0],[r*cos(a),r*sin(a)]);  
    }  
  }
  // Thicks
  xoffset=origin[0];
  yoffset=origin[1];
  if (labels=="topleft") {xoffset=origin[0]+xmin*xunitlength;yoffset=origin[1]+ymax*yunitlength;}
  if (labels=="topright") {xoffset=origin[0]+xmax*xunitlength;yoffset=origin[1]+ymax*yunitlength;}
  if (labels=="bottomleft") {xoffset=origin[0]+xmin*xunitlength;yoffset=origin[1]+ymin*yunitlength;}
  if (labels=="bottomright") {xoffset=origin[0]+xmax*xunitlength;yoffset=origin[1]+ymin*yunitlength;}
  pnode = myCreateElementSVG("path");
  st="M"+axisborder+","+(height-yoffset)+" "+(width-axisborder)+","+ (height-yoffset)
   +" M"+xoffset+","+axisborder+" "+xoffset+","+(height-axisborder-legendheight);
  for (x = xoffset; x<=width-axisborder+0.001; x = x+dx)
    st += " M"+x+","+(height-yoffset+ticklength)+" "+x+","+
           (height-yoffset-ticklength);
  for (x = xoffset; x>=0+axisborder; x = x-dx)
    st += " M"+x+","+(height-yoffset+ticklength)+" "+x+","+
           (height-yoffset-ticklength);
  for (y = height-yoffset; y<=height-axisborder-legendheight; y = y+dy)
    st += " M"+(xoffset+ticklength)+","+y+" "+(xoffset-ticklength)+","+y;
  for (y = height-yoffset; y>=0+axisborder-0.001; y = y-dy)
    st += " M"+(xoffset+ticklength)+","+y+" "+(xoffset-ticklength)+","+y;
  // Labels 
  if (labels!=null) {
    ldx = dx/xunitlength;
    ldy = dy/yunitlength;
    
    if (xmin<0 && xmax<=0) {lx=xmax;lyp="right";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmax+5/6*axisborder/xunitlength;ylblhor="middle";xoffset=ticklength/xunitlength;}
    if (xmin<0 && xmax>0) {lx=0;lyp="left";xlblx=0;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;}
    if (xmin>=0 && xmax>0) {lx=xmin;lyp="left";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;}
    
    if (ymin<0 && ymax<=0) {ly=ymax;lxp="above";xlbly=ymax+0.9*axisborder/yunitlength;xlblver="below";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=ticklength/yunitlength;titley=ymin-axisborder/yunitlength;}
    if (ymin<0 && ymax>0) {ly=0;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=0;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;}
    if (ymin>=0 && ymax>0) {ly=ymin;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;}

	if (labels=="topleft")  {
		ly=ymax;lxp="above";xlbly=ymax+0.9*axisborder/yunitlength;xlblver="below";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=ticklength/yunitlength;titley=ymin-axisborder/yunitlength;
		lx=xmin;lyp="left";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;
		}
	if (labels=="topright") {
		ly=ymax;lxp="above";xlbly=ymax+0.9*axisborder/yunitlength;xlblver="below";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=ticklength/yunitlength;titley=ymin-axisborder/yunitlength;
		lx=xmax;lyp="right";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmax+5/6*axisborder/xunitlength;ylblhor="middle";xoffset=ticklength/xunitlength;
		}
	if (labels=="bottomleft") {
		ly=ymin;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;
		lx=xmin;lyp="left";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmin-6/10*axisborder/xunitlength;ylblhor="middle";xoffset=-ticklength/xunitlength;
		}
	if (labels=="bottomright") {
		ly=ymin;lxp="below";xlbly=ymin-axisborder/yunitlength;xlblver="above";ylbly=(ymin+ymax)/2;ylblver="middle";yoffset=-ticklength/yunitlength;titley=ymax+axisborder/yunitlength;
		lx=xmax;lyp="right";xlblx=(xmin+xmax)/2;xlblhor="middle";ylblx=xmax+5/6*axisborder/xunitlength;ylblhor="middle";xoffset=ticklength/xunitlength;
		}

	setaxisfont();
    var ddx = floor(1.1-log(ldx)/log(10))+1;
    var ddy = floor(1.1-log(ldy)/log(10))+1;
    if (ddx<0) ddx=0;
    if (ddy<0) ddy=0;
    for (x = 0; x<=xmax; x = x+ldx)
      if (x>=xmin) text([x,ly+yoffset],AxisMML+chopZ(x.toFixed(ddx))+AxisMML,lxp,svgpicture.getAttribute("name")+"_hor"+x,null,0);
    for (x = -ldx; xmin<=x; x = x-ldx)
      if (x<=xmax) text([x,ly+yoffset],AxisMML+chopZ(x.toFixed(ddx))+AxisMML,lxp,svgpicture.getAttribute("name")+"_hor"+x,null,0);
    for (y = 0; y<=ymax; y = y+ldy)
      if (y>=ymin) text([lx+xoffset,y],AxisMML+chopZ(y.toFixed(ddy))+AxisMML,lyp,svgpicture.getAttribute("name")+"_ver"+y,null,0);
    for (y = -ldy; ymin<=y; y = y-ldy)
      if (y<=ymax) text([lx+xoffset,y],AxisMML+chopZ(y.toFixed(ddy))+AxisMML,lyp,svgpicture.getAttribute("name")+"_ver"+y,null,0);
    setlabelfont();
    text([xlblx,xlbly],LabelMML+xlabel+LabelMML,xlblver+xlblhor,svgpicture.getAttribute("name")+"_xlabel",null,0);
    text([ylblx,ylbly],LabelMML+ylabel+LabelMML,ylblver+ylblhor,svgpicture.getAttribute("name")+"_ylabel",null,-90);
    settitlefont();
    titlex=(xmin+xmax)/2;
    if (xlblver=="above") text([titlex,titley-ticklength/yunitlength],TitleMML+title+TitleMML,"belowmiddle",svgpicture.getAttribute("name")+"_title",null,0)
    if (xlblver=="below") text([titlex,titley+ticklength/yunitlength],TitleMML+title+TitleMML,"abovemiddle",svgpicture.getAttribute("name")+"_title",null,0)
  }
  pnode.setAttribute("d",st);
  setSVGAxesStroke(pnode);
  setSVGFill(pnode);
  svgpicture.appendChild(pnode);
  setdefaults();
}
