/*
ASCII2MathML-SVGPieBarCharts.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 initPieChart() {
  horizontallabels = true;
  verticallabels = true;
}

function initStackedBarChartH() {
  horizontallabels = false;
  verticallabels = true;
}

function initBarChartH() {
  horizontallabels = false;
  verticallabels = true;
}

function initStackedBarChartV() {
  horizontallabels = true;
  verticallabels = false;
}

function initBarChartV() {
  horizontallabels = true;
  verticallabels = false;
}

function addPieChartItem(setNumber, setFillColor, setBorderColor, setLabel, setGradient, setExcentricity) {
  piechartitems[piechartitems.length] = [setNumber, setFillColor, setBorderColor, setLabel, setGradient, setExcentricity];
}

function addStackedBarChartItemH(setNumber, setFillColor, setBorderColor, setLabel, setGradient, setExcentricity) {
  piechartitems[piechartitems.length] = [setNumber, setFillColor, setBorderColor, setLabel, setGradient, setExcentricity];
}

function addBarChartItemH(setNumber, setFillColor, setBorderColor, setLabel, setGradient, setExcentricity) {
  piechartitems[piechartitems.length] = [setNumber, setFillColor, setBorderColor, setLabel, setGradient, setExcentricity];
}

function addStackedBarChartItemV(setNumber, setFillColor, setBorderColor, setLabel, setGradient, setExcentricity) {
  piechartitems[piechartitems.length] = [setNumber, setFillColor, setBorderColor, setLabel, setGradient, setExcentricity];
}

function addBarChartItemV(setNumber, setFillColor, setBorderColor, setLabel, setGradient, setExcentricity) {
  piechartitems[piechartitems.length] = [setNumber, setFillColor, setBorderColor, setLabel, setGradient, setExcentricity];
}

function newPieChart(setTitle, setShowLabels, setShowNumbers) {
  piecharttitle = setTitle;
  piechartitems = [];
  piechartshowlabels = setShowLabels;
  piechartshownumbers = setShowNumbers;
}

function newStackedBarChartH(setTitle, setShowLabels, setShowNumbers) {
  piecharttitle = setTitle;
  piechartitems = [];
  piechartshowlabels = setShowLabels;
  piechartshownumbers = setShowNumbers;
}

function newBarChartH(setTitle, setShowLabels, setShowNumbers) {
  piecharttitle = setTitle;
  piechartitems = [];
  piechartshowlabels = setShowLabels;
  piechartshownumbers = setShowNumbers;
}

function newStackedBarChartV(setTitle, setShowLabels, setShowNumbers) {
  piecharttitle = setTitle;
  piechartitems = [];
  piechartshowlabels = setShowLabels;
  piechartshownumbers = setShowNumbers;
}

function newBarChartV(setTitle, setShowLabels, setShowNumbers) {
  piecharttitle = setTitle;
  piechartitems = [];
  piechartshowlabels = setShowLabels;
  piechartshownumbers = setShowNumbers;
}

function drawPieChart(p, radius) {
  var numberofitems, total, item, startangle, endangle, tx, ty, location, label, q;
  setaxisfont();
  location = "middlemiddle";
  numberofitems = piechartitems.length;
  total = 0;
  for (item = 0; item<numberofitems; item = item+1)
    total = total + piechartitems[item][0];
  startangle = 0;  
  for (item = 0; item<numberofitems; item = item+1) {
    endangle = startangle + piechartitems[item][0]/total*360;
    if (piechartitems[item][4]) {
      fill = "url(#"+piechartitems[item][1]+")";
    } else {
      fill = piechartitems[item][1];
    }
    stroke = piechartitems[item][2];
    tx = p[0]+piechartitems[item][5]*cos((startangle+endangle)/2*pi/180);
    ty = p[1]+piechartitems[item][5]*sin((startangle+endangle)/2*pi/180);
    sector([tx,ty], radius, startangle, endangle);
    if (piechartshowlabels) {
      tx = (1.01*radius+piechartitems[item][5])*cos((startangle+endangle)/2*pi/180);
      ty = (1.01*radius+piechartitems[item][5])*sin((startangle+endangle)/2*pi/180);
      var q = [p[0]+tx,p[1]+ty];
      if (startangle+endangle<=180) location = "rightabove";
      if (startangle+endangle>180 && startangle+endangle<=360) location = "leftabove";
      if (startangle+endangle>360 && startangle+endangle<=540) location = "leftbelow";
      if (startangle+endangle>540 && startangle+endangle<=720) location = "rightbelow";
      text(q,piechartitems[item][3],location);
    }
    if (piechartshownumbers) {
      location = "middlemiddle";
      tx = (0.65*radius+piechartitems[item][5])*cos((startangle+endangle)/2*pi/180);
      ty = (0.65*radius+piechartitems[item][5])*sin((startangle+endangle)/2*pi/180);
      q = [p[0]+tx,p[1]+ty];
      label = String(piechartitems[item][0])+" ("+round(piechartitems[item][0]/total*100)+"%)"; 
      text(q,label,location);
    }  
    startangle = endangle;
  }
  lx = p[0];
  ly = p[1]-1.2*radius;
  q = [lx,ly];
  setlabelfont();
  text(q,piecharttitle,"middlebelow");
}

function drawStackedBarChartH(p,chartwidth) {
  var numberofitems, total, item, startpos, endpos, tx, ty, location, label, q, r,dx;
  setaxisfont();
  numberofitems = piechartitems.length;
  total = 0;
  for (item = 0; item<numberofitems; item = item+1)
    total = total + piechartitems[item][0];
  startpos = 0;  
  for (item = 0; item<numberofitems; item = item+1) {
    endpos = startpos + piechartitems[item][0]/total*(ymax-p[1]);
    if (piechartitems[item][4]) {
      fill = "url(#"+piechartitems[item][1]+")";
    } else {
      fill = piechartitems[item][1];
    }
    dx = +piechartitems[item][5];
    stroke = piechartitems[item][2];
    q = [p[0]-chartwidth/2+dx,p[1]+startpos];
    r = [p[0]+chartwidth/2+dx,p[1]+endpos];
    rect(q, r);
    if (piechartshowlabels) {
      location = "rightmiddle";
      tx = p[0]+chartwidth/2+dx;
      ty = (q[1]+r[1])/2;
      q = [tx,ty];
      text(q,piechartitems[item][3],location);
    }
    if (piechartshownumbers) {
      location = "middlemiddle";
      tx = p[0]+dx;
      var q = [tx,ty];
      label = String(piechartitems[item][0])+" ("+round(piechartitems[item][0]/total*100)+"%)"; 
      text(q,label,location)
    }
    startpos = endpos;
  }
  lx = p[0];
  ly = p[1]-(ymax-ymin)/50;
  q = [lx,ly];
  setlabelfont();
  text(q,piecharttitle,"middlebelow");
}

function drawBarChartH(p,chartwidth) {
  var numberofitems, total, item, startpos, endpos, tx, ty, location, label, q, r,dx, dy;
  setaxisfont();
  numberofitems = piechartitems.length;
  total = 0;
  for (item = 0; item<numberofitems; item = item+1)
    total = total + piechartitems[item][0];
  dx = chartwidth/numberofitems;  
  startpos = p[0]-chartwidth/2;  
  for (item = 0; item<numberofitems; item = item+1) {
    endpos = startpos + dx;
    if (piechartitems[item][4]) {
      fill = "url(#"+piechartitems[item][1]+")";
    } else {
      fill = piechartitems[item][1];
    }
    stroke = piechartitems[item][2];
    q = [startpos,p[1]];
    r = [endpos,p[1]+piechartitems[item][0]];
    rect(q, r);
    if (piechartshowlabels) {
      location = "rightabove";
      tx = (startpos+endpos)/2;
      ty = r[1];
      q = [tx,ty];
      text(q,piechartitems[item][3],location,null,null,-90)
    }
    if (piechartshownumbers) {
      location = "middlemiddle";
      tx = (startpos+2*endpos)/3;
      ty = p[1]+piechartitems[item][0]/2;
      q = [tx,ty];
      label = String(piechartitems[item][0])+" ("+round(piechartitems[item][0]/total*100)+"%)"; 
      text(q,label,location, null, null, -90)
    }
    startpos = endpos;
  }
  lx = p[0];
  ly = p[1]-(ymax-ymin)/50;
  q = [lx,ly];
  setlabelfont();
  text(q,piecharttitle,"middlebelow");
}

function drawStackedBarChartV(p,chartwidth) {
  var numberofitems, total, item, startpos, endpos, tx, ty, location, label, q, r,dx, dy;
  setaxisfont();
  numberofitems = piechartitems.length;
  total = 0;
  for (item = 0; item<numberofitems; item = item+1)
    total = total + piechartitems[item][0];
  startpos = 0;  
  for (item = 0; item<numberofitems; item = item+1) {
    endpos = startpos + piechartitems[item][0]/total*(xmax-p[0]);
    if (piechartitems[item][4]) {
      fill = "url(#"+piechartitems[item][1]+")";
    } else {
      fill = piechartitems[item][1];
    }
    dy = +piechartitems[item][5];
    stroke = piechartitems[item][2];
    q = [p[0]+startpos,p[1]-chartwidth/2+dy];
    r = [p[0]+endpos,p[1]+chartwidth/2+dy];
    rect(q, r);
    if (piechartshowlabels) {
      location = "middlebelow";
      tx = (q[0]+r[0])/2;
      ty = r[1]-chartwidth;
      q = [tx,ty];
      text(q,piechartitems[item][3],location);
    }
    if (piechartshownumbers) {
      location = "middlemiddle";
      tx = p[0]+(startpos+endpos)/2;
      ty = p[1]+dy;
      var q = [tx,ty];
      label = String(piechartitems[item][0])+" ("+round(piechartitems[item][0]/total*100)+"%)"; 
      text(q,label,location);
    }
    startpos = endpos;
  }
  lx = p[0]+(xmax-xmin)/2;
  ly = p[1]+chartwidth/2;
  q = [lx,ly];
  setlabelfont();
  text(q,piecharttitle,"middleabove");
}

function drawBarChartV(p,chartwidth) {
  var numberofitems, total, item, startpos, endpos, tx, ty, location, label, q, r, dx, dy;
  setaxisfont();
  numberofitems = piechartitems.length;
  total = 0;
  for (item = 0; item<numberofitems; item = item+1)
    total = total + piechartitems[item][0];
  dy = chartwidth/numberofitems;  
  startpos = p[1]-chartwidth/2;  
  for (item = 0; item<numberofitems; item = item+1) {
    endpos = startpos + dy;
    if (piechartitems[item][4]) {
      fill = "url(#"+piechartitems[item][1]+")";
    } else {
      fill = piechartitems[item][1];
    }
    stroke = piechartitems[item][2];
    q = [p[0],startpos];
    r = [p[0]+piechartitems[item][0],endpos];
    rect(q, r);
    if (piechartshowlabels) {
      location = "rightmiddle";
      tx = r[0];
      ty = (startpos+endpos)/2;
      var q = [tx,ty];
      text(q,piechartitems[item][3],location,null,null,0)
    }
    if (piechartshownumbers) {
      location = "middlemiddle";
      tx = p[0]+piechartitems[item][0]/2;
      ty = (startpos+endpos)/2;
      q = [tx,ty];
      label = String(piechartitems[item][0])+" ("+round(piechartitems[item][0]/total*100)+"%)"; 
      text(q,label,location, null, null, 0)
    }
    startpos = endpos;
  }
  lx = (xmin+xmax)/2;
  ly = p[1]+chartwidth/2;
  q = [lx,ly];
  setlabelfont();
  text(q,piecharttitle,"middleabove");
}