<?php

include_once 'phplot/phplot.php';

$quantidadetotal = $_REQUEST[quantidadetotal];
$chdtext = explode('|', $_REQUEST[chd]);
$chdltext = explode('|', $_REQUEST[chdl]);

arsort($chdtext);

$chd = array();
$chdl = array();
$chco = array();
$i = 0;
$corhex = '000000';
$cores = array('1450A2', '55D037', 'E9F261','8F5EE5', 'E5885E', '5EE5D7','BD5EE5','A4B28A','B53636','36B5AB', 'FF6347', '9995A7', '006400', 'F9A3A3', 'EEE8AA', 'BC8F8F', 'B6A3F9', 'A0522D', 'DEB887', 'FA8072', 'FF8C00', '767715', 'FF1493', 'EE82EE', 'A020F0', 'CDB38B', 'C1CDC1'); // cores
foreach($chdtext as $indice=>$quantidade){
    array_push ($chd, array('', $quantidade));
    if($_REQUEST[tipo] == 'bars') array_push($chdl, utf8_decode(str_replace("|", "-", $chdltext[$indice])));
    else array_push($chdl, utf8_decode(str_replace("|", "-", $chdltext[$indice].' ('.$quantidade.' - '.round(($quantidade/$quantidadetotal * 100) * 1000)/1000 .'% )')));
    if($i < count($cores)) $corhex = $cores[$i];
    else{
        $cordec = hexdec($corhex) + hexdec('1450a2');
        $corhex = substr(dechex($cordec), 0, 6);
    }
    array_push($chco, "#$corhex");
    
    $i++;
}

switch($_REQUEST[tipo]){
    case 'pie':
        $tamanhomaxlabel = 1;
        foreach($chdltext as $label) if(strlen($label) > $tamanhomaxlabel) $tamanhomaxlabel = strlen($label);
        $larguraX = 555 + $tamanhomaxlabel * 4;
        $larguraY = (count($chdltext) > 6)? count($chdltext)*20 : 150;
        $plot = new PHPlot($larguraX, $larguraY);
        $plot->SetPlotType('pie');
        $plot->SetDataType('text-data-single');
        $plot->SetDataValues($chd);
        $plot->SetDataColors($chco);
        $plot->SetLegend($chdl);
        $plot->SetLegendStyle('left','left');
        $plot->SetLabelScalePosition(0.6);
        $plot->SetPlotAreaPixels(50,10, 190,140);
        $plot->SetLegendPixels(235, 5);
        break;
    case 'bars':
        $tamanhomaxlabel = 1;
        foreach($chdltext as $label) if(strlen($label) > $tamanhomaxlabel) $tamanhomaxlabel = strlen($label);
        $tamanhoXimagem = (count($chdltext) > 8)? count($chdltext)*20 : count($chdltext)*50;
        $tamanhoYimagem = (count($chdltext) > 8)? count($chdltext)*15 : 150;
        $larguraX = $tamanhoXimagem+270 + $tamanhomaxlabel*4;
        $larguraY = (count($chdltext) > 6)? count($chdltext)*20 : 150;
        $plot = new PHPlot($larguraX, $larguraY);
        $plot->SetPlotType('bars');
        $plot->SetXTickPos('none');          // Turn off X tick marks
        $plot->SetXTickLabelPos('none');     // Turn off X tick labels
        //$plot->SetXDataLabelPos('none');     // Turn off X data labels
        //$plot->SetYTickPos('none');          // Turn off Y tick marks
        //$plot->SetYTickLabelPos('none');     // Turn off Y tick labels
        $plot->SetPlotBorderType('none');    // Turn off plot area border
        $plot->SetDrawXGrid(False);          // Turn off X grid lines
        //$plot->SetDrawYGrid(False);          // Turn off Y grid lines
        //$plot->SetDrawXAxis(False);          // Don't draw X axis line
        //$plot->SetDrawYAxis(False);          // Don't draw Y axis line
        $plot->SetDataType('text-data');
        $plot->SetDataValues($chd);
        $plot->SetCallback('data_color', 'pickcolor', $chd);
        $plot->SetDataColors($chco);
        $plot->SetLegend($chdl);
        $plot->SetLegendStyle('left','left');
        $plot->SetLabelScalePosition(0.6);
        $plot->SetPlotAreaPixels(50,10, $tamanhoXimagem+50,$tamanhoYimagem-10);
        $plot->SetLegendPixels($tamanhoXimagem+70, 5);
        break;
}

$plot->DrawGraph();

function pickcolor($img, $data_array, $row, $col){
    return $row;
}

?>
