<?php
/*
Variables passed by the Plugin:
$field_value .- Field what show screen
$DataValue  .- Valor del campoque se utiliza en el plugin
$javascript  .- File of Javascript of Anychart
$theme .- File of Theme of Anychart
$language  .- File Javscript of language
$decimalsCount .-  Parameter
$zeroFillDecimals .-  Parameter
$decimalPoint .-  Parameter
$groupsSeparator .-  Parameter
$license .- License of ANYCHART  for PHPRunner
$id_field .-  Field occurrence identification number used to identify the "container".
*/
global $conn;

// Control  of dependencies de record master "
// $Convocatoria=$DataValue;
$Convocatoria = $_SESSION['idConvocatoria'];

$chartData = '';

$strSQLExists = "SELECT Codigo, Titulo, Color, Votos, PorcVotos, Escanos
FROM escanos join candidatura on (Candidatura_idCandidatura = idCandidatura)
where Escanos <> 0 and Convocatoria_idConvocatoria = $Convocatoria 
order by Escanos desc";

$rsExists = db_query($strSQLExists,$conn);
      while ($Row = db_fetch_array($rsExists)) {
            $Codigo="x:"."'".$Row["Codigo"]."'";
            $Escanos="value:".$Row["Escanos"];
            $Color="fill:"."'".$Row["Color"]."'";
            $Titulo="titulo:"."'".$Row["Titulo"]."'";
            $PorcVotos="porcvotos:".$Row["PorcVotos"];
            $chartData.="{"."$Codigo,$Escanos,$Color,$Titulo,$PorcVotos"."} \n,";
      }
$chartData = substr($chartData, 0, -1);

$strSQLExists1 = "SELECT Convocatoria_idConvocatoria, sum(Escanos) TotalEscanos FROM escanos
where Convocatoria_idConvocatoria = $Convocatoria
group by 1";
$rsExists1 = db_query($strSQLExists1,$conn);
$PETICION = db_fetch_array($rsExists1);
$TotalEscanos= $PETICION["TotalEscanos"];
  
$graphicDefinition= <<<EOT
// create pie chart with passed data
var data = [
 $chartData
  ];

// calculate total value
var halfOfTotal = data.reduce((total, currentRow) => { 
    return total + currentRow.value
		}, 0);

// add the second half that equals initial total
data.push({x: "hidden", value: halfOfTotal, fill: '#000 0'});

var chart = anychart.pie(data);
        
// License, out logo
$license

// turn on chart animation
chart.animation(true);
  // apply custom start angle
chart.startAngle(-90);

//set the inner radius
//  (to turn the pie chart into a doughnut chart)
chart.innerRadius("50%");
chart.padding(0).margin(0);

// hide the half of the chart beyond the stage
// chart.bounds(0, '50%', '100%', '100%');
chart.padding(0, 0, '-100%', 0);
  
 // set chart labels position to outside
// chart.labels().enabled(false).position('outside');
chart.labels().enabled(true);
        
chart.labels()
  .fontSize(9)
	.fontColor('white')
	.format("{%value}");

var tooltip = chart.tooltip();
tooltip.format("Escaños: {%value}\\nVotos válido: {%porcvotos}{type:number,decimalsCount:2}%");
 
// create standalone label and set settings
var label = anychart.standalones.label();
      label
        .enabled(true)
        .text('$TotalEscanos\\nescaños\\n\\n')
        .width('100%')
        .height('100%')
       // .adjustFontSize(true, true)
       // .minFontSize(8)
       // .maxFontSize(20)
       // .fontColor('#178ACC')
        .position('center')
        .anchor('center')
        .hAlign('center')
        .vAlign('middle');

// set label to center content of chart
chart.center().content(label);

chart.legend(false);
       
chart.container('container_$id_field');
// initiate chart drawing
chart.draw();
});

</script>
EOT;
$field_value .= $graphicDefinition;
?>
