JFreeChart não muda fonte[Resolvido]

1 resposta
rogick

Bom dia galera,

Eu criei um gráfico de linha no JFreeChart para exibí-lo via plugin do Struts2. O gráfico é gerado sem problemas, porém eu quero alterar(diminuir) a fonte dos labels e não estou conseguindo :cry:
Eu pensei que fosse o plugin do Struts2, porém fiz um teste salvando em um arquivo png e a fonte não é alterada.

Segue abaixo o código para gerar o gráfico:

public static JFreeChart gerarGraficoLinhaStruts(String tituloGrafico, String tituloEixoX,
                             String tituloEixoY, List arrayValores) throws Exception {

    	JFreeChart chart = null;

        try {

            DefaultCategoryDataset defaultCategoryDataset = new DefaultCategoryDataset();
            Iterator iterator = arrayValores.iterator();

            while (iterator.hasNext()) {
                ModeloGraficoItem modelo = (ModeloGraficoItem) iterator.next();
                defaultCategoryDataset.addValue(modelo.getQuantidade(), modelo.getEixoY(), modelo.getEixoX());
            }

            chart = ChartFactory.createLineChart(tituloGrafico, tituloEixoX, tituloEixoY, defaultCategoryDataset, PlotOrientation.VERTICAL, true, false, false);
            chart.setBorderVisible(true);
            chart.setBorderPaint(Color.black);

        } catch (Exception e) {
            throw new Exception(e);
        }
        return chart;
    }

E agora o código para (tentar) alterar a fonte:

public static void alterarFonteChart(JFreeChart chart, Font font){
    	CategoryPlot plot = (CategoryPlot)chart.getPlot();  
    	
    	plot.getDomainAxis().setLabelFont(plot.getDomainAxis().getLabelFont().deriveFont(new Float(font.getSize())));
    	plot.getRangeAxis().setLabelFont(font);
    	plot.getLegendItems().get(0).setLabelFont(font);
    	CategoryItemRenderer renderer = plot.getRenderer(); 
        renderer.setBaseItemLabelFont(font);
    }

Segue as versões dos jars:
jcommon-1.0.14.jar
freechart-1.0.11.jar

Desde já agradeço a atenção.

1 Resposta

rogick

Resolvi com o seguinte código retirado do http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=30181

public static void applyChartTheme(JFreeChart chart, Font font) {
        StandardChartTheme chartTheme = (StandardChartTheme)org.jfree.chart.StandardChartTheme.createJFreeTheme();
        
        chartTheme.setRegularFont(font);

        chartTheme.apply(chart);
    }
Criado 27 de dezembro de 2010
Ultima resposta 27 de dez. de 2010
Respostas 1
Participantes 1