Galera é o seguinte:
gostaria de criar um gráfico do tipo MeterChar usando JFreeChart. Consigo criar o gráfico bonitinho, só que não consigo definir cores deiferentes para cada intervalo de valores. Tipo valores de 0 a 70 na cor verde, 70 a 100 na cor amarela e 100 a 120 na cor vermelha.
O máximo que consigo é criá-lo com apenas uma cor de fundo.
Nesse link tem a imagem de como eu quero que ele seja:
http://br.geocities.com/honae1/meterCorreto.bmp
Nesse tem a imagem de como ele está ficando:
http://br.geocities.com/honae1/meterErrado.bmp
Já vi em alguns códigos as funções da classe MeterPlot
setNormalRange
setWarningRange
setCriticalRange
mas elas não existem mais.
Abaixo segue o meu código:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.util.Vector;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.DialShape;
import org.jfree.chart.plot.MeterInterval;
import org.jfree.chart.plot.MeterPlot;
import org.jfree.data.Range;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.ui.RectangleInsets;
public class GraficoMedidores {
private Color colorFundo;
private Color colorAgulha;
private Color colorValor;
private String titulo;
private String unidade;
private Font tituloFonte;
private float valor;
private float intervalo[];
private Vector intervalos;
private int angulo;
private boolean legenda;
/** The dataset. */
private DefaultValueDataset data ;
/** The meter plot (dial). */
private MeterPlot plot ;
/** The meter chart (dial). */
private JFreeChart meterchart;
/** The meter panel. */
private ChartPanel panelMeter;
public GraficoMedidores( Color colorFundo, Color colorAgulha, String titulo ,
float valor, String unidade, Font tituloFonte, float intervalo[] ,
int angulo, Vector intervalos, boolean legenda,
Color colorValor){
this.colorFundo = colorFundo;
this.colorAgulha = colorAgulha;
this.colorValor = colorValor;
this.titulo = titulo;
this.valor = valor;
this.unidade = unidade;
this.tituloFonte = tituloFonte;
this.intervalo = intervalo;
this.angulo = angulo;
this.intervalos = intervalos;
this.legenda = legenda;
inicialize();
}
public ChartPanel getPanelMeter(){
return panelMeter;
}
public void inicialize(){
/** The dataset. */
data = new DefaultValueDataset( this.valor );
/** The meter plot (dial). */
plot = new MeterPlot( this.data );
plot.setUnits( this.unidade );
plot.setRange(new Range( this.intervalo[0] , this.intervalo[1] ) );
for (int i = 0; i < intervalos.size(); i+=3) {
plot.addInterval( new MeterInterval( (String)intervalos.get( i ) ,
new Range( ((Float)intervalos.get( i + 1)).floatValue(), ((Float)intervalos.get( i + 2)).floatValue())) );
}
plot.setDialShape(DialShape.PIE);
plot.setNeedlePaint( this.colorAgulha );
plot.setDialBackgroundPaint( this.colorFundo );
plot.setValuePaint( this.colorValor );
plot.setMeterAngle( this.angulo );
plot.setTickLabelFont(new Font("SansSerif", Font.BOLD, 10));
meterchart = new JFreeChart(this.titulo,
this.tituloFonte,
this.plot, this.legenda);
panelMeter = new ChartPanel(this.meterchart);
}
public void setValor(float valor) {
this.valor = valor;
}
/**
- Starting point for the demo application.
-
@param args ignored.
*/
public static void main(final String[] args) {
JFrame frame = new JFrame();
Font fonte = new Font("Dialog", java.awt.Font.BOLD, 18);
float intervalo[] = {0.0f, 120.0f};
Vector vetor = new Vector();
vetor.add(0, "Normal");
vetor.add(1, new Float(0));
vetor.add(2, new Float(70));
vetor.add(3, "Warning");
vetor.add(4, new Float(70));
vetor.add(5, new Float(100));
vetor.add(6, "Critical");
vetor.add(7, new Float(100));
vetor.add(8, new Float(120));
GraficoMedidores panel = new GraficoMedidores(Color.BLACK, Color.BLUE,
"Meter Chart", 55.5f, "ºC", fonte, intervalo,
180, vetor, true, Color.blue );
frame.getContentPane().setLayout(new BorderLayout(5, 5));
frame.setDefaultCloseOperation(3);
frame.setTitle("Thermometer Test");
frame.getContentPane().add(panel.getPanelMeter(), BorderLayout.CENTER);
frame.setSize(700, 400);
final Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
}
E aí, tem como?
Só uma coisa, a imagem que representa o gráfico como eu quero que ele fique, foi tirada da documentação da JFreeChart. A imagem de como estou criando, foi tirada do resultado da minha implementação.
Grato!
Hélio