-- UNIDADE
	
V_TOTAL := 0;
for R1 in C110 (V_MOTIVO, V_UNIDADE, V_VINCULO) loop
	V_TOTAL := V_TOTAL + R1.N;
end loop;

htp.p('<div style="padding: 25 0 10 0pt; font-size: 14pt;">' || 'INSTITUTO' || '</div>');

htp.p('<table class="ImpPCS" style="font-size: 10pt;" width="100%">');
htp.p('<tr>');
htp.p('<td width="75%"><b>Instituto</b>');
htp.p('<td width="15%"><b>No. de atend.</b>');
htp.p('<td><b>%</b>');

v_n_max := 0;
v_chd := '';
v_chl := '';

for R1 in C110 (V_MOTIVO, V_UNIDADE, V_VINCULO) loop
	V_N_PCT := R1.N/ V_TOTAL*100;

	htp.p('<tr>');
	htp.p('<td>' || R1.UNIDADE);
	htp.p('<td align="right">' || R1.N);
	htp.p('<td align="right">' || ltrim(replace(to_char(V_N_PCT, '990.9'), '.', ',')));

	if V_N_PCT > v_n_max then
		v_n_max := V_N_PCT;
	end if;
	v_chd := v_chd || ltrim(to_char(V_N_PCT, '990.9')) || ',';
	v_chl := v_chl || CP_LM_CHAR_GRAF(R1.LAB) || '|';

end loop;

htp.p('<tr>');
htp.p('<td align="right"><b>TOTAL:</b>');
htp.p('<td align="right"><b>' || V_TOTAL || '</b>');
htp.p('<td align="right"><b>100,0</b>');

htp.p('</table>');

v_chd := substr(v_chd, 1, length(v_chd) -1);
v_chl := substr(v_chl, 1, length(v_chl) -1);

v_chart := 'chs=620x180' || '&amp;';
v_chart := v_chart || 'chd=t:' || v_chd || '&amp;';
v_chart := v_chart || 'chds=0,' || ltrim(to_char(v_n_max * 1.1, '999999999.9')) || '&amp;';
v_chart := v_chart || 'chxr=0,0,' || ltrim(to_char(V_TOTAL * v_n_max * 1.1 / 100, '999999999.9')) || '&amp;';
v_chart := v_chart || 'cht=bvs' || '&amp;';
v_chart := v_chart || 'chxt=y' || '&amp;';
v_chart := v_chart || 'chl=' || v_chl;

htp.p('<center><div style="padding: 25 10 25 10px;"><img src="http://chart.apis.google.com/chart?' || v_chart || '" alt="gráfico" /></div></center>');
--htp.p(v_chart);



-- CONVERTE STRING PARA GRAF

set define off

create or replace function CP_LM_CHAR_GRAF (
	TX in VARCHAR2 default null)
	return varchar2

is

	rt varchar2(1000);


begin

	rt := UPPER(TX);
	
	rt := TRANSLATE(rt, '$-_.+!*&,;:=?@<>#{}\^~" ', '++++++++++++++++++++++++'); 

	
	-- PRIMEIRO
	
	rt := REPLACE(rt, '%', '%25');

	
	-- DEPOIS
	
	rt := REPLACE(rt, 'Á', ' %C3%81');
	rt := REPLACE(rt, 'É', '%C3%89');
	rt := REPLACE(rt, 'Í', '%C3%8D');
	rt := REPLACE(rt, 'Ó', '%C3%93');
	rt := REPLACE(rt, 'Ú', '%C3%9A');
	rt := REPLACE(rt, 'Â', '%C3%82');
	rt := REPLACE(rt, 'Ê', '%C3%8A');
	rt := REPLACE(rt, 'Î', '%C3%8E');
	rt := REPLACE(rt, 'Ô', '%C3%94');
	rt := REPLACE(rt, 'Û', '%C3%9B');
	rt := REPLACE(rt, 'Ç', '%C3%87');
	rt := REPLACE(rt, 'Ã', '%C3%83');
	rt := REPLACE(rt, 'Õ', '%C3%95');
	rt := REPLACE(rt, 'À', '%C3%80');
	rt := REPLACE(rt, 'Ü', '%C3%9C');

	rt := REPLACE(rt, '/', '%2F');
	rt := REPLACE(rt, '(', '%28');
	rt := REPLACE(rt, ')', '%29');

	return (rt);


end CP_LM_CHAR_GRAF;