silva.fernandes 20 de nov. de 2006
Amigo, segue um exemplo de um grafico pizza com as devidas porcentagens .. oq vc deve acrescentar está comentado
import java.awt.Color ;
import java.awt.Dimension ;
import javax.swing.JPanel ;
import org.jfree.chart.* ;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator ;
import org.jfree.chart.plot.PiePlot ;
import org.jfree.data.general.DefaultPieDataset ;
import org.jfree.data.general.PieDataset ;
import org.jfree.ui.ApplicationFrame ;
import org.jfree.ui.RefineryUtilities ;
public class GraficoPizzaDemo extends ApplicationFrame {
/**
*
*/
private static final long serialVersionUID = 1L ;
public GraficoPizzaDemo () {
super ( null );
this . setTitle ( "Grafico de Pizza" );
JPanel jpanel = PanelDemostracao ();
jpanel . setPreferredSize ( new Dimension ( 500 , 270 ));
setContentPane ( jpanel );
}
private static PieDataset criaDadosGrafico () {
DefaultPieDataset defaultpiedataset = new DefaultPieDataset ();
defaultpiedataset . setValue ( "Conteúdo 1" , 43.23D );
defaultpiedataset . setValue ( "Conteúdo 2" , 10 D );
defaultpiedataset . setValue ( "Conteúdo 3" , 27.5D );
defaultpiedataset . setValue ( "Conteúdo 4" , 17.5D );
defaultpiedataset . setValue ( "Conteúdo 5" , 11 D );
defaultpiedataset . setValue ( "Conteúdo 6" , 19.39D );
return defaultpiedataset ;
}
private static JFreeChart criaGrafico ( PieDataset piedataset ) {
JFreeChart jfreechart = ChartFactory . createPieChart (
"Gráfico Pizza Demo " , piedataset , true , true , false );
PiePlot plotagem = ( PiePlot ) jfreechart . getPlot ();
plotagem . setLabelGenerator ( new StandardPieSectionLabelGenerator (
"{0} ({2})" )); //define porcentagem no gráfico
plotagem . setLabelBackgroundPaint ( new Color ( 220 , 220 , 220 ));
return jfreechart ;
}
public static JPanel PanelDemostracao () {
JFreeChart jfreechart = criaGrafico ( criaDadosGrafico ());
return new ChartPanel ( jfreechart );
}
public static void main ( String args [] ) {
GraficoPizzaDemo demo = new GraficoPizzaDemo ();
demo . pack ();
RefineryUtilities . centerFrameOnScreen ( demo );
demo . setVisible ( true );
}
}
Falowww
T+
adrianostanley 21 de nov. de 2006
Legal cara!! Funcionou que eh uma beleza!! Abracos!
adrianostanley 22 de nov. de 2006
E pra um grafico de Barras?? tem como exibir as porcentagens??
peduardo 23 de jul. de 2009
Oi, do jeito que está
pieplot3d . setLabelGenerator ( new StandardPieSectionLabelGenerator (
"{0} ({2})" ));
ele mostra o porcentagem, mas por exemplo de 3 regiões com valor 33, ele mostra 33% pra cada, como que so so fizesse para números inteiros.
Sabe como eu altero isso:
obrigado!
paula_regina 1 de ago. de 2011
olá, conseguisse resolver o problema de só mostrar valores inteiros nas porcentagens de gráficos??
ManoJava 1 de ago. de 2011
Boa tarde!
Pra resolver esse problema de apresentar as casas decimais para as porcentagens, vc terá que criar uma classe de customização e indicar ela na propriedade " customizer class" do seu gráfico de pizza, procure no google ou aqui no forum por " Customizer class for PieChart3D" vc vai achar vários exemplos.
Att.
paula_regina 1 de ago. de 2011
tens algum exemplo que possa me passar?
não encontrei nenhum exemplo muito claro…
Att,
ManoJava 2 de ago. de 2011
Bom dia!!
Segue um exemplo:
public class JasperPieChartCustomizer implements JRChartCustomizer
{
private static final int DECIMALS = 2 ;
private static final String DEFAULT_FORMAT = "{0} {1} {2}" ;
public void customize ( JFreeChart jFreeChart , JRChart jrChart )
{
Plot plot = jFreeChart . getPlot ();
if ( ! ( plot instanceof PiePlot ))
return ;
PiePlot piePlot = ( PiePlot ) plot ;
PieSectionLabelGenerator labelGen = piePlot . getLabelGenerator ();
PieSectionLabelGenerator legendGen = piePlot . getLegendLabelGenerator ();
String labelFormat = labelGen instanceof StandardPieSectionLabelGenerator ?
(( StandardPieSectionLabelGenerator ) labelGen ). getLabelFormat () :
DEFAULT_FORMAT ;
String legendFormat = legendGen instanceof StandardPieSectionLabelGenerator ?
(( StandardPieSectionLabelGenerator ) legendGen ). getLabelFormat () :
DEFAULT_FORMAT ;
Locale locale = Util . getLocale ();
NumberFormat numberFormat = NumberFormat . getNumberInstance ( locale );
NumberFormat percFormat = NumberFormat . getPercentInstance ( locale );
percFormat . setMinimumFractionDigits ( DECIMALS );
StandardPieSectionLabelGenerator newLabelGen = new StandardPieSectionLabelGenerator (
labelFormat , numberFormat , percFormat );
piePlot . setLabelGenerator ( newLabelGen );
StandardPieSectionLabelGenerator newLegendGen = new StandardPieSectionLabelGenerator (
legendFormat , numberFormat , percFormat );
piePlot . setLegendLabelGenerator ( newLegendGen );
}
}
Att.
paula_regina 2 de ago. de 2011
Funcionou perfeitamente!!
muitooo obrigada!!
apalmeira 3 de nov. de 2011
Muito bom, eu estava precisando exibir as porcentagens e funcionou perfeitamente.
Sds