Galera,
A rotina abaixo gera um gráfico de barras. Não estou conseguindo incluir o rótulo nas barras com os valores de cada uma e não estou conseguindo configurar a escala Y em uma determinada faixa de valores. Alguém pode me ajudar?
public class geraGrafico2Barras extends ApplicationFrame {
/**
* Creates a new demo instance.
*
* @param title the frame title.
*/
public geraGrafico2Barras (String title, String[] legendaGraficos, String[] dado1, String[] dado2, String[] dado3) {
super(title);
try{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
String series1 = legendaGraficos[0];
String series2 = legendaGraficos[1];
for (int j=0;j<dado1.length;j++){
dataset.addValue(Float.valueOf(dado2[j]),series1,dado1[j]);
dataset.addValue(Float.valueOf(dado3[j]),series2,dado1[j]);
}
JFreeChart chart = createChart(dataset, title);
ChartPanel chartPanel = new ChartPanel(chart, false);
chartPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartPanel);
}catch( Exception e ){
JOptionPane.showMessageDialog(null, "Atenção! Erro na contrução do gráfico! \n" +
"Favor informar a mensagem abaixo ao adminstrador do sistema! \n " +
e.getMessage(), "Erro de construção de gráfico", JOptionPane.WARNING_MESSAGE);
}
}
/**
* Creates a sample chart.
*
* @param dataset the dataset.
*
* @return The chart.
*/
private static JFreeChart createChart(CategoryDataset dataset, String title) {
// create the chart...
JFreeChart chart = ChartFactory.createBarChart(
title, // chart title
"Período", // domain axis label
"Classe", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips?
false // URLs?
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// set the background color for the chart...
chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.white);
// disable bar outlines...
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
// set up gradient paints for series...
GradientPaint gp0 = new GradientPaint(
0.0f, 0.0f, Color.blue,
0.0f, 0.0f, new Color(0, 0, 64)
);
GradientPaint gp1 = new GradientPaint(
0.0f, 0.0f, Color.green,
0.0f, 0.0f, new Color(0, 64, 0)
);
renderer.setSeriesPaint(0, gp0);
renderer.setSeriesPaint(1, gp1);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
);
// OPTIONAL CUSTOMISATION COMPLETED.
return chart;
}
Obrigado;
Edilson Cavalieri
EDIT - Por favor, use os tags [ code ] quando for incluir código-fonte em seu post. É simples, use o botãozinho [ code ] antes e depois de colar seu programa.