<?php
  include("connect.php");
  include ("jpgraph/jpgraph.php");
  include ("jpgraph/jpgraph_pie.php");
  include ("jpgraph/jpgraph_pie3d.php");
  
  check_valid();
  $result=mysql_query("select * from encuestas where id='".$id."' and (tipo_contenido='2' or tipo_contenido='".$current_valid."')",$link);
  $row=mysql_fetch_array($result) or die();

  $total=0;
  for($i=1;$i<=10;$i++) {
    if($row["opt".$i]!="") {
      $total=$total+$row["count".$i];
    }
  }
  for($i=1;$i<=10;$i++) {
    if($row["opt".$i]!="") {
      $data[]=$row["count".$i];
      $legend[]=$row["opt".$i]." (".round($row["count".$i]*100/$total)."%%, ".$row["count".$i]." vots)";
    }
  }

	/********
		*	developer: chris.alex.thomas@gmail.com
		*	note: I dont know why reversing the data array will cause the graph to display correctly, but testing it, it does actually work
		*/
  $data = array_reverse($data);  	
  
  $titulo=html_entity_decode($row["titulo"]);
  $fecha=convert_date($row["fecha"]);

  $graph=new PieGraph(465,260,"auto");
  $graph->SetColor("#EEEFF1");

  $graph->title->Set($titulo);
  $graph->title->SetFont(FF_FONT1,FS_BOLD);

  $p1=new PiePlot3D($data);
  $p1->SetSize(0.35);
  $p1->SetAngle(45);
  $p1->SetCenter(0.3,0.5);
  $p1->SetLegends($legend);

  $t1=new Text("Número total de vots: ".$total);
  $t1->Pos(465/2,225);
  $t1->SetFont(FF_FONT1,FS_NORMAL);
  $t1->Align("center","top");

  $t2=new Text("Data de publicació: ".$fecha);
  $t2->Pos(465/2,240);
  $t2->SetFont(FF_FONT1,FS_NORMAL);
  $t2->Align("center","top");

  $graph->Add($p1);
  $graph->AddText($t1);
  $graph->AddText($t2);
  $graph->Stroke();

  mysql_free_result($result);
  disconnect();
?>

