JFreeChart

11 respostas
Gustavo_Yu

Pessoal, tenho uma dúvida simples, mas está difícil encontrar a solução…
como faço para aparecer o valor de cada barra, junto com a barra?

por exemplo…

____
___                 |      |  10
 |   |  5            |      |
|   |               |      |
|   |               |      |
-------------------------

como faço para aparecer o 5 e o 10
Obrigado pela atenção!

11 Respostas

B

Ola,

Vc consegue isto atraves do itemLabelGenerator, segue um exemplo de codigo:
class LabelGenerator extends AbstractCategoryItemLabelGenerator
                                implements CategoryItemLabelGenerator {

        /** The threshold. */
        private double threshold;
        
        /**
         * Creates a new generator that only displays labels that are greater 
         * than or equal to the threshold value.
         *
         * @param threshold  the threshold value.
         */
        public LabelGenerator(double threshold) {
            super("", NumberFormat.getInstance());
            this.threshold = threshold;
        }

        /**
         * Generates a label for the specified item. The label is typically a 
         * formatted version of the data value, but any text can be used.
         *
         * @param dataset  the dataset (<code>null</code> not permitted).
         * @param series  the series index (zero-based).
         * @param category  the category index (zero-based).
         *
         * @return the label (possibly <code>null</code>).
         */
        public String generateLabel(CategoryDataset dataset,
                                    int series,
                                    int category) {
            
            String result = null;
            Number value = dataset.getValue(series, category);
            if (value != null) {
                double v = value.doubleValue();
                if (v > this.threshold) {
                    result = value.toString();  // could apply formatting here
                }
            }
            return result;
            
        }
        
    }
E vc adiciona isto no seu chart assim:
CategoryItemRenderer renderer = plot.getRenderer(); // O plot aqui é o CategoryPlot
        renderer.setBaseItemLabelGenerator(new LabelGenerator(50.0));
        renderer.setBaseItemLabelFont(new Font("Serif", Font.PLAIN, 20));
        renderer.setBaseItemLabelsVisible(true);
Gustavo_Yu

Vlw bruno, tentei implementar oq vc escreveu mas ta dando erro nessa linha

public LabelGenerator(double threshold) {   
            [color=red]super("", NumberFormat.getInstance());   [/color]
            this.threshold = threshold;   
        }

qual biblioteca q eu devo importar?

B

Aqui a gente trabalha com a versão 1.0.9 do JFreeChart, qual a versão que vc usa ae?

Gustavo_Yu

o problema da biblioteca consegui resolver… aqui usamos a 1.0.9 tb…
agora rodo tranquilo, fui debugando… mas quando chega nessa linha, ele para o debug e vai pro Finally…

[color=red]renderer.setBaseItemLabelGenerator(new LabelGenerator(50.0));[/color]

:cry:

B

Gustavo Yu:
o problema da biblioteca consegui resolver… aqui usamos a 1.0.9 tb…
agora rodo tranquilo, fui debugando… mas quando chega nessa linha, ele para o debug e vai pro Finally…

[color=red]renderer.setBaseItemLabelGenerator(new LabelGenerator(50.0));[/color]

:cry:

vai pro finally??? lança alguma exception???

Gustavo_Yu

Não lança cara!
Simplesmente vai pro Finally…

B

Muito estranho, vou tentar reproduzir aqui e ja te dou um retorno.

Gustavo_Yu

Bruno, consegui cara, segue a solução:

DecimalFormat decimalformat1 = new DecimalFormat("##,###");
CategoryItemRenderer renderer = new BarRenderer();
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", decimalformat1));
renderer.setBaseItemLabelsVisible(true);
chart.getCategoryPlot().setRenderer(renderer);

vlw pela ajuda brother! :wink:

B

PÕ, show de bola.

Foi mal a demora… é que as coisas tão freneticas… mas boa.

Gustavo_Yu

Putz, outra dúvida…
Alguém sabe como faço para setar o ângulo de inclinação das barras para gerar o efeito 3D?
Se eu fizer JFreeChart chart = ChartFactory.createBarChart3D
ele cria um gráfico de barras 3D, mas como faço para eu setar o ângulo do efeito?
Tks!

A

Troque:

CategoryItemRenderer renderer = new BarRenderer();

por

CategoryItemRenderer renderer = new BarRenderer3D();
Criado 23 de junho de 2008
Ultima resposta 11 de ago. de 2008
Respostas 11
Participantes 3